• 沒有找到結果。

Michael Tsai2010/12/2 GRAPH

N/A
N/A
Protected

Academic year: 2022

Share "Michael Tsai2010/12/2 GRAPH"

Copied!
36
0
0
顯示更多 ( 頁)

全文

(1)

Michael Tsai

2010/12/2

(2)

今日行程

助教講解部分期中考題目評分標準

發考卷

一些需要討論的事項

Minimum spanning tree

Shortest path

Activity network

(3)

補課事宜

12/17 ( 五 ) 將不在國內 , 停課乙次

之後需要補課

請班代 ( 雅喬 ) 統計 12/20-23 的晚上哪一天各位比較喜 歡

補課時間 6pm-9pm

(4)

資料結構與演算法下

續集將亦由在下任教

課本可以先訂 , 與單班目前課本同一本

預習讚

(5)

概念溝通

之後的課程 , 不可能每個部分都帶著大家一步一步看

那些東西呢 : ( 以下的一部分 )

怎麼把概念轉化成 code

time complexity 的分析

證明

有些課本內容將不會教 ( 練習自己看 , 會在課堂上提到 哪邊比較重要 )

有問題隨時都可以來找我討論 ( 雖然我不一定會 , 當場 不一定想得出來 )

(6)

Minimum cost spanning tree

一個 graph 可能有多個 spanning tree

假設每個 edge 上面都有 cost

哪一個 spanning tree 的總花費 ( 所有 edge cost 總和 ) 最小呢 ?

複習 : spanning tree 須滿足那些條件 ?

1. 因為是 tree, 所以沒有 cycle

2. 因為是 tree, 所以正好有 n-1 個 edge

(7)

Greedy algorithm

什麼是貪婪的演算法 (Greedy algorithm) 呢 ?

“ 根據目前所了解的狀況挑出一個最好的選擇”

“ 要挑就要挑最好的”

不一定最後會有最佳解 ( 但是許多狀況下是 , 這時候這 就是一個好的策略 )

下學期會介紹更多的 greedy algorithm

今天要介紹三種利用 greedy algorithm 解 minimum spanni ng tree 的方法

(8)

Kruskal’s algorithm

這個方法是我覺得最直觀的方法 .

T={}; // 在 spanning tree 裡面的 edge

while(T 中有少於 n-1 個 edge && E 不是空的 ) {

選出 E 中 cost 最小的 edge, 從 E 中拿掉 .

如果加入 T 不會造成 cycle 則加入 T, 不然就丟掉 .

}

0

5

1

6

4

3

2 10

25

22 24

12 18

16 14

28

N-1 條邊了 - 停止

(9)

細節們

T={}; // 在 spanning tree 裡面的 edge

while(T 中有少於 n-1 個 edge && E 不是空的 ) {

選出 E 中 cost 最小的 edge, 從 E 中拿掉 .

如果加入 T 不會造成 cycle 則加入 T, 不然就丟掉 .

}

主要的工作們 :

(1) 選出 E 中最小的 edge

(2) 檢查加入 T 會不會造成 cycle

怎麼做 ? 分別要花多少時間呢 ?

(10)

細節們

(1) 用 minimum heap. 這樣選出一個最小 cost 的 edge 要花 O(lo g e)

還有一開始做一個 heap 出來 ( 加入所有 edge) 要花 O(e log e)

(2) 用之前的 set union+find 的 algorithm

當要把 edge (i,j) 加入前 , 看 find(i) 是否 ==find(j) 屬於同一個 set

同一個 set 意思就是說已經連在一起了 , 再加會有 cycle

find = O(log e)

如果要加進去 , 再用 union

union = O(log e)

如果丟掉 , 當然也是 O(1)

O( e log e + (n-1)(log e + log e))

不然就沒有 spanning tree 了

所以最後可以化為 O(e log e)

•  

(11)

證明題

證明 Kruskal’s algorithm 會產生出 minimum cost spanning tree.

(1) 證明當某 graph 有 spanning tree 的時候 , Kruskal’s algorithm 會產生出 s panning tree.

(2) 證明這個產生出來的 spanning tree 一定是 cost 最小的 .

證明 (1):

什麼時候某 graph 一定有 spanning tree 呢 ?

 原本是 connected 的

Algorithm 什麼時候會停下來呢 ?

(1) 當 T 裡面已經有 n-1 個 edge 了 ( 成功 , 不管它 )

(2) T 裡面還沒有 n-1 個 edge, 但是 E 裡面已經沒有 edge, 造成有些 node 沒有連 接到 ( 我們的 algorithm 會不會造成這樣的情形呢 ?)

但是我們的程式只會把造成 cycle 的 edge 丟掉 , 當把造成 cycle 的 edge 丟掉的時 候 , 不會因此讓某個 node 在 T 裡面沒有跟其他 vertex connected.

所以不會造成 (2) 的情形

(12)

證明題

證明 (2)

假設 T 是用我們的 algorithm 做出來的 spanning tree

假設 U 是某一個 minimum cost spanning tree ( 可能有很 多個 , 其中一個 )

既然都是 spanning tree, T 和 U 都有 n-1 個 edge

有兩種情形 :

(1) T 和 U 一模一樣 , 則 T 就是 minimum cost spanning tree ( 沒什麼好證的 )

(2)T 和 U 不一樣 . 則我們假設它們有 k 條 edge 不一樣 , .

•  

(13)

證明題

每次我們從 T 取出 k 條不一樣的 edge 中其中一條 ( 此 edge 不在 U 中 ), 從 cost 最小的開始到 cost 最大的 .

把這條 edge( 我們叫它 e) 加入 U 的時候 , 會產生一個 cyc le 在 U 中

這個 cycle 裡面 , 一定有某一條 edge 不在 T 裡面 , 我們 叫它 f ( 因為 T 沒有 cycle). 我們把 f 從 U 拿掉 , 這個 新的 spanning tree 叫做 V

V 的 total cost 就是 cost(U)-cost(f)+cost(e).

(14)

證明題

但是 cost(e) 不能小於 cost(f), 否則 V 就比 U 的 cost 少 了 (contradiction)

cost(f) 也不能小於 cost(e),

不然當初我們做 T 的時候 , 應該會先選到 f, 但是因為它 會造成 cycle 所以才不選它 .

所以 f 和所有在 T 裡面 cost 跟 cost(f) 一樣大或者更小的 edge 會造成 cycle.

但是剛剛既然我們先選到 e ( 在 T 裡面不在 U 裡面最小的 一個 ), 表示這些 cost 跟 cost(f) 一樣大或者更小的 edge 都在 U 裡面

表示 f 和這些 edge 都在 U 裡面 , U 會有 cycle (contradit ion)

(15)

證明題

搞了半天 , 目前可以證明 cost(f)=cost(e)

所以 cost(V)=cost(U)

重複以上步驟 , 可以最後變成 V=T 且 cost(V)=cost(T)

=cost(U)

所以 T 也是 minimum cost spanning tree

(16)

Prim’s Algorithm

T={}

TV={0}

while(T 少於 n-1 條 edge) {

找出一條但中 cost 最小的 edge (u,v)

如果找不到就 break;

add v to TV

add (u,v) to T

}

如果 T 中少於 n-1 條 edge, 就 output 失敗

•  

0

5

1

6

4

3

2 10

25

22 24

12 18

16 14

28

(17)

細節們

必須 maintain 一個 d 陣列

每個元素代表到目前為止 , 從 vertex i 到已經在 TV 裡面 的 vertex 們最短的距離

time complexity 為

使用 adjacency matrix:

使用 adjacency lists + heap:

為什麼 ? 想想看囉 . ( 沒有答案的動腦時間 : 進階題 )

( 想不出來的話可以來找我討論 )

•  

(18)

Sollin’s algorithm

一開始每個 vertex 自己是一個 forest

保持都是 forest

每個 stage, 每個 forest 都選擇一個該 forest 連到其他 f orest 的 edge 中 cost 最小的一個

執行到沒有 edge 可以選了 , 或者是變成 tree 了 0

5

1

6

4

3

2 10

25

22 24

12 18

16 14

28

(19)

Shortest paths

目標 : 找出 vertex u 到 graph G 中任何一點的最短距離

0

3

1

4

2

5 4

5 5

0 1

0 2

0 2

0 1 5 1

5 3

3 0 3 1 5

0

TO VERTEX PATH LENGTH

3 0 3 10

4 0 3 4 25

1 0 3 4 1 45

2 0 2 45

5 No path

TO VERTEX PATH LENGTH

3 0 3 10

4 0 3 4 25

1 0 3 4 1 45

2 0 2 45

5 No path

(20)

Dijkstra’s algorithm

Set S 裡面有一些已經加入的 vertex ( 包括起始點 )

w 不在 S 中 , 則有 distance[w] 紀錄從經過 S 中的任意個 vertex 後 , 最後到達 w 的這樣形式的最短路徑

•  

S

  0 w

(21)

Dijkstra’s algorithm

每次選擇 u, 為 distance[u] 中最小的

選進 S 以後 , 到 u 的 shortest path 就找到了

所以找到 shortest path 的順序會是由最短的找到最長的

把 u 選進去 S 以後 , 就要把所有 u 的 edge <u,w> 都看一 遍 :

if distance[w]>distance[u]+cost(<u,w>)

distance[w]=distance[u]+cost(<u,w>)

•  

(22)

小證明

如果下一個選擇的 vertex 為 u, 為什麼到 u 的最短 path 會正好是前面的 vertex 都在 S 中 , 只有 u 不在 S 中呢 ?

因為假如有 path 中有 vertex w 也不在 S 中 , 那麼表示 pa th 中會有一段是到 w, 而且長度比較短 ( 因為是 sub pat h)

可是這樣的話 , 應該 w 之前應該就要被選過了 ( 應該要 在 S 中 ) (contradiction)

所以到 u 的最短 path 前面的 vertex 都在 S 中 , 只有 u 不 在 S 中

•  

(23)

例子

0

3

1

4

2

5 5

5

0 1

0 2

0 2

0 1 5 1

5 3

3 0 3 1 5

0

selecti

on 0 1 2 3 4 5

Initial 50 45 10

3 50 45 10 25

4 45 60 10 25

1 45 55 10 25

2 45 55 10 25

(24)

更多動畫例子

• http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dij kstra/DijkstraApp.shtml?demo1

Time complexity?

答案 :

每次都要看一次 distance[], 共 n 個

•  

(25)

Bellman-Ford algorithm

另外一種找到 shortest path 的 algorithm

可以 handle path cost 是負的情形

為什麼 Dijkastra 有問題 ?

如果下一個選擇的 vertex 為 u, 為什麼到 u 的最短 path 會正好 是前面的 vertex 都在 S 中 , 只有 u 不在 S 中呢 ?

因為假如有 path 中有 vertex w 也不在 S 中 , 那麼表示 path 中 會有一段是到 w, 而且長度比較短 ( 因為是 sub path)

可是這樣的話 , 應該 w 之前應該就要被選過了 ( 應該要在 S 中 ) (contradiction)

所以到 u 的最短 path 前面的 vertex 都在 S 中 , 只有 u 不在 S 中

•  

假設 cost 可以是負的 , 這就不 一定正確了 .

假設 cost 可以是負的 , 這就不 一定正確了 .

(26)

Bellman-Ford algorithm

定義 為從到 u 的最短路徑 , 最多經過 l 條 edge

一開始為 cost(<,u>) ( 如果沒有 edge 則為無限大 )

由算

最後算到為最後 shortest path 解 ( 因為每個 node 最多 走一遍 , 不然就有 cycle 了 )

•  

(27)

Bellman-Ford algorithm

由算

怎麼算呢 ?

有以下兩種可能性 :

如果從到 u 的使用最多 k 個 edge 的最短路徑其實使用了 k- 1 或更少 edge 的話 , 則

如果從到 u 的使用最多 k 個 edge 的最短路徑使用了 k 條 ed ge 的話 , 則最短路徑可能為經過別的 vertex i 的 shorte st path ( 用了最多 k-1 條 edges) 後再走 edge <i,u>.

綜合以上兩條規則 :

•  

(28)

例子

0 2

3

6

5 6

5

5 -2

-2 3

-1 3

k 1 2 3 4 5 6

1 6 5 5

2 3 3 5 5 4

3 1 3 5 2 4 7

4 1 3 5 0 4 5

5 1 3 5 0 4 3

6 1 3 5 0 4 3

k 1 2 3 4 5 6

1 6 5 5

2 3 3 5 5 4

3 1 3 5 2 4 7

4 1 3 5 0 4 5

5 1 3 5 0 4 3

6 1 3 5 0 4 3

1

其他例子 : http://www.ibiblio.org/links/applets/appindex/graphtheory.html

(29)

一些可以思考的東西

time complexity = ?

答案 :

adjacency matrix:

adjacency lists:

前面都沒有提到怎麼真的輸出 path 本身經過哪些 vertex

想想看 , 要怎麼做 ?

自行閱讀 : 課本 6.4.3-6.4.4

•  

答案: 每次up

da te dis ta 的時候, nce

順便u pd 附在旁邊的li ate

nked lis t.

用link ( ed

lis 表示p t

ath)

(30)

TANET

(31)

Routing protocol

要計算從某個點到另外一個點 的路徑 (route)

目標 : 連線速度 ?

path cost =?

Dijkstra 比較適合還是 Bellma n-ford 比較適合 ?

答案: 似乎B

ellm an-F

比較適合, ord 因為不需要知道所有link

cost 即可建立rout

ing ta ble

(32)

Activity-on-vertex (AOV) Network

什麼時候可以修 j 課程呢 ? 修完所有 edge<i,j> 中 i 課程以後

所有的” activity” 都是發生在 vertex 上

所以稱為 activity-on-vertex (AOV) network

問 : 如何找到一種修課順序呢 ?

此順序又稱為 topological order

計程 演算法上 演算法下

計概 作業系統

計算機網路 微積分上 微積分下 機率

計組

(33)

Topological sorting

for (i=0;i<n;++i) {

if ( 每個現存的 vertex 都還有祖先 )

return error;

選一個沒有祖先的 vertex v

output v

把 v 還有所有有連到它的 edge 們都刪掉

}

試試看 ?

想想看怎麼寫程式 ? ( 答案可以看課本 p.319)

time complexity: O(e+n)

(34)

Activity-on-edge (AOE) Network

不是 Ages of Empires ( 爛梗 )

“Activities” 發生在 edge 上

edge cost= 所需要完成 activity 的時間

例如 : vertex 4 可以解釋為完成及 後的時間

•  

1

2

0 4

6

7

8

3 5

start

finish

  1

=6

  4

=1

  3

=5

  6

= 2

  9

= 4

  5

=1

  7

= 9

  10

=2

  11

= 4

  8

=7

  2

= 4

(35)

可以思考的一些問題

vertex 8= 完成所有工作 . 完成的時間為 ?

完成的時間為從 start finish 的最長 path.

又稱為 critical path

想想看為什麼 ?

earliest time(edge e): 為該 activity 最早可以開始的時間

latest time(edge e):

不延誤最後完成時間的前提下 activity 可以開始的最晚時間

earliest time(e) == latest time(e)

then e 為 critical activity

( 完全不能延誤 )

1

2

0 4

6

7

8

3 5

start

finish

  1

=6

  4

=1

  3

=5

  6

= 2

  9

= 4

  5

=1

  7

= 9

  10

=2

  11

= 4

  8

=7

  2

= 4

(36)

周末愉快

怎麼算這些東西呢 ? 下回分解 .

作業早點開始寫 , 早點可以問問題

ptt2 開板中 (HsinMu), 當作問問題的另一管道 ( 或聊天 打屁也行 )

分數問題 , 請洽助教

參考文獻

相關文件

Interacti e Foregro nd E traction Interacti e Foregro nd E traction Interactive Foreground Extraction Interactive Foreground Extraction using Iterated Graph Cuts. using

• Interactive image segmentation using graph cutB. • Binary label: foreground vs.. B) – Similar in spirit to

報名截止 即日起自 109 年 8 月 2 日中午 12:00 止(以線上報名登錄時間為憑),恕 不接受其他方式報名。. 錄取通知 109 年 8 月

• 下面介紹三種使用 greedy algorithm 產生 minimum cost s panning

神秘的資料結構

We solve the three-in-a-tree problem on

 From a source vertex, systematically follow the edges of a graph to visit all reachable vertices of the graph.  Useful to discover the structure of

INFORMAÇÃO GLOBAL SOBRE AS ASSOCIAÇÕES DE SOLIDARIEDADE SOCIAL E OS SERVIÇOS SUBSIDIADOS REGULARMENTE PELO INSTITUTO DE ACÇÃO SOCIAL. STATISTICS ON SOCIAL SOLIDARITY ASSOCIATIONS

INFORMAÇÃO GLOBAL SOBRE AS ASSOCIAÇÕES DE SOLIDARIEDADE SOCIAL E OS SERVIÇOS SUBSIDIADOS REGULARMENTE PELO INSTITUTO DE ACÇÃO SOCIAL. STATISTICS ON SOCIAL SOLIDARITY ASSOCIATIONS

Myers effect condensation of mean field D(-1) Chern Simons term is induced. Fuzzy sphere is

Theorem (M.Kalkowski, M.Karonski, and F.Pfender, 2010) ([8]) Every connected graph G 6= K 2 is 5-edge weight colorable1. Theorem (T.Bartnicki, J.Grytczuk,

The vertex-cover problem is to find a vertex cover of minimum size in a given undirected graph. • 此問題的decision版本為NP-Complete

之後每次從 heap 取 出一個 item, 放入 sorted list.. array of items 想是

計算機網路 微積分上 微積分下

[r]

• An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.. • An algorithm is

(1) 99.8% detection rate, 50 minutes to finish analysis of a minute of traffic?. (2) 85% detection rate, 20 seconds to finish analysis of a minute

• 下面介紹三種使用greedy algorithm產生minimum cost spanning

Given an undirected graph with nonnegative edge lengths and nonnegative vertex weights, the routing requirement of a pair of vertices is assumed to be the product of their weights.

 Gouraud Shading: Different vertex normal, interpolated ve rtex color on a fragment..  Phong Shading: Different vertex normal, interpolated vert ex normal on

2 System modeling and problem formulation 8 3 Adaptive Minimum Variance Control of T-S Fuzzy Model 12 3.1 Stability of Stochastic T-S Fuzzy

Harvey Michael &amp; Novicevic Milorad(2002), “The role of political competence in global assignments of expatriate managers”, Journal of International Management Volume: 8, Issue:

Hal Amick, and Ahmad Bayat, A., “Dynamics of Stiff Floors for Advanced Technology Facilities,” Proceedings of 12 th ASCE Engineering Mechanics Conference, pp.. Hal Amick,