45
Elementary Graph Algorithms

Elementary Graph Algorithms

Embed Size (px)

DESCRIPTION

Elementary Graph Algorithms. Representations of graphs: undirected graph. 一個 Undirected graph ( 無向圖 ) ,有 5 個點 7 個邊: 無向圖 G=(V,E) , V 代表點集合, E 代表邊集合。 E 中的元素形式為集合 {u,v} ,代表邊的兩端。. 1. 2. 3. 5. 4. 2. 5. 1. 1. 3. 4. 5. 2. 2. 4. 3. 2. 3. 5. 4. 1. 2. 4. 5. Adjacency-list. - PowerPoint PPT Presentation

Citation preview

Page 1: Elementary Graph Algorithms

Elementary Graph Algorithms

Page 2: Elementary Graph Algorithms

Elementary Graph Algorithms 2

Representations of graphs:undirected graph

• 一個 Undirected graph ( 無向圖 ) ,有 5 個點 7 個邊:

• 無向圖 G=(V,E) , V 代表點集合, E 代表邊集合。 E 中的元素形式為集合 {u,v} ,代表邊的兩端。

1

5

2

4

3

Page 3: Elementary Graph Algorithms

Elementary Graph Algorithms 3

Adjacency-list

• 可藉由 Adjacency-list 表示,將每個點相鄰的點集合用一個 List 存起來。

2 5

1 3 4 5

2 4

2 3 5

1 2 4

1

2

3

4

5

Page 4: Elementary Graph Algorithms

Elementary Graph Algorithms 4

Adjacency-array

• 可藉由 Adjacency-array 表示,利用一個二維陣列將每對點之間是否有邊連起來,有存 1 ,沒有存0 。

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 1 0

Page 5: Elementary Graph Algorithms

Elementary Graph Algorithms 5

Representations of graphs: Directed graph

• 一個 Directed graph ( 有向圖 ) ,有 5 個點 8 個邊:

• 有向圖 G=(V,E) , V 代表點集合, E 代表邊集合。 E 中的元素形式為 (u,v) , u 代表起點, v 代表中點。

1

5

2

4

3

Page 6: Elementary Graph Algorithms

Elementary Graph Algorithms 6

Adjacency-list

• 可藉由 Adjacency-list 表示,將每個點所指向的點集合存在 List 中。

2 5

3 4

3 4

1

4

1

2

3

4

5

Page 7: Elementary Graph Algorithms

Elementary Graph Algorithms 7

Adjacency-array

• 可藉由 Adjacency-array 表示,利用一個二維陣列A 存,若 (u,v) 是一個邊則 A[u][v]=1 反之 A[u][v]=0 。

1 2 3 4 5

1 0 1 0 0 1

2 0 0 1 1 0

3 0 0 1 1 0

4 1 0 0 0 0

5 0 0 0 1 0

Page 8: Elementary Graph Algorithms

Elementary Graph Algorithms 8

Breadth-first search

• Breadth-first search( 簡稱 BFS ,先廣搜尋 ) 是最簡單的圖形搜尋演算法之一。

• 用於搜尋圖形 G 中,所有自點 s 開始出發,有路徑可以到達的點。

• BFS 利用 Queue 作為儲存將要探索的點的資料結構。

Page 9: Elementary Graph Algorithms

Elementary Graph Algorithms 9

Breadth-first search

• BFS 是許多重要的圖形演算法的原型,如 Prim’s minimum spanning tree 演算法以及 Dijkstra’s single-source shortest-path 演算法。

Page 10: Elementary Graph Algorithms

Elementary Graph Algorithms 10

BFS的運作範例

0

r s t u

v w x y

(a)Q

s

1

0

1

r s t u

v w x y

(b)Q

w r

1

0

1

2

2

r s t u

v w x y

(c)Q

t xr

0

1 1

1 2 2

Page 11: Elementary Graph Algorithms

Elementary Graph Algorithms 11

BFS的運作範例

1

2

0

1

2

2

r s t u

v w x y

(d)Q

t x

1

2

0

1

2

2

3

r s t u

v w x y

(e)Q

x

1

2

0

1

2

2

3

3

r s t u

v w x y

(f)Q

v u

v u

v

y

2 2 2

2 2 3

2 3 3

Page 12: Elementary Graph Algorithms

Elementary Graph Algorithms 12

BFS的運作範例

1

2

0

1

2

2

3

3

r s t u

v w x y

(g)Q

u y3 3

1

2

0

1

2

2

3

3

r s t u

v w x y

(h)Q

y3

1

2

0

1

2

2

3

3

r s t u

v w x y

(i)Q

empty

Page 13: Elementary Graph Algorithms

Elementary Graph Algorithms 13

BFS演算法

• 起始點是 s 。• 初始化時,將所有的點塗成白色。• 已經發現的點,塗成綠色。• 已經展開所有相鄰節點的點,塗成紅色。• u.d 儲存 s 到 u 的距離。• u.π 儲存自 s 到 u 的最短路徑中, u 之前的一個

點。

Page 14: Elementary Graph Algorithms

Elementary Graph Algorithms 14

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 = RED

Time Complexity: O(|E|+|V|)

Page 15: Elementary Graph Algorithms

Elementary Graph Algorithms 15

BFS演算法的性質

• 執行過 BFS 之後,自 s 可達的點都被塗成紅色。

• 如 v 是 s 可達的點,則 v.d 代表 s 到 v 的最短路徑長。

• 如 v 是 s 可達的點,則 (v.π, v) 是某條自 s 到 v 最短路徑的一個邊。

Page 16: Elementary Graph Algorithms

Elementary Graph Algorithms 16

BFS演算法的性質

• 邊集合 T={(v.π,v): v 自 s 可達 } 形成一個 breadth-first tree 。

1

2

0

1

2

2

3

3

r s t u

v w x y

藍色的邊形成一個Breadth-first tree

Page 17: Elementary Graph Algorithms

Elementary Graph Algorithms 17

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

Page 18: Elementary Graph Algorithms

Elementary Graph Algorithms 18

Depth-first search

• Depth-first search( 簡稱 DFS ,先深搜尋 ) 是最簡單的圖形搜尋演算法之一。同樣用於搜尋圖形 G中,所有自點 s 開始出發,有路徑可以到達的點。

• DFS 利用 Stack 作為儲存已經開始探索但尚未結束的點的資料結構。

• 相較於 BFS , DFS 可以利用程式語言的遞迴來避免自行實做資料結構。

Page 19: Elementary Graph Algorithms

Elementary Graph Algorithms 19

DFS演算法

• 起始點是 s 。• 初始化時,將所有的點塗成白色。• 點初次被發現的時候,塗成灰色。• 點做完 DFS-Visit 的時候,塗成黑色。• u.d 儲存 u 被發現的時間。• u.f 儲存 u 做完 DFS-Visit 的時間。• u.d < u.f

Page 20: Elementary Graph Algorithms

Elementary Graph Algorithms 20

DFS 運作範例

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

發現時間

Page 21: Elementary Graph Algorithms

Elementary Graph Algorithms 21

DFS 運作範例

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

Page 22: Elementary Graph Algorithms

Elementary Graph Algorithms 22

DFS 運作範例

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

Page 23: Elementary Graph Algorithms

Elementary Graph Algorithms 23

DFS 運作範例

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

Page 24: Elementary Graph Algorithms

Elementary Graph Algorithms 24

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)

Page 25: Elementary Graph Algorithms

Elementary Graph Algorithms 25

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]∈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

Page 26: Elementary Graph Algorithms

Elementary Graph Algorithms 26

DFS的性質

• DFS 演算法的時間複雜度為 O(|V|+|E|) 。

• 執行過 DFS 演算法之後,所有的點都是黑色的。

• {(v.π,v):v.π≠NIL} 將形成一個 Depth-first forest ,也就是一個元素為 Depth-first tree 的集合。

• 如 u 到 v 之間在 Depth-first forest 中有一路徑,則稱 u 是 v 的 ancestor ,稱 v 是 u 的 descendant 。

Page 27: Elementary Graph Algorithms

Elementary Graph Algorithms 27

邊的分類

• 假定邊為 (u,v)• Tree edges: 若初次發現 v 時是藉由 (u,v) ,即

v.π=u ,則此邊稱作 tree edge 。• Back edges: 若 v 是 u 的 ancestor ,則此邊稱作

back edge 。• Forward edges: 若 v 是 u 的 descendant 且 (u,v) 不

是 tree edge ,則此邊稱作 forward edge 。• Cross edges: 不屬於前三類的邊均稱為 Cross

edges 。

Page 28: Elementary Graph Algorithms

Elementary Graph Algorithms 28

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)

Page 29: Elementary Graph Algorithms

Elementary Graph Algorithms 29

(c)

s

z

y

x

w

v u

tB

B

C

C

C

C

F

Page 30: Elementary Graph Algorithms

Elementary Graph Algorithms 30

Topological sort

• 對一 DAG (Directed acyclic graph ,有向無迴圈圖 ) G=(V,E) , Topological sort( 拓樸排序 ) 是一對V 的排序,其中若 (u,v) ∈ E 則在排序中 u 必須排在 v 之前。

• 常用於決定排程。

Page 31: Elementary Graph Algorithms

Elementary Graph Algorithms 31

襪子 內褲 長褲 鞋子 手錶 襯衫 皮帶 領帶 夾克

內褲 襪子

長褲 鞋子

皮帶襯衫

領帶

夾克

手錶

一個表示穿衣服先後順序的圖

經拓樸排序後,可依此順序穿上所有衣物。

Page 32: Elementary Graph Algorithms

Elementary Graph Algorithms 32

Topological sort演算法

• 利用 DFS 演算法,計算出每一個點 u 的 Finish time (u.f) 。

• 當一個點執行完畢 DFS-Visit 時,將該點放入一公用的 Linked List 前端。

• 最後此 Linked List 存放的順序即為一 Topological sort 。

Page 33: Elementary Graph Algorithms

33

Topological sort操作範例

內褲 襪子

長褲 鞋子

皮帶襯衫

領帶

夾克

手錶11/16

12/15

6/7

1/8

2/5

3/4

17/18

13/14

9/10

襪子 內褲 長褲 鞋子 手錶 襯衫 皮帶 領帶 夾克

11/16 12/15 6/71/8 3/413/1417/18 9/10 2/5

以上為公用的 Linked List 在執行完 DFS 之後的順序。

每個點旁的數字代表Discovery time/Finish Time

Page 34: Elementary Graph Algorithms

Elementary Graph Algorithms 34

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. Thus (u,v)is a back edge.

Page 35: Elementary Graph Algorithms

Elementary Graph Algorithms 35

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

Page 36: Elementary Graph Algorithms

Elementary Graph Algorithms 36

Strongly connected components

• 對一個有向圖 G=(V,E) 而言,一個 Strongly Connected Component C 是一個滿足下列條件的點集合:– C⊆V– 對任 C 中相異兩點 u 及 v ,存在一條路徑由 u

到 v 且有另一路徑由 v 到 u 。

Page 37: Elementary Graph Algorithms

Elementary Graph Algorithms 37

Page 38: Elementary Graph Algorithms

Elementary Graph Algorithms 38

Strongly connected components演算法

1. 呼叫 DFS(G) 對所有點 u ,計算出 u.f ,即finishing time 。

2. 計算出 GT ,即點集合與 G 相同,而邊連接方向相反的圖。

3. 呼叫 DFS(GT) ,但在 DFS 主迴圈中,選擇點的順序是先挑取 u.f 值較大的點 u 。 ( 即以 u.f 遞減順序挑取。 )

4. 在 DFS(GT) 的 Depth-first forest 中,每一個樹均是一個 Strongly connected component 。

Page 39: Elementary Graph Algorithms

39

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

Page 40: Elementary Graph Algorithms

Elementary Graph Algorithms 40

Lemma 22.13 :Let C and C’ be distinct strongly connectedcomponents 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 G. Then there cannot also be a path from v’ to v in G.

Def: Let U V, define d(U) = min u U { u.d }

f (U) = max u U { u.f }

Page 41: Elementary Graph Algorithms

Elementary Graph Algorithms 41

Lemma 22.14Let 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: Case (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’).

Page 42: Elementary Graph Algorithms

Elementary Graph Algorithms 42

Lemma 22.14Let 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: Case (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. Why?Thus any w in C has w.f > y.f and so f(C) > f(C’).

Page 43: Elementary Graph Algorithms

Elementary Graph Algorithms 43

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 ET, 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 ).

Page 44: Elementary Graph Algorithms

Elementary Graph Algorithms 44

Theorem 22.16Strongly-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.

Page 45: Elementary Graph Algorithms

Elementary Graph Algorithms 45

All other vertices in C are descendant of u in its DFS.

By inductive hypothesis and the 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.