Prim's Algorithm for Minimum Spanning Tree (MST), Slides of Design and Analysis of Algorithms

The implementation and explanation of prim's algorithm for finding the minimum spanning tree (mst) of a weighted connected graph. The algorithm is presented step by step with visual examples.

Typology: Slides

2011/2012

Uploaded on 07/13/2012

eken
eken 🇮🇳

25 documents

1 / 70

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Minimum Spanning Tree
Algorithms
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46

Partial preview of the text

Download Prim's Algorithm for Minimum Spanning Tree (MST) and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Minimum Spanning Tree

Algorithms

docsity.com

Greedy Algorithms

^

Main Concept: Make the

best

or

greedy

choice at any

given step.

^

Choices are made in sequence such that»^

Each individual choice is best according to some limited “short-term” criterion, that is not too expensive to evaluate» Once a choice is made, it cannot be undone!^ 

Even if it becomes evident later that it was a poor choice  Sometimes life is like that

^

The goal is to»^

take a sequence of locally optimal actions, hoping to yield aglobally optimal solution.

docsity.com

Minimum Spanning Tree (MST)

^

A^

spanning tree

for a connected, undirected

graph,

G

=(

V , E ) is

1.^

a connected subgraph of

G^

that forms an

2.^

undirected tree incident with each vertex.

^

In a weighted graph

G

=(

V ,

E , W ),

»^

the weight of a subgraph is the sum of the weights ofthe edges in the subgraph.

^

A^

minimum spanning tree

(MST) for a weighted

graph is»^

a spanning tree with the minimum weight.

docsity.com

Minimum Spanning Tree

^

Problem: given a connected, undirected, weightedgraph:^14

find a

spanning tree

using edges that

minimize the total weight

docsity.com

Minimum Spanning Tree

^

Answer:

H^

B^

C

G^

E^

D

A F

docsity.com

Another Example

^

Given a weighted graph

G

V ,

E

, W

), find a MST

of G^3

docsity.com

Prime’s Algorithm(High-Level Pseudocode) ^

Prime(G)//Input: A weighted connected graph G = <V, E>//Output: E

--- the set of edges composing MST of GT^

VT^

= {v

ET^

=^ 

for

i = 1

to^

|V| - 1

do

find a minimum-weight edge e* = (u, v) among all the edges (u, v) such that u is in V

and v is in V-VT

T

VT^

= V

T

{v*}

ET^

= E

T

{e*}

return

ET

docsity.com

Prime’s Algorithm(High-Level Pseudocode) ^

Prime(G)//Input: A weighted connected graph G = <V, E>//Output: E

--- the set of edges composing MST of GT^

VT^

= {v

ET^

=^ 

for

i = 1

to^

|V| - 1

do

find a minimum-weight edge e* = (u, v) among all the edges (u, v) such that u is in V

and v is in V-VT

T

VT^

= V

T

{v*}

ET^

= E

T

{e*}

return

ET

H^

B^

C

G^

E^

D

A F

14

6 10 3

4 5

2

(^915)

8

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

Grow a single tree by repeatedlyadding the least cost edge thatconnects a vertex in the existing treeto a vertex not in the existing tree^ w(u,v);

Intermediary solution is a subtree

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

14^ w(u,v);

6 10 3

4 5

2

(^915)

8 Run on example graph

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

^ w(u,v);

^

^

14

6 10 3

4 5

2

(^915)

8 Pick a start vertex r

r

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

^ w(u,v);

^

^

14

6 10 3

4 5

2

(^915)

8

Red vertices have been removed from Q

u

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

14 w(u,v);

^

^

^3

14

6 10 3

4 5

2

(^915)

8

u

docsity.com

Prim’s Algorithm

MST-Prim(G,

w,

r)

Q^

=^

V[G];

for

each

u

Q

key[u]

key[r]

p[r]

NULL;

while

(Q

not

empty)

u^

=^

ExtractMin(Q); for

each

v

Adj[

u ]

if (v

Q

and

w(

u,v

)^

<^

key[

v ])

p[v]

u; key[v]

14 w(u,v);

^

^

^3

14

6 10 3

4 5

2

(^915)

8

u

docsity.com