Docsity

Minimum Spanning Trees and Prim's Algorithm, Lecture notes of Computer Engineering and Programming

Partial preview of the text

Download Minimum Spanning Trees and Prim's Algorithm and more Lecture notes Computer Engineering and Programming in PDF only on Docsity!

Lecture 7: Minimum Spanning Trees and

Prim’s Algorithm

CLRS Chapter 23

Outline of this Lecture

Spanning trees and minimum spanning trees.

The minimum spanning tree (MST) problem.

The generic algorithm for MST problem.

Prim’s algorithm for the MST problem.

- The algorithm - Correctness - Implementation + Running Time

Spanning Trees

Spanning Trees: A subgraph

of a undirected graph     is a spanning tree of

if it is a tree and

contains every vertex of

.

Example:

a b

c

d

e

a b

c

d

e

a b

c

d

e

a b

c

d

e

Graph spanning tree 1

spanning tree 2 spanning tree 3

Weighted Graphs

Weighted Graphs: A weighted graph is a graph, in

which each edge has a weight (some real number).

Weight of a Graph: The sum of the weights of all

edges.

Example:

a b

c

d

e

a b

c

d

e

a b

c

d

e

a b

c

d

e

10

9

7 32

23

10

32

23 9

7 7

9

32

23

32

23

10

weighted graph

Tree 2, w=71 Tree 3, w=

Tree 1. w=

Minimum spanning tree

Minimum Spanning Trees

A Minimum Spanning Tree in an undirected connected

weighted graph is a spanning tree of minimum weight

(among all spanning trees).

Example:

a b

c

d

e

a b

c

d

e

a b

c

d

e

a b

c

d

e

10

9

7 32

23

10

32

23 9

7 7

9

32

23

32

23

10

weighted graph

Tree 2, w=71 Tree 3, w=

Tree 1. w=

Minimum spanning tree

Minimum Spanning Tree Problem

MST Problem: Given a connected weighted undi-

rected graph

, design an algorithm that outputs a

minimum spanning tree (MST) of

.

Question: What is most intuitive way to solve?

Generic approach: A tree is an acyclic graph.

The idea is to start with an empty graph and try to add

edges one at a time, always making sure that what is

built remains acyclic. And if we are sure every time the

resulting graph always is a subset of some minimum

spanning tree, we are done.

Generic Algorithm for MST problem

Let be a set of edges such that 

, where is a MST. An edge

is a safe edge for , if

is also a subset of some MST.

If at each step, we can find a safe edge

, we

can ’grow’ a MST. This leads to the following generic

approach:

Generic-MST(G, w)

Let A=EMPTY;

while A does not form a spanning tree

find an edge (u, v) that is safe for A

add (u, v) to A

return A

How can we find a safe edge?

How to find a safe edge

Lemma

Let

be a connected, undirected graph

with a real-valued weight function defined on

. Let

be a subset of

that is included in some minimum

spanning tree for

, let

be any cut of

that

respects , and let

be a light edge crossing the

cut

. Then, edge

is safe for .

It means that we can find a safe edge by

1. first finding a cut that respects ,

  1. then finding the light edge crossing that cut.

That light edge is a safe edge.

Proof

1. Let 

, where

is a MST. Suppose

.

  1. The trick is to construct another MST

 that con-

tains both and

, thereby showing

is

a safe edge for .

  1. Add

to

, it creates a cycle. By removing

an edge from the cycle, it becomes a tree again.

In particular, we remove

(



) to make a

new tree

 .

  1. The weight of

 is





  1. Since

is a MST, we must have

 ,

hence

 is also a MST.

7. Since  

is also a subset of

 (a MST),

is safe for .

Prim’s Algorithm

The generic algorithm gives us an idea how to ’grow’

a MST.

If you read the theorem and the proof carefully, you

will notice that the choice of a cut (and hence the

corresponding light edge) in each iteration is imma-

terial. We can select any cut (that respects the se-

lected edges) and find the light edge crossing that cut

to proceed.

The Prim’s algorithm makes a nature choice of the cut

in each iteration – it grows a single tree and adds a

light edge in each iteration.

More Details

Step 0: Choose any element ; set



and

. (Take as the root of our spanning tree.)

Step 1: Find a lightest edge such that one endpoint

is in

and the other is in



. Add this edge to

and its (other) endpoint to

.

Step 2: If

  , then stop & output (minimum)

spanning tree

. Otherwise go to Step 1.

The idea: expand the current tree by adding the

lightest (shortest) edge leaving it and its endpoint.

e

24

20

r

a

b

c

d

26

f

g i r

a

b

c

d e

f

g i

8 8

12

16

14

new

24

20 26

16

14

12

(^23 )

new edge

12 12

h h

Prim’s Algorithm

Worked Example

a

b

c

d

e

f

g

4

8

9

8

2

1

9

7

10

5

6

2

b

c

d

e

f

g

4

8

10

8

2

1

7

9

5

6

2

S={a}

Step 0

a V \ S = {b,c,d,e,f,g}

9

Connected graph

lightest edge = {a,b}

Prim’s Algorithm

Prim’s Example – Continued

c

d

e

f

g

4

8

9

8

2

1

9

7

10

5

6

2

c

e

f

g

8

10

8

2

1

7

9

5

6

2

a

a

4

b

before

b Step 1.

S={a,b}

Step 1.2 after

S={a,b,d}

d

V \ S = {c,d,e,f,g}

V \ S = {c,e,f,g} 9

lightest edge = {b,d}, {a,c}

lightest edge = {d,c}

A={{a,b}}

A={{a,b},{b,d}}

Prim’s Algorithm

Prim’s Example – Continued

c

e

f

g

4

8

9

8

2

1

9

7

10

5

6

2

e

f

g

8

9

10

8

2

1

7

9

5

6

2

a

a

4

b

before

b

d

Step 1.

Step 1.3 after

S={a,b,d}

d

S={a,b,c,d}

V \ S = {c,e,f,g}

V \ S = {e,f,g}

c

lightest edge = {d,c}

lightest edge = {c,f}

A={{a,b},{b,d}}

A={{a,b},{b,d},{c,d}}