Showing posts with label DIJKSTRA’S ALGORITHM. Show all posts
Showing posts with label DIJKSTRA’S ALGORITHM. Show all posts

Wednesday, February 24, 2016

DIJKSTRA’S ALGORITHM

AIM:
         To implement a C++ program for Dijkstra’s Algorithm to find the shortest path algorithm.
ALGORITHM:

Step 1: Include the header files
Step 2: Save the adjacency matrix in a file.
Step 3: Get as input the source node and destination node.
Step 4: Search the shortest path from source to destination.
Step 5: Initialize all the distances in the shortest path graph to infinity and all the  nodes status to infinity.
Step 6: Start with the source node.
Step 7: Find the adjacent node and its shortest distance from  the source node.
Step 8: Take the minimum distance value node and repeat step(7) till all the nodes become visited.

PROGRAM:

#include<iostream>
#define INFINITY 999
using namespace std;
class Dijkstra
{
   private:
       int adjMatrix[15][15];
       int predecessor[15],distance[15];
       bool mark[15];    //keep track of visited node
       int source;
       int numOfVertices;
   public:
       void read();
       void initialize();
        int getClosestUnmarkedNode();
       void calculateDistance();
       void output();
       void printPath(int);
};
void Dijkstra::read()
{
   cout<<"Enter the number of vertices of the graph(should be > 0)\n";
   cin>>numOfVertices;

   while(numOfVertices <= 0)
{
       cout<<"Enter the number of vertices of the graph(should be > 0)\n";
       cin>>numOfVertices;
   }
   cout<<"Enter the adjacency matrix for the graph\n";
   cout<<"To enter infinity enter "<<INFINITY<<endl;
   for(int i=0;i<numOfVertices;i++)
{
       cout<<"Enter the (+ve)weights for the row "<<i<<endl;
       for(int j=0;j<numOfVertices;j++)
{
           cin>>adjMatrix[i][j];
           while(adjMatrix[i][j]<0)
{
               cout<<"Weights should be +ve. Enter the weight again\n";
               cin>>adjMatrix[i][j];
           }
       }
   }
   cout<<"Enter the source vertex\n";
   cin>>source;
   while((source<0) && (source>numOfVertices-1))
{
       cout<<"Source vertex should be between 0 and"<<numOfVertices-1<<endl;
       cout<<"Enter the source vertex again\n";
       cin>>source;
   }
}
void Dijkstra::initialize()
{
   for(int i=0;i<numOfVertices;i++)
{
       mark[i] = false;
       predecessor[i] = -1;
       distance[i] = INFINITY;
   }
   distance[source]= 0;
}
int Dijkstra::getClosestUnmarkedNode()
{
   int minDistance = INFINITY;
   int closestUnmarkedNode;
   for(int i=0;i<numOfVertices;i++)
{
       if((!mark[i]) && ( minDistance >= distance[i]))
{
           minDistance = distance[i];
           closestUnmarkedNode = i
}
   }
   return closestUnmarkedNode;
}
void Dijkstra::calculateDistance()
{
   initialize();
   int minDistance = INFINITY;
   int closestUnmarkedNode;
   int count = 0;
   while(count < numOfVertices)
{
       closestUnmarkedNode = getClosestUnmarkedNode();
       mark[closestUnmarkedNode] = true;
       for(int i=0;i<numOfVertices;i++)
{
           if((!mark[i]) && (adjMatrix[closestUnmarkedNode][i]>0) )
{
               if(distance[i] > distance[closestUnmarkedNode]+adjMatrix[closestUnmarkedNode][i])
{
                   distance[i] = distance[closestUnmarkedNode]+adjMatrix[closestUnmarkedNode][i];
                   predecessor[i] = closestUnmarkedNode;
               }
           }
       }
       count++;
   }
}
void Dijkstra::printPath(int node)
{
   if(node == source)
       cout<<(char)(node + 97)<<"..";
   else if(predecessor[node] == -1)
       cout<<"No path from “<<source<<”to "<<(char)(node + 97)<<endl;
   else
{
       printPath(predecessor[node]);
       cout<<(char) (node + 97)<<"..";
   }
}