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
for each edge
.
Assume adjacent list repersentation.
OK if edge costs are negative.
Output:
minimum cost tree
that spans all vertices. (cost:
sum of edge costs).
(1) T has no cycles. (2) the subgraph
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 = {
} .[
chosen arbitrarily].
T =
.[invariant: X = vertices spanned by tree-so-far T].
While X
V
Let
be the cheapest edge of G with
.
Add
to T.
Add
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:
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
and
. Since no edges cross
there is no
path in G.
not connected.
(
) Assume the LHS. Suppose G has no
path. Define
A = {Vertices reachable from
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
has an edge crossing the cut (
): then so does some other edge of C.
Lonely Cut Corollary: If
is the edge crossing some cut (
), then it is not in any cycle.
Running time of Prim’s Algorithms
Initialize X = {
} [
chosen arbitrarily ]
[invariant: X = vertices spanned by tree-so-far T]
While
Let
be the cheapest edge f G with
.
Add
to
, add
to
.
Runnig time of straightforward implementation:
iterations[where
# vertices]
time per iteration [where
# of edges]
time.
Prim’s Algorithm with Heaps
[Compare to fast implementation of Dijkstra’s algorithm]
Invariant #1: Elements in heap = vertices of
.
Invariant #2: For
Key[v] = cheapest edge
with
(or
if no such edges exist).
Check : Can initialize heap with
preprocessing.
To compare keys
Inserts
since
connected.
Note: Given invariants< Extract-Min yields next vertex
and edge(
) crossing (
) 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
added to X:
For each edge
:
if
:
Delete
from heap.
Recompute key[w] = min {
}
Re-Insert into heap.
Running Time with Heaps
Dominated by time required for heap operations.
(
) 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]
heap operations.[Recall
since
connected].
time.
Algorithm-part3-week1
猜你喜欢
转载自blog.csdn.net/qq_31805127/article/details/80421067
今日推荐
周排行