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"
29 scalar_t length, positivized_length;
30 Vertex *origin_vertex, *terminal_vertex;
32 // These are the links in the origin_vertex leaving edge list
33 Edge *next_leaving_edge, *pred_leaving_edge;
42 scalar_t distance_from_source;
43 Edge *best_pred_edge_to_source;
45 int iteration; // Used in find_shortest_path to know if we already
46 // added this vertex to the front
48 inline void add_edge(Edge *e);
49 inline void del_edge(Edge *e);
52 //////////////////////////////////////////////////////////////////////
56 positivized_length = 0;
57 origin_vertex->del_edge(this);
58 terminal_vertex->add_edge(this);
59 Vertex *t = terminal_vertex;
60 terminal_vertex = origin_vertex;
64 //////////////////////////////////////////////////////////////////////
70 void Vertex::add_edge(Edge *e) {
71 e->next_leaving_edge = leaving_edges;
72 e->pred_leaving_edge = 0;
73 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
77 void Vertex::del_edge(Edge *e) {
78 if(e == leaving_edges) { leaving_edges = e->next_leaving_edge; }
79 if(e->pred_leaving_edge) { e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge; }
80 if(e->next_leaving_edge) { e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge; }
83 //////////////////////////////////////////////////////////////////////
85 void MTPGraph::print() {
86 for(int k = 0; k < _nb_edges; k++) {
88 cout << e->origin_vertex->id
90 << e->terminal_vertex->id
100 void MTPGraph::print_dot() {
101 cout << "digraph {" << endl;
102 cout << " node[shape=circle];" << endl;
103 for(int k = 0; k < _nb_edges; k++) {
104 Edge *e = _edges + k;
106 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
107 << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
109 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
110 << " [color=gray,label=\"" << e->length << "\"];" << endl;
116 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
118 int source, int sink) {
119 _nb_vertices = nb_vertices;
120 _nb_edges = nb_edges;
122 _edges = new Edge[_nb_edges];
123 _vertices = new Vertex[_nb_vertices];
124 _front = new Vertex *[_nb_vertices];
125 _new_front = new Vertex *[_nb_vertices];
127 _source = &_vertices[source];
128 _sink = &_vertices[sink];
130 for(int v = 0; v < _nb_vertices; v++) {
134 for(int e = 0; e < nb_edges; e++) {
135 _vertices[from[e]].add_edge(_edges + e);
136 _edges[e].occupied = 0;
138 _edges[e].origin_vertex = _vertices + from[e];
139 _edges[e].terminal_vertex = _vertices + to[e];
144 MTPGraph::~MTPGraph() {
151 void MTPGraph::initialize_positivized_lengths_with_min() {
152 scalar_t length_min = 0;
153 for(int n = 0; n < _nb_vertices; n++) {
154 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
155 length_min = min(e->length, length_min);
158 for(int n = 0; n < _nb_vertices; n++) {
159 for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) {
160 e->positivized_length = e->length - length_min;
165 void MTPGraph::update_positivized_lengths() {
166 for(int k = 0; k < _nb_edges; k++) {
167 Edge *e = _edges + k;
168 e->positivized_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
172 void MTPGraph::force_positivized_lengths() {
174 scalar_t residual_error = 0.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 if(e->positivized_length < 0) {
180 residual_error -= e->positivized_length;
182 e->positivized_length = 0.0;
187 cerr << "residual_error " << residual_error << endl;
191 // This method does not change the edge occupation. It update
192 // distance_from_source and best_pred_edge_to_source.
193 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
200 for(int v = 0; v < _nb_vertices; v++) {
201 _vertices[v].distance_from_source = FLT_MAX;
202 _vertices[v].best_pred_edge_to_source = 0;
203 _vertices[v].iteration = 0;
208 int _front_size = 0, _new_front_size;
209 _front[_front_size++] = _source;
210 _source->distance_from_source = 0;
215 for(int f = 0; f < _front_size; f++) {
217 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
218 d = v->distance_from_source + e->positivized_length;
219 tv = e->terminal_vertex;
220 if(d < tv->distance_from_source) {
221 tv->distance_from_source = d;
222 tv->best_pred_edge_to_source = e;
223 if(tv->iteration < iteration) {
224 _new_front[_new_front_size++] = tv;
225 tv->iteration = iteration;
231 tmp_front = _new_front;
235 tmp_front_size = _new_front_size;
236 _new_front_size = _front_size;
237 _front_size = tmp_front_size;
238 } while(_front_size > 0);
241 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
242 scalar_t total_length;
246 for(int e = 0; e < _nb_edges; e++) {
247 _edges[e].length = lengths[e];
248 _edges[e].positivized_length = _edges[e].length;
251 // We use one iteration of find_shortest_path simply to propagate
252 // the distance to make all the edge lengths positive.
253 find_shortest_path(_front, _new_front);
254 update_positivized_lengths();
257 // initialize_positivized_lengths_with_min();
260 force_positivized_lengths();
261 find_shortest_path(_front, _new_front);
262 update_positivized_lengths();
266 // Do we reach the _sink?
267 if(_sink->best_pred_edge_to_source) {
268 // If yes, compute the length of the best path
270 while(v->best_pred_edge_to_source) {
271 total_length += v->best_pred_edge_to_source->length;
272 v = v->best_pred_edge_to_source->origin_vertex;
274 // If that length is negative
275 if(total_length < 0.0) {
277 cout << "Found a path of length " << total_length << endl;
279 // Invert all the edges along the best path
281 while(v->best_pred_edge_to_source) {
282 e = v->best_pred_edge_to_source;
283 v = e->origin_vertex;
285 // This is the only place where we change the occupations of
287 e->occupied = 1 - e->occupied;
292 } while(total_length < 0.0);
294 for(int k = 0; k < _nb_edges; k++) {
295 Edge *e = _edges + k;
296 if(e->occupied) { e->revert(); }
297 result_edge_occupation[k] = e->occupied;