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_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 //////////////////////////////////////////////////////////////////////
86 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
88 int source, int sink) {
89 _nb_vertices = nb_vertices;
92 _edges = new Edge[_nb_edges];
93 _vertices = new Vertex[_nb_vertices];
94 _front = new Vertex *[_nb_vertices];
95 _new_front = new Vertex *[_nb_vertices];
97 _source = &_vertices[source];
98 _sink = &_vertices[sink];
100 for(int v = 0; v < _nb_vertices; v++) {
104 for(int e = 0; e < nb_edges; e++) {
105 _vertices[from[e]].add_edge(_edges + e);
106 _edges[e].occupied = 0;
108 _edges[e].origin_vertex = _vertices + from[e];
109 _edges[e].terminal_vertex = _vertices + to[e];
116 MTPGraph::~MTPGraph() {
121 for(int p = 0; p < nb_paths; p++) delete paths[p];
125 //////////////////////////////////////////////////////////////////////
127 void MTPGraph::print(ostream *os) {
128 for(int k = 0; k < _nb_edges; k++) {
129 Edge *e = _edges + k;
130 (*os) << e->origin_vertex->id
132 << e->terminal_vertex->id
142 void MTPGraph::print_dot(ostream *os) {
143 (*os) << "digraph {" << endl;
144 // (*os) << " node [shape=circle];" << endl;
145 (*os) << " edge [color=gray]" << endl;
146 (*os) << " " << _source->id << " [peripheries=2];" << endl;
147 (*os) << " " << _sink->id << " [peripheries=2];" << endl;
148 for(int k = 0; k < _nb_edges; k++) {
149 Edge *e = _edges + k;
150 // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
154 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
155 << " [style=bold,color=black,label=\"" << e->length << "\"];" << endl;
157 (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
158 << " [label=\"" << e->length << "\"];" << endl;
161 (*os) << "}" << endl;
164 //////////////////////////////////////////////////////////////////////
166 void MTPGraph::update_positivized_lengths() {
167 for(int k = 0; k < _nb_edges; k++) {
168 Edge *e = _edges + k;
169 e->positivized_length +=
170 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
174 void MTPGraph::force_positivized_lengths() {
176 scalar_t residual_error = 0.0;
177 scalar_t max_error = 0.0;
179 for(int n = 0; n < _nb_vertices; n++) {
180 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
181 if(e->positivized_length < 0) {
183 residual_error -= e->positivized_length;
184 max_error = max(max_error, - e->positivized_length);
186 e->positivized_length = 0.0;
191 cerr << "residual_error " << residual_error << " max_error " << residual_error << endl;
195 // This method does not change the edge occupation. It update
196 // distance_from_source and pred_edge_toward_source.
197 void MTPGraph::find_shortest_path() {
204 for(int v = 0; v < _nb_vertices; v++) {
205 _vertices[v].distance_from_source = FLT_MAX;
206 _vertices[v].pred_edge_toward_source = 0;
207 _vertices[v].iteration = 0;
212 int _front_size = 0, _new_front_size;
213 _front[_front_size++] = _source;
214 _source->distance_from_source = 0;
220 for(int f = 0; f < _front_size; f++) {
222 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
223 d = v->distance_from_source + e->positivized_length;
224 tv = e->terminal_vertex;
225 if(d < tv->distance_from_source) {
226 tv->distance_from_source = d;
227 tv->pred_edge_toward_source = e;
228 if(tv->iteration < iteration) {
229 _new_front[_new_front_size++] = tv;
230 tv->iteration = iteration;
236 tmp_front = _new_front;
240 tmp_front_size = _new_front_size;
241 _new_front_size = _front_size;
242 _front_size = tmp_front_size;
243 } while(_front_size > 0);
246 void MTPGraph::find_best_paths(scalar_t *lengths) {
247 scalar_t total_length;
251 for(int e = 0; e < _nb_edges; e++) {
252 _edges[e].length = lengths[e];
253 _edges[e].occupied = 0;
254 _edges[e].positivized_length = _edges[e].length;
257 // We use one iteration of find_shortest_path simply to propagate
258 // the distance to make all the edge lengths positive.
259 find_shortest_path();
260 update_positivized_lengths();
263 force_positivized_lengths();
264 find_shortest_path();
265 update_positivized_lengths();
269 // Do we reach the _sink?
270 if(_sink->pred_edge_toward_source) {
271 // If yes, compute the length of the best path
273 while(v->pred_edge_toward_source) {
274 total_length += v->pred_edge_toward_source->length;
275 v = v->pred_edge_toward_source->origin_vertex;
277 // If that length is negative
278 if(total_length < 0.0) {
280 cerr << "Found a path of length " << total_length << endl;
282 // Invert all the edges along the best path
284 while(v->pred_edge_toward_source) {
285 e = v->pred_edge_toward_source;
286 v = e->origin_vertex;
288 // This is the only place where we change the occupations of
290 e->occupied = 1 - e->occupied;
295 } while(total_length < 0.0);
297 // Put back the graph in its original state (i.e. invert edges which
298 // have been inverted in the process)
299 for(int k = 0; k < _nb_edges; k++) {
300 Edge *e = _edges + k;
301 if(e->occupied) { e->invert(); }
305 int MTPGraph::retrieve_one_path(Edge *e, int *nodes) {
309 if(nodes) { nodes[l++] = e->origin_vertex->id; }
312 while(e->terminal_vertex != _sink) {
313 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
316 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
317 if(f->occupied) { nb_choices++; next = f; }
318 if(nb_choices == 0) {
319 cerr << "retrieve_one_path: Non-sink end point." << endl;
323 cerr << "retrieve_one_path: Non node-disjoint paths." << endl;
330 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
336 void MTPGraph::retrieve_disjoint_paths() {
339 for(int p = 0; p < nb_paths; p++) delete paths[p];
343 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
344 if(e->occupied) { nb_paths++; }
347 paths = new Path *[nb_paths];
350 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
352 int l = retrieve_one_path(e, 0);
353 paths[p] = new Path(l);
354 retrieve_one_path(e, paths[p]->nodes);