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() {
139 for(int k = 0; k < _nb_edges; k++) {
140 Edge *e = _edges + k;
141 cout << e->origin_vertex->id
143 << e->terminal_vertex->id
153 void MTPGraph::print_dot() {
154 cout << "digraph {" << endl;
155 cout << " node[shape=circle];" << endl;
156 for(int k = 0; k < _nb_edges; k++) {
157 Edge *e = _edges + k;
159 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
160 << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
162 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
163 << " [color=gray,label=\"" << e->length << "\"];" << endl;
169 //////////////////////////////////////////////////////////////////////
171 void MTPGraph::initialize_positivized_lengths_with_min() {
172 scalar_t length_min = 0;
173 for(int n = 0; n < _nb_vertices; n++) {
174 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
175 length_min = min(e->length, length_min);
178 for(int n = 0; n < _nb_vertices; n++) {
179 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
180 e->positivized_length = e->length - length_min;
185 void MTPGraph::update_positivized_lengths() {
186 for(int k = 0; k < _nb_edges; k++) {
187 Edge *e = _edges + k;
188 e->positivized_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
192 void MTPGraph::force_positivized_lengths() {
194 scalar_t residual_error = 0.0;
196 for(int n = 0; n < _nb_vertices; n++) {
197 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
198 if(e->positivized_length < 0) {
200 residual_error -= e->positivized_length;
202 e->positivized_length = 0.0;
207 cerr << "residual_error " << residual_error << endl;
211 // This method does not change the edge occupation. It update
212 // distance_from_source and best_pred_edge_to_source.
213 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
220 for(int v = 0; v < _nb_vertices; v++) {
221 _vertices[v].distance_from_source = FLT_MAX;
222 _vertices[v].best_pred_edge_to_source = 0;
223 _vertices[v].iteration = 0;
228 int _front_size = 0, _new_front_size;
229 _front[_front_size++] = _source;
230 _source->distance_from_source = 0;
235 for(int f = 0; f < _front_size; f++) {
237 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
238 d = v->distance_from_source + e->positivized_length;
239 tv = e->terminal_vertex;
240 if(d < tv->distance_from_source) {
241 tv->distance_from_source = d;
242 tv->best_pred_edge_to_source = e;
243 if(tv->iteration < iteration) {
244 _new_front[_new_front_size++] = tv;
245 tv->iteration = iteration;
251 tmp_front = _new_front;
255 tmp_front_size = _new_front_size;
256 _new_front_size = _front_size;
257 _front_size = tmp_front_size;
258 } while(_front_size > 0);
261 void MTPGraph::find_best_paths(scalar_t *lengths) {
262 scalar_t total_length;
266 for(int e = 0; e < _nb_edges; e++) {
267 _edges[e].length = lengths[e];
268 _edges[e].positivized_length = _edges[e].length;
271 // We use one iteration of find_shortest_path simply to propagate
272 // the distance to make all the edge lengths positive.
273 find_shortest_path(_front, _new_front);
274 update_positivized_lengths();
277 // initialize_positivized_lengths_with_min();
280 force_positivized_lengths();
281 find_shortest_path(_front, _new_front);
282 update_positivized_lengths();
286 // Do we reach the _sink?
287 if(_sink->best_pred_edge_to_source) {
288 // If yes, compute the length of the best path
290 while(v->best_pred_edge_to_source) {
291 total_length += v->best_pred_edge_to_source->length;
292 v = v->best_pred_edge_to_source->origin_vertex;
294 // If that length is negative
295 if(total_length < 0.0) {
297 cout << "Found a path of length " << total_length << endl;
299 // Invert all the edges along the best path
301 while(v->best_pred_edge_to_source) {
302 e = v->best_pred_edge_to_source;
303 v = e->origin_vertex;
305 // This is the only place where we change the occupations of
307 e->occupied = 1 - e->occupied;
312 } while(total_length < 0.0);
314 for(int k = 0; k < _nb_edges; k++) {
315 Edge *e = _edges + k;
316 if(e->occupied) { e->revert(); }
320 int MTPGraph::retrieve_one_path(Edge *e, int *nodes) {
324 if(nodes) { nodes[l++] = e->origin_vertex->id; }
327 while(e->terminal_vertex != _sink) {
328 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
331 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
332 if(f->occupied) { nb_choices++; next = f; }
333 if(nb_choices == 0) {
334 cerr << "Non-sink path end point?!" << endl;
338 cerr << "Non node-disjoint path, can not retrieve." << endl;
345 if(nodes) { nodes[l++] = e->terminal_vertex->id; }
351 void MTPGraph::retrieve_disjoint_paths() {
354 for(int p = 0; p < nb_paths; p++) delete paths[p];
358 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
359 if(e->occupied) { nb_paths++; }
362 paths = new Path *[nb_paths];
365 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
367 int l = retrieve_one_path(e, 0);
368 paths[p] = new Path(l);
369 retrieve_one_path(e, paths[p]->nodes);