5
5
#include <limits.h>
6
6
#include <stdbool.h>
7
7
#include <stdio.h>
8
- #include <time.h>
8
+ #include <time.h>
9
9
// Number of vertices in the graph
10
10
#define V 9
11
11
@@ -39,7 +39,7 @@ void printSolution(int dist[])
39
39
void dijkstra (int graph [V ][V ], int src )
40
40
{
41
41
int dist [V ]; // The output array. dist[i] will hold the
42
- // shortest
42
+ // shortest
43
43
// distance from src to i
44
44
45
45
bool sptSet [V ]; // sptSet[i] will be true if vertex i is
@@ -56,7 +56,8 @@ void dijkstra(int graph[V][V], int src)
56
56
dist [src ] = 0 ;
57
57
58
58
// Find shortest path for all vertices
59
- for (int count = 0 ; count < V - 1 ; count ++ ) {
59
+ for (int count = 0 ; count < V - 1 ; count ++ )
60
+ {
60
61
// Pick the minimum distance vertex from the set of
61
62
// vertices not yet processed. u is always equal to
62
63
// src in the first iteration.
@@ -73,9 +74,7 @@ void dijkstra(int graph[V][V], int src)
73
74
// there is an edge from u to v, and total
74
75
// weight of path from src to v through u is
75
76
// smaller than current value of dist[v]
76
- if (!sptSet [v ] && graph [u ][v ]
77
- && dist [u ] != INT_MAX
78
- && dist [u ] + graph [u ][v ] < dist [v ])
77
+ if (!sptSet [v ] && graph [u ][v ] && dist [u ] != INT_MAX && dist [u ] + graph [u ][v ] < dist [v ])
79
78
dist [v ] = dist [u ] + graph [u ][v ];
80
79
}
81
80
@@ -87,25 +86,24 @@ void dijkstra(int graph[V][V], int src)
87
86
int main ()
88
87
{
89
88
/* Let us create the example graph discussed above */
90
- int graph [V ][V ] = { { 0 , 4 , 0 , 0 , 0 , 0 , 0 , 8 , 0 },
91
- { 4 , 0 , 8 , 0 , 0 , 0 , 0 , 11 , 0 },
92
- { 0 , 8 , 0 , 7 , 0 , 4 , 0 , 0 , 2 },
93
- { 0 , 0 , 7 , 0 , 9 , 14 , 0 , 0 , 0 },
94
- { 0 , 0 , 0 , 9 , 0 , 10 , 0 , 0 , 0 },
95
- { 0 , 0 , 4 , 14 , 10 , 0 , 2 , 0 , 0 },
96
- { 0 , 0 , 0 , 0 , 0 , 2 , 0 , 1 , 6 },
97
- { 8 , 11 , 0 , 0 , 0 , 0 , 1 , 0 , 7 },
98
- { 0 , 0 , 2 , 0 , 0 , 0 , 6 , 7 , 0 } };
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 } };
99
98
100
99
// Function call
101
- clock_t start , end ;
102
- float cpu_time_used ;
103
- start = clock ();
104
- dijkstra (graph , 0 );
105
- end = clock ();
106
- cpu_time_used = ((float )(end - start )) / CLOCKS_PER_SEC ;
107
- printf ("\nTime complexity is: %f" , cpu_time_used );
108
-
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 );
109
107
110
108
return 0 ;
111
109
}
0 commit comments