You are on page 1of 7

Arante,CharmaineP.

Cabigting,JeremiahM.
Dizon,AngelineC.
Lacandola,JohnCarloO.
PrimsAlgorithm

AsystematicmethodoranalgorithmfordeterminingaMinimum
SpanningTree(MST)inaconnectedgraph.

Example:

Heresagraphcontainingthevertices
A, B, C, D, E, and F. We are looking
for a Minimum Spanning Tree that
connects all the vertices together
withoutanyloopsormultipleedges.

We start by choosing any starting


vertex.Itdoesntmatterwhichvertex
you choose but we will start with
vertexA.Wewillchoosetheedgewith
thelowestweightthatisconnectedto
vertex A. In this case its the edge
AB.Thisisthestartofourtree.

Then, we look out for the vertices


connected to A and B. These are the
edgeBCwithaweightof8,BDwitha
weightof6,ADwithaweightof5,and
AFwithaweightof6.Againwechoose
the edge with the lowest weight which
istheedgeADthenweaddthistoour
tree.

Then, we look out for the edges


connectedtothechosenvertices.They
areBCwithaweightof8,inthecase
of edge BD we will not consider with
becausethevertexDisalreadychose,
sothenextavailableedgesareAFwith
aweightof6,DFwithaweightof7,
DE with a weight of 6, and CD with a
weight of 3. Again we choose the edge
with the lowest weight which is the
edgeCDthenweaddthistoourtree.

Nowwelookfortheedgesconnected
to

verticesA,B,C,andDwhichareCE
withaweightof5,DEwithaweight

of

6,andDFwithaweightof7Again

we

choose the edge with the lowest


weightwhichistheedgeCEthenwe
addthistoourtree.

Sothereisonemorevertextoaddforustogetaminimumspanning
treeandthatisvertexF.

Nowwelookfortheedgewiththe
lowestweightthatareconnectedto
A,B,D,EandFwhichisedgeEF.
Thenweaddthistoourtree.

Thisistheminimumspanningtree.
Lastly, we look for the total
weight of this minimum spanning
treebyaddingtheweightofeach
edge.

Totalweight=
2+5+3+5+2=17

Notethatifedgeweightisdistinct,thisminimumspanningtreewill
beunique

Algorithm:
1. Chooseanystartingvertex.Lookatalledgesconnectingtothe
chosenstartingvertexandchoosetheedgewiththelowestweight
andaddthistothetree.
2. Lookatalltheedgesconnectedtothetree.Choosetheedgewith
thelowestweightconnectedtotheverticeschosenandaddthat
tothetree.Ifthereisanedgethatisalreadyconnectedtoa
chosenvertex,wedonotconsiderit.
3. Repeatstep2untilallverticesareinthetree.
PseudoCode:
1. Makeaqueue(Q)withalltheverticesofG(V);
2. ForeachmemberofQsettheprioritytoINFINITY;
3. Onlyforthestartingvertex(s)setthepriorityto0;
4. Theparentof(s)shouldbeNULL;
5. WhileQisntempty
6. GettheminimumfromQletssay(u);(priorityqueue);
7. Foreachadjacentvertexto(v)to(u)
8. If(v)isinQandweightof(u,v)<priorityof(v)then
9. Theparentof(v)issettobe(u)
10.
Thepriorityof(v)istheweightof(u,v)
ProgrammingCode:
HeresaPHPimplementationofthealgorithmofPrim,whichdirectly
followsthepseudocode:
define('INFINITY',100000000);
$G=array(
0=>array(0,4,0,0,0,0,0,0,8),
1=>array(4,0,8,0,0,0,0,0,11),
2=>array(0,8,0,7,0,4,2,0,0),
3=>array(0,0,7,0,9,14,0,0,0),
4=>array(0,0,0,9,0,10,0,0,0),
5=>array(0,0,4,14,10,0,0,2,0),

6=>array(0,0,2,0,0,0,0,6,7),
7=>array(0,0,0,0,0,2,6,0,1),
8=>array(8,11,0,0,0,0,7,1,0),
);
functionprim(&$graph,$start)
{
$q=array();//queue
$p=array();//parent
foreach(array_keys($graph)as$k){
$q[$k]=INFINITY;
}
$q[$start]=0;
$p[$start]=NULL;
asort($q);
while($q){
//gettheminimumvalue
$keys=array_keys($q);
$u=$keys[0];
foreach($graph[$u]as$v=>$weight){
if($weight>0&&in_array($v,$keys)&&$weight<
$q[$v]){
$p[$v]=$u;
$q[$v]=$weight;
}

unset($q[$u]);
asort($q);
}
return$p;
}
prim($G,5);

You might also like