[アルゴリズム]10週目2回目の運転時
9487 ワード
Chap08. Graph Optimization Problems and Greedy Algorithms
Graph Optimization Problems
Prim's Algorithm
Greedyを使用して最小生成ツリーを検索するアルゴリズム1
このときedgeは最小重みのedge-greedyアルゴリズム
ステータスは
O(n)回挿入、削除
PrimMST(G,n) // OUTLINE
Initialize all vertices as unseen. // O(n) time
Select an arbitrary vertex s to start the tree; reclassify it as tree.
Reclassify all vertices adjacent to s as fringe.
While there are fringe vertices; // O(n)번 수행
Select an edge of minimum weight between
a tree vertex t and a fringe vertex v; // O(n) time
Reclassify v as tree; add edge tv to the tree;
Reclassify all unseen vertices adjacent to v as fringe. // deg(v)
*min-priority queuesorted sequenceunsorted sequenceheapinsert()O(n)O(1)O(lg n)removeMin()O(1)O(n)O(lg n)decreasekey()O(n)O(1)O(lg n)
通常、heapを用いてより効率的に実施する
Example 1::ソートされていないシーケンスを使用して計算
2つの配列size=n-status、distanceを使用
1.statusはすべて非可視頂点に初期化し、距離はすべて無限-O(n)timeに初期化する
2.O(n)timeを反復するたびにstatusとdistanceを更新する
PrimMST(G,n) // OUTLINE
Initialize all vertices as unseen. // O(n) time
Select an arbitrary vertex s to start the tree; reclassify it as tree.
Reclassify all vertices adjacent to s as fringe.
While there are fringe vertices; // O(n)번 수행
Select an edge of minimum weight between
a tree vertex t and a fringe vertex v; // O(n) time
Reclassify v as tree; add edge tv to the tree;
Reclassify all unseen vertices adjacent to v as fringe. // deg(v)
O(n2n^2n2) time using the unsorted sequenceExample 2::heap計算を使用
PrimMST(G,n) // OUTLINE
Initialize all vertices as unseen. // O(n) time
Select an arbitrary vertex s to start the tree; reclassify it as tree.
Reclassify all vertices adjacent to s as fringe.
While there are fringe vertices; // O(n)번 삽입, 삭제
Select an edge of minimum weight between
a tree vertex t and a fringe vertex v; // O(n) time
Reclassify v as tree; add edge tv to the tree;
Reclassify all unseen vertices adjacent to v as fringe. // deg(v)
挿入と削除に必要なO(nlgn)時間各edgeは2回の減量キー()−O(m)回を実行する
従って、O(nlgn+mlgn)<=O(mlgn)time
O(n2n^2n2) time using the unsorted sequence
O(mlgn) time using a heap
与えられたパターンのmの大きさがO(n)の疎図である場合,heap.
mのsizeがO(n 2 n^2 n 2)に近い稠密図である場合,並べ替えられていないシーケンスは有効である.
Kruskal's Algorithm
Greedyを使用して最小生成ツリーを検索するアルゴリズム2
KruskalMST(G,n) // outline
R = E // R is remaining edges
F = none // F is forest edges
while (R is not empty)
Remove the lightest (shortest) edge, vw, from R;
// cycle이 발생하지 않을 때만 MST 추가
// union-find ADT(disjoint set)
if (vw does not make a cycle in F)
Add vw to F;
return F ;
O(nlgn)時間で実行edgeの重みに応じてより小さなedgeからMSTに追加
edgeソート
KruskalMST(G,n) // outline
R = E // sorting: O(mlgm) time
F = none
while (R is not empty)
Remove the lightest (shortest) edge, vw, from R;
if (vw does not make a cycle in F)
Add vw to F;
return F ;
O(mlgm) time管理
KruskalMST(G,n) // outline
R = E // m개 edges, O(m) time (bottom-up heap construction)
F = none
while (R is not empty) // O(m) 번
Remove the lightest (shortest) edge, vw, from R; // O(lgm) time
if (vw does not make a cycle in F)
Add vw to F;
return F ;
O(mlgm) time* m=O(n2n^2n2), O(mlgm) = O(mlgn2n^2n2) = O(2mlgn) = O(mlgn)
Data Structure for Kruskal's Algorithm
O(nlgn)よりも高速
mMSTm{MST}mMST=n-1
Reference
この問題について([アルゴリズム]10週目2回目の運転時), 我々は、より多くの情報をここで見つけました https://velog.io/@dkswlgus00/알고리즘-10주차-2차시テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol