1 / 34

Chapter 22: Elementary Graph Algorithms

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. Notation.

kisha
Download Presentation

Chapter 22: Elementary Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Q(V, E) means Q(|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 Q(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 aij = 1 if (i, j)  E, aij = 0 otherwise For a weighted graph, set aij = w(i,j) if (i, j)  E Disadvantage is requires Q(V2) 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

  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 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] , p[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, p[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 Resolve ambiguity by some rule like alphabetical order)

  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 p[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

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

  11. Finding shortest path is most common application of Breadth-first search d(s,u) = shortest path between s and any u  V d(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 BFS of weighted graph may not find the least weight path

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

  13. 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 p[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]

  14. 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 Edges (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

  15. DFS Pseudocodes: DFS(G) for each u  V do color[u]  white, p[u]  NIL time  0 for each u  V do if color[u] = white then DFS-Visit(u) DFG-Visit(u) color[u]  gray, time  time +1, d[u]  time (vertex u discovered) for each v  adj[u] do if color[v] = white thenp[v]  u, DFG-Visit(v) color[u]  black, f[u]  time, time  time +1 (finished with vertex u)

  16. DFS: Fig 22.4 p605

  17. Predecessor subgraph Gp = (V,Ep); Ep = {(p[v],v) ; v  V and p[v]  NIL} Gp contains all the vertices of G; hence, may be multiple trees Predecessor subgraph showing Non-tree edges B-edge: head () contains tail () F-edge: tail () contains head () C-edge: connects disjoint () Note C-edges inside trees. Predecessor subgraph showing “parenthesis” structure

  18. Properties of DFS: Gp of DFS is a forest of trees, each of which reflects the pattern of recursive calls to DFS-Visit (slide 15) Pattern of discovery and finishing times has “parenthesis structure” (slide 18) 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 and neither u nor v is a descendent of the other (2) [d[u], f[u]] is contained entirely in [d[v], f[v]] and u is a descendent of v (3) [d[v], f[v]] is contained entirely in [d[u], f[u]] and v is a descendent of u. 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]

  19. Theorem 22.10: Only tree and back edges occur in a DFS of an undirected graph In DFS of undirected graph, edge type is determined by direction of search when edge is encountered. Consider edge(u,v) with v in u’s adjacency list. If edge(u,v) is traversed from u to v, then v is discovered on the traversal and edge(u,v) is a tree edge. The only remaining option for traversal of edge(u,v) is from v to u = p(v), which we call a “back” edge in an undirected graph. Predecessor graph is a single tree

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

  21. topological sort: linear ordering of vertices in a dag such that if G contains (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: Same V and E but all edges point to the right f(v) decreases left-to-right

  22. White-Path Theorem: Vertex v is a descendant of u in the depth-first forest of G (directed or undirected) 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.

  23. 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 decendent of v  (u,v) must be a back edge

  24. 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 in slide #19) 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

  25. Strongly Connected Components (SCCs) Strongly connected components (SCCs) of directed graph G(V,E) are a set of vertices VSCC 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 GSCC = (VSCC, ESCC) Let C1, C2, ..., Ck denote the SCCs of directed graph G VSCC = {v1, v2, ..., vk} contains a vertex from each SCC edge (vi,vj)  ESCC if G contains edge (x,y) for some x  Ci and y  Cj (i.e. edges the component graph connect the SCCs of G) Transpose of directed G(V,E) = GT(V,ET) where ET = {(u,v) : (v,u)  E} (i.e. GT has same vertices a G, but its edges are reversed)

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

  27. CptS 450 Spring 2014 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 10: due 4/28/14 1. ex 22.2-1 p 601 BFS graph in Fig 22.2a p590 2. ex 22.2-2 p 601 BFS graph in Fig 22.3 p596 different sourse 3. ex 22.3-2 p 610 DFS graph in Fig 22.6 p611 4. ex 22.4-1 p 614 Topo sort on DAG of Fig 22.8 p615 5. ex 22.5-2 p 620 SCCs on graph in Fig 22.6 p611

  28. Theorems behind the SCC pseudo-code 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)  ET be an edge with u in C and v in C’, then f(C) < f(C’)

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

  30. Ex 23.1-1: (u,v) is minimum-weight edge in a connected graph G. Show that (u,v) is in some MST of G Proof: Let A be null set edges. Let (s, v-s) be any cut that crosses (u,v) By Theorem 23.1 (u,v) is a safe edge of A Theorem 23.1 Let (S,V-S) be a cut in G that respects A. Let (u,v) be a light edge crossing (S,V-S). Then (u,v) is a safe edge for A

  31. Ex 23.1-3: (u,v) is contained in some MST of G. Show that (u,v) is a light edge crossing some cut of G Proof: Removal (u,v) from MST breaks it into 2 parts. This allows for a cut that respects the 2 parts and crosses (u,v). (u,v) is a light edge because any other edge crossed by cut was not part of MST.

  32. Ex 23.1-4: Every edge (u,v) of a connected graph has the property that there exist a cut (s, v-s) such that (u,v) is a light edge of (s, v-s). Give a simple example of such a graph that is not an MST w(A,B) = w(B,C) = w(C,A) A Not an MST because cyclic C B

  33. Ex 23.1-6: For every cut of G there exist a unique light edge. Show that the MST of G is unique. Proof: Given MSTs T and T’ of G, show that T = T’ Remove (u,v) for T then there exist a cut (s,v-s) such that (u,v) is a light edge (ex 12.1-3) Suppose (x,y) of T’ also crosses (s,v-s), then it is also a light edge. Otherwise it would not be part of T’ (ex 12.1-3) But for every cut of G the light edge is unique. Therefore (u,v) = (x,y) and T = T’

  34. Ex 23.1-10: T is an MST of G that includes (x,y). w(x,y) is reduced by k. Show that T remains and MST of G. Proof: Let w(t) be the sum of weights of any spanning tree t Let w’(t) be the sum of weights after w(x,y) is reduced by k Let T’ by any spanning tree that is different from MST T. Then w(T’) > w(T) If (x,y) is not part of T’ then w’(T’) = w(T’) > w(T) > w’(T) If (x,y) is part of T’ then w’(T’) = w(T’) – k > w(T) – k = w’(T) In either case, w’(T’) > w’(T); hence T remains an MST

More Related