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 c e for each edge e E .
Assume adjacent list repersentation.
OK if edge costs are negative.
Output:
minimum cost tree T E that spans all vertices. (cost: sum of edge costs).
(1) T has no cycles. (2) the subgraph ( 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 } .[ s V chosen arbitrarily].
T = .[invariant: X = vertices spanned by tree-so-far T].
While X V
Let e = ( u , v ) be the cheapest edge of G with u X , v X .
Add e to T.
Add 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 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 A and v B . Since no edges cross ( A , B ) there is no u , v path in G. G not connected.
( ) Assume the LHS. Suppose G has no u v path. Define
A = {Vertices reachable from 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 C E has an edge crossing the cut ( A , B ): then so does some other edge of C.
Lonely Cut Corollary: If 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 } [ s V chosen arbitrarily ]
T = [invariant: X = vertices spanned by tree-so-far T]
While X V
Let e = ( u , v ) be the cheapest edge f G with u X , v X .
Add e to T , add v to X .
Runnig time of straightforward implementation:
O ( n ) iterations[where n = # vertices]
O ( m ) time per iteration [where m = # of edges]
O ( m n ) time.
Prim’s Algorithm with Heaps
[Compare to fast implementation of Dijkstra’s algorithm]
Invariant #1: Elements in heap = vertices of V X .
Invariant #2: For v V X , Key[v] = cheapest edge ( u , v ) with i X (or + if no such edges exist).
Check : Can initialize heap with O ( m + n l o g n ) = O ( m l o g n ) preprocessing.
To compare keys n 1 Inserts m n 1 since G connected.
Note: Given invariants< Extract-Min yields next vertex v X and edge( u , v ) crossing ( 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 added to X:
For each edge ( v , w ) E :
if w V X :
Delete w from heap.
Recompute key[w] = min { k e y [ w ] , c v w }
Re-Insert into heap.
Running Time with Heaps
Dominated by time required for heap operations.
( n 1 ) Inserts during preprocessing.
( n 1 ) 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 ) heap operations.[Recall m n 1 since G connected].
O ( m l o g ( n ) ) time.

猜你喜欢

转载自blog.csdn.net/qq_31805127/article/details/80421067