forked from ongjinzen/social-network-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.c
189 lines (153 loc) · 4.33 KB
/
Dijkstra.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Dijkstra ADT interface for Ass2 (COMP2521)
#include "Dijkstra.h"
#include "PQ.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
// Helper function to add a node to an adjacency list.
void add_to_list(PredNode **list, Vertex vertex, Vertex pred) {
// Allocate memory for new list.
PredNode *new = malloc(sizeof(PredNode));
new->v = pred;
new->next = NULL;
// If the list is empty, the new node is the start of the list.
// Else, place the new node at the end of the list.
if (list[vertex] == NULL) {
list[vertex] = new;
} else {
PredNode *curr = list[vertex];
// If node is already in the list,
// do not add it again and return.
if (curr->v == pred) {
return;
}
while (curr->next != NULL) {
curr = curr->next;
if (curr->v == pred) {
return;
}
}
curr->next = new;
}
}
// Frees all nodes on an index of an adjacency list.
void clear_list(PredNode **list, Vertex vertex) {
if (list[vertex] == NULL) {
return;
} else if (list[vertex]->next == NULL) {
PredNode *curr = list[vertex];
free(curr);
} else {
PredNode *prev = list[vertex];
PredNode *curr = prev->next;
while (curr != NULL) {
free (prev);
prev = curr;
curr = curr->next;
}
}
list[vertex] = NULL;
}
ShortestPaths dijkstra(Graph g, Vertex v) {
// Initialist the ShortestPaths struct.
ShortestPaths answer;
answer.noNodes = numVerticies(g);
answer.src = v;
answer.dist = malloc(answer.noNodes * sizeof(answer.dist));
answer.pred = malloc(answer.noNodes * sizeof(PredNode *));
int visited[answer.noNodes];
// Set all distances in the struct to infinity
// Set all predecessors in the struct to NULL
for(int i = 0; i < answer.noNodes; i++) {
answer.dist[i] = __INT_MAX__;
answer.pred[i] = NULL;
visited[i] = 0;
}
// Set the distance for the source node to 0.
answer.dist[answer.src] = 0;
ItemPQ edge;
PQ set = newPQ();
int length = 0;
// Add all vertices to queue.
for (int i = 0; i < answer.noNodes; i++) {
edge.key = i;
edge.value = answer.dist[i];
addPQ(set, edge);
}
// While priority queue is not empty
while (!PQEmpty(set)) {
// Dequeue node with shortest distance
edge = dequeuePQ(set);
visited[edge.key] = 1;
// Find all adjacent nodes to.
AdjList head = outIncident(g, edge.key);
while (head != NULL) {
// If node has never been visited,
// check the length of the path taken.
if (visited[head->w] == 0) {
// If the vertice with a distance of infinity
// is dequeued, all other vertices still in the PQ
// also have a distance of infinity and cannot be
// reached from the source.
if (answer.dist[edge.key] == __INT_MAX__) {
break;
} else {
length = answer.dist[edge.key] + head->weight;
}
// If the length is same as another path taken,
// Add it to the predecessor list.
if (length == answer.dist[head->w]) {
add_to_list(answer.pred, head->w, edge.key);
// If the length is shorter than a previous path
// taken, clear the list, add the new predecessor
// to the adjacency list, update the shortest
// distance to the node and update the distances
// in the priority queue.
} else if (length < answer.dist[head->w]) {
answer.dist[head->w] = length;
clear_list(answer.pred, head->w);
add_to_list(answer.pred, head->w, edge.key);
ItemPQ update;
update.key = head->w;
update.value = length;
updatePQ(set, update);
}
}
head = head->next;
}
}
// If there are nodes with distances
// still at infinity then it is unreachable,
// so we set the distances back to 0.
for (int i = 0; i < answer.noNodes; i++) {
if (answer.dist[i] == __INT_MAX__) {
answer.dist[i] = 0;
}
}
freePQ(set);
return answer;
}
void showShortestPaths(ShortestPaths paths) {
for (int i = 0; i < paths.noNodes; i++) {
printf("The shortest path from vertex %d to vertex %d is length %d.\n", paths.src, i, paths.dist[i]);
if (paths.pred[i] != NULL) {
printf("The predecessor nodes are: ");
for(PredNode *curr = paths.pred[i]; curr != NULL; curr = curr->next) {
if (curr->next == NULL) {
printf("%d\n\n", curr->v);
} else {
printf("%d, ", curr->v);
}
}
} else {
printf("\n");
}
}
}
void freeShortestPaths(ShortestPaths paths) {
free(paths.dist);
for (int i = 0; i < paths.noNodes; i++) {
clear_list(paths.pred, i);
}
free(paths.pred);
}