Removed miniksp, cosmetics on mtp.
[mtp.git] / mtp.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 // Multi-Tracked Path
20
21 // #define VERBOSE
22
23 #include <iostream>
24 #include <fstream>
25 #include <cmath>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <float.h>
29
30 using namespace std;
31
32 typedef float scalar_t;
33
34 #ifdef DEBUG
35 #define ASSERT(x) if(!(x)) { \
36   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
37   abort(); \
38 }
39 #else
40 #define ASSERT(x)
41 #endif
42
43 class Vertex;
44
45 class Edge {
46 public:
47   int id, occupied;
48   scalar_t length, work_length;
49   Vertex *terminal_vertex;
50   Edge *next, *pred;
51 };
52
53 class Vertex {
54 public:
55   int id;
56
57   Edge *root_edge;
58   scalar_t distance_from_source;
59
60   Vertex *pred_vertex;
61   Edge *pred_edge;
62
63   Vertex() { root_edge = 0; }
64
65   inline void add_edge(Edge *e) {
66     e->next = root_edge;
67     e->pred = 0;
68     if(root_edge) { root_edge->pred = e; }
69     root_edge = e;
70   }
71
72   inline void del_edge(Edge *e) {
73     if(e == root_edge) { root_edge = e->next; }
74     if(e->pred) { e->pred->next = e->next; }
75     if(e->next) { e->next->pred = e->pred; }
76   }
77 };
78
79 class Graph {
80   void initialize_work_lengths();
81   void update_work_length();
82   void find_shortest_path(Vertex **front, Vertex **new_front);
83
84   int nb_vertices;
85   Edge *edge_heap;
86   Vertex *vertices;
87   Vertex *source, *sink;
88
89 public:
90   Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
91         int source, int sink);
92
93   ~Graph();
94
95   void find_best_paths(int *result_edge_occupation);
96   void print();
97 };
98
99 void Graph::print() {
100   for(int n = 0; n < nb_vertices; n++) {
101     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
102       cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
103       if(e->occupied) {
104         cout << " *";
105       }
106       cout << endl;
107     }
108   }
109 }
110
111 Graph::Graph(int nb_vrt, int nb_edges,
112              int *from, int *to, scalar_t *lengths,
113              int src, int snk) {
114   nb_vertices = nb_vrt;
115
116   edge_heap = new Edge[nb_edges];
117   vertices = new Vertex[nb_vertices];
118
119   source = &vertices[src];
120   sink = &vertices[snk];
121
122   for(int v = 0; v < nb_vertices; v++) {
123     vertices[v].id = v;
124   }
125
126   for(int e = 0; e < nb_edges; e++) {
127     vertices[from[e]].add_edge(&edge_heap[e]);
128     edge_heap[e].occupied = 0;
129     edge_heap[e].id = e;
130     edge_heap[e].length = lengths[e];
131     edge_heap[e].terminal_vertex = &vertices[to[e]];
132   }
133 }
134
135 Graph::~Graph() {
136   delete[] vertices;
137   delete[] edge_heap;
138 }
139
140 void Graph::initialize_work_lengths() {
141   scalar_t length_min = 0;
142   for(int n = 0; n < nb_vertices; n++) {
143     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
144       length_min = min(e->length, length_min);
145     }
146   }
147   for(int n = 0; n < nb_vertices; n++) {
148     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
149       e->work_length = e->length - length_min;
150     }
151   }
152 }
153
154 void Graph::update_work_length() {
155   for(int n = 0; n < nb_vertices; n++) {
156     scalar_t d = vertices[n].distance_from_source;
157     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
158       e->work_length += d - e->terminal_vertex->distance_from_source;
159     }
160   }
161 }
162
163 void Graph::find_shortest_path(Vertex **front, Vertex **new_front) {
164   Vertex **tmp_front;
165   int tmp_front_size;
166   Vertex *v, *tv;
167   scalar_t d;
168
169 #ifdef DEBUG
170   for(int n = 0; n < nb_vertices; n++) {
171     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
172       if(e->work_length < 0) {
173         cerr << "DEBUG error in find_shortest_path: Edge fixed lengths have to be positive."
174              << endl;
175         abort();
176       }
177     }
178   }
179 #endif
180
181   for(int v = 0; v < nb_vertices; v++) {
182     vertices[v].distance_from_source = FLT_MAX;
183     vertices[v].pred_vertex = 0;
184     vertices[v].pred_edge = 0;
185   }
186
187   int front_size = 0, new_front_size;
188   front[front_size++] = source;
189   source->distance_from_source = 0;
190
191   do {
192     new_front_size = 0;
193     for(int f = 0; f < front_size; f++) {
194       v = front[f];
195       for(Edge *e = v->root_edge; e; e = e->next) {
196         d = v->distance_from_source + e->work_length;
197         tv = e->terminal_vertex;
198         if(d < tv->distance_from_source) {
199           tv->distance_from_source = d;
200           tv->pred_vertex = v;
201           tv->pred_edge = e;
202           new_front[new_front_size++] = tv;
203         }
204       }
205     }
206
207     tmp_front = new_front;
208     new_front = front;
209     front = tmp_front;
210
211     tmp_front_size = new_front_size;
212     new_front_size = front_size;
213     front_size = tmp_front_size;
214   } while(front_size > 0);
215 }
216
217 void Graph::find_best_paths(int *result_edge_occupation) {
218   Vertex **front = new Vertex *[nb_vertices];
219   Vertex **new_front = new Vertex *[nb_vertices];
220
221   scalar_t total_length;
222
223   initialize_work_lengths();
224
225   do {
226     total_length = 0.0;
227     find_shortest_path(front, new_front);
228     update_work_length();
229
230     // Do we reach the sink?
231     if(sink->pred_edge) {
232
233       // If yes, compute the length of the best path
234       for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
235         total_length += v->pred_edge->length;
236       }
237
238       // If that length is negative
239       if(total_length < 0.0) {
240         // Invert all the edges along the best path
241         for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
242           Edge *e = v->pred_edge;
243           e->terminal_vertex = v->pred_vertex;
244           e->occupied = 1 - e->occupied;
245           e->length = - e->length;
246           e->work_length = - e->work_length;
247           v->pred_vertex->del_edge(e);
248           v->add_edge(e);
249         }
250       }
251     }
252   } while(total_length < 0.0);
253
254   delete[] front;
255   delete[] new_front;
256
257   for(int n = 0; n < nb_vertices; n++) {
258     Vertex *v = &vertices[n];
259     for(Edge *e = v->root_edge; e; e = e->next) {
260       result_edge_occupation[e->id] = e->occupied;
261     }
262   }
263 }
264
265 void find_best_paths(int nb_vertices,
266                      int nb_edges, int *ea, int *eb, scalar_t *el,
267                      int source, int sink,
268                      int *result_edge_occupation) {
269   Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
270   graph.find_best_paths(result_edge_occupation);
271 }
272
273 void dot_print(int nb_vertices,
274                int nb_edges, int *ea, int *eb, scalar_t *el,
275                int source, int sink,
276                int *edge_occupation) {
277   cout << "digraph {" << endl;
278   cout << "  node[shape=circle];" << endl;
279   for(int e = 0; e < nb_edges; e++) {
280     if(edge_occupation[e]) {
281       cout << "  " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
282     } else {
283       cout << "  " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
284     }
285   }
286   cout << "}" << endl;
287 }
288
289 //////////////////////////////////////////////////////////////////////
290
291 int main(int argc, char **argv) {
292
293   if(argc < 2) {
294     cerr << argv[0] << " <graph file>" << endl;
295     exit(EXIT_FAILURE);
296   }
297
298   ifstream *file = new ifstream(argv[1]);
299
300   int nb_edges, nb_vertices;
301   int source, sink;
302
303   if(file->good()) {
304
305     (*file) >> nb_vertices >> nb_edges;
306     (*file) >> source >> sink;
307
308     scalar_t *edge_lengths = new scalar_t[nb_edges];
309     int *vertex_from = new int[nb_edges];
310     int *vertex_to = new int[nb_edges];
311     int *result_edge_occupation = new int[nb_edges];
312
313     for(int e = 0; e < nb_edges; e++) {
314       (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
315     }
316
317     find_best_paths(nb_vertices, nb_edges,
318                     vertex_from, vertex_to, edge_lengths,
319                     source, sink,
320                     result_edge_occupation);
321
322     dot_print(nb_vertices, nb_edges,
323               vertex_from, vertex_to, edge_lengths,
324               source, sink,
325               result_edge_occupation);
326
327     delete[] result_edge_occupation;
328     delete[] edge_lengths;
329     delete[] vertex_from;
330     delete[] vertex_to;
331
332   } else {
333
334     cerr << "Can not open " << argv[1] << endl;
335
336     delete file;
337     exit(EXIT_FAILURE);
338
339   }
340
341   delete file;
342   exit(EXIT_SUCCESS);
343 }