1 the graph abstract data type cs 5050 chapter 6

48
1 The Graph Abstract Data Type CS 5050 Chapter 6

Post on 20-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 The Graph Abstract Data Type CS 5050 Chapter 6

1

The Graph Abstract Data Type

CS 5050

Chapter 6

Page 2: 1 The Graph Abstract Data Type CS 5050 Chapter 6

2

Terminology

• Vertex (vertices) – position, holds an object• Edge – also a position, holds an object• Directed, undirected ((a)symmetric)• Graph is a set of vertices and a bag of edges (can be

duplicated)– Parallel and self-edges– Directed, undirected, mixed (both directed and undirected

edges)– convert to digraph– End vertices/endpoints (of an edge), origin/destination,

adjacent vertices (endpoints of same edge)– Incident edge, incoming, outgoing, in (out)-degree

Page 3: 1 The Graph Abstract Data Type CS 5050 Chapter 6

3

Terminology continued• Simple graphs have no parallel (multiple

edges between same vertices) or self loop• Path – sequence of alternating vertices and

edges which match up• Cycle - path with same first and last vertex• Simple and directed paths and cycles• (Spanning – contains all vertices) subgraph • Connected graph, connected component• Forest (acyclic), free (no root) trees

(connected forest), spanning tree

Page 4: 1 The Graph Abstract Data Type CS 5050 Chapter 6

4

Graph Facts• Assume the graph G has n vertices (V) and

m edges (E)– The sum over V of degrees is 2m– If G is directed, the sum over V of in-degrees =

the sum over V of out-degrees = m– If G is simple and undirected, m <= n(n-1)/2(worst case is every pair)– If G is simple and directed, m <= n(n-1)– So m is O( |V|2 )– If G is connected, m >= n-1, if a tree m = n-1– If G is a forest, m <= n-1

Page 5: 1 The Graph Abstract Data Type CS 5050 Chapter 6

5

Adjacency List -partitioned incidence container (in/out)

Page 6: 1 The Graph Abstract Data Type CS 5050 Chapter 6

6

Adjacency Matrix

Page 7: 1 The Graph Abstract Data Type CS 5050 Chapter 6

7

Traversals

• Visit each node – e.g., web crawler• Have to restart traversal in each connected

component, but this allows us to identify components

• Reachability in a digraph is an important issue – the transitive closure graph

• Book permits counter-direction motion in general traversals

Page 8: 1 The Graph Abstract Data Type CS 5050 Chapter 6

8

Looking for a spanning Tree

• Many times algorithms depend on some ordering of the nodes of a graph

• Labeling the edges is some way is helpful in organizing thinking about a graph

Page 9: 1 The Graph Abstract Data Type CS 5050 Chapter 6

9

Depth-First Search (undirected graph)

• Simple recursive backtracking algorithm calls recursive function at starting point– For each incident edge to a vertex

• If opposite (other) vertex is unvisited – Label edge as “discovery”

– Recur on the opposite vertex

• Else label edge as “back”

• Discovery edges form a component spanning tree, back edges go to an ancestor

• with m edges, O(m) using an adjacency list, but not using an adjacency matrix

Page 10: 1 The Graph Abstract Data Type CS 5050 Chapter 6

10

Depth-First Traversal

Page 11: 1 The Graph Abstract Data Type CS 5050 Chapter 6

11

Biconnectivity

SEA PVD

MIASNA

ORDFCO

Page 12: 1 The Graph Abstract Data Type CS 5050 Chapter 6

12

Outline and Reading• Definitions (§6.3.2)

– Separation vertices and edges

– Biconnected graph

– Biconnected components

– Equivalence classes

– Linked edges and link components

• Algorithms (§6.3.2)– Auxiliary graph

– Proxy graph

Page 13: 1 The Graph Abstract Data Type CS 5050 Chapter 6

13

Separation Edges and Vertices• Definitions

– Let G be a connected graph– A separation edge of G is an edge whose removal disconnects G – A separation vertex of G is a vertex whose removal disconnects G

• Applications– Separation edges and vertices represent single points of failure in a network

and are critical to the operation of the network

• Example– DFW, LGA and LAX are separation vertices– (DFW,LAX) is a separation edge

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

Page 14: 1 The Graph Abstract Data Type CS 5050 Chapter 6

14

Biconnected Graph• Equivalent definitions of a biconnected graph G

– Graph G has no separation edges and no separation vertices– For any two vertices u and v of G, there are two disjoint simple

paths between u and v (i.e., two simple paths between u and v that share no other vertices or edges)

– For any two vertices u and v of G, there is a simple cycle containing u and v

• Example

ORDPVD

MIADFW

SFO

LAX

LGAHNL

Page 15: 1 The Graph Abstract Data Type CS 5050 Chapter 6

15

Biconnected Components• Biconnected component of a graph G

– A maximal biconnected subgraph of G, or– A subgraph consisting of a separation edge of G and its end vertices

• Interaction of biconnected components– An edge belongs to exactly one biconnected component– A nonseparation vertex belongs to exactly one biconnected component– A separation vertex belongs to two or more biconnected components

• Example of a graph with four biconnected components

ORD PVD

MIADFW

SFO

LAX

LGA

HNLRDU

Page 16: 1 The Graph Abstract Data Type CS 5050 Chapter 6

16

Equivalence Classes• Given a set S, a relation R on S is a set of ordered pairs of elements of

S, i.e., R is a subset of SS• An equivalence relation R on S satisfies the following properties

Reflexive: (x,x)RSymmetric: (x,y)R (y,x) RTransitive: (x,y)R (y,z)R (x,z) R

• An equivalence relation R on S induces a partition of the elements of S into equivalence classes

• Example (connectivity relation among the vertices of a graph):– Let V be the set of vertices of a graph G– Define the relation

C = {(v,w) VV such that G has a path from v to w} – Relation C is an equivalence relation– The equivalence classes of relation C are the vertices in each connected

component of graph G

Page 17: 1 The Graph Abstract Data Type CS 5050 Chapter 6

17

Link Relation• Edges e and f of connected

graph G are linked if– e f, or– G has a simple cycle

containing e and f

Theorem:The link relation on the edges of a graph is an equivalence relationProof Sketch:– The reflexive and symmetric

properties follow from the definition

– For the transitive property, consider two simple cycles sharing an edge

ab

g

cj

d

e

f

i

Equivalence classes of linked edges:{a} {b, c, d, e, f} {g, i, j}

ab

g

cj

d

e

f

i

Page 18: 1 The Graph Abstract Data Type CS 5050 Chapter 6

18

Link Components• The link components of a connected graph G are the equivalence classes

of edges with respect to the link relation• A biconnected component of G is the subgraph of G induced by an

equivalence class of linked edges• A separation edge is a single-element equivalence class of linked edges• A separation vertex has incident edges in at least two distinct

equivalence classes of linked edge

ORD PVD

MIADFW

SFO

LAX

LGA

HNLRDU

Page 19: 1 The Graph Abstract Data Type CS 5050 Chapter 6

19

Auxiliary Graph• Auxiliary graph B for a connected

graph G– Associated with a DFS traversal of

G

– The vertices of B are the edges of G

– For each back edge e of G, B has edges (e,f1), (e,f2) , …, (e,fk),where f1, f2, …, fk are the discovery edges of G that form a simple cycle with e

– Its connected components correspond to the the link components of G

a

b

g

c

j

d

e

f

i

Auxiliary graph B

DFS on graph G

a

d

b

c

eh

i

jf

h

g

i

Page 20: 1 The Graph Abstract Data Type CS 5050 Chapter 6

20

Auxiliary Graph (cont.)• In the worst case, the number of edges of the auxiliary

graph is proportional to nm

Auxiliary graph BDFS on graph G

Page 21: 1 The Graph Abstract Data Type CS 5050 Chapter 6

21

Proxy GraphAlgorithm proxyGraph(G)

Input connected graph GOutput proxy graph F for GF empty graphDFS(G, s) { s is any vertex of G}for all discovery edges dEdge of G

F.insertVertex(dEdge)setLabel(dEdge, UNLINKED)

for all vertices v of G in DFS visit orderfor all back edges bEdge (u,v)

F.insertVertex(bEdge)repeat

dEdge discovery edge with dest. u

F.insertEdge(bEdge,dEdge,) if getLabel(dEdge)

UNLINKEDsetLabel(dEdge, LINKED)u origin of dEdge

elseu v { ends the loop }

until u vreturn F

a

b

g

c

j

d

e

f

i

Proxy graph F

DFS on graph G

a

d

b

c

eh

i

jf

h

g

i

Page 22: 1 The Graph Abstract Data Type CS 5050 Chapter 6

22

Proxy Graph (cont.)• Proxy graph F for a connected graph

G – Spanning forest of the auxiliary graph

B– Has m vertices and O(m) edges– Can be constructed in O(n m) time– Its connected components (trees)

correspond to the the link components of G

• Given a graph G with n vertices and m edges, we can compute the following in O(n m) time– The biconnected components of G– The separation vertices of G– The separation edges of G

a

b

g

c

j

d

e

f

i

Proxy graph F

DFS on graph G

a

d

b

c

eh

i

jf

h

g

i

Page 23: 1 The Graph Abstract Data Type CS 5050 Chapter 6

23

Breadth-First Search

• By levels, typically using queues

Page 24: 1 The Graph Abstract Data Type CS 5050 Chapter 6

24

BFS Facts

• There are discovery and cross edges

(why no back edges?) –

Once marked, don’t follow again.

• Discovery edges form spanning tree

• Tree edges are paths, minimal in length

• Cross edges differ by at most one in level

• Try writing the code to do a BFS

Page 25: 1 The Graph Abstract Data Type CS 5050 Chapter 6

25

Thm 6.19: Algorithms based on BFS

• Test for connectivity• compute spanning forest• compute connected components• find shortest path between two points (in number

of links)• compute a cycle in graph, or report none• (have cross edges)• Good for shortest path information, while DFS

better for complex connectivity questions

Page 26: 1 The Graph Abstract Data Type CS 5050 Chapter 6

26

Digraphs

• A digraph is a graph whose edges are all directed– Short for “directed graph”

• Applications– one-way streets

– flights

– task scheduling A

C

E

B

D

Page 27: 1 The Graph Abstract Data Type CS 5050 Chapter 6

27

Digraph Properties

• A graph G=(V,E) such that– Each edge goes in one direction:

• Edge (a,b) goes from a to b, but not b to a.

• If G is simple, m < n*(n-1).• If we keep in-edges and out-edges in separate

adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size.

A

C

E

B

D

Page 28: 1 The Graph Abstract Data Type CS 5050 Chapter 6

28

Digraph Application• Scheduling: edge (a,b) means task a must be

completed before b can be started

The good life

ics141ics131 ics121

ics53 ics52ics51

ics23ics22ics21

ics161

ics151

ics171

Page 29: 1 The Graph Abstract Data Type CS 5050 Chapter 6

29

Digraph Facts

• Directed DFS gives directed paths from root to each reachable vertex

• Used for O(n(n+m)) algorithm [dfs is O(n+m), these algorithms use n dfs searches]– Find all induced subgraphs (from each vertex, v, find

subgraph reachable from v)

– Test for strong connectivity

– Compute the transitive closure

• Directed BFS has discovery, back, cross edges

Page 30: 1 The Graph Abstract Data Type CS 5050 Chapter 6

30

Directed DFS• We can specialize the traversal

algorithms (DFS and BFS) to digraphs by traversing edges only along their direction

• In the directed DFS algorithm, we have four types of edges– discovery edges– back edges– forward edges– cross edges

• A directed DFS starting a a vertex s determines the vertices reachable from s

A

C

E

B

D

Page 31: 1 The Graph Abstract Data Type CS 5050 Chapter 6

31

Reachability

• DFS tree rooted at v: vertices reachable from v via directed paths

A

C

E

B

D

FA

C

E D

A

C

E

B

D

F

Page 32: 1 The Graph Abstract Data Type CS 5050 Chapter 6

32

Strong Connectivity• Each vertex can reach all other vertices

a

d

c

b

e

f

g

Page 33: 1 The Graph Abstract Data Type CS 5050 Chapter 6

33

• Pick a vertex v in G.

• Perform a DFS from v in G.– If there’s a w not visited, print “no”.

• Let G’ be G with edges reversed.

• Perform a DFS from v in G’.– If there’s a w not visited, print “no”.

– Else, print “yes”.

• Running time: O(n+m).

Strong Connectivity Algorithm

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Page 34: 1 The Graph Abstract Data Type CS 5050 Chapter 6

34

• Maximal subgraphs such that each vertex can reach all other vertices in the subgraph

• Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity).

Strongly Connected Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 35: 1 The Graph Abstract Data Type CS 5050 Chapter 6

35

Transitive Closure• Given a digraph G, the

transitive closure of G is the digraph G* such that– G* has the same vertices as

G– if G has a directed path

from u to v (u v), G* has a directed edge from u to v

• The transitive closure provides reachability information about a digraph

B

A

D

C

E

B

A

D

C

E

G

G*

Page 36: 1 The Graph Abstract Data Type CS 5050 Chapter 6

36

Computing the Transitive Closure

• We can perform DFS starting at each vertex– O(n(n+m))

If there's a way to get from A to B and from B to C, then there's a way to get from A to C.

Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm

Page 37: 1 The Graph Abstract Data Type CS 5050 Chapter 6

37

Floyd-Warshall Transitive Closure

• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Consider paths that use only vertices

numbered 1, 2, …, k, as intermediate vertices:

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(add this edge if it’s not already in)

Page 38: 1 The Graph Abstract Data Type CS 5050 Chapter 6

38

Floyd-Warshall’s Algorithm• Floyd-Warshall’s algorithm

numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn

– G0=G

– Gk has directed edge (vi, vj) if G has directed path from vi to vj with intermediate vertices in the set {v1 , …, vk}

• We have that Gn = G*

• In phase k, digraph Gk is computed from Gk 1

• Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

Algorithm FloydWarshall(G)Input digraph G (vertices numbered)Output transitive closure G* of GG0 Gfor k 1 to n do

Gk Gk 1

for i 1 to n (i k) dofor j 1 to n (j i, k) do

if Gk 1.areAdjacent(vi vk) Gk 1.areAdjacent(vk vj)

if Gk.areAdjacent(vi vj) Gk.insertDirectedEdge(vi vj)

return Gn

Page 39: 1 The Graph Abstract Data Type CS 5050 Chapter 6

39

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 40: 1 The Graph Abstract Data Type CS 5050 Chapter 6

40

1 2 3 4 5 6 7

1 y

2

3 y y y

4 y

5 y y

6 y y

7 y y

Page 41: 1 The Graph Abstract Data Type CS 5050 Chapter 6

41

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 42: 1 The Graph Abstract Data Type CS 5050 Chapter 6

42

DAGs and Topological Ordering

• A directed acyclic graph (DAG) is a digraph that has no directed cycles

• A topological ordering of a digraph is a numbering

v1 , …, vn

of the vertices such that for every edge (vi , vj), we have i j

• Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints

Theorem

A digraph admits a topological ordering if and only if it is a DAG

B

A

D

C

E

DAG G

B

A

D

C

E

Topological ordering of

G

v1

v2

v3

v4 v5

Page 43: 1 The Graph Abstract Data Type CS 5050 Chapter 6

43

write c.s. program

play

Topological Sorting

• Number vertices, so that (u,v) in E implies u < v

wake up

eat

nap

study computer sci.

more c.s.

work out

sleep

dream about graphs

A typical student day1

2 3

4 5

6

7

8

9

1011

make cookies for professors

Page 44: 1 The Graph Abstract Data Type CS 5050 Chapter 6

44

• Note: This algorithm is different than the one in Goodrich-Tamassia

• Running time: O(n + m). How…?

Algorithm for Topological Sorting

Method TopologicalSort(G) H G // Temporary copy of G n G.numVertices() while H is not empty do

Let v be a vertex with no outgoing edgesLabel v nn n - 1Remove v from H

Page 45: 1 The Graph Abstract Data Type CS 5050 Chapter 6

45

•Topological Sorting DFS Algorithm

• Simulate the algorithm by using depth-first search

• O(n+m) time.

Algorithm topologicalDFS(G, v)Input graph G and a start vertex v of G

(having no input arcs from unvisited nodes)Output labeling of the vertices of G

in the connected component of v setLabel(v,VISITED)for all edges (v,w)

if getLabel(w) !VISITEDtopologicalDFS(G, w)

Label v with topological number n n n - 1

Algorithm topologicalDFS(G)Input dag GOutput topological ordering of G

n G.numVertices()for all u G.vertices()

setLabel(u, !VISITED)for all v G.vertices() which have no incoming edges.

if getLabel(v) VISITED topologicalDFS(G, v)

Page 46: 1 The Graph Abstract Data Type CS 5050 Chapter 6

46

Topological Sorting Example

a

f

c

g

e

d

b

h

Page 47: 1 The Graph Abstract Data Type CS 5050 Chapter 6

47

Topological Sorting Example

2

7

4

8

56

3

9

Page 48: 1 The Graph Abstract Data Type CS 5050 Chapter 6

48

Topological Sorting Example

2

7

4

8

56

1

3

9