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 *terminal_vertex;
38 scalar_t distance_from_source;
42 Vertex() { root_edge = 0; }
44 inline void add_edge(Edge *e) {
47 if(root_edge) { root_edge->pred = e; }
51 inline void del_edge(Edge *e) {
52 if(e == root_edge) { root_edge = e->next; }
53 if(e->pred) { e->pred->next = e->next; }
54 if(e->next) { e->next->pred = e->pred; }
58 void MTPGraph::print() {
59 for(int n = 0; n < _nb_vertices; n++) {
60 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
61 cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
70 void MTPGraph::print_dot() {
71 cout << "digraph {" << endl;
72 cout << " node[shape=circle];" << endl;
73 for(int n = 0; n < _nb_vertices; n++) {
74 int a = vertices[n].id;
75 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
76 int b = e->terminal_vertex->id;
78 cout << " " << b << " -> " << a << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
80 cout << " " << a << " -> " << b << " [color=gray,label=\"" << e->length << "\"];" << endl;
87 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
90 _nb_vertices = nb_vertices;
93 edges = new Edge[_nb_edges];
94 vertices = new Vertex[_nb_vertices];
95 _front = new Vertex *[_nb_vertices];
96 _new_front = new Vertex *[_nb_vertices];
98 _source = &vertices[src];
99 _sink = &vertices[snk];
101 for(int v = 0; v < _nb_vertices; v++) {
105 for(int e = 0; e < nb_edges; e++) {
106 vertices[from[e]].add_edge(&edges[e]);
107 edges[e].occupied = 0;
109 edges[e].terminal_vertex = &vertices[to[e]];
114 MTPGraph::~MTPGraph() {
121 void MTPGraph::initialize_work_lengths() {
122 scalar_t length_min = 0;
123 for(int n = 0; n < _nb_vertices; n++) {
124 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
125 length_min = min(e->length, length_min);
128 for(int n = 0; n < _nb_vertices; n++) {
129 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
130 e->work_length = e->length - length_min;
135 void MTPGraph::update_work_lengths() {
136 for(int n = 0; n < _nb_vertices; n++) {
137 scalar_t d = vertices[n].distance_from_source;
138 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
139 e->work_length += d - e->terminal_vertex->distance_from_source;
144 void MTPGraph::force_positive_work_lengths() {
146 scalar_t residual_error = 0.0;
148 for(int n = 0; n < _nb_vertices; n++) {
149 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
150 if(e->work_length < 0) {
152 residual_error -= e->work_length;
154 e->work_length = 0.0;
159 cerr << "residual_error " << residual_error << endl;
163 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
169 for(int v = 0; v < _nb_vertices; v++) {
170 vertices[v].distance_from_source = FLT_MAX;
171 vertices[v].pred_vertex = 0;
172 vertices[v].pred_edge = 0;
173 vertices[v].iteration = 0;
178 int _front_size = 0, _new_front_size;
179 _front[_front_size++] = _source;
180 _source->distance_from_source = 0;
185 for(int f = 0; f < _front_size; f++) {
187 for(Edge *e = v->root_edge; e; e = e->next) {
188 d = v->distance_from_source + e->work_length;
189 tv = e->terminal_vertex;
190 if(d < tv->distance_from_source) {
191 tv->distance_from_source = d;
194 if(tv->iteration < iteration) {
195 _new_front[_new_front_size++] = tv;
196 tv->iteration = iteration;
202 tmp_front = _new_front;
206 tmp_front_size = _new_front_size;
207 _new_front_size = _front_size;
208 _front_size = tmp_front_size;
209 } while(_front_size > 0);
212 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
213 scalar_t total_length;
215 for(int e = 0; e < _nb_edges; e++) {
216 edges[e].length = lengths[e];
217 edges[e].work_length = edges[e].length;
220 find_shortest_path(_front, _new_front);
221 update_work_lengths();
223 // initialize_work_lengths();
226 force_positive_work_lengths();
227 find_shortest_path(_front, _new_front);
228 update_work_lengths();
232 // Do we reach the _sink?
233 if(_sink->pred_edge) {
235 // If yes, compute the length of the best path
236 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
237 total_length += v->pred_edge->length;
240 // If that length is negative
241 if(total_length < 0.0) {
242 // Invert all the edges along the best path
243 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
244 Edge *e = v->pred_edge;
245 e->terminal_vertex = v->pred_vertex;
246 e->occupied = 1 - e->occupied;
247 e->length = - e->length;
248 e->work_length = - e->work_length;
249 v->pred_vertex->del_edge(e);
255 } while(total_length < 0.0);
257 for(Edge *e = _sink->root_edge; e; e = e->next) {
260 cout << "PATH " << _sink->id;
262 cout << " " << f->terminal_vertex->id;
263 for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
269 for(int n = 0; n < _nb_vertices; n++) {
270 Vertex *v = &vertices[n];
271 for(Edge *e = v->root_edge; e; e = e->next) {
272 result_edge_occupation[e->id] = e->occupied;