65
Minimum spanning tree Minimum spanning tree

Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

Minimum spanning tree

Minimum spanning tree

Page 2: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 3: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 4: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 5: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 6: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 7: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 8: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 9: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 10: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 11: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 12: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 13: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 14: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 15: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 16: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 17: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 18: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 19: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 20: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 21: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 22: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 23: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 24: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 25: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 26: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 27: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 28: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 29: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 30: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 31: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 32: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 33: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 34: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 35: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 36: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 37: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 38: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 39: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 40: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 41: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 42: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 43: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 44: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 45: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 46: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 47: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 48: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 49: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 50: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 51: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 52: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 53: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 54: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 55: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 56: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 57: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 58: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 59: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 60: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 61: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 62: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 63: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 64: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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

Page 65: Minimum spanning treeds202/wiki.files/16-MST.pdfMinimum spanning tree Trees Theorem 2 Let T = (V;E) be a tree. Let e be a pair of vertices such that e 2=E. Then, 1 The graph G = (V;E

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