# Include <iostream>
# Include "Dijkstra. h"
Using namespace std;
Int main ()
{
Cout <"Enter the k value in the two-dimensional array (k * k :";
Int k;
Cin> k;
Int ** p = new int * [k];
For (int I = 0; I <k; ++ I)
{
P [I] = new int [k];
}
Cout <"Enter the elements in the two-dimensional array (100 indicates infinity ):";
For (int I = 0; I <k; ++ I)
{
For (int j = 0; j <k; ++ j)
{
Cin> p [I] [j];
}
}
Bool * S = new bool [k];
Int * D = new int [k];
Int * Par = new int [k];
Int s = 0;
Cout <"Enter the source point :";
Cin> s;
Dijkstra (p, s, S, D, Par, k );
For (int d = 0; d <k; ++ d)
{
Display (s, d, D, Par, k );
}
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
# Define INFINITE 100
Bool emptyBlue (bool S [], int size );
Void Dijkstra (int ** p, int s, bool S [], int D [], int Par [], int size );
Int chooseBlue (bool S [], int D [], int size );
Void relax (int ** p, bool S [], int D [], int Par [], int size );
Void addRed (bool S [], int n );
Int getVertex (int ** p, int s, int size );
Void initializeS (bool S [], int s, int size );
Void initializeupa (int Par [], int ** p, int s, int size );
Void initializeD (int ** p, int D [], int s, int size );
String route (int s, int d, int Par [], int size );
String reverse (string s );
Void display (int s, int d, int D [], int Par [], int size );
Void Dijkstra (int ** p, int s, bool S [], int D [], int Par [], int size)
{
InitializeS (S, s, size );
Initializeupa (Par, p, s, size );
InitializeD (p, D, s, size );
While (! EmptyBlue (S, size ))
{
Int nextRed = getVertex (p, s, size );
AddRed (S, nextRed );
Relax (p, S, D, Par, size );
NextRed = chooseBlue (S, D, size );
AddRed (S, nextRed );
}
}
Bool emptyBlue (bool S [], int size)
{
For (int I = 0; I <size; ++ I)
{
If (S [I] = false)
{
Return false;
}
}
Return true;
}
Int chooseBlue (bool S [], int D [], int size)
{
Int min = INFINITE;
Int index = 0;
For (int I = 0; I <size; ++ I)
{
If (S [I] = false & D [I] <min)
{
Index = I;
Min = D [I];
}
}
Return index;
}
Void relax (int ** p, bool S [], int D [], int Par [], int size)
{
For (int I = 0; I <size; ++ I)
{
If (S [I] = true)
{
For (int j = 0; j <size; ++ j)
{
If (S [j] = false)
{
If (D [I] + p [I] [j] <D [j])
{
Par [j] = I;
D [j] = D [I] + p [I] [j];
}
}
}
}
}
}
Void addRed (bool S [], int n)
{
S [n] = true;
}
Int getVertex (int ** p, int s, int size)
{
Int min = INFINITE;
Int index = 0;
For (int I = 0; I <size; ++ I)
{
If (I = s)
{
Continue;
}
If (p [s] [I] <min)
{
Min = p [s] [I];
Index = I;
}
}
Return index;
}
Void initializeS (bool S [], int s, int size)
{
For (int I = 0; I <size; ++ I)
{
If (I = s)
{
S [I] = true;
}
Else
{
S [I] = false;
}
}
}
Void initializeupa (int Par [], int ** p, int s, int size)
{
For (int I = 0; I <size; ++ I)
{
If (I = s)
{
Par [I] =-1;
}
Else
{
If (p [s] [I] <INFINITE)
{
Par [I] = s;
}
Else
{
Par [I] =-1;
}
}
}
}
Void initializeD (int ** p, int D [], int s, int size)
{
For (int I = 0; I <size; ++ I)
{
D [I] = p [s] [I];
}
} String route (int s, int d, int Par [], int size)
{
Int * t = new int [size];
For (int I = 0; I <size; ++ I)
{
T [I] = Par [I];
}
String str = "";
Char * c = new char;
If (Par [s] = t [d])
{
Itoa (s, c, 10 );
Str + = c;
}
Else
{
Itoa (d, c, 10 );
Str + = c;
While (Par [s]! = T [d])
{
Itoa (t [d], c, 10 );
Str + = c;
Int k = t [d];
T [d] = Par [k];
// Str + = "";
}
}
}
String reverse (string s)
{
Int k = s. length ();
String s1 = "";
For (int I = 0; I <k; ++ I)
{
S1 + = s [k-I-1];
}
Return s1;
}
Void display (int s, int d, int D [], int Par [], int size)
{
String str = reverse (route (s, d, Par, size ));
Cout <s <"---->" <d <"Shortest Path" <D [d] <"Father is"
<Par [d] <"Path:" <str <endl;
}