Algorithm-part3-week1

Minimum spanning trees
Informal goal:
Connect a bunch of points together as cheaply as possible.
Application: Clustering(more later), networking.
Blazingly Fast Greedy Algorithms:
− − Prim’s Algorithm
− − Kruskal’s Algorithm
O(mlogn) time : (using suitable data structures)
m: # of edges. n : # of vertices
Problem Definition
Input: Undirected graph G = (V,E) and a cost ce c e for each edge eE e ∈ E .
Assume adjacent list repersentation.
OK if edge costs are negative.
Output:
minimum cost tree TE T ⊆ E that spans all vertices. (cost: sum of edge costs).
(1) T has no cycles. (2) the subgraph (V,T) ( V , T ) is connected.
(i.e., contains path between each pair of vertices).
Standing Assumptions
Assumption #1: Input graph G is connected.
Else no spanning trees.
Easy to check in preprocessing (depth-first search).
Assumption #2: Edge costs are distinct.
Prim’s MST Algorithm
Initialize X = {s} .[ sV s ∈ V chosen arbitrarily].
T = .[invariant: X = vertices spanned by tree-so-far T].
While X V
− − Let e=(u,v) e = ( u , v ) be the cheapest edge of G with uX,vX u ∈ X , v ∉ X .
− − Add e e to T.
Add v v to X.
While loop: increase # of spanned vertices in cheapest way possible.
Correctness of Prim’s Algorithm
Theorem: Prim’s algorithm always computes an MST
Part I: Computes a spanning tree T.
[Will use basic properties of graphs and spanning trees]
(Useful also in Kruskal’s MST algorithm)
Part II: T T ∗ is an MST.
[Will use the “Cut Property”] (Useful also in Kruskal’s MST algorithm)
Later : Fast [O(m log n)] implementation using heaps.
Empty Cut Lemma
Empty Cut Lemma: A graph is not connected ⇔ ∃
cut(A,B) with no crossing edges.
Proof:( ) Assume the RHS. Pick any uA u ∈ A and vB v ∈ B . Since no edges cross (A,B) ( A , B ) there is no u,v u , v path in G. G ⇒ G not connected.
( ) Assume the LHS. Suppose G has no uv u − v path. Define
A = {Vertices reachable from u u in G}.(u’s connected components).
B = {All other vertices} (all other connected components)
Note: No edges cross cut (A,B) (otherwise A would be bigger.)
Two Easy Facts.
Double-Crossing Lemma: Suppose the cycle CE C ⊆ E has an edge crossing the cut ( A,B A , B ): then so does some other edge of C.
Lonely Cut Corollary: If e e is the edge crossing some cut (A,B), then it is not in any cycle.
Running time of Prim’s Algorithms
Initialize X = {s} [ sV s ∈ V chosen arbitrarily ]
T= − T = ∅ [invariant: X = vertices spanned by tree-so-far T]
While XV
− − Let e=(u,v) e = ( u , v ) be the cheapest edge f G with uX,vX u ∈ X , v ∉ X .
− − Add e e to T, add v v to X.
Runnig time of straightforward implementation:
O(n) − O ( n ) iterations[where n= n = # vertices]
O(m) − O ( m ) time per iteration [where m= m = # of edges]
O(mn) ⇒ O ( m n ) time.
Prim’s Algorithm with Heaps
[Compare to fast implementation of Dijkstra’s algorithm]
Invariant #1: Elements in heap = vertices of VX V − X .
Invariant #2: For vVX, v ∈ V − X , Key[v] = cheapest edge (u,v) ( u , v ) with iX i ∈ X (or + + ∞ if no such edges exist).
Check : Can initialize heap with O(m+nlogn)=O(mlogn) O ( m + n l o g n ) = O ( m l o g n ) preprocessing.
To compare keys n1 n − 1 Inserts mn1 m ≥ n − 1 since G G connected.
Note: Given invariants< Extract-Min yields next vertex vX and edge( u,v u , v ) crossing ( X,VX X , V − X ) to add to X and T, respectively.
Maintaining Invariant #2
Issue: Might need to recompute some keys to maintain Invariant #2 after
each Extract-Min.
Pseudocode: When v v added to X:
For each edge (v,w)E ( v , w ) ∈ E :
− − if wVX w ∈ V − X :
− − Delete w w from heap.
Recompute key[w] = min { key[w],cvw k e y [ w ] , c v w }
− − Re-Insert into heap.
Running Time with Heaps
Dominated by time required for heap operations.
( n1 n − 1 ) Inserts during preprocessing.
(n1) Extract-Mins (one per iteration of while loop).
Each edge(v,w) triggers one Delete/Insert combo.
[When its first endpoint is sucked into X]
O(m) ⇒ O ( m ) heap operations.[Recall mn1 m ≥ n − 1 since G G connected].
O(mlog(n)) ⇒ O ( m l o g ( n ) ) time.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值