You are on page 1of 5

# Dijkstra’s Shortest Path Algorithm is popular algorithm for finding shortest path between different nodes.

The algorithm (Pseudo Code) is as follows
procedure Dijkstra (G): weighted connected simple graph, with all weights positive) [G has vertices a = v0, v1, , vn = ! and weights w(v1, v") where w(vi, vj) = #\$%#\$#&' i( [vi, vj) is not an edge in G) (or i := 1 to n *(vi) := #\$%#\$#&' *(a) := 0 + := \$,** [ the la-els are now initiali!ed so that the la-el o( a is 0 and all other la-els are #\$#%#\$#&', + is empt. set) while ! is not -elongs to + -egin u := a verte/ not in + with *(u) minimal + := + , [u) (or all vertices u not in + #( *(u) 0 w(u,v) 1 *(v) then *(v) := *(u) 0 w(u,v) [this adds a verte/ to + with minimal la-el and updates the la-els vertices no in +) end [*(!) = length o( a shortest path (rom a to !)

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

s positive &he source verte/ must also -e positive (rom 0 < to no:(.ertices > 1 <9 void read()5 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.distance[18)5 -ool mark[18)5 99keep track o( visited node int source5 int num:(. &he mark is initiali!ed < to (alse and predecessor is initiali!ed to >1 <9 void initiali!e()5 9< . =djacenc. 7atri/ and source < 7atri/ (rom the user &he num-er o( vertices must -e greather than < !ero. all mem-ers o( =djacenc. 7atri/ must -e postive as distances < are alwa.ertices5 pu-lic: 9< < %unction read() reads \$o o( vertices.ow the following sour!e !ode implements the abo#e e"ample 2include1iostream3 2de(ine #\$%#\$#&' 444 using namespace std5 class Dijkstra6 private: int adj7atri/[18)[18)5 int predecessor[18).

ertices5i00) 6 mark[i) = (alse5 predecessor[i) = >15 distance[i) = #\$%#\$#&'5 A distance[source)= 05 .ertices 1= 0) 6 cout11BCnter the num-er o( vertices o( the graph(should -e 3 0)DnB5 cin33num:(. then it search < (or another node <9 int get?losest.ertices5 while(num:(.ertices>111endl5 cout11BCnter the source verte/ againDnB5 cin33source5 A A void Dijkstra::initiali!e()6 (or(int i=05i1num:(. marked as visited.nmarked\$ode returns the node which is nearest (rom the < @redecessor marked node #( the node is alread. matri/ (or the graphDnB5 cout11B&o enter in(init. enter B11#\$%#\$#&'11endl5 (or(int i=05i1num:(.ertices5 A cout11BCnter the adjacenc.ertices5i00) 6 cout11BCnter the (0ve)weights (or the row B11i11endl5 (or(int j=05j1num:(.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:(.< %unction get?losest.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:(.

nmarked\$ode5 (or(int i=05i1num:(.nmarked\$ode) 0adj7atri/[closest.ertices5i00) 6 i((i == source) cout11(char)(source 0 4H)11B else print@ath(i)5 cout11B>3B11distance[i)11endl5 A B11source5 .nmarked\$ode5 int count = 05 while(count 1 num:(.nmarked\$ode)[i)5 predecessor[i) = closest.ertices5i00) 6 i(((Gmark[i)) FF ( minDistance 3= distance[i))) 6 minDistance = distance[i)5 closest.nmarked\$ode) 0adj7atri/[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:(.nmarked\$ode)[i)) 6 distance[i) = distance[closest.A int Dijkstra::get?losest.nmarked\$ode5 A void Dijkstra::calculateDistance()6 initiali!e()5 int minDistance = #\$%#\$#&'5 int closest.ertices5i00) 6 i(((Gmark[i)) FF (adj7atri/[closest.nmarked\$ode)[i)30) ) 6 i((distance[i) 3 distance[closest.nmarked\$ode = i5 A A return closest.ertices) 6 closest.nmarked\$ode) = true5 (or(int i=05i1num:(.nmarked\$ode = get?losest.nmarked\$ode()5 mark[closest.nmarked\$ode()6 int minDistance = #\$%#\$#&'5 int closest.

A int main()6 Dijkstra G5 G read()5 G calculateDistance()5 G output()5 return 05 A The output of abo#e program is .