Docsity

Minimum Spanning Tree (MST) Algorithm: Kruskal's Algorithm and Cut Property, Study notes of Algorithms and Programming

Partial preview of the text

Download Minimum Spanning Tree (MST) Algorithm: Kruskal's Algorithm and Cut Property and more Study notes Algorithms and Programming in PDF only on Docsity!

Design and analysis of algorithms

Lecture 18 & 19^ Edyta Szyma ´

nska [email protected]

CS3510 A, Fall 2005 – p. 1/

Minimum spanning tree (MST)

Problem:

given a set of computers and a maintenance cost associated with every potential link, build a communicationnetwork which is connected and has the cheapest cost.

CS3510 A, Fall 2005 – p. 2/

Minimum spanning tree (MST)

Problem:

given a set of computers and a maintenance cost associated with every potential link, build a communicationnetwork which is connected and has the cheapest cost.^34 B

1 4 4

2

4 6

5 C^

E F

A

D Observation:

the optimal set of edges cannot contain a cycle.

CS3510 A, Fall 2005 – p. 2/

Minimum spanning tree (MST)

Problem:

given a set of computers and a maintenance cost associated with every potential link, build a communicationnetwork which is connected and has the cheapest cost.^34 B

1 4 4

2

4 6

5 C^

E F

A

D Observation:

the optimal set of edges cannot contain a cycle.Thus, the solution must be connected and acyclic -

TREE

CS3510 A, Fall 2005 – p. 2/

Minimum spanning tree (MST)

INPUT: an undirected, connected graph

G^ = (

V, E)

with edge weights

w(e)

OUTPUT: a tree

T^ = (

′V, E

)^ spanning all the vertices of

G^ and

minimizing weight

(T^ ) =

∑ ′ e∈E w(e)

(^34) B 1 4 4

2

4 6

5 C^

E F

A

D MST cost in the example above is 16.

CS3510 A, Fall 2005 – p. 3/

Minimum spanning tree (MST)

INPUT: an undirected, connected graph

G^ = (

V, E)

with edge weights

w(e)

OUTPUT: a tree

T^ = (

′V, E

)^ spanning all the vertices of

G^ and

minimizing weight

(T^ ) =

∑ ′ e∈E w(e)

(^34) B 1 4 4

2

4 6

5 C^

E F

A

D MST cost in the example above is 16.

CS3510 A, Fall 2005 – p. 3/

Minimum spanning tree (MST)

INPUT: an undirected, connected graph

G^ = (

V, E)

with edge weights

w(e)

OUTPUT: a tree

T^ = (

′V, E

)^ spanning all the vertices of

G^ and

minimizing weight

(T^ ) =

∑ ′ e∈E w(e)

(^34) B 1 4 4

2

4 6

5 C^

E F

A

D MST cost in the example above is 16. Is it unique?

CS3510 A, Fall 2005 – p. 3/

Greedy approach

Algorithms?

CS3510 A, Fall 2005 – p. 4/

Greedy approach

Algorithms? "Greed ... is good. Greed is right. Greed works." Kruskal’s greedy algorithm: build a spanning tree by successively adding edges inorder of increasing weights and in such a way that they donot form a cycle with those edges already chosen.

CS3510 A, Fall 2005 – p. 4/

Greedy approach

Algorithms? "Greed ... is good. Greed is right. Greed works." Kruskal’s greedy algorithm: build a spanning tree by successively adding edges inorder of increasing weights and in such a way that they donot form a cycle with those edges already chosen. See example on the board.

CS3510 A, Fall 2005 – p. 4/

Why is Kruskal’s algorithm correct?

We will proof a more general property first:

CS3510 A, Fall 2005 – p. 5/

Why is Kruskal’s algorithm correct?

Cut property:

Let^ X

⊆^ E

be part of some minimum spanning tree

T

of^ G^

= (V, E

).^ Pick any set of nodes

S^ ⊂

V^ such that there is no

edge in

X^ which connects a node in

S^ to one in

V^ ^

S.^ If^ e

∈^ E

is the

minimum-weight edge between

S^ and

V^ −

S,^ then

X^ ∪ {

e}^ is also

part of some MST.

CS3510 A, Fall 2005 – p. 5/

Why is Kruskal’s algorithm correct?

Proof:

Assume

e /∈^ T.^ Then

T^ ∪ {

e}^ contains a cycle

C.

Now

C^ contains another edge

′^ eacross the cut

(S, V

^ S)

e e’ S^

V^ ^ S

CS3510 A, Fall 2005 – p. 6/

Why is Kruskal’s algorithm correct?

Proof:

Assume

e /∈^ T.^ Then

T^ ∪ {

e}^ contains a cycle

C.

Now

C^ contains another edge

′^ eacross the cut

(S, V

^ S)

e e’ S^

V^ ^ S

Consider

′^ T =

T^ ∪ {

e} − {

′′e}.It is a tree with

′ w(T ) =^ w

(T^ ) +

w(e)

−^ w

′(e)^ ≤ w(T

),^ because

w(e)

≤^ w

′(e).^ Since

T^ is MST, so is

′ T.

CS3510 A, Fall 2005 – p. 6/