Write a program to perform the following in C++: 1)Implement Dijkstra’s Algorith
ID: 3917806 • Letter: W
Question
Write a program to perform the following in C++:
1)Implement Dijkstra’s Algorithm .
2)Read a graph data file. Each line contains weighted edge: “vertex, vertex, weight”
3)Using the implemented algorithm, find the shortest-paths tree from vertex M.
4)Report the shortest-paths tree from vertex M and distance to each other vertex.
graph.txt:
A,J,91
A,H,86
A,B,38
A,N,37
A,L,100
A,V,64
A,S,84
A,M,9
A,W,54
A,K,46
B,J,27
B,V,93
B,H,40
B,G,49
B,F,94
B,N,95
B,C,45
B,Z,42
B,L,15
C,Q,14
C,W,10
C,L,87
C,R,77
C,N,92
C,S,79
C,T,3
C,D,60
C,J,43
D,P,26
D,W,12
D,K,21
D,Q,5
D,V,75
D,T,59
D,J,2
D,R,11
F,J,63
F,X,72
F,L,88
F,G,51
F,V,61
F,P,39
F,M,69
F,W,65
G,N,76
G,X,13
G,Z,56
G,H,82
G,Q,32
G,W,20
G,V,18
H,Z,74
H,P,85
H,L,62
H,M,44
H,K,25
H,J,98
H,R,19
J,V,31
J,M,50
J,S,57
J,Q,17
J,T,22
J,P,68
K,T,67
K,L,81
K,M,55
K,P,52
K,S,83
K,X,41
L,R,36
L,N,30
L,T,53
L,S,23
L,Q,66
M,S,6
M,Q,29
M,N,71
M,R,8
M,T,96
N,T,16
N,R,78
N,Q,73
N,V,24
P,Z,28
P,W,48
P,Q,7
P,T,47
Q,W,99
Q,S,1
Q,Z,89
R,V,70
R,X,35
R,S,33
S,T,34
S,X,90
T,X,4
T,W,58
V,X,80
W,Z,97
Explanation / Answer
// C++ program for Dijkstra's single
// source shortest path algorithm.
// The program is for adjacency matrix
// representation of the graph.
#include <iostream>
#include <climits>
#include <fstream>
using namespace std;
// Number of vertices
// in the graph
#define V 26
// A utility function to find the
// vertex with minimum distance
// value, from the set of vertices
// not yet included in shortest
// path tree
int minDistance(int dist[],
bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false &&
dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
// Function to print shortest
// path from source to j
// using parent array
void printPath(int parent[], int j)
{
// Base Case : If j is source
if (parent[j] == - 1)
return;
printPath(parent, parent[j]);
printf("%d ", j);
}
// A utility function to print
// the constructed distance
// array
void printSolution(int dist[], int n,
int parent[])
{
int src = 0;
printf("Vertex Distance Path");
for (int i = 1; i < V; i++)
{
printf(" %d -> %d %d %d ",
src, i, dist[i], src);
printPath(parent, i);
}
}
// Funtion that implements Dijkstra's
// single source shortest path
// algorithm for a graph represented
// using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
// The output array. dist[i]
// will hold the shortest
// distance from src to i
int dist[V];
// sptSet[i] will true if vertex
// i is included / in shortest
// path tree or shortest distance
// from src to i is finalized
bool sptSet[V];
// Parent array to store
// shortest path tree
int parent[V];
// Initialize all distances as
// INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
{
parent[0] = -1;
dist[i] = INT_MAX;
sptSet[i] = false;
}
// Distance of source vertex
// from itself is always 0
dist[src] = 0;
// Find shortest path
// for all vertices
for (int count = 0; count < V - 1; count++)
{
// Pick the minimum distance
// vertex from the set of
// vertices not yet processed.
// u is always equal to src
// in first iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex
// as processed
sptSet[u] = true;
// Update dist value of the
// adjacent vertices of the
// picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is
// not in sptSet, there is
// an edge from u to v, and
// total weight of path from
// src to v through u is smaller
// than current value of
// dist[v]
if (!sptSet[v] && graph[u][v] &&
dist[u] + graph[u][v] < dist[v])
{
parent[v] = u;
dist[v] = dist[u] + graph[u][v];
}
}
// print the constructed
// distance array
printSolution(dist, V, parent);
}
void readFromFile(int graph[26][26]) {
char start, end;
int weight;
ifstream inFile;
inFile.open("graph.txt");
if (!inFile) {
printf("Unable to open file");
return;
}
while (inFile >> start >> end >> weight) {
graph[start-65][end-65] = weight;
}
inFile.close();
}
// Driver Code
int main()
{
// Let us create the example
// graph discussed above
int graph[V][V];
for(int i=0; i<V; i++) {
for(int j=0; j<V; j++) {
graph[i][j] = 0;
}
}
readFromFile(graph);
dijkstra(graph, 12); // 12 represents 'M'
return 0;
}
NOTE: Please note that due to lack of time, my reading from file logic reads only space separated lines (not comma separated). Hence, you might need to change the graph.txt to something like this:
graph.txt:
A J 91
A H 86
A B 38
............
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.