-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortestpath.c
277 lines (214 loc) · 6.9 KB
/
shortestpath.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* shortestpath.c, by Marius Posta, 2009
* Check LICENSE.txt for the legal blah-blah.
*/
#include "shortestpath.h"
void simple_graph_destroy (simple_graph_t* graphp)
{
free(graphp->arc_cost);
free(graphp->dist);
free(graphp->node_flag);
free(graphp->pred_node);
free(graphp->pred_arc);
}
simple_graph_t simple_graph_create (cmnd_t* instp)
{
simple_graph_t graph;
int n_nodes = instp->n_nodes;
int n_arcs = instp->n_arcs;
graph.commodity = -1;
graph.instp = instp;
graph.arc_cost = malloc(n_arcs * sizeof(double));
graph.dist = malloc(n_nodes * sizeof(double));
graph.node_flag = malloc(n_nodes * sizeof(int));
graph.pred_node = (malloc(n_nodes * sizeof(double)));
graph.pred_arc = (malloc(n_nodes * sizeof(double)));
return graph;
}
void simple_graph_reset (simple_graph_t* graphp, int commodity)
// resets all arc costs according to commodity
{
int i, j, src, snk;
graphp->commodity = commodity;
for (i = 0; i < graphp->instp->n_arcs; i++)
graphp->arc_cost[i] = graphp->instp->arc_commod_ucost[i][commodity];
for (j = 0; j < graphp->instp->n_nodes; j++) {
graphp->node_flag[j] = 1;
graphp->dist[j] = inf;
graphp->pred_node[j] = -1;
graphp->pred_arc[j] = -1;
}
// sets the artificial arc as linking the source and the sink
// indentified by the value graphp->instp->n_arcs, which is not a valid arc index
// this remains so until a better path is found
src = graphp->instp->commod_orig_node[commodity];
snk = graphp->instp->commod_dest_node[commodity];
graphp->dist[src] = 0.0;
graphp->dist[snk] = graphp->instp->commod_overflow_ucost[commodity];
graphp->pred_node[snk] = src;
graphp->pred_arc[snk] = graphp->instp->n_arcs;
}
double simple_graph_solve_shortest_path (simple_graph_t* graphp)
// applies Dijkstra's algorithm
{
int i, j, k, l, c = -1;
double cdist, alt;
for (l = 0; l < graphp->instp->n_nodes; l++) {
cdist = inf;
c = -1;
for (j = 0; j < graphp->instp->n_nodes; j++)
if (graphp->node_flag[j] && graphp->dist[j] < cdist) {
c = j;
cdist = graphp->dist[j];
}
if (c == -1)
break;
graphp->node_flag[c] = 0;
for (k = 0; k < graphp->instp->node_n_outgoing_arcs[c]; k++) {
i = graphp->instp->node_outgoing_arc[c][k];
j = graphp->instp->arc_dest_node[i];
alt = cdist + graphp->arc_cost[i];
if (alt < graphp->dist[j]) {
graphp->dist[j] = alt;
graphp->pred_node[j] = c;
graphp->pred_arc[j] = i;
}
}
}
return graphp->dist[graphp->instp->commod_dest_node[graphp->commodity]];
}
void residual_graph_destroy (residual_graph_t* graphp)
{
int j;
for (j = 0; j < graphp->instp->n_nodes; j++)
free(graphp->node_outward[j]);
free(graphp->list);
free(graphp->arc_cost);
free(graphp->arc_cost_inv);
free(graphp->dist);
free(graphp->pred_node);
free(graphp->pred_arc);
free(graphp->node_n_outward);
free(graphp->node_outward);
}
residual_graph_t residual_graph_create (cmnd_t* instp)
{
residual_graph_t graph;
int j;
int n_nodes = instp->n_nodes;
int n_arcs = instp->n_arcs;
graph.list_size = 0;
graph.instp = instp;
graph.list = (malloc(n_nodes * sizeof(double)));
graph.arc_cost = (malloc(n_arcs * sizeof(double)));
graph.arc_cost_inv = (malloc(n_arcs * sizeof(double)));
graph.dist = (malloc(n_nodes * sizeof(double)));
graph.pred_node = (malloc(n_nodes * sizeof(double)));
graph.pred_arc = (malloc(n_nodes * sizeof(double)));
graph.node_n_outward = (malloc(n_nodes * sizeof(int)));
graph.node_outward = (malloc(n_nodes * sizeof(resnode_t*)));
for (j = 0; j < instp->n_nodes; j++) {
graph.node_n_outward[j] = 0;
graph.node_outward[j] = (malloc(2*n_nodes * sizeof(resnode_t)));
}
return graph;
}
int resnode_cmp (const void* a, const void* b)
// comparison function for qsort, comparing neighbors, preferring those linked with the cheapest arcs
{
resnode_t* rsap = (resnode_t*) a;
resnode_t* rsbp = (resnode_t*) b;
if (*rsap->distp == *rsbp->distp)
return 0;
else
return ((*rsap->distp < *rsbp->distp) ? -1 : 1);
}
void residual_graph_reset (residual_graph_t* graphp)
// resets arc costs and node neighbors
{
int i, j;
for (i = 0; i < graphp->instp->n_arcs; i++)
graphp->arc_cost[i] = graphp->arc_cost_inv[i] = inf;
for (j = 0; j < graphp->instp->n_nodes; j++)
graphp->node_n_outward[j] = 0;
for (j = 0; j < graphp->instp->n_nodes; j++) {
graphp->dist[j] = inf;
graphp->pred_node[j] = -1;
graphp->pred_arc[j] = -1;
}
graphp->list_size = 0;
}
void residual_graph_prune (residual_graph_t* graphp)
// finds all node neighbors linked without infinite cost arcs
{
int i, j, o, d;
resnode_t resnode;
for (i = 0; i < graphp->instp->n_arcs; i++) {
o = graphp->instp->arc_orig_node[i];
d = graphp->instp->arc_dest_node[i];
if (graphp->arc_cost[i] != inf) {
resnode.node = d;
resnode.arc = i;
resnode.distp = &graphp->arc_cost[i];
assert(graphp->node_n_outward[o] < 2*graphp->instp->n_nodes);
graphp->node_outward[o][(graphp->node_n_outward[o])++] = resnode;
}
if (graphp->arc_cost_inv[i] != inf) {
resnode.node = o;
resnode.arc = i;
resnode.distp = &graphp->arc_cost_inv[i];
assert(graphp->node_n_outward[d] < 2*graphp->instp->n_nodes);
graphp->node_outward[d][(graphp->node_n_outward[d])++] = resnode;
}
}
for (j = 0; j < graphp->instp->n_nodes; j++)
// for each node, sort the neighbors in order of ascending distance
qsort(graphp->node_outward[j], graphp->node_n_outward[j], sizeof(resnode_t), resnode_cmp);
}
int is_in_list (residual_graph_t* graphp, int node_maybe_in_list)
// returns true if node_maybe_in_list is in visited nodes list
{
int i;
for (i = 0; i < graphp->list_size; i++)
if (graphp->list[i] == node_maybe_in_list)
return 1;
return 0;
}
int is_in_path (residual_graph_t* graphp, int node_current, int node_maybe_in_path)
// returns true if node_maybe_in_path is in the path leading from the source to node_current
{
int c;
for (c = node_current; c != -1; c = graphp->pred_node[c])
if (node_maybe_in_path == c)
return 1;
return 0;
}
void residual_graph_solve_shortest_path (residual_graph_t* graphp, int source_node)
// finds shortest or almost-shortest paths from source_node
{
int j, k, o;
resnode_t resnode;
// reset precedence information
for (j = 0; j < graphp->instp->n_nodes; j++) {
graphp->dist[j] = inf;
graphp->pred_node[j] = -1;
graphp->pred_arc[j] = -1;
}
graphp->list_size = 1;
graphp->list[0] = source_node;
graphp->dist[source_node] = 0.0;
// apply Ford-Bellman
while (graphp->list_size > 0) {
o = graphp->list[--graphp->list_size];
for (k = 0; k < graphp->node_n_outward[o]; k++) {
resnode = graphp->node_outward[o][k];
if (graphp->dist[resnode.node] > graphp->dist[o] + (*resnode.distp) && !is_in_path(graphp, o, resnode.node)) {
graphp->dist[resnode.node] = graphp->dist[o] + (*resnode.distp);
graphp->pred_node[resnode.node] = o;
graphp->pred_arc[resnode.node] = resnode.arc;
if (!is_in_list(graphp, resnode.node))
graphp->list[graphp->list_size++] = resnode.node;
}
}
}
}