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
e∈E
e
∈
E
.
−
−
Assume adjacent list repersentation.
OK if edge costs are negative.
Output:
minimum cost tree
T⊆E
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∈V
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
u∈X,v∉X
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 .
[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∈A
u
∈
A
and
v∈B
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
u−v
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 () (otherwise A would be bigger.)
Two Easy Facts.
Double-Crossing Lemma: Suppose the cycle
C⊆E
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 (), then it is not in any cycle.
Running time of Prim’s Algorithms
−
−
Initialize X = {} [
s∈V
s
∈
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∉X
u
∈
X
,
v
∉
X
.
−−
−
−
Add
e
e
to , add
v
v
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
(
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
V
−
X
.
Invariant #2: For
v∈V−X,
v
∈
V
−
X
,
Key[v] = cheapest edge
(u,v)
(
u
,
v
)
with
i∈X
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
n−1
n
−
1
Inserts
m≥n−1
m
≥
n
−
1
since
G
G
connected.
Note: Given invariants< Extract-Min yields next vertex and edge(
u,v
u
,
v
) crossing (
X,V−X
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
w∈V−X
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.
(
n−1
n
−
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−1
m
≥
n
−
1
since
G
G
connected].
⇒O(mlog(n))
⇒
O
(
m
l
o
g
(
n
)
)
time.
Algorithm-part3-week1
最新推荐文章于 2018-06-02 21:50:40 发布