440 likes | 593 Vues
Graph Algorithms. 1. 2. 3. 5. 4. edge. vertex. Representations of graphs:undirected graph An undirected graph G have five vertices and seven edges An adjacency-list representation of G The adjacency-matrix representation of G. 1. 2. 5. /. 2. 1. 5. 3. 4. /. 3. 2. 4. /. 4.
E N D
1 2 3 5 4 edge vertex • Representations of graphs:undirected graph • An undirected graph G have five vertices and seven edges • An adjacency-list representation of G • The adjacency-matrix representation of G 1 2 5 / 2 1 5 3 4 / 3 2 4 / 4 2 5 3 / 5 4 1 1 / 1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 1 3 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 0 0
1 2 4 / 2 5 / 3 6 5 / 4 2 / 5 4 / 6 6 / 1 2 3 • Representations of graphs:directed graph • An directed graph G have six vertices and eight edges • An adjacency-list representation of G • The adjacency-matrix representation of G 4 5 6 1 2 3 4 5 6 1 0 1 0 1 0 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1
Elementary Graph Algorithms Time Complexity: O(|E|+|V|) BFS(G,s) • for each vertex u∈ G.V-{s} • u.color = WHITE • u.d = ∞ • u.π = NIL • s.color = GREEN • s.d = 0 • s.π = NIL • Q = empty • Enqueue(Q,s) • while Q ≠ empty • u = Dequeue(Q) • for each v∈ G.Adj[u] • if v.color == WHITE • v.color = GREEN • v.d = u.d + 1 • v.π = u • Enqueue(Q,v) • u.color = BLACK
r s t u r s t u (b) (a) 0 1 0 • The operation of BFS on an undirected graph 1 x v w y x v w y Q w r s Q 0 1 1 r s t u r s t u (c) (d) 0 1 2 0 1 2 1 1 2 2 2 x v w y x v w y Q Q x r v t t x 2 1 2 2 1 2 r s t u r s t u (e) (f) 0 0 1 1 2 2 3 3 1 1 3 2 2 2 2 x x v w y v w y Q Q u y x v v u 3 3 2 2 2 3
r s t u r s t u (g) (h) 0 0 1 1 2 2 3 3 2 1 1 3 2 3 2 2 2 x x v w y v w y Q u y y 3 3 3 r s t u (i) 0 1 2 3 1 3 2 2 x v w y Q
Breadth-first search: • Initially, vertices are colored white. • Discovered vertices are colored green. • When done, discovered vertices are colored black. • u.d stores the distance from s to u. • u.π is a predecessor to u on its shortest path. • Q is a first-in first-out queue.
Properties of Breadth-first search: • After execution of BFS, all nodes reachable from the source s are colored black. • After execution of BFS, v.d is the distance of a shortest path from the source s to v for vertices v reachable from s. • After execution of BFS, if v is reachable from s, then one of the shortest paths to v passes through the edge( v.π, v) at the end. • After execution of BFS, the edges( v.π, v ) for v reachable from s form a breadth-first tree.
u 1 s v • Lemma 22.1: • G=(V,E):a directed or undirected graph. • s:an arbitrary vertex • : the shortest-path distance from s to v. Then for any Proof:
Lemma 22.2: • G=(V,E):a directed or undirected graph. • s:an arbitrary vertex • u.d: the distance from s to u computed by the algorithm Suppose that BFS is run on G from s. Then on termination, for each vertex v ∈ V, the value v.d computed by BFS satisfies v.d ≥δ (s, v). Proof: By induction on the number Enqueue operations. Basis: when s is placed in Q. s.d =0 = δ(s, s) for all Induction Step: Consider a white vertex v discovered during the search from a vertex u. Inductive hypothesis implies u.d ≥δ (s, u). By Lemma 22.1, v.d = u.d +1 ≥ δ(s, u) + 1 ≥ δ(s, v) From then on, v.d will not be changed again.
head tail Then and for i=1,2,…,r-1. • Lemma 22.3: • Q:<v1,v2,…,vr>~ the queue when BFS executes on a graph G=(V,E). Proof: By induction on the number of queue operations. Basis: when Q has only s. Induction Step: 1>: after dequeue: v2 becomes the new head in Q. by inductive hypothesis. 2>:after enqueue a new vertex v into the Q. Let vr+1 be v. neighbors Note that u’s adjacency list is being scanned and u is just removed. Thus, And The rest , for i=1,…,r-1, remain unaffected.
Thm 22.5: (Correctness of BFS) • 1. During the execution of BFS on G=(V,E), BFS discovers every vertex v ∈ V that is reachable from s, and on termination v.d = δ(s, v) • 2. For any vertex v ≠ s reachable from s, one of the shortest paths from s to v is the shortest path from s to v.π followed by the edge ( v.π, v).
Proof: If v unreachable from s, s.d (s, v)=. By BFS, v is never discovered. By contradiction, suppose some vertex has a d value not equal to its shortest distance. Let v be the vertex with minimum (s, v) that has an incorrect d value. Let u be v’s immediate predecessor on the shortest path from s to v. So(s, v) = (s, u) +1. Because (s, u) < (s, v), we have u.d=(s, u). (*) Thus v.d > (s, v) = (s, u) + 1= u.d + 1. Consider the time when BFS chooses to dequeue u from Q. At this time v is either white, gray or black. Case 1: (v is white) line 15 set v.d = u.d + 1---contradicting (*) Case 2: (v is black) v is already not in Q, thus v.d u.d --- contradicting (*) Case 3: (v is gray) v became gray while dequeuing some vertex w, which was removed earlier than u. Thus v.d= w.d +1 and w.d u.d and so v.d u.d + 1 – again a contradiction! Thus we conclude v.d = (s, v) for all v. All vertices reachable from s must be discovered. If v.π=u, then v.d = u.d +1. Thus, we can obtain a shortest path from s to v by taking a shortest path from s to v.π then traversing the edge (v.π, v).
Breadth-first trees: For a graph G=(V, E) with source s, define the predecessor subgraph of G as G’=(V’, E’), where V’ = { v V: v.π≠ NIL} {s} and E’ = {(v.π, v): v V’- {s}}. G’ is a BF tree if V’ consists of the vertices from s to v in G’ and, for all v in V’, there is a unique simple path from s to v, which is also a shortest path from s to v in G. Lemma 22. 6: When applied to a graph G=(V, E), procedure BFS construct π so that G’= (V’, E’) is a BF tree. Proof: Line 16 of BFS sets v.π =u iff (u, v) ∈ E and (s, v) < . Thus V’ consists of the vertices in V reachable from s. Since G’ forms a tree, it contains a unique path from s to each vertex in V’. By Thm 22.5, inductively, we have that every such path is a shortest path.
Elementary Graph Algorithms Print path演算法 • 可在執行過BFS演算法的圖上印出s到v的最短路徑。如無路徑也會印出沒有路徑的訊息。 Print-Path(G, s, v) • if v == s • print s • elseif v.π == NIL • print “no path from” s “to” v • else Print-Path(G,s,v.π) • print v
Depth-first search • Nodes are initially white • Nodes become gray when first discovered • Nodes become black when they are done • v.d records when v is first discovered • v.f records when v is done • u.d< u.f
Discovery time (a) (b) v w v w u u 1/ 1/ 2/ y z y z x x (c) (d) v w v w u u 1/ 2/ 1/ 2/ 3/ 4/ 3/ y z y z x x
(e) (f) v w v w u u 1/ 2/ 1/ 2/ B B 4/ 3/ 4/5 3/ y z y z x x (g) v w u (h) v w u 1/ 2/ 1/ 2/7 B B 4/5 3/6 4/5 3/6 y z x y z x
(i) v w u (j) v w u 1/ 2/7 1/8 2/7 B B F F 4/5 3/6 4/5 3/6 y z x y z x (k) v w u (l) v w u 1/8 2/7 9/ 1/8 2/7 9/ B F B C F 4/5 3/6 4/5 3/6 y z x y z x
(m) v w u (n) v w u 1/8 2/7 9/ 1/8 2/7 9/ B C B F C F B 4/5 3/6 10/ 4/5 3/6 10/ y z x y z x (o) (o) v w v w u u 1/8 2/7 9/ 1/8 2/7 9/12 B B C C F F B B 4/5 3/6 10/11 4/5 3/6 10/11 y z y z x x
(u,v) • Back edges: if u is connected to an ancestor v in a depth- first tree. (eg self-loop) • Forward edges: if u is connected to an descendant v in a depth-first tree. • Cross edges: if u is not connected to an ancestor v in the same depth-first tree. OR if v is not connected to an ancestor u in the same depth-first tree. OR if u and v belong to different depth-first trees.
Elementary Graph Algorithms DFS演算法 DFS(G) • for each vertex u∈ G.V • do u.color = WHITE • u.π = NIL • time = 0 • for each vertex u ∈ G.V • if u.color == WHITE • DFS-Visit(G,u)
Elementary Graph Algorithms DFS-Visit演算法 DFS-Visit(G, u) • time = time+1 //u has just been discovered • u.d = time • u.color = GRAY • for each v ∈ G.Adj[u] //explore edge (u,v) • if v.color == WHITE • v.π = u • DFS-Visit(G, v) • u.color = BLACK //DFS-Visit(G, u) is done • time = time + 1 • u.f = time
The running time of DFS is O(V+E) • After execution of DFS, all nodes are colored black • After execution of DFS, the edges( v.𝛑, v) form a collection of depth-first tree, called a depth-first forest.
Edge Classification • 1. Tree edges( u, v ): u was discovered first using ( u,v ) • 2. Back edges( u, v ): v is an ancestor of u in the DFS tree • 3. Forward edges( u, v ): v is a descendent of u, not a tree edge • 4. Cross edges( u, v ): Other edges • Example • In a depth-first search of an undirected graph, every edge is either a tree edge or a back edge
(a) t z s y 11/16 3/6 2/9 1/10 B F C B 14/15 4/5 7/8 12/13 C C C u w v x (b) s t z v u w y x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ( s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
(c) s t C B F z C u v B C y w C x
Thm 22.7: (Parenthesis theorem) In any DFS of a graph G=( V,E ), for any two vertices u and v, exactly one of the following 3 conditions holds: (1). The intervals [u.d, u.f] and [v.d, v.f] are disjoint, (2). The interval [u.d, u.f] is contained entirely within the interval [v.d, v.f], and u is a descendant of v in the depth-first tree, (3). or as above with v as a descendant of u
Pf: 1. If u.d < v.d case 1: v.d < u.f : So v is finished before finishing u. Thus (3) holds case 2: u.f < v.d u.d < u.f < v.d < v.f (1) holds 2. If v.d < u.d: Similarly, by switching the roles of u and v v was discovered while u was still gray. • v is a descendant of u, and all v’s outgoing edges are explored
Cor8: v is a proper descendant of vertex u in the depth-first forest for a graph G iff u.d < v.d < v.f < u.f • Thm9: (White-path theorem) In a DF forest of G=( V,E ), vertex v is a descendant of u iff at the time u.d that the search discovers u, v can be reached from u along a path consisting entirely of white vertices
u w v w v u Descendant of u • Pf: “” v: descendant of u “” Assume at time u.d, v is reachable from u along a path of white vertices, but v does not become a descendant of u in DF tree Thus, u.d < v.d < w.f <= u.f Thm7 implies [v.d, v.f] is contained entirely in [u.d, u.f] By Cor8, v is a descendant of u. u.d < w.d, by the above cor. Thus w is white at time u.d w.f <= u.f v must be discovered after u is discovered, but before w is finished.
Thm 22.10: In a DFS of an undirected graph G, every edge of G is either a tree edge or a back edge Pf: Let and suppose u.d < v.d. Then v must be discovered and finished before finishing u • If (u, v) is explored first in the direction from u to v, then (u, v) becomes a tree edge • If (u, v) is explored first in the direction from v to u, then (u, v) is a back edge, since u is still gray at the time (u, v) is first explored
undershorts socks 17/18 11/16 watch 9/10 12/15 pants shoes 13/14 1/8 shirt belt tie 2/5 6/7 jacket 3/4 socks undershorts pants shoes watch shirt belt tie jacket • Topological sort • 定義:A topological sort of a dag G=(V, E) is a linear ordering of all its vertices. (dag: Directed acyclic graph)如 edge(u, v), u appears before v in the ordering
undershorts socks 17/18 11/16 watch 9/10 12/15 pants shoes 13/14 1/8 shirt belt tie 2/5 6/7 jacket 3/4 socks undershorts pants shoes watch shirt belt tie jacket 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 • TOPOLOGICAL-SORT(G): (V+E) 1. Call DFS(G) to compute finishing times v.f for each vertex v (V+E) 2. As each vertex is finished, insert it onto the front of a linked list O(1) 3. Return the linked list of vertices
Lemma 22.11 A directed graph G is acyclic iff DFS(G) yields no back edges. pf: Suppose there is a back edge (u,v), v is an ancestor of u. Thus there is a path from v to u and a cycle exists. Suppose G has a cycle c. We show DFS(G) yields a back edge. Let v be the first vertex to be discovered in c, and (u,v) be the preceding edge in c. At time v.d, there is a path of white vertices from v to u. By the white-path thm., u becomes a descendant of v in the DF forest. (u,v)is a back edge.
Thm 22.12 TOPOLOGICAL-SORT(G) produces a topological sort of G pf: Suppose DFS is run to determinate finishing times for vertices. It suffices to show that for any pair of u,v , if there is an edge from u to v, then v.f< u.f. When (u,v) is explored by DFS(G), v cannot be gray. Therefore v must be either white or black. 1. If v is white, it becomes a descendant of u, so v.f < u.f 2. If v is black, then v.f < u.f
Strongly connected components: • A strongly connected component of a directed graph G(V,E) is a maximal set of vertices U V s.t. for every pair u, vU, u and v are reachable from each other. • Given G=(V,E), define GT=(V,ET), where ET={(u,v): (v,u)E}Given a G with adjacency-list representation, it takes O(V+E) to create GT. • G and GT have the same strongly connected components.
a b c d 13/14 11/16 1/10 8/9 G: 12/15 3/4 2/7 5/6 cd e f g h abe h a b c d 13/14 11/16 1/10 8/9 fg GT: 12/15 3/4 2/7 5/6 e f g h
StronglyConnectedComponents(G) 1. Call DFS(G) to compute finishing times u.f for each vertex u 2. Compute GT 3. Call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing u.f 4. Output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component • Time: (V+E)
Lemma 22.13: Let C and C’ be distinct strongly connected components in directed graph G=(V, E), let u, v in C and u’, v’ in C’, and suppose there is a path from u to u’ in in G. Then there cannot also be a path from v’ to v in G. Def: Let U V, then we define d(U) = min { u.d } u U f (U) = max { u.f } u U
Lemma 22.14 Let C and C’ be distinct strongly connected components in directed graph G=(V, E). Suppose that there is an edge (u, v) in E, where u in C and v in C’. Then f (C) > f ( C’). pf: (1) If d(C) < d(C’): let x be the 1st discovered vertex in C. At time x.d all vertices in C and C’ are white. There is a path from x to all (white) vertices in C and to all vertices in C’: x~↝ u v ~↝ w. All vertices in C and C’ are descendants of x. Thus x.f = f (C) > f(C’). (2) If d(C) > d(C’): let y be the 1st discovered vertex in C’. At time d[y] all vertices in C’ are white and there is a path from y to all vertices in C’. I.e. all vertices in C’ are descendants of y. So y.f = f(C’). There cannot be a path from C’ to C. Thus any w in C has w.f > y.f and so f(C) > f(C’).
Corollary 22.15: Let C and C’ be distinct strongly connected components in directed graph G=(V, E). Suppose that there is an edge (u, v) in , where u in C and v in C’. Then f ( C ) < f ( C’ ). Pf: It is clear that (v, u) is in E. By the previous lemma we have f( C’ ) > f ( C ).
Thm 22.16 Strongly-Connected-Component(G) correctly computes the strongly connected components of a directed graph. pf: By induction on the number (k) of depth-first trees found in the DFS of GT, where each tree forms a strongly connected component. Basis: trivial for k=0. Inductive step: assume each of the first k DFS trees produced in line 3 is a SCC. Consider the (k+1)-st tree produced. Let u be the root of this tree and let u be in SCC C. Thus u.f = f ( C ) > f ( C’ ) for any other SCC C’ yet to be visited. Note we visit in decreasing finishing time.
All other vertices in C are descendant of u in its DFS. By inductive hypothesis and the previous Corollary 15 any edges in GT that leave C must be to SCC’s already visited. Thus, no vertex in any SCC other than C will be a descendant of u during the DFS of GT. Thus, the vertices of the DFS tree in GT that is rooted at u form exactly one SCC.