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