Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Similar presentations


Presentation on theme: "Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first."— Presentation transcript:

1 Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first search (BFS) depth-first search(DFS) topological sort strongly connected components

2 Notation Graph G(V,E) is a data structure defined by a set of vertices V a set of eges E |V| = # of vertices |E| = # of edges Usually omit | | in asymptotic notation  (V, E) means  (|V|, |E|)

3 Adj = adjacency-list representation of G(V,E) is an array of |V| lists Adj[u] contains all vertices adjacent to vertex u For a directed graph, edge (u  v) represented by v in Adj[u].  sum of lengths of adjacency lists = |E| For an undirected graph, edge (u,v) appears as v in Adj[u] and as u in Adj[v].  sum of lengths of adjacency lists = 2|E| Memory requirement is  (V+E) A weighted graph has a function w(u,v) defined on the domain E. The weight of edge (u,v) can be stored as property of vertex v in Adj[u].

4 adjacency-matrix representation : Number the vertices 1,2,...|V| Define |V| x |V| matrix A such that a ij = 1 if (i, j)  E, a ij = 0 otherwise For a weighted graph, set a ij = w(i,j) if (i, j)  E Disadvantage is requires  (V 2 ) memory regardless of |E| Advantage is speed of determining if edge (u,v) is in graph For an undirected graph A is symmetric A sparse graphs mean |E| << |V| 2 adjacency-list representation preferred (saves space) A dense graph means |E| ~ |V| 2 adjacency-matrix representation preferred (speed)

5 Graph representations 1 1

6 Breadth-first search: Given G(V,E) and a source vertex s, Breadth-first search does the following: (1) finds every vertex v reachable from s (2) calculates distance (minimum number of edges) between s and v (3) produces a Breadth-first tree with s as the root and every reachable vertex as a node The path from s to v in the Breadth-first tree corresponds to the shortest path between s and v in G. “Breadth-first” finds all vertices at distance k from s before searching for any vertices at distance k+1.

7 BFS(G,s) pseudo code for each u  V(G) – {s} do color[u]  white, d[u]  ,  [u]  NIL (Initialization: color all vertices white for “undiscovered” set distance from s as infinite, set predecessor in Breadth-first tree to NIL) color[s]  gray, d[s]  0,  [s]  NIL (initialize s as gray for “discovered” i.e. reachable from s A gray vertex has been discovered but its adjacency list has not been searched for links to other vertices. After adjacency list is searched, color is changed to black) Q  0, Enqueue(Q,s) (A “first-in first-out” queue holds a list of the current gray vertices Gray vertices with the smallest distance from s are processed first

8 BFS(G,s) algorithm continued while Q  0 do u  Dequeue(Q) for each v  Adj[u] do if color[v] = white then color[v] = gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) color[u]  black (adj list searched) Runtime analysis : initialization requires O(V). each vertex is discovered at most once  queue operations require O(V). searching adjacency lists requires O(E)  total runtime O(V+E )

9 Example of BFS (p596) using pseudo-code

10 Predecessor of v:  (v) is the vertex in whose adjacency list v was discovered Predecessor sub-graph G  (V ,E  ) V  = {v  V :  [v]  NIL}  {s} E  = {(  [v],v) : v  V  - {s}} Predecessor sub-graph of BFS is a single tree

11 BFS by hand: Unnecessary to perform all steps of pseudo-code Resolve ambiguities by alphabetic order

12 Finding shortest path is most common application of Breadth-first search  (s,u) = shortest path between s and any u  V  (s,u) =  if u is not reachable from s Weight of a path is the sum of the weights of its edges For an unweighted graph, weight of path = number of edges = length of path For weighted graph, least-weight is not necessarily shortest path

13 Theorem 22.5: On termination of BFS(G,s), (1)all reachable vertices have been found (2) d[v] =  (s,v) for v  V (some may be infinite) (3) for any reachable v  s, a shortest path includes  [v] followed by edge (  [v],v).

14 Depth-first search : Explores all edges leaving a given vertex, v, before “backtracking” to explore edges leaving the vertex from which v was discovered Continues until all vertices reachable from a given source are discovered If the graph still contains undiscovered vertices, choose a new source  [v] = u if v was discovered in a search of the adjacency list of u As in BFS, vertices initialized to white, colored gray when discovered, colored back after their adjacency list has been examined d[v] is the timestamp field when v was discovered f[v] is the timestamp field when v was blackened range of timestamps is 1 to 2|V|d[v] < f[v]

15 Classification of edges: Tree edge connects vertex to its predecessor Back edge connects vertex to an ancestor in the same tree (also self loops in directed graph) Forward edge connects vertex to descendant in the same tree Cross edge: all others If in the same tree, then one vertex cannot be an ancestor of the other Edge (u,v) can be classified by the color v when it is first explored white v  tree edge gray v  back edge black v  forward or cross edge

16 DFS Pseudo codes: DFS(G) for each u  V (initialization) do color[u]  white,  [u]  NIL time  0 for each u  V (choose a new source) do if color[u] = white then DFS-Visit(u) DFG-Visit(u) (DFS from source u) color[u]  gray, time  time +1, d[u]  time (vertex u discovered) for each v  adj[u] do if color[v] = white then  [v]  u, DFG-Visit(v) color[u]  black, f[u]  time, time  time +1 (finished with vertex u)

17 DFS: Fig 22.4 p605

18 DFS by hand: unnecessary to perform all steps of pseudo-code

19 CptS 450 Spring 2015 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 17: due 4/11/16 1.ex 22.2-2 p 601Draw G  Show all edges 2.ex 22.3-2 p 610Draw G  Show all edges

20 Properties of DFS: G  of DFS is a forest of trees, each of which reflects the pattern of recursive calls to DFS-Visit Pattern of discovery and finishing times has “parenthesis structure” For any 2 vertices u and v, exactly one of the following is true (1) intervals [d[u], f[u]] and [d[v], f[v]] are disjoint neither u nor v is a descendent of the other edges connecting u and v must be cross edges (2) [d[u], f[u]] is contained entirely in [d[v], f[v]] and u is a descendent of v v -> u forward edge u -> v back edge (3) [d[v], f[v]] is contained entirely in [d[u], f[u]] and v is a descendent of u u -> v forward edge v -> u back edge

21 Corollary of the Parenthesis Theorem: Vertex v is a descendant of u in the depth-first forest of G if and only if d[u] < d[v] < f[v] < f[u]

22 Predecessor subgraph G  = (V,E  ); E  = {(  [v],v) ; v  V and  [v]  NIL} G  contains all the vertices of G; hence, may be multiple trees Predecessor subgraph showing “parenthesis” structure Predecessor subgraph showing non-tree edges Fig 22.5 p607

23 A simpler way to illustrate parenthesis structure

24 Theorem 22.10: DFS of an undirected graphhas no cross edges. The predecessor graph of a DFS of undirected graph is a single tree. One call to DGS-visit will discover all vertices. Hence, no between-tree cross edges. All non-tree edges connect vertices in the same branch. They could have been tree edges if a different path to discover vertices had been taken. Hence, no between-branch cross edges. Non-tree edges in DFS of undirected graph are between related vertices. Text chooses to call then “back” edges.

25 DFS of undirected graph

26 White-Path Theorem: Vertex v is a descendant of u in the predecessor forest of a DFS if and only if at the time d[u] when u is discovered, v can be reached from u by a path consisting entirely of white vertices.

27 Lemma 22:11 A directed graph is acyclic if and only if a DFS yields no back edges Proof: If G has a back edge (u->v) then v is an ancestor of u and (u->v) completes a cycle in G. If G contains cycle c and v is the first vertex discovered in c, then a white path exist to vertex u the last vertex in c By white-path theorem, u is a descendent of v  (u->v) must be a back edge

28 DAG = directed acyclic graph Precedence among events: common use of DFS on DAGs Example: precedence of events getting dressed

29 topological sort: linear ordering of vertices in a dag such that if G contains edge (u->v), then u appears before v in the ordering Pseudocode for topological sort: Topological-Sort(G) DFS(G) yields f[v] for all vertices At each f[v], insert the vertex into the front of a linked list return list New graph topology: all edges point left-to-right f(v) decreases left-to-right

30 Same DFS different source

31 Theorem 22.12: Proof of the correctness of Topological-Sort(G) When edge (u->v) is explored in the DFS of a dag, v must be white or black because if v is gray, then (u->v) is a back edge (we would be exploring the adjacency list of v) and DFS of a dag cannot yield a back edge. If v is white, then v is a descendant of u and f[v] < f[u] (Corollary of the Parenthesis Theorem) If v is black, then f[v] has already been assigned and, since we are still exploring the adj[u], f[v] < f[u]. Thus, for any edge(u->v) in the dag, f[v] < f[u]  in topological sort, v will be on the right of u  all edges point to the right

32 Strongly Connected Components (SCCs) Strongly connected components (SCCs) of directed graph G(V,E) are a set of vertices V SCC  V such that for every pair of vertices u and v, u ~> v and v ~> u (u reachable from v and v reachable from u) Component graph G SCC = (V SCC, E SCC ) Let C 1, C 2,..., C k denote the SCCs of directed graph G V SCC = {v 1, v 2,..., v k } contains a vertex from each SCC edge (v i,v j )  E SCC if G contains edge (x,y) for some x  C i and y  C j (i.e. edges the component graph connect the SCCs of G) Transpose of directed G(V,E) = G T (V,E T ) where E T = {(u,v) : (v,u)  E} (i.e. G T has same vertices a G, but its edges are reversed)

33 SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices Construct the transpose of G Call DFS(G T ) with vertices in order of decreasing f[u] of DFS(G) The vertices of each tree of the DFS(G T ) forest are a SCC of G Example: Fig 22.9 p616

34 SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices Construct the transpose of G Call DFS(G T ) with vertices in order of decreasing f[u] of DFS(G) The vertices of each tree of the DFS(G T ) forest are a SCC of G

35 Same problem different source

36 CptS 450 Spring 2015 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 18: due 4/15/16 1.ex 22.4-1 p 614Topo sort on DAG of Fig 22.8 p615 2.ex 22.5-2 p 620SCCs on graph in Fig 22.6 p611

37 Lemma 22.14 Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v)  E be an edge with u in C and v in C’, then f(C) > f(C’) Case 1: d(C) < d(C’) use white path theorem Case 2: d(C) > d(C’) use parenthesis structure Corollary 22.15 Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v)  E T be an edge with u in C and v in C’, then f(C) < f(C’) Theorems behind the SCC pseudo-code

38 How Strongly-Connected-Components(G) works : In the DFS of G T, start with a vertex in C max, the SCC that has the largest finish time. The search will visit all of the vertices of C max but will not find an edge that connects C max to any other SCC. If such an edge were found to C’, then by Corollary 22.15 of Lemma 22.14 f(C max ) < f(C’), which violates the choice of C max as the SCC with the largest finish time As the source of the next DFS in G T, chose a vertex in C’, the SCC with f(C’) larger than all other finishing time except those in C max. The search will visit all the vertices of C’ and may find some edges that connect it to C max but not to any other SCC. Since the vertices of C max have already been discovered, the only new vertices visited from the second source are those in C’.


Download ppt "Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first."

Similar presentations


Ads by Google