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 cece for each edge e∈Ee∈E.
−− Assume adjacent list repersentation.
OK if edge costs are negative.
Output:
minimum cost tree T⊆ET⊆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∈Vs∈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 u∈X,v∉Xu∈X,v∉X.
−−−− Add ee to T.
Add vv 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 .
[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 u∈Au∈A and v∈Bv∈B. Since no edges cross (A,B)(A,B) there is no u,vu,v path in G. ⇒G⇒G not connected.
(⇒⇒) Assume the LHS. Suppose G has no u−vu−v path. Define
A = {Vertices reachable from uu in G}.(u’s connected components).
B = {All other vertices} (all other connected components)
Note: No edges cross cut () (otherwise A would be bigger.)
Two Easy Facts.
Double-Crossing Lemma: Suppose the cycle C⊆EC⊆E has an edge crossing the cut (A,BA,B): then so does some other edge of C.
Lonely Cut Corollary: If ee is the edge crossing some cut (), then it is not in any cycle.
Running time of Prim’s Algorithms
−− Initialize X = {} [s∈Vs∈V chosen arbitrarily ]
−T=∅−T=∅ [invariant: X = vertices spanned by tree-so-far T]
−− While
−−−− Let e=(u,v)e=(u,v) be the cheapest edge f G with u∈X,v∉Xu∈X,v∉X.
−−−− Add ee to , add vv to .
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(mn) time.
Prim’s Algorithm with Heaps
[Compare to fast implementation of Dijkstra’s algorithm]
Invariant #1: Elements in heap = vertices of V−XV−X.
Invariant #2: For v∈V−X,v∈V−X, Key[v] = cheapest edge (u,v)(u,v) with i∈Xi∈X (or +∞+∞ if no such edges exist).
Check : Can initialize heap with O(m+nlogn)=O(mlogn)O(m+nlogn)=O(mlogn) preprocessing.
To compare keys n−1n−1 Inserts m≥n−1m≥n−1 since GG connected.
Note: Given invariants< Extract-Min yields next vertex and edge(u,vu,v) crossing (X,V−XX,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 vv added to X:
For each edge(v,w)∈E(v,w)∈E:
−−−− if w∈V−Xw∈V−X :
−−−− Delete ww from heap.
Recompute key[w] = min {key[w],cvwkey[w],cvw}
−−−− Re-Insert into heap.
Running Time with Heaps
−− Dominated by time required for heap operations.
(n−1n−1) Inserts during preprocessing.
−− () Extract-Mins (one per iteration of while loop).
−− Each edge() triggers one Delete/Insert combo.
[When its first endpoint is sucked into X]
⇒O(m)⇒O(m) heap operations.[Recall m≥n−1m≥n−1 since GG connected].
⇒O(mlog(n))⇒O(mlog(n)) time.
Algorithm-part3-week1
最新推荐文章于 2024-11-03 14:12:43 发布