|
1 |
| -// C program for Dijkstra's single source shortest path |
2 |
| -// algorithm. The program is for adjacency matrix |
3 |
| -// representation of the graph |
4 |
| - |
5 |
| -#include <limits.h> |
6 |
| -#include <stdbool.h> |
7 | 1 | #include <stdio.h>
|
8 |
| -#include <time.h> |
9 |
| -// Number of vertices in the graph |
10 |
| -#define V 9 |
11 |
| - |
12 |
| -// A utility function to find the vertex with minimum |
13 |
| -// distance value, from the set of vertices not yet included |
14 |
| -// in shortest path tree |
15 |
| -int minDistance(int dist[], bool sptSet[]) |
16 |
| -{ |
17 |
| - // Initialize min value |
18 |
| - int min = INT_MAX, min_index; |
19 |
| - |
20 |
| - for (int v = 0; v < V; v++) |
21 |
| - if (sptSet[v] == false && dist[v] <= min) |
22 |
| - min = dist[v], min_index = v; |
23 |
| - |
24 |
| - return min_index; |
25 |
| -} |
26 |
| - |
27 |
| -// A utility function to print the constructed distance |
28 |
| -// array |
29 |
| -void printSolution(int dist[]) |
| 2 | +#define INFINITY 9999 |
| 3 | +#define MAX 10 |
| 4 | +void dijkstra(int G[MAX][MAX], int n, int startnode); |
| 5 | +int main() |
30 | 6 | {
|
31 |
| - printf("Vertex \t\t Distance from Source\n"); |
32 |
| - for (int i = 0; i < V; i++) |
33 |
| - printf("%d \t\t\t\t %d\n", i, dist[i]); |
| 7 | + int G[MAX][MAX], i, j, n, u; |
| 8 | + printf("Enter no. of vertices: "); |
| 9 | + scanf("%d", &n); |
| 10 | + printf("\nEnter the adjacency matrix:\n"); |
| 11 | + for (i = 0; i < n; i++) |
| 12 | + for (j = 0; j < n; j++) |
| 13 | + scanf("%d", &G[i][j]); |
| 14 | + printf("\nEnter the starting node: "); |
| 15 | + scanf("%d", &u); |
| 16 | + dijkstra(G, n, u); |
| 17 | + return 0; |
34 | 18 | }
|
35 |
| - |
36 |
| -// Function that implements Dijkstra's single source |
37 |
| -// shortest path algorithm for a graph represented using |
38 |
| -// adjacency matrix representation |
39 |
| -void dijkstra(int graph[V][V], int src) |
| 19 | +void dijkstra(int G[MAX][MAX], int n, int startnode) |
40 | 20 | {
|
41 |
| - int dist[V]; // The output array. dist[i] will hold the |
42 |
| - // shortest |
43 |
| - // distance from src to i |
44 |
| - |
45 |
| - bool sptSet[V]; // sptSet[i] will be true if vertex i is |
46 |
| - // included in shortest |
47 |
| - // path tree or shortest distance from src to i is |
48 |
| - // finalized |
49 |
| - |
50 |
| - // Initialize all distances as INFINITE and stpSet[] as |
51 |
| - // false |
52 |
| - for (int i = 0; i < V; i++) |
53 |
| - dist[i] = INT_MAX, sptSet[i] = false; |
54 |
| - |
55 |
| - // Distance of source vertex from itself is always 0 |
56 |
| - dist[src] = 0; |
57 |
| - |
58 |
| - // Find shortest path for all vertices |
59 |
| - for (int count = 0; count < V - 1; count++) |
| 21 | + int cost[MAX][MAX], distance[MAX], pred[MAX]; |
| 22 | + int visited[MAX], count, mindistance, nextnode, i, j; |
| 23 | + // pred[] stores the predecessor of each node |
| 24 | + // count gives the number of nodes seen so far |
| 25 | + // create the cost matrix |
| 26 | + for (i = 0; i < n; i++) |
| 27 | + for (j = 0; j < n; j++) |
| 28 | + if (G[i][j] == 0) |
| 29 | + cost[i][j] = INFINITY; |
| 30 | + else |
| 31 | + cost[i][j] = G[i][j]; |
| 32 | + // initialize pred[],distance[] and visited[] |
| 33 | + for (i = 0; i < n; i++) |
60 | 34 | {
|
61 |
| - // Pick the minimum distance vertex from the set of |
62 |
| - // vertices not yet processed. u is always equal to |
63 |
| - // src in the first iteration. |
64 |
| - int u = minDistance(dist, sptSet); |
65 |
| - |
66 |
| - // Mark the picked vertex as processed |
67 |
| - sptSet[u] = true; |
68 |
| - |
69 |
| - // Update dist value of the adjacent vertices of the |
70 |
| - // picked vertex. |
71 |
| - for (int v = 0; v < V; v++) |
72 |
| - |
73 |
| - // Update dist[v] only if is not in sptSet, |
74 |
| - // there is an edge from u to v, and total |
75 |
| - // weight of path from src to v through u is |
76 |
| - // smaller than current value of dist[v] |
77 |
| - if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) |
78 |
| - dist[v] = dist[u] + graph[u][v]; |
| 35 | + distance[i] = cost[startnode][i]; |
| 36 | + pred[i] = startnode; |
| 37 | + visited[i] = 0; |
79 | 38 | }
|
80 |
| - |
81 |
| - // print the constructed distance array |
82 |
| - printSolution(dist); |
83 |
| -} |
84 |
| - |
85 |
| -// driver's code |
86 |
| -int main() |
87 |
| -{ |
88 |
| - /* Let us create the example graph discussed above */ |
89 |
| - int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, |
90 |
| - {4, 0, 8, 0, 0, 0, 0, 11, 0}, |
91 |
| - {0, 8, 0, 7, 0, 4, 0, 0, 2}, |
92 |
| - {0, 0, 7, 0, 9, 14, 0, 0, 0}, |
93 |
| - {0, 0, 0, 9, 0, 10, 0, 0, 0}, |
94 |
| - {0, 0, 4, 14, 10, 0, 2, 0, 0}, |
95 |
| - {0, 0, 0, 0, 0, 2, 0, 1, 6}, |
96 |
| - {8, 11, 0, 0, 0, 0, 1, 0, 7}, |
97 |
| - {0, 0, 2, 0, 0, 0, 6, 7, 0}}; |
98 |
| - |
99 |
| - // Function call |
100 |
| - clock_t start, end; |
101 |
| - float cpu_time_used; |
102 |
| - start = clock(); |
103 |
| - dijkstra(graph, 0); |
104 |
| - end = clock(); |
105 |
| - cpu_time_used = ((float)(end - start)) / CLOCKS_PER_SEC; |
106 |
| - printf("\nTime complexity is: %f", cpu_time_used); |
107 |
| - |
108 |
| - return 0; |
| 39 | + distance[startnode] = 0; |
| 40 | + visited[startnode] = 1; |
| 41 | + count = 1; |
| 42 | + while (count < n - 1) |
| 43 | + { |
| 44 | + mindistance = INFINITY; |
| 45 | + // nextnode gives the node at minimum distance |
| 46 | + for (i = 0; i < n; i++) |
| 47 | + if (distance[i] < mindistance && !visited[i]) |
| 48 | + { |
| 49 | + mindistance = distance[i]; |
| 50 | + nextnode = i; |
| 51 | + } |
| 52 | + // check if a better path exists through nextnode |
| 53 | + visited[nextnode] = 1; |
| 54 | + for (i = 0; i < n; i++) |
| 55 | + if (!visited[i]) |
| 56 | + if (mindistance + cost[nextnode][i] < distance[i]) |
| 57 | + { |
| 58 | + distance[i] = mindistance + cost[nextnode][i]; |
| 59 | + pred[i] = nextnode; |
| 60 | + } |
| 61 | + count++; |
| 62 | + } |
| 63 | + // print the path and distance of each node |
| 64 | + for (i = 0; i < n; i++) |
| 65 | + if (i != startnode) |
| 66 | + { |
| 67 | + printf("\nDistance of node%d= %d", i, distance[i]); |
| 68 | + printf("\nPath= %d", i); |
| 69 | + j = i; |
| 70 | + do |
| 71 | + { |
| 72 | + j = pred[j]; |
| 73 | + printf("<-%d", j); |
| 74 | + } while (j != startnode); |
| 75 | + } |
109 | 76 | }
|
0 commit comments