Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Algorithms.

Similar presentations


Presentation on theme: "Graph Algorithms."— Presentation transcript:

1 Graph Algorithms

2 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 edge 1 2 3 4 5 vertex 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 1 1 2 1 1 1 1 3 1 1 4 1 1 1 5 1 1

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 1 2 3 4 5 6 1 2 4 / 5 3 6 1 2 3 4 5 6 1 1 1 2 1 3 1 1 4 1 5 1 6 1

4 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 Time Complexity: O(|E|+|V|) 第一個迴圈,初始化除s外所有的節點成白色,距離無限大,前一個點為空。 之後初始化s為綠色,距離0,前一個點是空。至此花去O(|V|) 接下來將s放入Q中,之後反覆執行Dequeue,直到Q空了為止。 每個Dequeue出來的點u,均將其白色的鄰居放入Q中, 並且將他們塗成綠色,距離設定成d[u]+1以及將前一個點設定為u。 處理完所有的鄰居之後,就將u塗成紅色。 因此每個點被放入Q一次,並且一個點被檢驗是不是白色的總次數為|E|, 故此部分花費O(|E|+|V|)的時間。 Elementary Graph Algorithms

5 The operation of BFS on an undirected graph
1 1 v w x y v w x y Q Q s w r 1 1 (c) r s t u (d) r s t u 1 2 1 2 1 2 2 1 2 v w x y v w x y Q r t x Q t x v 1 2 2 1 2 2 (e) r s t u (f) r s t u 1 2 3 1 2 3 2 1 2 2 1 2 3 v w x y v w x y Q x v u Q v u y 2 2 3 2 3 3

6 (g) r s t u (h) r s t u 1 2 3 1 2 3 2 2 1 2 3 2 1 2 3 v w x y v w x y Q u y y 3 3 3 (i) r s t u 1 2 3 2 1 2 3 v w x y Q

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

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

9 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: s u v 1

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

11 Lemma 22.3: Q:<v1,v2,…,vr>~ the queue when BFS executes on a graph G=(V,E). head tail Then and for i=1,2,…,r-1. 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.

12 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).

13 from s to v.π then traversing 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 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).

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

15 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 此演算法利用BFS建立的表格π,並利用遞迴的方式, 利用之前提過的BFS性質, (π[v],v)是某一條最短路徑上的一邊, 找出一條path並且列印出來。 Elementary Graph Algorithms

16 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

17 (a) (b) 1/ 1/ 2/ (c) (d) 1/ 2/ 1/ 2/ 3/ 4/ 3/ u v w u v w x y z x y z
Discovery time (a) u v w (b) u v w 1/ 1/ 2/ x y z x y z (c) u v w (d) u v w 1/ 2/ 1/ 2/ 3/ 4/ 3/ x y z x y z

18 (e) (f) 1/ 2/ 1/ 2/ 4/ 3/ 4/5 3/ (g) (h) 1/ 2/ 1/ 2/7 4/5 3/6 4/5 3/6
u v w (f) u v w 1/ 2/ 1/ 2/ B B 4/ 3/ 4/5 3/ x y z x y z (g) u v w (h) u v w 1/ 2/ 1/ 2/7 B B 4/5 3/6 4/5 3/6 x y z x y z

19 (i) (j) 1/ 2/7 1/8 2/7 4/5 3/6 4/5 3/6 (k) (l) 1/8 2/7 9/ 1/8 2/7 9/
u v w (j) u v w 1/ 2/7 1/8 2/7 B F B F 4/5 3/6 4/5 3/6 x y z x y z (k) u v w (l) u v w 1/8 2/7 9/ 1/8 2/7 9/ B F B C F 4/5 3/6 4/5 3/6 x y z x y z

20 (m) (n) 1/8 2/7 9/ 1/8 2/7 9/ 4/5 3/6 10/ 4/5 3/6 10/ (o) (o) 1/8 2/7
u v w (n) u v w 1/8 2/7 9/ 1/8 2/7 9/ B C F B C F B 4/5 3/6 10/ 4/5 3/6 10/ x y z x y z (o) u v w (o) u v w 1/8 2/7 9/ 1/8 2/7 9/12 B C B C F F B B 4/5 3/6 10/11 4/5 3/6 10/11 x y z x y z

21 (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.

22 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

23 DFS-Visit演算法 DFS-Visit(G, u)
time = time //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 Elementary Graph Algorithms

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

25 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

26 3/6 2/9 1/10 4/5 7/8 12/13 (a) 14/15 11/16 y z s x w v B C u t F (b) s
( s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)

27 (c) s C t B F z C u v B C y w C x

28 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

29 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

30 Cor8: v is a proper descendant of vertex u in
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

31 Pf: “” v: descendant of u “” u.d < w.d, by the above cor.
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. u w v Thus w is white at time u.d u w v Descendant of u w.f <= u.f v must be discovered after u is discovered, but before w is finished.

32 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

33 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 pants shoes belt shirt tie jacket watch 11/16 12/15 6/7 1/8 2/5 3/4 17/18 13/14 9/10 socks undershorts pants shoes watch shirt belt tie jacket

34 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 undershorts socks pants shoes belt shirt tie jacket watch 11/16 12/15 6/7 1/8 2/5 3/4 17/18 13/14 9/10 socks undershorts pants shoes watch shirt belt tie jacket 11/16 12/15 6/7 1/8 3/4 13/14 17/18 9/10 2/5

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

36 Thm 22.12 TOPOLOGICAL-SORT(G) produces a topological sort of G pf: Suppose DFS is run to determine 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

37 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, vU, 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.

38 G: GT: 13/14 11/16 12/15 3/4 1/10 2/7 8/9 5/6 a b c d e f g h abe cd h

39 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)

40 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 }

41 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 y.d 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’).

42 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 ).

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

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


Download ppt "Graph Algorithms."

Similar presentations


Ads by Google