Cosmetics.
[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, positivized_length;
30   Vertex *origin_vertex, *terminal_vertex;
31
32   // These are the links in the origin_vertex leaving edge list
33   Edge *next_leaving_edge, *pred_leaving_edge;
34
35   inline void revert();
36 };
37
38 class Vertex {
39 public:
40   int id;
41   Edge *leaving_edges;
42   scalar_t distance_from_source;
43   Edge *best_pred_edge_to_source;
44
45   int iteration; // Used in find_shortest_path to know if we already
46                  // added this vertex to the front
47   Vertex();
48   inline void add_edge(Edge *e);
49   inline void del_edge(Edge *e);
50 };
51
52 //////////////////////////////////////////////////////////////////////
53
54 void Edge::revert() {
55   length = - length;
56   positivized_length = 0;
57   origin_vertex->del_edge(this);
58   terminal_vertex->add_edge(this);
59   Vertex *t = terminal_vertex;
60   terminal_vertex = origin_vertex;
61   origin_vertex = t;
62 }
63
64 //////////////////////////////////////////////////////////////////////
65
66 Vertex::Vertex() {
67   leaving_edges = 0;
68 }
69
70 void Vertex::add_edge(Edge *e) {
71   e->next_leaving_edge = leaving_edges;
72   e->pred_leaving_edge = 0;
73   if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
74   leaving_edges = e;
75 }
76
77 void Vertex::del_edge(Edge *e) {
78   if(e == leaving_edges) { leaving_edges = e->next_leaving_edge; }
79   if(e->pred_leaving_edge) { e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge; }
80   if(e->next_leaving_edge) { e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge; }
81 }
82
83 //////////////////////////////////////////////////////////////////////
84
85 void MTPGraph::print() {
86   for(int k = 0; k < _nb_edges; k++) {
87     Edge *e = _edges + k;
88     cout << e->origin_vertex->id
89          << " -> "
90          << e->terminal_vertex->id
91          << " "
92          << e->length;
93     if(e->occupied) {
94       cout << " *";
95     }
96     cout << endl;
97   }
98 }
99
100 void MTPGraph::print_dot() {
101   cout << "digraph {" << endl;
102   cout << "  node[shape=circle];" << endl;
103   for(int k = 0; k < _nb_edges; k++) {
104     Edge *e = _edges + k;
105     if(e->occupied) {
106       cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
107            << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
108     } else {
109       cout << "  " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
110            << " [color=gray,label=\"" << e->length << "\"];" << endl;
111     }
112   }
113   cout << "}" << endl;
114 }
115
116 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
117                    int *from, int *to,
118                    int source, int sink) {
119   _nb_vertices = nb_vertices;
120   _nb_edges = nb_edges;
121
122   _edges = new Edge[_nb_edges];
123   _vertices = new Vertex[_nb_vertices];
124   _front = new Vertex *[_nb_vertices];
125   _new_front = new Vertex *[_nb_vertices];
126
127   _source = &_vertices[source];
128   _sink = &_vertices[sink];
129
130   for(int v = 0; v < _nb_vertices; v++) {
131     _vertices[v].id = v;
132   }
133
134   for(int e = 0; e < nb_edges; e++) {
135     _vertices[from[e]].add_edge(_edges + e);
136     _edges[e].occupied = 0;
137     _edges[e].id = e;
138     _edges[e].origin_vertex = _vertices + from[e];
139     _edges[e].terminal_vertex = _vertices + to[e];
140   }
141
142 }
143
144 MTPGraph::~MTPGraph() {
145   delete[] _vertices;
146   delete[] _edges;
147   delete[] _front;
148   delete[] _new_front;
149 }
150
151 void MTPGraph::initialize_positivized_lengths_with_min() {
152   scalar_t length_min = 0;
153   for(int n = 0; n < _nb_vertices; n++) {
154     for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
155       length_min = min(e->length, length_min);
156     }
157   }
158   for(int n = 0; n < _nb_vertices; n++) {
159     for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
160       e->positivized_length = e->length - length_min;
161     }
162   }
163 }
164
165 void MTPGraph::update_positivized_lengths() {
166   for(int k = 0; k < _nb_edges; k++) {
167     Edge *e = _edges + k;
168     e->positivized_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
169   }
170 }
171
172 void MTPGraph::force_positivized_lengths() {
173 #ifdef VERBOSE
174   scalar_t residual_error = 0.0;
175 #endif
176   for(int n = 0; n < _nb_vertices; n++) {
177     for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
178       if(e->positivized_length < 0) {
179 #ifdef VERBOSE
180         residual_error -= e->positivized_length;
181 #endif
182         e->positivized_length = 0.0;
183       }
184     }
185   }
186 #ifdef VERBOSE
187   cerr << "residual_error " << residual_error << endl;
188 #endif
189 }
190
191 // This method does not change the edge occupation. It update
192 // distance_from_source and best_pred_edge_to_source.
193 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
194   Vertex **tmp_front;
195   int tmp_front_size;
196   Vertex *v, *tv;
197   Edge *e;
198   scalar_t d;
199
200   for(int v = 0; v < _nb_vertices; v++) {
201     _vertices[v].distance_from_source = FLT_MAX;
202     _vertices[v].best_pred_edge_to_source = 0;
203     _vertices[v].iteration = 0;
204   }
205
206   int iteration = 0;
207
208   int _front_size = 0, _new_front_size;
209   _front[_front_size++] = _source;
210   _source->distance_from_source = 0;
211
212   do {
213     _new_front_size = 0;
214     iteration++;
215     for(int f = 0; f < _front_size; f++) {
216       v = _front[f];
217       for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
218         d = v->distance_from_source + e->positivized_length;
219         tv = e->terminal_vertex;
220         if(d < tv->distance_from_source) {
221           tv->distance_from_source = d;
222           tv->best_pred_edge_to_source = e;
223           if(tv->iteration < iteration) {
224             _new_front[_new_front_size++] = tv;
225             tv->iteration = iteration;
226           }
227         }
228       }
229     }
230
231     tmp_front = _new_front;
232     _new_front = _front;
233     _front = tmp_front;
234
235     tmp_front_size = _new_front_size;
236     _new_front_size = _front_size;
237     _front_size = tmp_front_size;
238   } while(_front_size > 0);
239 }
240
241 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
242   scalar_t total_length;
243   Vertex *v;
244   Edge *e;
245
246   for(int e = 0; e < _nb_edges; e++) {
247     _edges[e].length = lengths[e];
248     _edges[e].positivized_length = _edges[e].length;
249   }
250
251   // We use one iteration of find_shortest_path simply to propagate
252   // the distance to make all the edge lengths positive.
253   find_shortest_path(_front, _new_front);
254   update_positivized_lengths();
255
256   // #warning
257   // initialize_positivized_lengths_with_min();
258
259   do {
260     force_positivized_lengths();
261     find_shortest_path(_front, _new_front);
262     update_positivized_lengths();
263
264     total_length = 0.0;
265
266     // Do we reach the _sink?
267     if(_sink->best_pred_edge_to_source) {
268       // If yes, compute the length of the best path
269       v = _sink;
270       while(v->best_pred_edge_to_source) {
271         total_length += v->best_pred_edge_to_source->length;
272         v = v->best_pred_edge_to_source->origin_vertex;
273       }
274       // If that length is negative
275       if(total_length < 0.0) {
276 #ifdef VERBOSE
277         cout << "Found a path of length " << total_length << endl;
278 #endif
279         // Invert all the edges along the best path
280         v = _sink;
281         while(v->best_pred_edge_to_source) {
282           e = v->best_pred_edge_to_source;
283           v = e->origin_vertex;
284           e->revert();
285           // This is the only place where we change the occupations of
286           // edges
287           e->occupied = 1 - e->occupied;
288         }
289       }
290     }
291
292   } while(total_length < 0.0);
293
294   for(int k = 0; k < _nb_edges; k++) {
295     Edge *e = _edges + k;
296     if(e->occupied) { e->revert(); }
297     result_edge_occupation[k] = e->occupied;
298   }
299 }