The tracker seems to work.
[mtp.git] / mtp_graph.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 #include "mtp_graph.h"
20
21 #include <iostream>
22 #include <float.h>
23
24 using namespace std;
25
26 class Edge {
27 public:
28   int id, occupied;
29   scalar_t length, work_length;
30   Vertex *terminal_vertex;
31   Edge *next, *pred;
32 };
33
34 class Vertex {
35 public:
36   int id, iteration;
37   Edge *root_edge;
38   scalar_t distance_from_source;
39   Vertex *pred_vertex;
40   Edge *pred_edge;
41
42   Vertex() { root_edge = 0; }
43
44   inline void add_edge(Edge *e) {
45     e->next = root_edge;
46     e->pred = 0;
47     if(root_edge) { root_edge->pred = e; }
48     root_edge = e;
49   }
50
51   inline void del_edge(Edge *e) {
52     if(e == root_edge) { root_edge = e->next; }
53     if(e->pred) { e->pred->next = e->next; }
54     if(e->next) { e->next->pred = e->pred; }
55   }
56 };
57
58 void MTPGraph::print() {
59   for(int n = 0; n < _nb_vertices; n++) {
60     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
61       cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
62       if(e->occupied) {
63         cout << " *";
64       }
65       cout << endl;
66     }
67   }
68 }
69
70 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
71                    int *from, int *to,
72                    int src, int snk) {
73   _nb_vertices = nb_vertices;
74   _nb_edges = nb_edges;
75
76   edge_heap = new Edge[_nb_edges];
77   vertices = new Vertex[_nb_vertices];
78
79   source = &vertices[src];
80   sink = &vertices[snk];
81
82   for(int v = 0; v < _nb_vertices; v++) {
83     vertices[v].id = v;
84   }
85
86   for(int e = 0; e < nb_edges; e++) {
87     vertices[from[e]].add_edge(&edge_heap[e]);
88     edge_heap[e].occupied = 0;
89     edge_heap[e].id = e;
90     edge_heap[e].terminal_vertex = &vertices[to[e]];
91   }
92 }
93
94 MTPGraph::~MTPGraph() {
95   delete[] vertices;
96   delete[] edge_heap;
97 }
98
99 void MTPGraph::initialize_work_lengths() {
100   scalar_t length_min = 0;
101   for(int n = 0; n < _nb_vertices; n++) {
102     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
103       length_min = min(e->length, length_min);
104     }
105   }
106   for(int n = 0; n < _nb_vertices; n++) {
107     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
108       e->work_length = e->length - length_min;
109     }
110   }
111 }
112
113 void MTPGraph::update_work_length() {
114   for(int n = 0; n < _nb_vertices; n++) {
115     scalar_t d = vertices[n].distance_from_source;
116     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
117       e->work_length += d - e->terminal_vertex->distance_from_source;
118     }
119   }
120 }
121
122 void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
123   Vertex **tmp_front;
124   int tmp_front_size;
125   Vertex *v, *tv;
126   scalar_t d;
127
128 #ifdef VERBOSE
129   scalar_t residual_error = 0.0;
130 #endif
131   for(int n = 0; n < _nb_vertices; n++) {
132     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
133       if(e->work_length < 0) {
134 #ifdef VERBOSE
135         residual_error -= e->work_length;
136 #endif
137         e->work_length = 0.0;
138       }
139     }
140   }
141 #ifdef VERBOSE
142   cerr << "residual_error " << residual_error << endl;
143 #endif
144
145   for(int v = 0; v < _nb_vertices; v++) {
146     vertices[v].distance_from_source = FLT_MAX;
147     vertices[v].pred_vertex = 0;
148     vertices[v].pred_edge = 0;
149     vertices[v].iteration = 0;
150   }
151
152   int iteration = 0;
153
154   int front_size = 0, new_front_size;
155   front[front_size++] = source;
156   source->distance_from_source = 0;
157
158   do {
159     new_front_size = 0;
160     iteration++;
161     for(int f = 0; f < front_size; f++) {
162       v = front[f];
163       for(Edge *e = v->root_edge; e; e = e->next) {
164         d = v->distance_from_source + e->work_length;
165         tv = e->terminal_vertex;
166         if(d < tv->distance_from_source) {
167           tv->distance_from_source = d;
168           tv->pred_vertex = v;
169           tv->pred_edge = e;
170           if(tv->iteration < iteration) {
171             new_front[new_front_size++] = tv;
172             tv->iteration = iteration;
173           }
174         }
175       }
176     }
177
178     tmp_front = new_front;
179     new_front = front;
180     front = tmp_front;
181
182     tmp_front_size = new_front_size;
183     new_front_size = front_size;
184     front_size = tmp_front_size;
185   } while(front_size > 0);
186 }
187
188 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
189   Vertex **front = new Vertex *[_nb_vertices];
190   Vertex **new_front = new Vertex *[_nb_vertices];
191
192   scalar_t total_length;
193
194   for(int e = 0; e < _nb_edges; e++) {
195     edge_heap[e].length = lengths[e];
196   }
197
198   initialize_work_lengths();
199
200   do {
201     total_length = 0.0;
202     find_shortest_path(front, new_front);
203     update_work_length();
204
205     // Do we reach the sink?
206     if(sink->pred_edge) {
207
208       // If yes, compute the length of the best path
209       for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
210         total_length += v->pred_edge->length;
211       }
212
213       // If that length is negative
214       if(total_length < 0.0) {
215         // Invert all the edges along the best path
216         for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
217           Edge *e = v->pred_edge;
218           e->terminal_vertex = v->pred_vertex;
219           e->occupied = 1 - e->occupied;
220           e->length = - e->length;
221           e->work_length = - e->work_length;
222           v->pred_vertex->del_edge(e);
223           v->add_edge(e);
224         }
225       }
226     }
227   } while(total_length < 0.0);
228
229   delete[] front;
230   delete[] new_front;
231
232   for(int n = 0; n < _nb_vertices; n++) {
233     Vertex *v = &vertices[n];
234     for(Edge *e = v->root_edge; e; e = e->next) {
235       result_edge_occupation[e->id] = e->occupied;
236     }
237   }
238 }
239
240 void dot_print(int nb_vertices,
241                int nb_edges, int *ea, int *eb, scalar_t *el,
242                int source, int sink,
243                int *edge_occupation) {
244   cout << "digraph {" << endl;
245   cout << "  node[shape=circle];" << endl;
246   for(int e = 0; e < nb_edges; e++) {
247     if(edge_occupation[e]) {
248       cout << "  " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
249     } else {
250       cout << "  " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
251     }
252   }
253   cout << "}" << endl;
254 }