minimum spanning tree - bguds192/wiki.files/17-mst.pdf · generic mst algorithm let a be a set of...

60
Minimum spanning tree Minimum spanning tree

Upload: others

Post on 10-Feb-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

Minimum spanning tree

Minimum spanning tree

Page 2: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

The minimum spanning tree (MST) problem

Input: A connected undirected graph G = (V ,E ) and aweight function w : E → R.Goal: Find T ⊆ E such that

1 (V ,T ) is connected and acyclic.

2 w(T ) =∑

(u,v)∈T w(u, v) is minimum.

A set T satisfying 1 is called a spanning tree.A set T satisfying 1 and 2 is called a minimum spanning tree.

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 3: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

The minimum spanning tree (MST) problem

Input: A connected undirected graph G = (V ,E ) and aweight function w : E → R.Goal: Find T ⊆ E such that

1 (V ,T ) is connected and acyclic.

2 w(T ) =∑

(u,v)∈T w(u, v) is minimum.

A set T satisfying 1 is called a spanning tree.A set T satisfying 1 and 2 is called a minimum spanning tree.

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 4: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 some MST, we say that (u, v) is safe 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 5: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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

fMinimum spanning tree

Page 6: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

Finding a safe edge

Theorem

Suppose that

A ⊆ E is a subset 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

fMinimum spanning tree

Page 7: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

Proof

Let T be an MST that includes A.

If (u, v) is in T we are done.

Otherwise, let P be the unique path from u to v in T .

Let x be the last vertex from S in P , and let y be thenext vertex in P .

(x , y) crosses (S ,V \ S), so w(x , y) ≥ w(u, v).

Therefore, T ′ = T \ {(x , y)} ∪ {(u, v)} is an MST thatincludes (u, v).

in T, not in Ain A & T

in Sin V-S

u

v y

x

Minimum spanning tree

Page 8: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 9: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 10: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 11: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 12: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 13: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 14: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 15: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 16: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 17: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 20: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 21: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 22: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 23: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 24: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 25: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 26: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 29: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 30: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 31: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 32: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 34: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 35: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 36: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 37: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 38: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 39: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 40: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 41: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 42: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 43: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 44: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 45: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 46: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 47: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 48: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 49: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 50: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 51: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 52: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 53: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 54: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 55: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 56: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 57: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 58: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 59: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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 60: Minimum spanning tree - BGUds192/wiki.files/17-MST.pdf · Generic MST algorithm Let A be a set of edges that is a subset of some MST. If (u;v) 2E nA is an edge for which A[f(u;v)gis

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