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 *pred_edge_toward_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_leaving_edge(Edge *e);
50 inline void del_leaving_edge(Edge *e);
53 //////////////////////////////////////////////////////////////////////
57 positivized_length = 0;
58 origin_vertex->del_leaving_edge(this);
59 terminal_vertex->add_leaving_edge(this);
60 Vertex *t = terminal_vertex;
61 terminal_vertex = origin_vertex;
65 //////////////////////////////////////////////////////////////////////
71 void Vertex::add_leaving_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_leaving_edge(Edge *e) {
79 if(e == leaving_edges) {
80 leaving_edges = e->next_leaving_edge;
82 if(e->pred_leaving_edge) {
83 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
85 if(e->next_leaving_edge) {
86 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
90 //////////////////////////////////////////////////////////////////////
92 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
94 int source, int sink) {
95 _nb_vertices = nb_vertices;
98 _edges = new Edge[_nb_edges];
99 _vertices = new Vertex[_nb_vertices];
100 _front = new Vertex *[_nb_vertices];
101 _new_front = new Vertex *[_nb_vertices];
103 _source = &_vertices[source];
104 _sink = &_vertices[sink];
106 for(int v = 0; v < _nb_vertices; v++) {
110 for(int e = 0; e < nb_edges; e++) {
111 _vertices[from[e]].add_leaving_edge(_edges + e);
112 _edges[e].occupied = 0;
114 _edges[e].origin_vertex = _vertices + from[e];
115 _edges[e].terminal_vertex = _vertices + to[e];
122 MTPGraph::~MTPGraph() {
127 for(int p = 0; p < nb_paths; p++) delete paths[p];
131 //////////////////////////////////////////////////////////////////////
133 void MTPGraph::print(ostream *os) {
134 for(int k = 0; k < _nb_edges; k++) {
135 Edge *e = _edges + k;
136 (*os) << e->origin_vertex->id
138 << e->terminal_vertex->id
148 void MTPGraph::print_dot(ostream *os) {
149 (*os) << "digraph {" << endl;
150 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
151 (*os) << " edge [color=gray,arrowhead=open]" << endl;
152 (*os) << " " << _source->id << " [peripheries=2];" << endl;
153 (*os) << " " << _sink->id << " [peripheries=2];" << endl;
154 // (*os) << " " << _source->id << " [style=bold,color=red];" << endl;
155 // (*os) << " " << _sink->id << " [style=bold,color=green];" << 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
161 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
164 (*os) << "style=bold,color=black,";
166 (*os) << "label=\"" << e->length << "\"];" << endl;
168 (*os) << "}" << endl;
171 //////////////////////////////////////////////////////////////////////
173 void MTPGraph::update_positivized_lengths() {
174 for(int k = 0; k < _nb_edges; k++) {
175 Edge *e = _edges + k;
176 e->positivized_length +=
177 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
181 void MTPGraph::force_positivized_lengths() {
183 scalar_t residual_error = 0.0;
184 scalar_t max_error = 0.0;
186 for(int n = 0; n < _nb_vertices; n++) {
187 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
188 if(e->positivized_length < 0) {
190 residual_error -= e->positivized_length;
191 max_error = max(max_error, - e->positivized_length);
193 e->positivized_length = 0.0;
198 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
202 // This method does not change the edge occupation. It update
203 // distance_from_source and pred_edge_toward_source.
204 void MTPGraph::find_shortest_path() {
211 for(int v = 0; v < _nb_vertices; v++) {
212 _vertices[v].distance_from_source = FLT_MAX;
213 _vertices[v].pred_edge_toward_source = 0;
214 _vertices[v].iteration = 0;
219 int _front_size = 0, _new_front_size;
220 _front[_front_size++] = _source;
221 _source->distance_from_source = 0;
227 for(int f = 0; f < _front_size; f++) {
229 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
230 d = v->distance_from_source + e->positivized_length;
231 tv = e->terminal_vertex;
232 if(d < tv->distance_from_source) {
233 tv->distance_from_source = d;
234 tv->pred_edge_toward_source = e;
235 if(tv->iteration < iteration) {
236 _new_front[_new_front_size++] = tv;
237 tv->iteration = iteration;
243 tmp_front = _new_front;
247 tmp_front_size = _new_front_size;
248 _new_front_size = _front_size;
249 _front_size = tmp_front_size;
250 } while(_front_size > 0);
253 void MTPGraph::find_best_paths(scalar_t *lengths) {
254 scalar_t total_length;
258 for(int e = 0; e < _nb_edges; e++) {
259 _edges[e].length = lengths[e];
260 _edges[e].occupied = 0;
261 _edges[e].positivized_length = _edges[e].length;
264 // We use one iteration of find_shortest_path simply to propagate
265 // the distance to make all the edge lengths positive.
266 find_shortest_path();
269 update_positivized_lengths();
270 force_positivized_lengths();
271 find_shortest_path();
275 // Do we reach the _sink?
276 if(_sink->pred_edge_toward_source) {
277 // If yes, compute the length of the best path
279 while(v->pred_edge_toward_source) {
280 total_length += v->pred_edge_toward_source->length;
281 v = v->pred_edge_toward_source->origin_vertex;
283 // If that length is negative
284 if(total_length < 0.0) {
286 cerr << "Found a path of length " << total_length << endl;
288 // Invert all the edges along the best path
290 while(v->pred_edge_toward_source) {
291 e = v->pred_edge_toward_source;
292 v = e->origin_vertex;
294 // This is the only place where we change the occupations of
296 e->occupied = 1 - e->occupied;
301 } while(total_length < 0.0);
303 // Put back the graph in its original state (i.e. invert edges which
304 // have been inverted in the process)
305 for(int k = 0; k < _nb_edges; k++) {
306 Edge *e = _edges + k;
307 if(e->occupied) { e->invert(); }
311 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
316 path->nodes[l++] = e->origin_vertex->id;
317 path->length = e->length;
320 while(e->terminal_vertex != _sink) {
322 path->nodes[l++] = e->terminal_vertex->id;
323 path->length += e->length;
326 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
327 if(f->occupied) { nb_choices++; next = f; }
328 if(nb_choices == 0) {
329 cerr << "retrieve_one_path: Non-sink end point." << endl;
333 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
341 path->nodes[l++] = e->terminal_vertex->id;
342 path->length += e->length;
348 void MTPGraph::retrieve_disjoint_paths() {
351 for(int p = 0; p < nb_paths; p++) delete paths[p];
355 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
356 if(e->occupied) { nb_paths++; }
359 paths = new Path *[nb_paths];
362 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
364 int l = retrieve_one_path(e, 0);
365 paths[p] = new Path(l);
366 retrieve_one_path(e, paths[p]);