Greedy minimum spanning tree- prim's algorithm

Preview:

DESCRIPTION

 

Citation preview

GreedyMinimum 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.

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

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

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

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

Graphs

EXAMPLE:Weighted graph

Spanning Tree(⽣生成樹)

Minimum Spanning Tree

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

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

Application

Optimal substructure

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

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

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

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

Optimal substructure

MST T

Optimal substructure

MST T

Remove any edge (u,v) ∈ T

Optimal substructure

MST T

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

T1

T2

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

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

–Greedy Agorithm

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

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.

Proof of theorem

Consider the unique simple path from u to v in T

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

Proof of theorem

A lighter-weight spanning tree than T results.

Proving that greedy choice is good enough.

Prim’s Algorithm

Prim’s AlgorithmUse Priority Queue

Prim’s Algorithm

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

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

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

Recommended