Question

Dijstra Shortest path3. Implement Dijkstra Shortest Path algorithm for any input graph. Implementation will be checked with a number of test cases. Sample test case, Number of nodes 5 Edge table: 0, 1,1 0, 2,4 1, 2, 5 1, 3, 3 1, 4,2 3, 2, 5 3, 1,1 4, 3, 3 Source node 0 Vertex Distance from Source 2 4 4 3 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

comments have been used to explain working of the code.

#include <bits/stdc++.h>
#define V 5 // update NO of vertices according to question.
using namespace std;  


// 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;
}

// A utility function to print the constructed distance array
int printSolution(int dist[] , int n)
{
cout<<"Vertex Distance from Source"<<endl;
for (int i = 0; i < V; i++)
cout<<i<<" "<<dist[i]<<endl;
}

// Function 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)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i

bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized

// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
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 the 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] != INT_MAX  
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

// print the constructed distance array
printSolution(dist, V);
}

// driver program to test above function
int main()
{
int src, des,weight;
int graph[V][V];
memset(graph,0,sizeof(graph));
char ch;
cout<<"Enter the edges and weigths"<<endl;
while(1){
cin>>src>>des>>weight; // adding edges in a egde table
graph[src][des]=weight;
graph[des][src]=weight;
cout<<"More edges(Y for yes and N for no) :"<<endl;
cin>>ch;
if(ch!='Y'){
break;
}
  
}
dijkstra(graph, 0);

return 0;
}

output

OnlineGDB beta printsolution(dist, v): code comple nun dsbug share IDE e 17 driver program to test obove function 71 Int nain() int sre, des,weight; Progranning Questices 4 Int graph[V][v] Ve rtex Distance from Source Sign Up char ch; 7 Cout<cEnter the edges and weigthsccendl; 78-while() cin sre des>weight;/ adding edpes in a egde toble graph[src][des) weight; St graph[des][src] weight cout<c More edges(Y for yes and N for no) enl: cin ch; 3 2 4 3 Studorts and Teachers im up to 60% on Adobe 89 dijkstra(eh, : raph.。); return 0 20180 (OB Online

Add a comment
Know the answer?
Add Answer to:
Dijstra Shortest path 3. Implement Dijkstra Shortest Path algorithm for any input graph. Implementation will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT