Docsity

Minimal Spanning Tree Problem-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Minimal Spanning Tree Problem-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 32

Minimal Spanning Tree Problem

•^

Importance of Minimal Spanning Trees (MST)

-^

MST Problem– Definitions and analysis– Generic Solution– Proofs of correctness

-^

Kruskal’s Algorithm– Algorithm– Analysis

-^

Prim’s Algorithm– Algorithm– Analysis

-^

Conclusion

Today Lecture Covers

Example of MST

•^

Minimum spanning trees are not unique–^

If we replace (b, c) with (a, h), get a different spanningtree with the same cost

-^

MST have no cycles–^

We can take out an edge of

-^

a cycle, and still have the

-^

vertices connected while reducing the cost

4 8

11

8

2

7

1

2

6

(^74)

14

(^910)

a

b

g

h

i

c

d f

e

Generic Solution : To Compute MST

Minimum-spanning-tree problem

: Find a MST for a

connected, undirected graph, with a weight functionassociated with its edges A generic solution: •^

Build a set A of edges(initially empty)

-^

Incrementally add edges to Asuch that they would belong to a MST

-^

An edge

(u, v)

is

safe

for A

A

{(u, v)

}

is also a subset of some MST

4 8

11

8

2

7

1

2

6

(^74)

14

(^910)

a

b

g

h

i

c

d f

e

GENERIC-MST(
G

,^ w

A^
Ø

while

A

does not form a spanning tree

do

find an edge

( u

,^ v

) that is safe for

A
A^
A

u ,

v

return

A

InitializationTermination

Maintenance

Generic Algorithm: Minimum Spanning Tree

•^

The algorithm uses greedy strategy which growsMST one edge at a time.

-^

Given a connected, undirected graph

G

= (V, E) with

a weight function

w :

E
R
•^

Algorithm manages a set of edges

A

, maintaining

loop invariant^ Prior to each iteration, A is a subset of some MST

-^

An edge

(u, v)

is a

safe edge

for

A

such that

A

u, v

)} is also a subset of some MST.

Algorithms, discussed here, to find safe edge are

Strategy: Growing Minimum Spanning Tree1.Kruskal’s Algorithm2.Prim’s Algorithm

Let

G
V
,^ E

) be a connected, undirected graph

with a real-valued weight function

w

on

E

. Let

A

be

a subset of

E

that is included in some minimum

spanning tree for

G

, let (

S
,^ V
S

) be any cut of

G

that respects

A

, and let (

u ,

v

) be a light edge

crossing (

S
,^ V
-^
S

). Then, edge (

u ,

v

) is safe for

A

Proof•^

Let

T

be a minimum spanning

tree that includes

A (edges

of A are shaded)

•^

Assume that

T

does not

contain the light edge (

u ,

v

since if it does, we are done.

Theorem (Kruskal’s Algorithm)

y

u

x

v

S

V - S

Construction of another MST,•^

For (u, v)

T

-^

We construct another MST T

′^ that includes A

{(u, v)}

by cut-and-paste, and showing that (u, v) is a safe A.

-^

Since (u, v) crosses cut set (S, V-S) and

-^

(u, v)

T,

-^

Hence there must be an edge (x, y)

T which crosses

the cut set

-^

By removing (x, y) breaks T into two components.

-^

Adding (u, v) reconnects them to form a new spanningtree T

′^ = T - {(x, y)}

{(u, v)}.

Theorem (Contd..)

Kruskal’s Algorithm

MST-KRUSKAL (
G

,^ w

A
Ø

for

each vertex

v

V
[ G
]

do

MAKE-SET

( v

4 sort edges in non-decreasing order by weight

w

5 for

each

( u

,^ v

) in non-decreasing order by weight

do if

FIND-SET

( u

)^ ≠
FIND-SET(

v )

then

A
A

u ,

v

UNION (

u ,

v

9 return

A

Total Running time = O (E lg V),

2 6

10

2

7

4

8

7

(^118)

1

4

14

9

a

b

g

h

i

c

d f

e

Kruskal’s Algorithm

Initial sets =

{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}

Edges

Weight

(g, h)

1

(c, i)

2

(f, g)

2

(a, b)

4

(c, f)

4

(g, i)

6

(c, d)

7

(h, i)

7

(a, h)

8

(b, c)

8

(d, e)

9

(e, f)

10

(b h)

11

(d, f)

14

2 6

10

2

7

4

8

7

(^118)

1

4

14

9

a

b

g

h

i

c

d f

e

Kruskal’s Algorithm

(c, i)

is the least weight edge FIND-SET

(c)

≠

FIND-SET

(i)

A

A

{(c, i)}

UNION

(c, i)

Initial sets =

{a}, {b}, {

c }, {d}, {e}, {f}, {g, h}, {

i }

Final sets =

{a}, {b}, {

c, i

}, {d}, {e}, {f}, {g, h}

2 6

10

2

7

4

8

7

(^118)

1

4

14

9

a

b

g

h

i

c

d f

e

Kruskal’s Algorithm

(f, g)

is the least weight edge FIND-SET

(f)

≠

FIND-SET

(g)

A

A

{(f, g)}

UNION

(f, g)

Initial sets =

{a}, {b}, {c, i}, {d}, {e}, {

f }, {

g , h}

Final sets =

{a}, {b}, {c, i}, {d}, {e}, {

f, g, h

}

2 6

10

2

7

4

8

7

(^118)

1

4

14

9

a

b

g

h

i

c

d f

e

Kruskal’s Algorithm

(c, f)

is the least weight edge FIND-SET

(c)

≠

FIND-SET

(f)

A

A

{(c, f)}

UNION

(c, f)

Initial sets =

{a, b}, {

c , i}, {d}, {e}, {

f , g, h}

Final sets

= {a, b}, {

c, f, g, h

,^ i }, {d}, {e}

2 6

10

2

7

4

8

7

(^118)

1

4

14

9

a

b

g

h

i

c

d f

e

Kruskal’s Algorithm

(g, i)

is the least weight edge FIND-SET

(g) =

FIND-SET

(i)

Initial sets =

{a, b}, {c, f,

g,

h ,^ i }, {d}, {e}

Final sets =

{a, b}, {c, f, g, h

,^ i}, {d}, {e}