-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijikstra.c
99 lines (78 loc) · 2.19 KB
/
dijikstra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ROWS 9
#define COLS 9
int minDistance(int *distance, int *spt) {
int i = 0;
int min = INT_MAX, min_index = 0;
for(i = 0; i < ROWS; i++) {
if(distance[i] <= min && spt[i] == 0) {
min_index = i;
min = distance[i];
}
}
return min_index;
}
int *spt(int **grid, int rows, int cols, int start) {
int i = 0, j = 0, min = INT_MAX, *distance;
int *spt, vertex = 0;
#if 0
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
printf("%d ", grid[i][j]);
}
printf("\n");
}
#endif
// to start with, define an array of length ROWS, with all the values set to some very large number.
// this array will be updated every time we add a node to the vertex set.
distance = malloc(ROWS * sizeof(int));
distance[start] = 0; // distance from the source vertex is always 0
for(i = 1; i < rows; i++) distance[i] = INT_MAX;
i = 0;
#if 0
for(i = 0; i < rows; i++) {
printf("%d ", distance[i]);
}
printf("\n");
#endif
// this is the shortest path tree
spt = malloc(ROWS * sizeof(int));
for(i = 0; i < rows; i++)
spt[i] = 0;
i = 0;
while(i < ROWS - 1) { // calculate the distances of
vertex = minDistance(distance, spt); // pick the shortest path to the vertex not yet included in the shortest path tree
spt[vertex] = 1; // mark this vertex as visited
// update the values of the adjacent vertices
for(j = 0; j < ROWS; j++) {
if(!spt[j] && grid[vertex][j] && distance[vertex] != INT_MAX
&& (distance[vertex] + grid[vertex][j]) < distance[j])
distance[j] = distance[vertex] + grid[vertex][j];
}
i++;
}
return distance;
}
int main() {
int row, cols, start = 0;
int **grid, *shortestPathTree;
printf("Enter adjacency matrix\n");
grid = (int **)malloc(ROWS * sizeof(int *));
for(row = 0 ; row < ROWS; row++) {
grid[row] = (int *)malloc(COLS * sizeof(int)) ;
}
for(row = 0; row < COLS; row++) {
for(cols = 0; cols < COLS; cols++) {
scanf("%d", &grid[row][cols]);
}
}
printf("starting node: ");
scanf("%d", &start);
shortestPathTree = spt(grid, ROWS, COLS, start);
for(row = 0; row < ROWS; row++) {
printf("distance from source to %d : %d\n", row, shortestPathTree[row]);
}
return 0;
}