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. //
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. //
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/>. //
15 // Written by and Copyright (C) Francois Fleuret //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
19 #include "mtp_graph.h"
30 scalar_t length, positivized_length;
31 Vertex *origin_vertex, *terminal_vertex;
33 // These are the links in the origin_vertex leaving edge list
34 Edge *next_leaving_edge, *pred_leaving_edge;
43 scalar_t distance_from_source;
44 Edge *best_pred_edge_to_source;
46 int iteration; // Used in find_shortest_path to know if we already
47 // added this vertex to the front
49 inline void add_edge(Edge *e);
50 inline void del_edge(Edge *e);
53 //////////////////////////////////////////////////////////////////////
57 positivized_length = 0;
58 origin_vertex->del_edge(this);
59 terminal_vertex->add_edge(this);
60 Vertex *t = terminal_vertex;
61 terminal_vertex = origin_vertex;
65 //////////////////////////////////////////////////////////////////////
71 void Vertex::add_edge(Edge *e) {
72 e->next_leaving_edge = leaving_edges;
73 e->pred_leaving_edge = 0;
74 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
78 void Vertex::del_edge(Edge *e) {
79 if(e == leaving_edges) { leaving_edges = e->next_leaving_edge; }
80 if(e->pred_leaving_edge) { e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge; }
81 if(e->next_leaving_edge) { e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge; }
84 //////////////////////////////////////////////////////////////////////
88 nodes = new int[length];
95 //////////////////////////////////////////////////////////////////////
97 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
99 int source, int sink) {
100 _nb_vertices = nb_vertices;
101 _nb_edges = nb_edges;
103 _edges = new Edge[_nb_edges];
104 _vertices = new Vertex[_nb_vertices];
105 _front = new Vertex *[_nb_vertices];
106 _new_front = new Vertex *[_nb_vertices];
108 _source = &_vertices[source];
109 _sink = &_vertices[sink];
111 for(int v = 0; v < _nb_vertices; v++) {
115 for(int e = 0; e < nb_edges; e++) {
116 _vertices[from[e]].add_edge(_edges + e);
117 _edges[e].occupied = 0;
119 _edges[e].origin_vertex = _vertices + from[e];
120 _edges[e].terminal_vertex = _vertices + to[e];
127 MTPGraph::~MTPGraph() {
132 for(int p = 0; p < nb_paths; p++) delete paths[p];
136 //////////////////////////////////////////////////////////////////////
138 void MTPGraph::print(ostream *os) {
139 for(int k = 0; k < _nb_edges; k++) {
140 Edge *e = _edges + k;
141 (*os) << e->origin_vertex->id
143 << e->terminal_vertex->id
153 void MTPGraph::print_dot(ostream *os) {
154 (*os) << "digraph {" << endl;
155 (*os) << " node[shape=circle];" << endl;
156 for(int k = 0; k < _nb_edges; k++) {
157 Edge *e = _edges + k;
158 // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
162 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
163 << " [style=bold,color=black,label=\"" << e->length << "\"];" << endl;
165 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
166 << " [color=gray,label=\"" << e->length << "\"];" << endl;
169 (*os) << "}" << endl;
172 //////////////////////////////////////////////////////////////////////
174 void MTPGraph::initialize_positivized_lengths_with_min() {
175 scalar_t length_min = 0;
176 for(int n = 0; n < _nb_vertices; n++) {
177 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
178 length_min = min(e->length, length_min);
181 for(int n = 0; n < _nb_vertices; n++) {
182 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
183 e->positivized_length = e->length - length_min;
188 void MTPGraph::update_positivized_lengths() {
189 for(int k = 0; k < _nb_edges; k++) {
190 Edge *e = _edges + k;
191 e->positivized_length +=
192 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
196 void MTPGraph::force_positivized_lengths() {
198 scalar_t residual_error = 0.0;
199 scalar_t max_error = 0.0;
201 for(int n = 0; n < _nb_vertices; n++) {
202 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
203 if(e->positivized_length < 0) {
205 residual_error -= e->positivized_length;
206 max_error = max(max_error, fabs(e->positivized_length));
208 e->positivized_length = 0.0;
213 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
217 // This method does not change the edge occupation. It update
218 // distance_from_source and best_pred_edge_to_source.
219 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
226 for(int v = 0; v < _nb_vertices; v++) {
227 _vertices[v].distance_from_source = FLT_MAX;
228 _vertices[v].best_pred_edge_to_source = 0;
229 _vertices[v].iteration = 0;
234 int _front_size = 0, _new_front_size;
235 _front[_front_size++] = _source;
236 _source->distance_from_source = 0;
242 // for(int k = 0; k < _nb_edges; k++) {
243 // Edge *e = _edges + k;
244 // d = e->origin_vertex->distance_from_source + e->positivized_length;
245 // if(d < e->terminal_vertex->distance_from_source) {
246 // e->terminal_vertex->distance_from_source = d;
247 // _new_front_size++;
251 // for(int n = 0; n < _nb_vertices; n++) {
252 // v = &_vertices[n];
253 // for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
254 // d = v->distance_from_source + e->positivized_length;
255 // tv = e->terminal_vertex;
256 // if(d < tv->distance_from_source) {
257 // tv->distance_from_source = d;
258 // tv->best_pred_edge_to_source = e;
259 // if(tv->iteration < iteration) {
260 // _new_front[_new_front_size++] = tv;
261 // tv->iteration = iteration;
267 for(int f = 0; f < _front_size; f++) {
269 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
270 d = v->distance_from_source + e->positivized_length;
271 tv = e->terminal_vertex;
272 if(d < tv->distance_from_source) {
273 tv->distance_from_source = d;
274 tv->best_pred_edge_to_source = e;
275 if(tv->iteration < iteration) {
276 _new_front[_new_front_size++] = tv;
277 tv->iteration = iteration;
283 tmp_front = _new_front;
287 tmp_front_size = _new_front_size;
288 _new_front_size = _front_size;
289 _front_size = tmp_front_size;
290 } while(_front_size > 0);
293 scalar_t min_delta = 0, delta;
294 for(int k = 0; k < _nb_edges; k++) {
295 Edge *e = _edges + k;
296 // d = e->origin_vertex->distance_from_source + e->positivized_length;
297 // if(d > e->terminal_vertex->distance_from_source) { abort(); }
298 delta = e->positivized_length +
299 (e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source);
300 min_delta = min(delta, min_delta);
302 cout << "min_delta = " << delta << endl;
306 void MTPGraph::find_best_paths(scalar_t *lengths) {
307 scalar_t total_length;
311 for(int e = 0; e < _nb_edges; e++) {
312 _edges[e].length = lengths[e];
313 _edges[e].occupied = 0;
314 _edges[e].positivized_length = _edges[e].length;
317 // We use one iteration of find_shortest_path simply to propagate
318 // the distance to make all the edge lengths positive.
319 find_shortest_path(_front, _new_front);
320 update_positivized_lengths();
323 // initialize_positivized_lengths_with_min();
326 force_positivized_lengths();
327 find_shortest_path(_front, _new_front);
328 update_positivized_lengths();
332 // Do we reach the _sink?
333 if(_sink->best_pred_edge_to_source) {
334 // If yes, compute the length of the best path
336 while(v->best_pred_edge_to_source) {
337 total_length += v->best_pred_edge_to_source->length;
338 v = v->best_pred_edge_to_source->origin_vertex;
340 // If that length is negative
341 if(total_length < 0.0) {
343 cerr << "Found a path of length " << total_length << endl;
345 // Invert all the edges along the best path
347 while(v->best_pred_edge_to_source) {
348 e = v->best_pred_edge_to_source;
349 v = e->origin_vertex;
351 // This is the only place where we change the occupations of
353 e->occupied = 1 - e->occupied;
358 } while(total_length < 0.0);
360 for(int k = 0; k < _nb_edges; k++) {
361 Edge *e = _edges + k;
362 if(e->occupied) { e->revert(); }
366 int MTPGraph::retrieve_one_path(Edge *e, int *nodes) {
370 if(nodes) { nodes[l++] = e->origin_vertex->id; }
373 while(e->terminal_vertex != _sink) {
374 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
377 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
378 if(f->occupied) { nb_choices++; next = f; }
379 if(nb_choices == 0) {
380 cerr << "Non-sink path end point?!" << endl;
384 cerr << "Non node-disjoint path, can not retrieve." << endl;
391 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
397 void MTPGraph::retrieve_disjoint_paths() {
400 for(int p = 0; p < nb_paths; p++) delete paths[p];
404 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
405 if(e->occupied) { nb_paths++; }
408 paths = new Path *[nb_paths];
411 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
413 int l = retrieve_one_path(e, 0);
414 paths[p] = new Path(l);
415 retrieve_one_path(e, paths[p]->nodes);