Beruflich Dokumente
Kultur Dokumente
Example: ow lets !ome to an e"ample whi!h further illustrates abo#e algorithm. Consider a weighted graph
$ere a% b% ! .. are nodes of the graph and the number between nodes are weights (distan!es) of the graph. ow we are going to find the shortest path between sour!e (a) and remaining #erti!es. The adja!en!& matri" of the graph is
9< < %unction initiali!e initiali!es all the data mem-ers at the -egining o( < the e/ecution &he distance -etween source to source is !ero and all other < distances -etween source and vertices are in(init. &he mark is initiali!ed < to (alse and predecessor is initiali!ed to >1 <9 void initiali!e()5 9<
< %unction get?losest,nmarked$ode returns the node which is nearest (rom the < @redecessor marked node #( the node is alread. marked as visited, then it search < (or another node <9 int get?losest,nmarked$ode()5 9< < %unction calculateDistance calculates the minimum distances (rom the source node to < :ther node <9 void calculateDistance()5 9< < %unction output prints the results <9 void output()5 void print@ath(int)5 A5 void Dijkstra::read()6 cout11BCnter the num-er o( vertices o( the graph(should -e 3 0)DnB5 cin33num:(;ertices5 while(num:(;ertices 1= 0) 6 cout11BCnter the num-er o( vertices o( the graph(should -e 3 0)DnB5 cin33num:(;ertices5 A cout11BCnter the adjacenc. matri/ (or the graphDnB5 cout11B&o enter in(init. enter B11#$%#$#&'11endl5 (or(int i=05i1num:(;ertices5i00) 6 cout11BCnter the (0ve)weights (or the row B11i11endl5 (or(int j=05j1num:(;ertices5j00) 6 cin33adj7atri/[i)[j)5 while(adj7atri/[i)[j)10) 6 cout11BEeights should -e 0ve Cnter the weight againDnB5 cin33adj7atri/[i)[j)5 A A A cout11BCnter the source verte/DnB5 cin33source5 while((source10) FF (source3num:(;ertices>1)) 6 cout11B+ource verte/ should -e -etween 0 andB11num:(;ertices>111endl5 cout11BCnter the source verte/ againDnB5 cin33source5 A A void Dijkstra::initiali!e()6 (or(int i=05i1num:(;ertices5i00) 6 mark[i) = (alse5 predecessor[i) = >15 distance[i) = #$%#$#&'5 A distance[source)= 05
A int Dijkstra::get?losest,nmarked$ode()6 int minDistance = #$%#$#&'5 int closest,nmarked$ode5 (or(int i=05i1num:(;ertices5i00) 6 i(((Gmark[i)) FF ( minDistance 3= distance[i))) 6 minDistance = distance[i)5 closest,nmarked$ode = i5 A A return closest,nmarked$ode5 A void Dijkstra::calculateDistance()6 initiali!e()5 int minDistance = #$%#$#&'5 int closest,nmarked$ode5 int count = 05 while(count 1 num:(;ertices) 6 closest,nmarked$ode = get?losest,nmarked$ode()5 mark[closest,nmarked$ode) = true5 (or(int i=05i1num:(;ertices5i00) 6 i(((Gmark[i)) FF (adj7atri/[closest,nmarked$ode)[i)30) ) 6 i((distance[i) 3 distance[closest,nmarked$ode) 0adj7atri/[closest,nmarked$ode)[i)) 6 distance[i) = distance[closest,nmarked$ode) 0adj7atri/[closest,nmarked$ode)[i)5 predecessor[i) = closest,nmarked$ode5 A A A count005 A A void Dijkstra::print@ath(int node)6 i((node == source) cout11(char)(node 0 4H)11B B5 else i((predecessor[node) == >1) cout11B$o path (rom I11source11Jto B11(char)(node 0 4H)11endl5 else 6 print@ath(predecessor[node))5 cout11(char) (node 0 4H)11B B5 A A void Dijkstra::output()6 (or(int i=05i1num:(;ertices5i00) 6 i((i == source) cout11(char)(source 0 4H)11B else print@ath(i)5 cout11B>3B11distance[i)11endl5 A
B11source5