Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Prim's Algorithm-Algorithm Design and Analysis-Lecture Slides, Slides of Design and Analysis of Algorithms

This lecture is part of lecture series for Design and Analysis of Algorithms course. This course was taught by Dr. Bhaskar Sanyal at Maulana Azad National Institute of Technology. It includes: Prim, Algorithm, Running, Time, Priority, Implementation, Performance, Tree, Queue, Kruskal, Greedy

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dharmadaas
dharmadaas 🇮🇳

4.3

(55)

267 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
26
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u Q
key[u] = ;
key[r] = 0;
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);
10 2
0 8 15
3
14 10
3
6 4
5
2
9
15
8
u
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Prim's Algorithm-Algorithm Design and Analysis-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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 10

3

6 4 5

2

9

15

8

u

Docsity.com

Review: Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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; DecreaseKey(v, w(u,v));

delete the smallest element from the min-heap

decrease an element’s value in the min-heap (outline an efficient algorithm for it) Docsity.com

Review: Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; 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; DecreaseKey(v, w(u,v));

How often is ExtractMin() called?

How often is DecreaseKey() called?

Docsity.com

In-Class Exercise (9.1.7)

Docsity.com

Finding a MST

 Principal greedy methods: algorithms by Prim and Kruskal

 Prim

» Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree  Intermediary solution is a subtree

 Kruskal

» Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far  Intermediary solution is a spanning forest

Docsity.com

Kruskal’s Algorithm

(High-Level Pseudocode)

 Kruskal(G) //Input: A weighted connected graph G = <V, E> //Output: ET --- the set of edges composing MST of G Sort E in nondecreasing order of the edge weight ET = ; encounter = 0 //initialize the set of tree edges and its size k = 0 //initialize the number of processed edges while encounter < |V| - 1 k = k+ if ET  {ek} is acyclic ET = ET  {ek}; encounter = encounter + 1 return ET

H B C

G E D

F

A

14 10

3

6 4 5

2

9

15

(^8) Docsity.com

Kruskal’s Algorithm

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Grow a tree by repeatedly adding the least

cost edge that does not introduce a cycle

among the edges included so far

Intermediary solution is a spanning

forest

Docsity.com

Kruskal’s Algorithm

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com

Kruskal’s Algorithm

Kruskal() { T = ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }

Run the algorithm:

Docsity.com