Please submit the source code files (.c or .cpp).
Dijkstra's shortest path algorithm
Description: Dijkstra's Shortest Path Algorithm is a popular algorithm for finding the shortest path. It was developed by Wybe Dijkstra. Dijkstra's algorithm can work on both directed graphs and undirected graphs, as long as they have non-negative edge weights and are connected.
You are supposed to implement Dijkstra's algorithm in C and output the shortest path from the source vertex to the destination vertex. Here's a look at the algorithm itself:
1. Fix a node as the initial node; this will be the node at which we are starting. Let the distance of node Y be the distance from the initial node to Y.
2. In Dijkstra's algorithm, some initial distance values are assigned, and these values are improved step by step. The algorithm procedure is given below:
a. A tentative distance value is assigned to every node; this value is set to zero for the initial node and to infinity for all other nodes.
b. All unvisited nodes are marked, and the initial node is set as the current node. An unvisited set (a set of all the unvisited nodes) consisting of all the nodes is created.
c. For the current/initial node, take into account all the unvisited nearby nodes and calculate their tentative distances. Make a comparison of the current assigned value and the newly calculated tentative distance; assign the smaller value. For example, if the current/initial node X was marked with a distance of 4, and the edge connecting it with a nearby neighbor node Y has length 1, then the distance through X to Y will be 4 + 1 = 5. If Y was previously assigned a value greater than 5, then change it to 5. Otherwise, keep the value as it is.
d. A visited node is never to be checked again. So, after finishing the above steps with all the neighbors of the current node, mark that node as visited and remove it from the unvisited set.
e. Stop the algorithm if, when planning a route between two specific nodes, the destination node has been marked visited.
f. Also, stop the algorithm if, when planning a complete traversal, the smallest tentative distance among the nodes in the unvisited set is infinity. This case is a result of no connection between the initial node and the remaining unvisited nodes.
g. Find the unvisited node assigned with the smallest tentative distance value, and this will be the new current node. Go back to step c and continue.
Input:
For the first line, enter two positive integers n (n<=100) and e, which represent the number of vertices and edges in a directed network, respectively.
For each line in the following e lines, enter a pair of vertices having a weighted edge between them, followed by the weight.
For the last line, enter the source vertex and the destination vertex.
Output:
On the first line, output the shortest path from the source vertex to the destination vertex. Connect vertices with arrows.
On the second line, output the length of the above shortest path.
Input sample:
7 8
a b 2
a c 6
b d 5
c d 8
d e 10
d f 15
e g 2
f g 6
a g
Output sample:
a->b->d->e->g
19