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, 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 void MTPGraph::print_dot() {
71   cout << "digraph {" << endl;
72   cout << "  node[shape=circle];" << endl;
73   for(int n = 0; n < _nb_vertices; n++) {
74     int a = vertices[n].id;
75     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
76       int b = e->terminal_vertex->id;
77       if(e->occupied) {
78         cout << "  " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
79       } else {
80         cout << "  " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
81       }
82     }
83   }
84   cout << "}" << endl;
85 }
86
87 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
88                    int *from, int *to,
89                    int src, int snk) {
90   _nb_vertices = nb_vertices;
91   _nb_edges = nb_edges;
92
93   edges = new Edge[_nb_edges];
94   vertices = new Vertex[_nb_vertices];
95   _front = new Vertex *[_nb_vertices];
96   _new_front = new Vertex *[_nb_vertices];
97
98   _source = &vertices[src];
99   _sink = &vertices[snk];
100
101   for(int v = 0; v < _nb_vertices; v++) {
102     vertices[v].id = v;
103   }
104
105   for(int e = 0; e < nb_edges; e++) {
106     vertices[from[e]].add_edge(&edges[e]);
107     edges[e].occupied = 0;
108     edges[e].id = e;
109     edges[e].terminal_vertex = &vertices[to[e]];
110   }
111
112 }
113
114 MTPGraph::~MTPGraph() {
115   delete[] vertices;
116   delete[] edges;
117   delete[] _front;
118   delete[] _new_front;
119 }
120
121 void MTPGraph::initialize_work_lengths() {
122   scalar_t length_min = 0;
123   for(int n = 0; n < _nb_vertices; n++) {
124     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
125       length_min = min(e->length, length_min);
126     }
127   }
128   for(int n = 0; n < _nb_vertices; n++) {
129     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
130       e->work_length = e->length - length_min;
131     }
132   }
133 }
134
135 void MTPGraph::update_work_lengths() {
136   for(int n = 0; n < _nb_vertices; n++) {
137     scalar_t d = vertices[n].distance_from_source;
138     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
139       e->work_length += d - e->terminal_vertex->distance_from_source;
140     }
141   }
142 }
143
144 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
145   Vertex **tmp_front;
146   int tmp_front_size;
147   Vertex *v, *tv;
148   scalar_t d;
149
150 #ifdef VERBOSE
151   scalar_t residual_error = 0.0;
152 #endif
153   for(int n = 0; n < _nb_vertices; n++) {
154     for(Edge *e = vertices[n].root_edge; e; e = e->next) {
155       if(e->work_length < 0) {
156 #ifdef VERBOSE
157         residual_error -= e->work_length;
158 #endif
159         e->work_length = 0.0;
160       }
161     }
162   }
163 #ifdef VERBOSE
164   cerr << "residual_error " << residual_error << endl;
165 #endif
166
167   for(int v = 0; v < _nb_vertices; v++) {
168     vertices[v].distance_from_source = FLT_MAX;
169     vertices[v].pred_vertex = 0;
170     vertices[v].pred_edge = 0;
171     vertices[v].iteration = 0;
172   }
173
174   int iteration = 0;
175
176   int _front_size = 0, _new_front_size;
177   _front[_front_size++] = _source;
178   _source->distance_from_source = 0;
179
180   do {
181     _new_front_size = 0;
182     iteration++;
183     for(int f = 0; f < _front_size; f++) {
184       v = _front[f];
185       for(Edge *e = v->root_edge; e; e = e->next) {
186         d = v->distance_from_source + e->work_length;
187         tv = e->terminal_vertex;
188         if(d < tv->distance_from_source) {
189           tv->distance_from_source = d;
190           tv->pred_vertex = v;
191           tv->pred_edge = e;
192           if(tv->iteration < iteration) {
193             _new_front[_new_front_size++] = tv;
194             tv->iteration = iteration;
195           }
196         }
197       }
198     }
199
200     tmp_front = _new_front;
201     _new_front = _front;
202     _front = tmp_front;
203
204     tmp_front_size = _new_front_size;
205     _new_front_size = _front_size;
206     _front_size = tmp_front_size;
207   } while(_front_size > 0);
208 }
209
210 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
211   scalar_t total_length;
212
213   for(int e = 0; e < _nb_edges; e++) {
214     edges[e].length = lengths[e];
215   }
216
217   initialize_work_lengths();
218
219   do {
220     total_length = 0.0;
221     find_shortest_path(_front, _new_front);
222     update_work_lengths();
223
224     // Do we reach the _sink?
225     if(_sink->pred_edge) {
226
227       // If yes, compute the length of the best path
228       for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
229         total_length += v->pred_edge->length;
230       }
231
232       // If that length is negative
233       if(total_length < 0.0) {
234         // Invert all the edges along the best path
235         for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
236           Edge *e = v->pred_edge;
237           e->terminal_vertex = v->pred_vertex;
238           e->occupied = 1 - e->occupied;
239           e->length = - e->length;
240           e->work_length = - e->work_length;
241           v->pred_vertex->del_edge(e);
242           v->add_edge(e);
243         }
244       }
245     }
246   } while(total_length < 0.0);
247
248   for(int n = 0; n < _nb_vertices; n++) {
249     Vertex *v = &vertices[n];
250     for(Edge *e = v->root_edge; e; e = e->next) {
251       result_edge_occupation[e->id] = e->occupied;
252     }
253   }
254 }