Question: Use following code structure of (Dijkstra method) to answer following question (make necessary changes where it is necessary and comment out clearly)(if you found any
Use following code structure of (Dijkstra method) to answer following question (make necessary changes where it is necessary and comment out clearly)(if you found any error cleat it also comment that thing)
#include #include
using namespace std ; struct node { int nd, weight ; }temp;
struct myqueue { int name, key ; bool flag ; }Q[100];
vector g[100] ; int n ; int dis[100], par[100] ;
int pop() { int min = 1000000 ; int index ; for(int i = 0 ; i < n ; i++) { if(Q[i].key < min && Q[i].flag == true){ min = Q[i].key ; index = i ; } } Q[index].flag = false ; return Q[index].name ; } bool isEmpty(){ for(int i = 0 ; i < n ; i++){ if(Q[i].flag) return false ; } return true ; } void Dijkstra(){ while(!isEmpty()){ int u = pop() ; // printf("u : %d ", u) ; for(int i = 0 ; i < g[u].size() ; i++){ node t = g[u][i] ; int v = t.nd ; int w = t.weight ; // printf("v : %d w: %d ", v, w) ; if(dis[v] > dis[u] + w){ dis[v] = dis[u] + w ; par[v] = u ; Q[v].key = dis[v] ; } } }
} int main() {
int u, v, w, i ; int edge ;
scanf("%d %d", &n, &edge) ; for(i = 0 ; i < edge ; i++) { scanf("%d %d %d", &u, &v, &w) ; temp.nd = v ; temp.weight = w ; g[u].push_back(temp) ; temp.nd = u ; g[v].push_back(temp) ; } for(i = 0 ; i < n ; i++) { dis[i] = 99999 ; par[i] = -1 ; } int src = 0 ; dis[src] = 0 ; for(i = 0 ; i < n ; i++){ Q[i].name = i ; Q[i].key = dis[i] ; Q[i].flag = true ; } Dijkstra() ; for(i = 0 ; i < n ; i++){ printf("%d ", dis[i]) ;
} return 0 ; } 1. a. Take a weighted graph as input. Find shortest cost from source node to all other nodes using Bellman ford algorithm.
b. Find shortest path from source to all other node. for example shortest path from node 0 to node 1 be like 0->3->2->1(Bellman ford algorithm)
Use c++ language ( Please read the questions carefully then explain it properly )
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
