Upload
skip
View
42
Download
1
Embed Size (px)
DESCRIPTION
Graph Algorithms. 圖形 G 是由兩個集合 V 和 E 所構成的,可以寫成 G =( V,E ) 。其中 V 是非空的由有限個頂點 (Vertex) 所構成的集合, E 則是由頂點對所構成的集合,這些頂點對叫做邊 (edge) 。 V ( G ) 和 E ( G ) 各表示組成 G 的頂點集合和邊集合。依據 E 中邊之型態,所組成之圖形又可分成下列兩種: E 中之邊 沒有方向性 ,亦即 ( V 1, V 2 ) 和 ( V 2, V 1 ) 表示相同的邊,如此構成之圖形稱作無向圖形( undirected graph )。 - PowerPoint PPT Presentation
Citation preview
Graph Algorithms
p2.
圖形 G 是由兩個集合 V 和 E 所構成的,可以寫成G=(V,E) 。其中 V 是非空的由有限個頂點(Vertex) 所構成的集合, E 則是由頂點對所構成的集合,這些頂點對叫做邊 (edge) 。 V(G) 和 E(G)各表示組成 G 的頂點集合和邊集合。依據 E 中邊之型態,所組成之圖形又可分成下列兩種: E 中之邊沒有方向性,亦即(V 1, V 2 )和(V 2, V
1 )表示相同的邊,如此構成之圖形稱作無向圖形( undirected graph )。
E 之邊沒有方向性,頂點對(邊)以<V 1, V 2 >表示,其中V2表頭( head ),V 1 表尾( tail );很自然地,<V 2, V 1 >和,<V 1, V 2 >是完全不同的兩個邊。以此種邊集合構成之圖形稱作有向圖形( directed graph ,又稱 digraph )。
p3.
complete graph :各種頂點的組合均存在之圖形稱作complete graph 。無向圖形若有 n 個頂點,則最大可能之邊組合有()= 種,而有向圖形將有兩倍於此為 n(n-1) 種組合;含有 n 個頂點之 complete graph 將有如上數目之邊。
subgraph :若 Ĝ 為G之 subgraph ,則 Ĝ 為一 graph ,且V (Ĝ) < V(V) , E(G) < E(G) 。
path :由圖形中頂點所構成的序列V p, V 1, V 2,… VN,V q ,若其中 ( V p, V 1), ( V 1, V 2),… , ( VN, V q)均為圖形中之邊,則此序列稱做一 path ;一 path 中 edge之數目稱做該 path 之長度;在有向圖形之情況下,則要求<Vp, V 1 > , <V 1, V 2 > ,… ,<VN, V q >均為有向edge ,而此 path 又稱做 directed path 。
Simple path :在上面之定義中,其除了V p 和V q 之外,其他vertex 均不重複出現的 path 稱之。在 simple path 中,若V p=V q ,則稱之為 cycle 或 circuit 。一 graph 若有 cycle 則稱cyclic ,反之則可稱做 acyclic 。
一 path 若不為 simple ,則其必有相交之情況。
p4.
頂點和邊之關係 , 以“ adjacent” 和“ incident” 描述之:
V 1 V 2 V 1 V 2
E 1 E 2
( 有向 )
( 無向 )
V 1 和V 2 為 adjacent V 1 adjacent fromV 2E 1 incident on V 1( V 2) V 2 adjacent to V 1
E 2 incident to V 1( V 2)
p5.
相連( Connected ) connected of two vertices :若一圖形中之兩頂點間存在
任何 path ,則稱他們為 connected 的。在有向圖形中,若兩點頂間存在自其中某頂點到另一頂點和回來之有向 path ,則稱此兩頂點為 strongly connected 。
connected of a graph :若一圖形其所有頂點對均為 6 connected ,則此 graph 為 connected :相似地,若一有向圖形所有頂點對間均為 strongly connected ,該圖形亦為 strongly connected 。
degree (of a vertex) :表示一個 vertex 所adjacent 之其他 vertices 之個數。在有向圖形中,degree 又分成 in-degree 和 out-degree ,其中前者表示該 vertex 所 adjacent from 之vertex 個數,而後者表示所 adjacent to 之vertex 個數。
p6.
connected component :對無向圖形而言,其connected component (或 component )表示其孤立的 connected 的子圖,這可能有好幾個。在有向圖形中, strongly connected component 表示一盡量伸展而仍然保持 strongly connected 的子圖(不一定要孤立)。以下四個圖形G1 到G4 前三個均只有一個 component (G3沒有 strongly connected component ),而G4 有兩個 component 。
1
2 3
4G1
1
2 3
7654
G2
2
1
3
▼ ▲
G3
1
3 2
4 G4
H1 5
6 7
8
H2
p7.
1 4
2 3
路徑長度為 n 之鄰接矩陣
p8.
在找出路徑矩陣 p 時,首先要找到 a2 ,然後 a3 ,一直到 an ,最後再將 a1 , a2 …, , an 加起來得到 Sn ,再由 Sn 得到 p 。
Warshall’s algorithm:1. p←a (把鄰接矩陣 a 拷貝至 p )2. for ( k=1 ; k<=n ; k++ ) for ( i=1 ; i<=n ; i++ ) for ( j=1 ; j<=n ; j++ ) p[i][j]=p[i][j] | p[i][k]&p[k][j] ;
Warshall’s algorithm
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
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)
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
22
3
33
(i)0
t
v w
r s
y
u
x
Q
1
1 2
22
3
3
p13.
Breadth-first search : Initially, vertices are colored white. Discovered vertices are colored green. When done, discovered vertices are colored black. d[u] stores the distance from s to u. is a predecessor to u on its shortest path. Q is a first-in first-out queue.
][u
Algorithm :
Complexity :
BFS(G,s)
for each vertex u in V[G] – {s}
do color[u] <- white1.2.
d[u] <- infinity3.pi[u] <- NIL4.
color[s] <- gray5.
d[s] <- 06.
pi[s] <- NIL7.
Q <- {s}8.While Q .ne. {}9.
do u <- head[Q]10.
for each v in Adj[u]11.
do if color[v] = white12.
then color[v] <- gray13.
d[v] <- d[u]+114.Pi[v] <- u15.
Enqueue(Q,v)16.
Dequeue(Q)17.
color[u] <- black18.
)( VEO
p15.
Properties of Breadth-first search : After execution of BFS, all nodes reachable from the
source s are colored black. After execution of BFS, d[v] 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 ( ) at the end.
After execution of BFS, the edges( ) for v reachable from s from a breadth-first tree.
vu],[
vu],[
p16.
Lemma1 : 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
Lemma2 :
G=(V,E) : a directed or undirected graph. s : an arbitrary vertex : the distance from s to u computed by the
algorithm )(ud
Suppose that BFS is run on G from s.
Proof :
Then on termination, for each vertex , the value d[v] computed byBFS satisfies
Vv).,(][ vsvd
By induction on the number of times a vertex is placed in the queue Q.
Basis : when s is placed in Q. ).,(][ sssd 0
),(][ vsvd for all }.{sVv
Induction Step : Consider a white vertex v discovered during the search from a vertex u.
Inductive hypothesis implies ).,(][ usud
By lemma1, ),(),(][][ ususudvd 11
From then on, d[v] wont be changed again.
Lemma3 :
Q : <v1,v2,…,vr>~ the queue when BFS executes on a graph G=(V,E).
Then Proof :
11 ][][ vdvd r
By induction on the number of queue operations.
Basis : when Q has only s. .][][ 1 sdsd
Induction Step :
11 21 ][][][ vdvdvd r
by inductive hypothesis.
head tail
and ][][ 1 ii vdvd for i=1,2,…,r-1.
1> : after dequeue :v2 becomes the new head in Q.
121 rivdvd ii ,...,for ][][2> : after enqueue a new vertex v into the Q.
Let vr+1 be v.
Note that vi’s adjacency list is being scanned.
neighbors
Thus, 111 ][][ vdvd r
And ][][][ 11 1 rr vdvdvd
The rest , for I=1,…,r-1, remain unaffected. ][][ 1 ii vdvd
p19.
Thm : 1. During the execution of BFS on G=(V,E), BFS
discovers every vertex that is reachable from s, and on termination 2. For any vertex reachable form s, one of the
shortest paths from s to v is the shortest path form s to
followed by the edge
Vv).,(][ vsvd
sv][v
).],[( vv
Proof :If v unreachable from s,
By induction on k, we want to prove for each there is exactly on point duringThe execution of BFS at which 1. V is grayed
2. D[v]=k 3. if then 4. v is inserted into Q.
.),(][ vssd By BFS, v is never discovered.
Let }),(:{ kvsVvVk
,kVv
,sv 1 kVuuv ,][
Basis : k=0, Vk={s} ~ clear !
Induction Step : Q until BFS terminates.
Once u is inserted into Q, d[u] and never change. ][uBy lemma 3, rr vvQvdvdvd ,...,],[...][][ 121
grayed
Let , then by lemma 2. 1 kVv k , ,][ kvd
Thm :The monotonicity property, with and the inductive hypothesis impliesthat v must be discovered after all vertices in Vk-1 are enqueued, if v is discovered at all.
kvd ][
Since a path of k edges from s to v, such that kvs ),( and 1 kVu.),( Evu
At some point u must be the head in Q.Then u’s neighbors are scanned and v is discovered.
Line 13 grays v, line 14 sets d[v]=d[u]+1=k.Line 15 sets .][ uv Line 15 enqueues v.
Thus, the inductive hypothesis holds.
If then ,kVv .][ 1 kVvThus, we can obtain a shortest path from s to v by taking a shortest path from s to then traversing the edge ][v ).],[( vv
p22.
Depth-first search : Nodes are initially white Nodes become green when first discovered Nodes become black when they are done d[v] records when v is first discovered F[v] records when v is done d[u] < f[u]
p23.
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
p24.
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
p25.
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
p26.
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
p27.
(u,v) Black 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.
p28.
DFS(G)for each vertex u in V[G]
do color[u] whitepi[u] NIL
time 0for each vertex u in V[G]
do if color[u] = whitethen DFS-Visit(u)
O(V)
O(E)
p29.
DFS-Visit(u)color[u] grayd[u] time time + 1for each v in Adj[u]
do if color[v] = whitethen pi[v] u
DFS-Visit(v)color[u] blackf[u] time time + 1Finishing
time
Discovery time
p30.
The running time of DFS is O(V+E)
After execution of DFS, all nodes are colored black
After execution of DFS, the edges( ) form a collection of depth-first tree, called a depth-first forest.
v π[v],
p31.
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
p32.
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)
p33.
(c)
s
z
y
x
w
v u
tB
B
C
C
C
C
F
p34.
Thm6: In any DFS of a graph G=( V,E ), for any two vertices u,v, exactly one of the following 3 conditions holds:
(1). The intervals [d[u], f[u]] and [d[u], f[u]] are disjoint,
(2). The interval [d[u], f[u]] is contained entirely within the interval [d[v], f[v]], and u is a descendant of v in the depth-first tree,
(3). or as above with v as a descendant of u
p35.
Pf:1. If d[u] < d[v]
case 1: d[v] < f[u] :
So v is finished before finishing u. Thus (3) holds
case 2: f[u] < d[v]d[u] < f[u] < d[v] < f[v] (1)
holds2. If d[v] < d[u]:
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
p36.
Cor7: v is a proper descendant of vertex u in the depth-first forest for a graph G
iff d[u] < d[v] < f[v] < f[u]
Thm8: In a DF forest of G=( V,E ), vertex v is a descendant of u iff at the time d[u] that the search discovers u, v can be reached from u along a path consisting entirely of white vertices
p37.
Pf:“”
v: descendant of u
“”Assume at time d[u], v is reachable from u along a path of white vertices, but v does not become a descendant of u in DF tree
Thus, d[u] < d[v] < f[w] <= f[u]Thm6 implies [d[v], f[v]] is contained entirely in [d[u], f[u]] By Cor7, v is a descendant of u.
d[u] < d[w], by the above cor.
Thus w is white at d[u]
f[w] <= f[u]v must be discovered after u is discovered,but before w is finished.
uv w
uw v
Descendant of u
p38.
Thm 9: In a DFS of an undirected graph G, every edge of G is either a
tree edge or a back edgePf:
Let and suppose d[u] < d[v]. 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, (
p39.
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
p40.
TOPOLOGICAL-SORT(G): (V+E)
1. Call DFS(G) to compute finishing times f[v] 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
p41.
Lemma 23.10
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 d[v], 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.
p42.
Thm 23.11TOPOLOGICAL-SORT(G) produces a topological
sort of Gpf:
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 f[v]<f[u]. 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
f[v]<f[u]2. If v is black, then f[v]<f[u]
p43.
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
p45.
StronglyConnectedComponents(G)1. Call DFS(G) to compute finishing times f[u] for each vertex u2. Compute GT
3. Call DFS(GT), but in the main loop of DFS, consider the
vertices in order of decreasing f[u]4. Output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component
Time: (V+E)
p46.
Lemma 12:If 2 vertices are in the same strongly connected
component,then no path between them ever leaves the
stronglyconnecter component.
pf:Assume that: u, v in the same component w is a vertex on the path from u to v
uw wvu u, w are in the same component
u wv
p47.
Thm 13In any depth-first search, all vertices in the
same strongly connected component are placed in the same depth-first treepf:
By lemma 12 and thm 8, every vertex in the strongly
connected component becomes a descendant of r in the
depth-first tree
r: the first discovered vertex in the component
p48.
(u): forefather of u the vertex w such that u w and f[w] is
maximized f[u] f[(u)] (*) ((u) )= (u), for any u,vV, u v implies f[(v)] f[(u)]
{w: v w}{w: u w}(1) u v w(2) u w’
Since u (u) f[((u))] f[(u)]By (*), we have f[(u)] f[((u))]Thus, f[(u)]= f[((u))] and (u)= ((u)), since only one vertex can be finished at a time.
w’ w
p49.
Thm 14
In a directed graph G=(V,E), the forefather (u) of any vertex
uV in any depth-first search of G is an ancestor of u.pf:
(u)=u : trivial, u is reachable from itself. (u) u : at time d[u], (u) can be (i) black (ii) gray (iii) white
(i) If (u) is black, then f[(u)]<f[u]. impossible!!(ii) If (u) is gray, then (u) is an ancestor of u. Done!!(iii) Claim (u) can’t be white: 1. If every intermediate vertex is white, then (u) becomes a descendant of u. But then f[(u)] < f[u]. 2. If some intermediate vertex is nonwhite,
t must be gray, since there is no blackwhite edge. Then there is a white path from t to (u). So (u) is a descendant of t. Thus, f[t] > f[(u)]
Since (u) should have the maximum finishing time
u(u)all white
u(u)
Last nonwhite vertex on this path
t
p50.
Corollary 15In any DFS of a directed graph G, vertices u and
(u), for all uV, lie in the same strongly connected
component.pf:
u (u), by definition. (u) u, some (u) is an ancestor of u, by Thm
14
p51.
Thm 16
G=(V,E): a directed graphu,vV lie in the same strongly connected component they have the same forefather in a DFS of Gpf:“” u,v in the same component Every vertex reachable from u is reachable from v and vice versa. By the definition of forefather, (u)=(v) “” Assume (u)=(v)
By cor. 15, u,w are in the same component, and v,w are in the same component, too. Thus, u,v are in the same strongly connected component
wu
v
p52.
Thm 17
Strongly-Connected-Component(G) correctly computes the
strongly connected components of a directed graph.pf:
By induction on the number of depth-first trees found in the
DFS of GT, where each tree forms a strongly connectedcomponent.Basic: When the first tree produced, there are no
previous trees.Consider a depth-first tree T with root r produced in
the depth-first search of GT.
p53.
Let C(r)={vV: (v)=r}Now prove that u is placed in T uC(r)
”” By Thm 13, every vertex in C(r) ends up in the same DF tree.
Since rC(r) and r is the root of T, every element of C(r)
ends up in T.”” w with (1) f[(w)]>f[r] or (2) f[(w)]<f[r], is not placed in T
(1) at the time r is selected w will have been placed in T with
root (w) (2) if w is placed in T, it implies w r, thus by (*) and
r=(r), we have f[(w)] f[(r)]=f[r] Thus, T contains only u with (u)=r. i.e. T=C(r)
p54.
Exercise-5
在文字檔中以 3 tuple 資料表示二 vertex connected & their distance: <1,2,12> indicates that v1, v2 is connected and their distance is 12
Please construct the directed graph 以圖形顯示 具下列功能 : D F S, B F S, minimum spanning tree,
topological sort, single shortest path( 輸入起點及終點 ,列出最點路徑長度及中間所經過 vertex) and all pairs shortest path