






























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 70
This page cannot be seen from the preview
Don't miss anything!































































docsity.com
^
^
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
^
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.^
2.^
In a weighted graph
G
=(
V ,
E , W ),
A^
minimum spanning tree
(MST) for a weighted
graph is»^
docsity.com
docsity.com
docsity.com
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
for
i = 1
to^
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
{v*}
ET^
{e*}
return
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
for
i = 1
to^
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
{v*}
ET^
{e*}
return
14
6 10 3
4 5
2
(^915)
8
docsity.com
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
and
w(
u,v
key[
v ])
p[v]
u; key[v]
docsity.com
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
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
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
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
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
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
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
and
w(
u,v
key[
v ])
p[v]
u; key[v]
14 w(u,v);
14
6 10 3
4 5
2
(^915)
8
u
docsity.com
MST-Prim(G,
w,
r)
for
each
u
key[u]
key[r]
p[r]
while
not
empty)
u^
ExtractMin(Q); for
each
v
Adj[
u ]
if (v
and
w(
u,v
key[
v ])
p[v]
u; key[v]
14 w(u,v);
14
6 10 3
4 5
2
(^915)
8
u
docsity.com