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, work_length;
30 Vertex *origin_vertex, *terminal_vertex;
32 // These are the links in the origin_vertex leaving edge list
42 scalar_t distance_from_source;
43 Edge *best_pred_edge_to_source;
46 inline void add_edge(Edge *e);
47 inline void del_edge(Edge *e);
50 //////////////////////////////////////////////////////////////////////
55 origin_vertex->del_edge(this);
56 terminal_vertex->add_edge(this);
57 Vertex *t = terminal_vertex;
58 terminal_vertex = origin_vertex;
62 //////////////////////////////////////////////////////////////////////
68 void Vertex::add_edge(Edge *e) {
71 if(root_edge) { root_edge->pred = e; }
75 void Vertex::del_edge(Edge *e) {
76 if(e == root_edge) { root_edge = e->next; }
77 if(e->pred) { e->pred->next = e->next; }
78 if(e->next) { e->next->pred = e->pred; }
81 //////////////////////////////////////////////////////////////////////
83 void MTPGraph::print() {
84 for(int k = 0; k < _nb_edges; k++) {
86 cout << e->origin_vertex->id
88 << e->terminal_vertex->id
98 void MTPGraph::print_dot() {
99 cout << "digraph {" << endl;
100 cout << " node[shape=circle];" << endl;
101 for(int k = 0; k < _nb_edges; k++) {
102 Edge *e = _edges + k;
104 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
105 << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
107 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
108 << " [color=gray,label=\"" << e->length << "\"];" << endl;
114 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
116 int source, int sink) {
117 _nb_vertices = nb_vertices;
118 _nb_edges = nb_edges;
120 _edges = new Edge[_nb_edges];
121 _vertices = new Vertex[_nb_vertices];
122 _front = new Vertex *[_nb_vertices];
123 _new_front = new Vertex *[_nb_vertices];
125 _source = &_vertices[source];
126 _sink = &_vertices[sink];
128 for(int v = 0; v < _nb_vertices; v++) {
132 for(int e = 0; e < nb_edges; e++) {
133 _vertices[from[e]].add_edge(_edges + e);
134 _edges[e].occupied = 0;
136 _edges[e].origin_vertex = _vertices + from[e];
137 _edges[e].terminal_vertex = _vertices + to[e];
142 MTPGraph::~MTPGraph() {
149 void MTPGraph::initialize_work_lengths_with_min() {
150 scalar_t length_min = 0;
151 for(int n = 0; n < _nb_vertices; n++) {
152 for(Edge *e = _vertices[n].root_edge; e; e = e->next) {
153 length_min = min(e->length, length_min);
156 for(int n = 0; n < _nb_vertices; n++) {
157 for(Edge *e = _vertices[n].root_edge; e; e = e->next) {
158 e->work_length = e->length - length_min;
163 void MTPGraph::update_work_lengths() {
164 for(int k = 0; k < _nb_edges; k++) {
165 Edge *e = _edges + k;
166 e->work_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
170 void MTPGraph::force_positive_work_lengths() {
172 scalar_t residual_error = 0.0;
174 for(int n = 0; n < _nb_vertices; n++) {
175 for(Edge *e = _vertices[n].root_edge; e; e = e->next) {
176 if(e->work_length < 0) {
178 residual_error -= e->work_length;
180 e->work_length = 0.0;
185 cerr << "residual_error " << residual_error << endl;
189 // This method does not change the edge occupation. It update
190 // distance_from_source and best_pred_edge_to_source.
191 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
197 for(int v = 0; v < _nb_vertices; v++) {
198 _vertices[v].distance_from_source = FLT_MAX;
199 _vertices[v].best_pred_edge_to_source = 0;
200 _vertices[v].iteration = 0;
205 int _front_size = 0, _new_front_size;
206 _front[_front_size++] = _source;
207 _source->distance_from_source = 0;
212 for(int f = 0; f < _front_size; f++) {
214 for(Edge *e = v->root_edge; e; e = e->next) {
215 d = v->distance_from_source + e->work_length;
216 tv = e->terminal_vertex;
217 if(d < tv->distance_from_source) {
218 tv->distance_from_source = d;
219 tv->best_pred_edge_to_source = e;
220 if(tv->iteration < iteration) {
221 _new_front[_new_front_size++] = tv;
222 tv->iteration = iteration;
228 tmp_front = _new_front;
232 tmp_front_size = _new_front_size;
233 _new_front_size = _front_size;
234 _front_size = tmp_front_size;
235 } while(_front_size > 0);
238 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
239 scalar_t total_length;
243 for(int e = 0; e < _nb_edges; e++) {
244 _edges[e].length = lengths[e];
245 _edges[e].work_length = _edges[e].length;
248 // We use one iteration of find_shortest_path simply to propagate
249 // the distance to make all the edge lengths positive.
250 find_shortest_path(_front, _new_front);
251 update_work_lengths();
254 // initialize_work_lengths_with_min();
257 force_positive_work_lengths();
258 find_shortest_path(_front, _new_front);
259 update_work_lengths();
263 // Do we reach the _sink?
264 if(_sink->best_pred_edge_to_source) {
265 // If yes, compute the length of the best path
267 while(v->best_pred_edge_to_source) {
268 total_length += v->best_pred_edge_to_source->length;
269 v = v->best_pred_edge_to_source->origin_vertex;
271 // If that length is negative
272 if(total_length < 0.0) {
274 cout << "Found a path of length " << total_length << endl;
276 // Invert all the edges along the best path
278 while(v->best_pred_edge_to_source) {
279 e = v->best_pred_edge_to_source;
280 v = e->origin_vertex;
282 e->occupied = 1 - e->occupied;
287 } while(total_length < 0.0);
289 for(int k = 0; k < _nb_edges; k++) {
290 Edge *e = _edges + k;
291 if(e->occupied) { e->revert(); }
292 result_edge_occupation[k] = e->occupied;