graph algorithms. representations of graphs : undirected graph an undirected graph g have five...
TRANSCRIPT
Graph Algorithms
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
3
45
vertexedge
1 22
5 /1
3 2 4 /4 25 4
1 2 3 4 5
12345
0 1 0 0 11 0 1 1 10 1 0 1 00 1 1 0 11 1 0 0 0
5 3 4 /
5 3 /1 1 /
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
54
1 22
4 /5 /
3 6 5 /4 2 /5 4 /6 6 /
1 2 3 4 512345
0 1 0 1 00 0 0 0 10 0 0 0 10 1 0 0 00 0 0 1 0
6
6 0 0 0 0 0
6001001
Elementary Graph Algorithms 4
BFS(G,s)1. for each vertex u G.V-{s}∈2. u.color = WHITE3. u.d = ∞4. u.π = NIL5. s.color = GREEN6. s.d = 07. s.π = NIL8. Q = empty9. Enqueue(Q,s)10. while Q ≠ empty11. u = Dequeue(Q)12. for each v G.Adj[u]∈13. if v.color == WHITE14. v.color = GREEN15. v.d = u.d + 116. v.π = u17. Enqueue(Q,v)18. u.color = BLACK
Time Complexity: O(|E|+|V|)
The operation of BFS on an undirected graph(a)
0t
v
w
r s
y
u
x
s0
Q
(b)0
t
v w
r s
y
u
x
w
1
Q
1
1
r
1
(c)0
t
v w
r s
y
u
x
r
1
Q
1
1
t
2
2
2
x
(d)0
t
v w
r s
y
u
x
t
1
Q
1
1
x
2
2
2
v
2
2 2
(e)0
t
v w
r s
y
u
x
x
2
Q
1
1
v
2
2
2
u
2
3
3(f)
0t
v w
r s
y
u
x
v
2
Q
1
1
u
3
2
2
y
2
3
3
3
(g)
2
0t
v w
r s
y
u
x
u
3
Q
1
1
y
3
2
22
3(h)
0t
v w
r s
y
u
x
y
3
1
1 2
2
3
33
(i)0
t
v w
r s
y
u
x
Q
1
1 2
2
3
3
2
2
p7.
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.
p8.
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.
p9.
Lemma 22.1 : G=(V,E) : a directed or undirected graph. s : an arbitrary vertex : the shortest-path distance from s to v.
),( vs
Then for any 1 ),(),(,),( usvsEvu Proof :
s
u
v
1),( us
),( vs
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.
Proof :
Then on termination, for each vertex v ∈ V, the value v.d computed byBFS satisfies v.d ≥ δ (s, v).
By induction on the number Enqueue operations.
Basis : when s is placed in Q. s.d =0 = δ(s, s)
),(. vsdv for all }.{sVv
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.
Lemma 22.3 :
Q : <v1,v2,…,vr>~ the queue when BFS executes on a graph G=(V,E).
Then Proof :
1.. 1 dvdvr
By induction on the number of queue operations.Basis : when Q has only s. .1.. dsdsInduction Step :
1.1.. 21 dvdvdvr
by inductive hypothesis.
head tail
and dvdv ii .. 1 for i=1,2,…,r-1.
1> : after dequeue :v2 becomes the new head in Q.
1,...,2for .. 1 ridvdv ii
2> : after enqueue a new vertex v into the Q.Let vr+1 be v.
Note that u’s adjacency list is being scanned and u is just removed.
neighbors
Thus,
And
The rest , for i=1,…,r-1, remain unaffected. dvdv ii .. 1
1.1.. 11 dvdudvr11 old theis where,.1.. vudvdudv rr
p12.
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)=.
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.
By BFS, v is never discovered.
By contradiction, suppose some vertex has a d value not equal to its shortest distance.
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.
p15.Elementary Graph Algorithms 15
Print path 演算法
可在執行過 BFS演算法的圖上印出 s 到 v 的最短路徑。如無路徑也會印出沒有路徑的訊息。
Print-Path(G, s, v)
1. if v == s
2. print s
3. elseif v.π == NIL
4. print “no path from” s “to” v
5. else Print-Path(G,s,v.π)
6. print v
p16.
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
p17.
1/(a) u v w
x y z
1/ 2/(b) u v w
x y z
1/ 2/
3/
(c) u v w
x y z
1/ 2/
4/ 3/
(d) u v w
x y z
Discovery time
p18.
1/ 2/
4/ 3/
(e) u v w
x y z
B
1/ 2/
4/5 3/
(f) u v w
x y z
B
1/ 2/
4/5 3/6
(g) u v w
x y z
B1/ 2/7
4/5 3/6
(h) u v w
x y z
B
p19.
1/ 2/7
4/5 3/6
(i) u v w
x y z
BF
1/8 2/7
4/5 3/6
(j) u v w
x y z
BF
1/8 2/7 9/
4/5 3/6
(k) u v w
x y z
BF1/8 2/7 9/
4/5 3/6
(l) u v w
x y z
BF C
p20.
1/8 2/7 9/
4/5 3/6 10/
(m) u v w
x y z
BF C1/8 2/7 9/
4/5 3/6 10/
(n) u v w
x y z
BF CB
1/8 2/7 9/
4/5 3/6 10/11
(o) u v w
x y z
BF CB
1/8 2/7 9/12
4/5 3/6 10/11
(o) u v w
x y z
BF CB
p21.
(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.
p22.Elementary Graph Algorithms 22
DFS演算法
DFS(G)
1. for each vertex u G.V∈2. do u.color = WHITE
3. u.π = NIL
4. time = 0
5. for each vertex u G.V∈6. if u.color == WHITE
7. DFS-Visit(G,u)
p23.Elementary Graph Algorithms 23
DFS-Visit演算法
DFS-Visit(G, u)
1. time = time+1 //u has just been discovered
2. u.d = time
3. u.color = GRAY
4. for each v G.Adj[u] //explore edge (u,v)∈5. if v.color == WHITE
6. v.π = u
7. DFS-Visit(G, v)
8. u.color = BLACK //DFS-Visit(G, u) is done
9. time = time + 1
10. u.f = time
p24.
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.
p25.
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
p26.
3/6 2/9 1/10
4/5 7/8 12/13
(a) y z s
x w v
B
C C14/15
u
11/16t
F
C
C B
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)
s
z
y w
x
t
v u
(b)
p27.
(c)
s
z
y
x
w
v u
tB
B
C
C
C
C
F
p28.
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
p29.
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.du.d < u.f < v.d < v.f (1)
holds2. 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
p30.
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
p31.
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.fThm7 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.fv must be discovered after u is discovered,but before w is finished.
uw v
uw v
Descendant of u
p32.
Thm 22.10: In a DFS of an undirected graph G, every edge of G is either a tree edge or a back edgePf:
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
E ) v u, (
p33.
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
socks undershorts pants shoes watch shirt belt tie jacket
undershorts socks
pants shoes
beltshirt
tie
jacket
watch11/16
12/15
6/7
1/8
2/5
3/4
17/18
13/14
9/10
p34.
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
beltshirt
tie
jacket
watch11/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/71/8 3/413/1417/18 9/10 2/5
p35.
Lemma 22.11A 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.
p36.
Thm 22.12TOPOLOGICAL-SORT(G) produces a topological
sort of Gpf:
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
p37.
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.
13/14 11/16
12/15 3/4
1/10
2/7
8/9
5/6
a b c d
e f g h
G:
13/14 11/16
12/15 3/4
1/10
2/7
8/9
5/6
a b c d
e f g h
GT:
abe
cd
h
fg
p39.
StronglyConnectedComponents(G)1. Call DFS(G) to compute finishing times u.f for each vertex u2. Compute GT
3. Call DFS(GT), but in the main loop of DFS, consider the
vertices in order of decreasing u.f4. Output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component
Time: (V+E)
p40.
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
p41.
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’).
p42.
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 ).
TE
p43.
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 connectedcomponent.
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.
p44.
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.