Description of The Algorithm
Description of The Algorithm
The problem
The Algorithm
Dijkstra’s algorithm is one of the most common solutions
for this problem. Even so, it only works on graphs which
have no edge of negative weight, and the actual speed of
the algorithm can vary from O(n*lg(lg (n))) to O(n2)
Create_Graph() function
This function will create the adjacency matrix by reading the
distance of each node from previously stored a route
distance in RouteDist.txt from source city to destination
city .And also is used to save the color of each node to green
in an array format to show the nodes are not yet used in the
algorithm.
1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0
Initialize () function
Because of the Create_Graph function, we do have the
Graph and now we already know that the graph has no
edges of negative weight, here the function will set the
length of the shortest path to all cities (nodes) to
Big(Infinity),the length of the shortest path to the source as
zero(0) in the distance storage array dist
1 2 3 4 5 6 7 8 9
10 11 12
0 99 99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99 99
1 2 3 4 5 6 7 8 9 10 11 12
0 1 0 0 0 0 1 1 0 1 0 0
RunSortestPath() function
This function is the main working function of Dijkstra’s
Algorithm in which all the functions used are called from this
function (Create_Graph,Initiaklize,Update). In this function
we use two variables flg,and p
• Flg Variable-this variable lets us to check
whether the task on a given node is completed or not
using the array colour .i.e,checks the value on array
colour of the given node is whether red(value 2) or
not
• P variable –this variable is used to label the
minimum edge distance to YELLOW(value 1) ,i.e. the
node(city) edge is selected as shortest path to reach
to destination node(city)
Get_Min_Distance() function
The use of this function is to label the node (city) to YELLOW
with the minimum edge distance from the source to the
wanted destination node(city) and return the position ‘P’ on
which the colour of the edge to be changed to YELLOW on
array colour.
Update () function
Here this function is used to update the min distance for the
YELLOW coloured edge and modify the predecessor of the
destination with the one shortest distance to it.
struct Truck{
char pNumber[10];
char dName[15];
char dId[10];
char cStation[10];
float tMileAge;
char npRoute[15];
};
struct City {
char cName[10];
int sNumber;
};
class ShortestPath{
int Main_Menu();
void Insert_Data_Menu();
void Insert_Truck();
void Insert_City();
void Insert_Route_Distance();
void search_at_CTY();
void Display_Info_Menu();
void Display_Location(Truck& t);
void Display_By_Plate();
void Display_By_Name();
void print_Header();
void main(void){
clrscr();
int choice = 0;
int Main_Menu(void)
{
int choice;
cout<<"\n\t # # # # # # # # ##### # # # # ";
cout<<"\n\tzzzzz ## ## # # # ## # ## ## # ## # # # zzzzz";
cout<<"\n\tzzzzz # # # ##### # # # # # # # #### # # # # # zzzzz";
cout<<"\n\tzzzzz # # # # # # ## # # # # ## # # zzzzz";
cout<<"\n\t # # # # # # # # # ##### # # ### \n\n\n";
cout<<"\n\t\t+-----------------------------------------------+";
cout<<"\n\t\t| 1. Insert new data |";
cout<<"\n\t\t| |";
cout<<"\n\t\t| 2. Display Information |";
cout<<"\n\t\t| |";
cout<<"\n\t\t| 3. Calculate shortest path |";
cout<<"\n\t\t| |";
cout<<"\n\t\t| 4. Search Truck with lowest mile age at CTY |";
cout<<"\n\t\t| |";
cout<<"\n\t\t| 5. Exit |";
cout<<"\n\t\t+-----------------------------------------------+";
cout<<"\n\n\nPlease enter your choice : ";
cin>>choice;
return choice;
} // Main_Menu
void Insert_Data_Menu(){
int choice;
do{
cout<<"\n\t # # # #### #### #### ##### #### # ##### # ";
cout<<"\n\tzzzzz # ## # # # # # # # # # # # # # zzzzz";
cout<<"\n\tzzzzz # # # # ### #### # # # # # ##### # ##### zzzzz";
cout<<"\n\tzzzzz # # ## # # # # # # # # # # # # zzzzz";
}//Insert_data_Menu
void Insert_Truck(){
fstream tFile("c:\\truck.txt",ios::app|ios::in|ios::out);
char ch;
Truck t;
do{
cout<<"Enter Plate Number:";
cin>>t.pNumber;
cout<<"Enter driver name : ";
cin>>t.dName;
cout<<"Enter driver ID : ";
cin>>t.dId;
cout<<"Enter current station : ";
cin>>t.cStation;
cout<<"Enter total mile Age : ";
cin>>t.tMileAge;
cout<<"Enter next planned route : ";
cin>>t.npRoute;
tFile.write((char *)&t,sizeof(t));
cout<<"\nWant to add more truck info?(y/n)\n";
cin>>ch;
} while (ch=='y' || ch=='Y');
tFile.close();
} // Insert_Truck
void Insert_City(){
Course project on Analysis of Algorithm
8 of 19
fstream cFile("c:\\city.txt",ios::in|ios::out|ios::app);
char ch;
City city_rec;
do{
cout<<"Enter city name:";
cin>>city_rec.cName;
cout<<"Enter city station Number : ";
cin>>city_rec.sNumber;
cFile.write((char *)&city_rec,sizeof(city_rec));
cout<<"\nWant to add more city info?(y\n)\n";
cin>>ch;
} while (ch=='y' || ch=='Y');
} // Insert_City
void Insert_Route_Distance(){
float dis;
clrscr();
float file[MAX][MAX];
fstream tFile("c:\\RouteDis.txt",ios::in|ios::out);
cout<<"\nEnter Distance node if they are connected, otherwise
zero\n\n\n";
for(int i=1;i<=12;i++){
for(int j=1;j<=12;j++){
cout<<"Enter Distance station:"<<i<<"->"<<j<<":";
cin>>dis;
tFile<<dis<<" ";
}
}
tFile.close();
}//Insert_Route_Distance
void Display_Info_Menu(){
int choice;
Truck t;
do{
cout<<"\n #### # #### ### # # # # # # #### # # # # ";
cout<<"\nzzzzz # # # # # # # # # # # ## ## # ## # # # zzzzz";
cout<<"\nzzzzz # # # ### ### # ##### # # # # #### # # # # # zzzzz";
cout<<"\nzzzzz # # # # # # # # # # # # # ## # # zzzzz";
cout<<"\n #### # #### # #### # # # # # #### # # ### \n";
}//Display_Info_Menu
fstream tFile("c:\\truck.txt",ios::in|ios::out);
if(!tFile){
cout<<"Error in Opening Truck File....Program End\n";
exit(1);
}
else{
tFile.seekg(0);
tFile.read((char*)&t,sizeof(t));
cout<<endl;
print_Header();
while(!tFile.eof()){
cout<<setw(10)<<setfill(' ')<<t.pNumber;
cout<<setw(10)<<setfill(' ')<<t.cStation;
cout<<setw(10)<<setfill(' ')<<t.tMileAge<<endl;
tFile.read((char*)&t,sizeof(t));
}
}
cout<<"\n------------+----------+--------------+\n";
tFile.close();
}//Display_Location
Truck Truck_rec[20];
int i=0;
fstream tFile("c:\\truck.txt",ios::in|ios::out);
if (!tFile) {
printf("Error in Opening files... Program End\n");
getch();
exit(1);
}
tFile.seekg(0);
tFile.read((char*)&Truck_rec[i],sizeof(Truck));
while(!tFile.eof()){
tFile.read((char*)&Truck_rec[i],sizeof(Truck));
i++;
}
tFile.close();
int j, k;
int n=i;
cout<<"\n+----------------------------------------------------------+\n\n";
cout<<"\nPress any key to continue";
getch();
}// Display_by_Name
void Display_By_Plate(){
Truck Truck_rec[20];
int i=0;
fstream tFile("c:\\truck.txt",ios::in|ios::out);
if (!tFile) {
printf("Error in Opening files... Program End\n");
getch();
exit(1);
}
tFile.seekg(0);
tFile.read((char*)&Truck_rec[i],sizeof(Truck));
while(!tFile.eof()){
tFile.read((char*)&Truck_rec[i],sizeof(Truck));
i++;
}
tFile.close();
int j, k;
int n=i;
cout<<"\n+----------------------------------------------------------+\n\n";
cout<<"\nPress any key to continue";
getch();
}// Display_By_Plate
void print_Header(){
cout<<"\n\n------------+----------+--------------+\n";
cout<<"Plate Number C_Station T_Mileage";
cout<<"\n ------------+----------+--------------+\n";
} // print_Header
if(!file){
cout<<"Error openning a file program End";
getch();
exit(1);
}
file.seekg(0);
while(!file.eof())
{
if(strcmp(CTY,s.cStation)==0)
{
sResult=1;
strcpy(truckPlate[i],s.pNumber);
mileAge[i]=s.tMileAge;
i++;
}
file.read((char*)&s,sizeof(s));
}
if(sResult){
int n=i-1;
char* plate;
long int min;
min=mileAge[0];
for(i=1;i<=n;i++){
if(mileAge[i]<min){
min=mileAge[i];
loc=i;
}
}
cout<<"\n+-------------------------------------------------------------+\n";
cout<<"\nThe truck with lowest mile age at city: "<<CTY<<" is:
\n";
cout<<"\n\t plate Number "<<truckPlate[loc];;
cout<<"\t Mileage "<<min;
cout<<"\n+-------------------------------------------------------------+\n";
}
else
cout<<"\n No Truck at city "<<CTY<<" Found";
cout<<"\nDo you want to continue? (y / n) ";
cin>>ch;
file.close();
}//search_at_CTY
void ShortestPath::Create_Graph(){
fstream RdFile("c:\\RouteDis.txt",ios::in|ios::out);
RdFile.close();
for(i=1;i<=n;i++)
colour[i]=GREEN;
cin>>sCTY;
}//Create_Graph
void ShortestPath::Initialize(){
for(int i=1;i<=n;i++){
if(i==sCTY)
dist[i]=0;
else
dist[i]=Big;
}
int ShortestPath::Get_Min_Distance(){
int min=Big;
int p=0;
for(int i=1;i<=n;i++){
if(colour[i]==GREEN){
if(min>=dist[i]){
min=dist[i];
p=i;
}
}
}
return p;
}// Get_Min_Distance
void ShortestPath::DisplayPath(){
for(int i=1;i<=n;i++){
if(pred[i]==0 && i!=sCTY){
cout<<"path does not exists between"<<i<<" and the sCTY city
"<<sCTY<<endl;
exit(1);
}// DisplayPath
void ShortestPath::RunShortestPath(){
Create_Graph();
Initialize();
int flg=0;
int i;
for(i=1;i<=n;i++)
if(colour[i]!=RED)
flg=1;
while(flg){
int p=Get_Min_Distance();
colour[p]=YELLOW;
UPDATE(p);
colour[p]=RED;
flg=0;
for(i=1;i<=n;i++)
if(colour[i]!=RED)
flg=1;
}// RunShortestPath