29
Greedy Minimum Spanning Tree: Prim's Algorithm 研究:鍾聖彥 教授: 許慶昇

Greedy minimum spanning tree- prim's algorithm

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Greedy minimum spanning tree- prim's algorithm

GreedyMinimum Spanning Tree: Prim's Algorithm

研究⽣生:鍾聖彥 教授: 許慶昇 ⽼老師

Page 2: Greedy minimum spanning tree- prim's algorithm

Key ingredients of greedy algorithms

• Optimal substructure: An optimal solution to the problem contains within it optimal solutions to subproblems.

• Greedy-choice property: We can assemble a globally optimal solution by making locally optimal (greedy) choices.

Page 3: Greedy minimum spanning tree- prim's algorithm

什麼問題可以使⽤用greedy algorithm來解?

1.如果做出⼀一個choice之後,  可以找到剩下要解的單⼀一個subproblem    

2.Greedy-choice property(必須證明最佳解裡⾯面⼀一定有greedy choice)

3.有optimal substructure (必須證明⼤大問題的最佳解裡⾯面有⼩小問題的最佳解)

Page 4: Greedy minimum spanning tree- prim's algorithm

Graphs

Page 5: Greedy minimum spanning tree- prim's algorithm

EXAMPLE:Weighted graph

Page 6: Greedy minimum spanning tree- prim's algorithm

Spanning Tree(⽣生成樹)

Page 7: Greedy minimum spanning tree- prim's algorithm

Minimum Spanning Tree

⺫⽬目的:移除weighted graph中的部份邊線,使得⼦子圖仍然保持連通性,且邊線weight總和為最⼩小

權重最⼩小的⽣生成樹就是最⼩小⽣生成樹

Page 8: Greedy minimum spanning tree- prim's algorithm

Application

Page 9: Greedy minimum spanning tree- prim's algorithm

Optimal substructure

• ⼀一、兩棵 MST ,要合併成⼀一棵 MST 時,以兩棵 MST 之間權重最⼩小的邊進⾏行連結,當然會是最好的。

• ⼆二、三棵 MST ,要合併成⼀一棵 MST 時,先連結其中兩棵連結權重最⼩小的 MST ,然後才連結第三棵,總是⽐比較好。

• 三、⼀一個單獨的點,可以視作⼀一棵 MST 。

• 由以上三點,可以歸納出⼀一個 greedy 演算法:以權重最⼩小的邊連結各棵 MST ,⼀一定⽐比較好。

Page 10: Greedy minimum spanning tree- prim's algorithm

Optimal substructure

MST T

Page 11: Greedy minimum spanning tree- prim's algorithm

Optimal substructure

MST T

Remove any edge (u,v) ∈ T

Page 12: Greedy minimum spanning tree- prim's algorithm

Optimal substructure

MST T

Remove any edge (u,v) ∈ T. Then it is partitioned into two subtrees T1 and T2

T1

T2

Page 13: Greedy minimum spanning tree- prim's algorithm

Optimal substructure

MST T

Theorem. The subtree T1 is MST of G1 = (V1,E1),the subgragh of G induced by the vertex of T1

Similarly for T2

T1

T2

G1

Page 14: Greedy minimum spanning tree- prim's algorithm

Proof of Optimal substructure

MST T

T1

T2

G1

w(T) = w(u, v) + w(T1) + w(T2)If T1′ were a lower-weight spanning treat than T1 for G1, then T′ = {(u, v)} ∪ T1′ ∪ T2 would be a

lower-weight spanning tree than T of G

Page 15: Greedy minimum spanning tree- prim's algorithm

–Greedy Agorithm

Greedy-choice property"A locally optimal choice is globally optimal

Page 16: Greedy minimum spanning tree- prim's algorithm

TheoremLet T be the MST of G = (V,E), and let A ⊆ V.

Suppose that (u,v)∈ E is the least-weight edge connecting A to V-A, then (u,v)∈ T

The proof is by contradiction(⽭矛盾)

Cut-and-Paste is a way used in proofing graph theory concepts, Idea: Assume you have solution for Problem A, you want to say some edge/node, should be available in solution.

You will assume you have solution without specified edge/node, you try to reconstruct a solution by cutting an edge/node and pasting specified edge/node and say new solution benefit is at least as same as previous solution.

Page 17: Greedy minimum spanning tree- prim's algorithm

Proof of theorem

Consider the unique simple path from u to v in T

Page 18: Greedy minimum spanning tree- prim's algorithm

Proof of theorem

Swap(u, v) with the first edge on this path that connects a vertex in A to a vertex in V–A

Page 19: Greedy minimum spanning tree- prim's algorithm

Proof of theorem

A lighter-weight spanning tree than T results.

Proving that greedy choice is good enough.

Page 20: Greedy minimum spanning tree- prim's algorithm

Prim’s Algorithm

Page 21: Greedy minimum spanning tree- prim's algorithm

Prim’s AlgorithmUse Priority Queue

Page 22: Greedy minimum spanning tree- prim's algorithm

Prim’s Algorithm

• 增加節點的觀念做為出發點。

• ⾸首先以某⼀一節點當作出發點,在與其相連且尚未被選取的節點裡,選擇權重最⼩小的邊, 將新的節點加⼊入。

• 如此重覆加⼊入新節點,直到增加了n - 1條邊為⽌止。(假設有 n 個節點)

Page 23: Greedy minimum spanning tree- prim's algorithm
Page 24: Greedy minimum spanning tree- prim's algorithm
Page 25: Greedy minimum spanning tree- prim's algorithm
Page 26: Greedy minimum spanning tree- prim's algorithm
Page 27: Greedy minimum spanning tree- prim's algorithm
Page 28: Greedy minimum spanning tree- prim's algorithm
Page 29: Greedy minimum spanning tree- prim's algorithm