Question
Translate the Pseudocode to C Some Pseudocode function Dijkstra( Graph , source ): 2 3 create vertex set Q 4 5 for each vertex v
Translate the Pseudocode to C
Some Pseudocode
function Dijkstra(Graph, source): 2 3 create vertex set Q 4 5 for each vertex v in Graph: // Initialization 6 dist[v] INFINITY // Unknown distance from source to v 7 prev[v] UNDEFINED // Previous node in optimal path from source 8 add v to Q // All nodes initially in Q (unvisited nodes) 9 10 dist[source] 0 // Distance from source to source 11 12 while Q is not empty: 13 u vertex in Q with min dist[u] // Node with the least distance will be selected first 14 remove u from Q 15 16 for each neighbor v of u: // where v is still in Q. 17 alt dist[u] + length(u, v) 18 if alt < dist[v]: // A shorter path to v has been found 19 dist[v] alt 20 prev[v] u 21 22 return dist[], prev[]
//returns the shortest path as an array of VertexId, ordered from source to target int * function path() : more Pseudocode 1 S empty sequence 2 u target int size = 0; 3 while prev[u] is defined: // Construct the shortest path with a stack S 4 insert u at the beginning of S // Push the vertex onto the stack 5 u prev[u] // Traverse from target to source size++; 6 insert u at the beginning of S // Push the source onto the stack 7 int path[size] 8 pop contents of stack S into array path 9 return path
Down below is the Dijkstra's Algorithm for reference
/* Dijkstra's Algorithm in C */ #include #include #include #include #include #define IN 99 #define N 6 int dijkstra(int cost[][N], int source, int target); int main() { int cost[N][N],i,j,w,ch,co; int source, target,x,y; printf("\t The Shortest Path Algorithm ( DIJKSTRA'S ALGORITHM in C "); for(i=1;i< N;i++) for(j=1;j< N;j++) cost[i][j] = IN; for(x=1;x< N;x++) { for(y=x+1;y< N;y++) { printf("Enter the weight of the path between nodes %d and %d: ",x,y); scanf("%d",&w); cost [x][y] = cost[y][x] = w; } printf(" "); } printf(" Enter the source:"); scanf("%d", &source); printf(" Enter the target"); scanf("%d", &target); co = dijsktra(cost,source,target); printf(" The Shortest Path: %d",co); } int dijsktra(int cost[][N],int source,int target) { int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j; char path[N]; for(i=1;i< N;i++) { dist[i] = IN; prev[i] = -1; } start = source; selected[start]=1; dist[start] = 0; while(selected[target] ==0) { min = IN; m = 0; for(i=1;i< N;i++) { d = dist[start] +cost[start][i]; if(d< dist[i]&&selected[i]==0) { dist[i] = d; prev[i] = start; } if(min>dist[i] && selected[i]==0) { min = dist[i]; m = i; } } start = m; selected[start] = 1; } start = target; j = 0; while(start != -1) { path[j++] = start+65; start = prev[start]; } path[j]='\0'; strrev(path); printf("%s", path); return dist[target]; }
This is the url from the Dijkstra's code if it helps http://www.codewithc.com/dijkstras-algorithm-in-c/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started