Code For Dijkstra’s Algorithm In C++

Note: Because of encoding problems the following code may not work. If it doesn’t work download the code from the following link. Right click on the link and click on ‘save target as ‘ and save the text file

Description:
adjMatric[][] : This array is used to store the adjacency matrix of the input graph
predecessor[] : This array is used to store the predecessor elements. predecessor[i] is the previous element in the path from source to current node i.
ex: If 1->2->4->3 is the shortest path from node1(source) to node 3. predecessor[3] is 4.
distance[] : This array will store the minimum distance from the source to corresponding node. It will be updated in each iteration in dijkstra’s algorithm.
mark[] — This array is used to mark the nodes as visited. mark[i] = true, This means node i is marked as visited.
source — source is the starting node from which we have to find out the shortest distance to all other nodes in the graph.
numOfVertices — The number of nodes in the graph.
Note: Node ranges will be 0 to numOfVertices-1

getClosestUnmarkedNode() : This method will return the closest unmarked node from the current marked node.
printPath(i) : This will print the path from source node to i in recursive manner
output() : This method will print paths to all nodes from source and their shortest distance
read() : This method will read the graph values from input
initialize() : This method will set the initial values of predecessor[] to -1 and
dijkstra() : This method will implement the dijkstra’s algorithm

void Graph::read()
{
cout<<“Enter the number of vertices of the graph(should be > 0)\n”;
cin>>numOfVertices;
//Number of vertices should always greater than zero
while(numOfVertices <= 0) {
cout<<“Enter the number of vertices of the graph(should be > 0)\n”;
cin>>numOfVertices;
}

//read the source node from which the shortest paths to other nodes has to be found
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;
}
}

Wow, for someone who has gone out of their way to post some nice code (thanks btw.) there sure are a lot of demanding people posting their “needs.” Aside from looking like kid trying to get someone else to do their work for them I humm… no sadly that’s what all these comments look like, aside from the odd thanks in there :).

Implement modified Dijkstra’s shortest paths algorithm that also gives the count of number of shortest paths to every node.

Input: The weighted (positive weights) directed graph
with node 1 considered as source is taken as input from a file the first line
having the size of the graph to be read (the size of the graph is dynamically set)

The file format:

n
adjacency matrix of size n

Output: The shortest paths from source to every node has to be printed along with the count of paths at that node.

Hi can you please explain what exactly is the source vertex?
ANd i would like to ask if you could help me to find a shortest path with specific nodes and vertex but with random weight for those that have weight !
Please email to me !

what to do for distances on line 69 where distance=0?
how can be a distance array intialized like distance=0????????????
it’s gives error for that reply soon………….
ha but very useful prg……
in lab sessions………

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..