minimum spanning treeds202/wiki.files/16-mst.pdfminimum spanning tree trees theorem 2 let t = (v;e)...
TRANSCRIPT
Minimum spanning tree
Minimum spanning tree
Trees
Theorem 1
Let G = (V ,E ) be an undirected graph. The followingconditions are equivalent.
1 G is a tree (i.e., G is connected and acyclic).
2 |E | = |V | − 1 and G is acyclic.
3 |E | = |V | − 1 and G is connected.
4 For every pair x , y of vertices, there is a unique simplepath between x and y.
Minimum spanning tree
Trees
Theorem 2
Let T = (V ,E ) be a tree. Let e be a pair of vertices suchthat e /∈ E. Then,
1 The graph G = (V ,E ∪ {e}) contains exactly one simplecycle C . The cycle C passes through e.
2 For every edge e ′ in C , T ′ = (V , (E ∪ {e}) \ {e ′}) is atree.
e
Minimum spanning tree
The minimum spanning tree (MST) problem
Input: A connected undirected graph G = (V ,E ) and aweight function w : E → R.Goal: Find a subgraph T = (V ,ET ) of G such that
1 T is a tree.
2 w(T ) =∑
(u,v)∈ETw(u, v) is minimum.
If T satisfies 1 and 2 then T is called a minimum spanningtree.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
The minimum spanning tree (MST) problem
Input: A connected undirected graph G = (V ,E ) and aweight function w : E → R.Goal: Find a subgraph T = (V ,ET ) of G such that
1 T is a tree.
2 w(T ) =∑
(u,v)∈ETw(u, v) is minimum.
If T satisfies 1 and 2 then T is called a minimum spanningtree.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
Generic MST algorithm
Let A be a set of edges that is a subset of some MST. If(u, v) ∈ E \ A is an edge for which A ∪ {(u, v)} is asubset of the edge set of some MST, we say that (u, v) issafe for A.
The following generic algorithm finds an MST of a graphG .
GenericMST(G ,w)
(1) A← ∅(2) while A is not a spanning tree(3) Find an edge (u, v) that is safe for A(4) A← A ∪ {(u, v)}(5) return A
Minimum spanning tree
Finding a safe edge
A cut in a graph G = (V ,E ) is a partition (S ,V \ S) ofthe vertices of G into two sets.
An edge (u, v) ∈ E crosses the cut (S ,V \ S) if one of itsendpoints is in S and the other is in V \ S .
A cut respects a set A ⊆ E if no edge in A crosses thecut.
A light edge crossing a cut is an edge with minimumweight among all edges crossing the cut.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
iS
V\S
c d
e
f
Minimum spanning tree
Finding a safe edge
Theorem 3
Suppose that
A ⊆ E is a subset of the edge set of some MST.
(S ,V \ S) is a cut that respects A.
(u, v) is a light edge crossing (S ,V \ S).
Then, (u, v) is safe for A.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
iS
V\S
c d
e
f
Minimum spanning tree
Proof of Theorem 3
Let T = (V ,ET ) be an MST such that A ⊆ ET .
If (u, v) ∈ ET we are done.
Otherwise, let H = (V ,ET ∪ {(u, v)}).By Theorem 2, H contains a single simple cycle C .
Traverse C from u in the direction opposite to v .Let y be the first encountered vertex which is not in S ,and let x be the preceding vertex in C .
in Sin V\S
u
v
x
y
Minimum spanning tree
Proof of Theorem 3
Let T ′ = (V , (ET ∪ {(u, v)}) \ {(x , y)}).
By Theorem 2, T ′ is a tree.
(x , y) crosses (S ,V \ S), so w(x , y) ≥ w(u, v).
Therefore, w(T ′) ≤ w(T ).
Since T is an MST, it follows that T ′ is also an MST.
in Sin V\S
u
v
x
y
Minimum spanning tree
Kruskal’s algorithm
(1) Initialize A← ∅.(2) Go over the edges of G in nondecreasing order by weight.(3) If an edge (u, v) connects vertices from two trees in
(V ,A), add (u, v) to A.// In this case, (u, v) is safe in the cut (S ,V \ S),// where S is the set of vertices of the tree of u.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
Kruskal’s algorithm
(1) Initialize A← ∅.(2) Go over the edges of G in nondecreasing order by weight.(3) If an edge (u, v) connects vertices from two trees in
(V ,A), add (u, v) to A.// In this case, (u, v) is safe in the cut (S ,V \ S),// where S is the set of vertices of the tree of u.
4
8
11
1 2
8 7
6
2 49
10
147
v
u
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c},{d},{e}{f},{g},{h},{i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c},{d},{e}{f},{g},{h},{i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c},{d},{e}{f},{g,h},{i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c},{d},{e}{f},{g,h},{i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c,i},{d},{e}{f},{g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c,i},{d},{e}{f},{g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c,i},{d},{e}{f,g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a},{b},{c,i},{d},{e}{f,g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,i},{d},{e}{f,g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,i},{d},{e}{f,g,h}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,f,g,h,i},{d},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,f,g,h,i},{d},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,f,g,h,i},{d},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b},{c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,f,g,h,i},{e}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,e,f,g,h,i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,e,f,g,h,i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,e,f,g,h,i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,e,f,g,h,i}
Minimum spanning tree
Kruskal’s algorithm
MST-Kruskal(G ,w)
(1) A← ∅(2) foreach v ∈ G .V(3) MakeSet(v)(4) sort G .E into nondecreasing order by weight w(5) foreach (u, v) ∈ G .E in nondecreasing order by weight(6) if FindSet(u) 6= FindSet(v)(7) A← A ∪ {(u, v)}(8) Union(u, v)(9) return A 4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f{a,b,c,d,e,f,g,h,i}
Minimum spanning tree
Complexity
Lines 2–3: O(V ).
Line 4: O(E log E ) = O(E logV ).
Lines 5–8: O((V + E )α(V )) = O(Eα(V )).
Total time: O(E logV ).
If edges are already sorted, the time is O(Eα(V )).
Minimum spanning tree
Prim’s algorithm
(1) Initialize A← ∅.(2) Start from an arbitrary vertex r . Repeat |V | − 1 times:(3) Let S be the connected component of (V ,A) that
contains r .(4) Let (u, v) be a light edge crossing (S ,V \ S).(5) Add (u, v) to A.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
Prim’s algorithm
(1) Initialize A← ∅.(2) Start from an arbitrary vertex r . Repeat |V | − 1 times:(3) Let S be the connected component of (V ,A) that
contains r .(4) Let (u, v) be a light edge crossing (S ,V \ S).(5) Add (u, v) to A.
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
ab cdefghi
0∞ ∞∞∞∞∞∞∞
NULLNULLNULLNULLNULLNULLNULLNULLNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
b cdefghi
∞ ∞∞∞∞∞∞∞
NULLNULLNULLNULLNULLNULLNULLNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
b cdefghi
4∞∞∞∞∞8∞
aNULLNULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
b cdefghi
4∞∞∞∞∞8∞
aNULLNULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
cdefghi
∞∞∞∞∞8∞
NULLNULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
cdefghi
8∞∞∞∞8∞
bNULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
cdefghi
8∞∞∞∞8∞
bNULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defghi
∞∞∞∞8∞
NULLNULLNULLNULLaNULL
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defghi
7∞4∞82
cNULLcNULLac
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defghi
7∞4∞82
cNULLcNULLac
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defgh
7∞4∞8
cNULLcNULLa
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defgh
7∞467
cNULLcii
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
defgh
7∞467
cNULLcii
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
gh
7∞
67
cNULL
ii
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
gh
710
27
cf
fi
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
gh
710
27
cf
fi
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
h
710
7
cf
i
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
h
710
1
cf
g
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de
h
710
1
cf
g
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de710
cf
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
de710
cf
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
e 10f
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
e 9 d
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
e 9 d
Minimum spanning tree
Prim’s algorithm
MST-Prim(G ,w , r)
(1) foreach u ∈ G .V(2) u.key←∞(3) u.π ← NULL(4) r .key← 0(5) build a priority queue Q on G .V(6) while Q 6= ∅(7) u ← ExtractMin(Q)(8) foreach v ∈ G .Adj[u](9) if v ∈ Q and w(u, v) < v .key(10) v .π ← u(11) DecreseKey(Q, v ,w(u, v))
4
8
11
1 2
8 7
6
2 49
10
147
a
b
h g
i
c d
e
f
Minimum spanning tree
Complexity
Lines 1–4: O(V ).
Line 5: O(V ).
Line 7: O(V logV ).
Line 11: O(E logV ).
Total time: O(E logV ).
Minimum spanning tree