Loading…
DAA · Graphs · Program 23 · In the record
Write a program in c to find the shortest path from a source vertex to all other vertices using Dijkstra's algorithm and find its execution time using the time function.
Algorithm: DA(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G, s)
2. S <- Ø
3. Q <- V[G]
4. while Q != Ø
5. do u <- EXTRACT-MIN(Q)
6. S <- S U {u}
7. for each vertex v in Adj[u]
8. do RELAX(u, v, w)
INITIALIZE-SINGLE-SOURCE(G, s)
1. for each vertex v in V[G]
2. do d[v] <- infinity
3. p[v] <- NIL
4. d[s] <- 0
EXTRACT-MIN(Q)
1. u <- the vertex in Q with minimum d[u]
2. Q <- Q - {u}
3. return u
RELAX(u, v, w)
1. if d[v] > d[u] + w(u,v)
2. then d[v] <- d[u] + w(u,v)
3. p[v] <- u1#include<stdio.h>2#include<time.h>3#define INF 999994 5int n,count=0;6int w[50][50],d[50],p[50],S[50],Q[50];7 8void initialize_single_source(int s) {9 int v;10 for(v=1;v<=n;v++) {11 d[v]=INF;12 p[v]=0;13 }14 d[s]=0;15}16 17int extract_min() {18 int v,u=0;19 for(v=1;v<=n;v++) {20 count++;21 if(Q[v]==1 && (u==0 || d[v]<d[u]))22 u=v;23 }24 Q[u]=0;25 return u;26}27 28void relax(int u,int v) {29 count++;30 if(d[v]>d[u]+w[u][v]) {31 d[v]=d[u]+w[u][v];32 p[v]=u;33 }34}35 36void path(int v) {37 if(p[v]==0) {38 printf("%d",v);39 return;40 }41 path(p[v]);42 printf(" -> %d",v);43}44 45int main() {46 int i,j,u,v,s,q_size,step=0;47 printf("Enter the number of vertices:");48 scanf("%d",&n);49 printf("Enter the %dx%d weight matrix (0 for no edge):\n",n,n);50 for(u=1;u<=n;u++)51 for(v=1;v<=n;v++)52 scanf("%d",&w[u][v]);53 printf("Enter the source vertex:");54 scanf("%d",&s);55 56 int order[n+1],pos[n+1],trace[n+1][n+1];57 char cell[12];58 59 clock_t start=clock();60 initialize_single_source(s);61 for(v=1;v<=n;v++)62 S[v]=0;63 for(v=1;v<=n;v++)64 Q[v]=1;65 q_size=n;66 while(q_size!=0) {67 u=extract_min();68 q_size--;69 S[u]=1;70 step++;71 order[step]=u;72 pos[u]=step;73 for(v=1;v<=n;v++)74 trace[step][v]=d[v];75 for(v=1;v<=n;v++)76 if(w[u][v]!=0)77 relax(u,v);78 }79 clock_t end=clock();80 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;81 82 printf("\nS = { }");83 printf("\nS = {%d} (source, d[%d] = 0)\n",s,s);84 printf("\nu ");85 for(v=1;v<=n;v++)86 if(v!=s)87 printf("%-6d",v);88 printf("S");89 for(i=2;i<=step;i++) {90 if(trace[i][order[i]]==INF)91 break;92 printf("\n%-6d",order[i]);93 for(v=1;v<=n;v++) {94 if(v==s)95 continue;96 if(trace[i][v]==INF)97 printf("%-6s","INF");98 else if(pos[v]<=i) {99 sprintf(cell,"(%d)",trace[i][v]);100 printf("%-6s",cell);101 }102 else103 printf("%-6d",trace[i][v]);104 }105 printf("{");106 for(j=1;j<=i;j++)107 printf(j==1?"%d":", %d",order[j]);108 printf("}");109 }110 printf("\n( ) = vertex is in S, its shortest distance is final");111 112 printf("\n\nVertex\tDistance\tPath");113 for(i=1;i<=n;i++) {114 if(d[i]==INF)115 printf("\n%d\tunreachable",i);116 else {117 printf("\n%d\t%d\t\t",i,d[i]);118 path(i);119 }120 }121 printf("\nNumber of comparisons:%d",count);122 printf("\nTime taken:%f seconds",time_taken);123 printf("\nTime complexity: O(V^2) with adjacency matrix, O(E log V) with adjacency list");124 printf("\nSpace complexity: O(V^2) with adjacency matrix, O(V+E) with adjacency list");125 printf("\n");126 return 0;127}Sample 1
6 ⏎ 0 2 4 0 0 0 ⏎ 0 0 1 7 0 0 ⏎ 0 0 0 0 3 0 ⏎ 0 0 0 0 0 1 ⏎ 0 0 0 2 0 5 ⏎ 0 0 0 0 0 0 ⏎ 1Enter the number of vertices:6
Enter the 6x6 weight matrix (0 for no edge):
0 2 4 0 0 0
0 0 1 7 0 0
0 0 0 0 3 0
0 0 0 0 0 1
0 0 0 2 0 5
0 0 0 0 0 0
Enter the source vertex:1
S = { }
S = {1} (source, d[1] = 0)
u 2 3 4 5 6 S
2 (2) 4 INF INF INF {1, 2}
3 (2) (3) 9 INF INF {1, 2, 3}
5 (2) (3) 9 (6) INF {1, 2, 3, 5}
4 (2) (3) (8) (6) 11 {1, 2, 3, 5, 4}
6 (2) (3) (8) (6) (9) {1, 2, 3, 5, 4, 6}
( ) = vertex is in S, its shortest distance is final
Vertex Distance Path
1 0 1
2 2 1 -> 2
3 3 1 -> 2 -> 3
4 8 1 -> 2 -> 3 -> 5 -> 4
5 6 1 -> 2 -> 3 -> 5
6 9 1 -> 2 -> 3 -> 5 -> 4 -> 6
Number of comparisons:44
Time taken:0.000002 seconds
Time complexity: O(V^2) with adjacency matrix, O(E log V) with adjacency list
Space complexity: O(V^2) with adjacency matrix, O(V+E) with adjacency listSample 2
6 ⏎ 0 50 45 10 0 0 ⏎ 0 0 10 15 0 0 ⏎ 0 0 0 0 30 0 ⏎ 20 0 0 0 15 0 ⏎ 0 20 35 0 0 0 ⏎ 0 0 0 0 3 0 ⏎ 1Enter the number of vertices:6
Enter the 6x6 weight matrix (0 for no edge):
0 50 45 10 0 0
0 0 10 15 0 0
0 0 0 0 30 0
20 0 0 0 15 0
0 20 35 0 0 0
0 0 0 0 3 0
Enter the source vertex:1
S = { }
S = {1} (source, d[1] = 0)
u 2 3 4 5 6 S
4 50 45 (10) INF INF {1, 4}
5 50 45 (10) (25) INF {1, 4, 5}
2 (45) 45 (10) (25) INF {1, 4, 5, 2}
3 (45) (45) (10) (25) INF {1, 4, 5, 2, 3}
( ) = vertex is in S, its shortest distance is final
Vertex Distance Path
1 0 1
2 45 1 -> 4 -> 5 -> 2
3 45 1 -> 3
4 10 1 -> 4
5 25 1 -> 4 -> 5
6 unreachable
Number of comparisons:47
Time taken:0.000002 seconds
Time complexity: O(V^2) with adjacency matrix, O(E log V) with adjacency list
Space complexity: O(V^2) with adjacency matrix, O(V+E) with adjacency listRun program 23 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.