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;
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 k = 0; k < _nb_edges; k++) {
61 cout << e->origin_vertex->id
63 << e->terminal_vertex->id
73 void MTPGraph::print_dot() {
74 cout << "digraph {" << endl;
75 cout << " node[shape=circle];" << endl;
76 for(int k = 0; k < _nb_edges; k++) {
79 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
80 << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
82 cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
83 << " [color=gray,label=\"" << e->length << "\"];" << endl;
89 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
92 _nb_vertices = nb_vertices;
95 edges = new Edge[_nb_edges];
96 vertices = new Vertex[_nb_vertices];
97 _front = new Vertex *[_nb_vertices];
98 _new_front = new Vertex *[_nb_vertices];
100 _source = &vertices[src];
101 _sink = &vertices[snk];
103 for(int v = 0; v < _nb_vertices; v++) {
107 for(int e = 0; e < nb_edges; e++) {
108 vertices[from[e]].add_edge(&edges[e]);
109 edges[e].occupied = 0;
111 edges[e].origin_vertex = &vertices[from[e]];
112 edges[e].terminal_vertex = &vertices[to[e]];
117 MTPGraph::~MTPGraph() {
124 void MTPGraph::initialize_work_lengths() {
125 scalar_t length_min = 0;
126 for(int n = 0; n < _nb_vertices; n++) {
127 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
128 length_min = min(e->length, length_min);
131 for(int n = 0; n < _nb_vertices; n++) {
132 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
133 e->work_length = e->length - length_min;
138 void MTPGraph::update_work_lengths() {
139 for(int k = 0; k < _nb_edges; k++) {
141 e->work_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
145 void MTPGraph::force_positive_work_lengths() {
147 scalar_t residual_error = 0.0;
149 for(int n = 0; n < _nb_vertices; n++) {
150 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
151 if(e->work_length < 0) {
153 residual_error -= e->work_length;
155 e->work_length = 0.0;
160 cerr << "residual_error " << residual_error << endl;
164 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
170 for(int v = 0; v < _nb_vertices; v++) {
171 vertices[v].distance_from_source = FLT_MAX;
172 vertices[v].pred_vertex = 0;
173 vertices[v].pred_edge = 0;
174 vertices[v].iteration = 0;
179 int _front_size = 0, _new_front_size;
180 _front[_front_size++] = _source;
181 _source->distance_from_source = 0;
186 for(int f = 0; f < _front_size; f++) {
188 for(Edge *e = v->root_edge; e; e = e->next) {
189 d = v->distance_from_source + e->work_length;
190 tv = e->terminal_vertex;
191 if(d < tv->distance_from_source) {
192 tv->distance_from_source = d;
195 if(tv->iteration < iteration) {
196 _new_front[_new_front_size++] = tv;
197 tv->iteration = iteration;
203 tmp_front = _new_front;
207 tmp_front_size = _new_front_size;
208 _new_front_size = _front_size;
209 _front_size = tmp_front_size;
210 } while(_front_size > 0);
213 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
214 scalar_t total_length;
216 for(int e = 0; e < _nb_edges; e++) {
217 edges[e].length = lengths[e];
218 edges[e].work_length = edges[e].length;
222 // find_shortest_path(_front, _new_front);
223 // update_work_lengths();
225 initialize_work_lengths();
228 force_positive_work_lengths();
229 find_shortest_path(_front, _new_front);
230 update_work_lengths();
234 // Do we reach the _sink?
235 if(_sink->pred_edge) {
237 // If yes, compute the length of the best path
238 for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
239 total_length += v->pred_edge->length;
242 // If that length is negative
243 if(total_length < 0.0) {
245 cout << "Found a path of length " << total_length << endl;
247 // Invert all the edges along the best path
248 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
249 Edge *e = v->pred_edge;
250 e->occupied = 1 - e->occupied;
251 e->length = - e->length;
252 e->work_length = - e->work_length;
253 e->origin_vertex->del_edge(e);
254 e->terminal_vertex->add_edge(e);
255 Vertex *t = e->terminal_vertex;
256 e->terminal_vertex = e->origin_vertex;
257 e->origin_vertex = t;
262 } while(total_length < 0.0);
264 for(int k = 0; k < _nb_edges; k++) {
267 e->length = - e->length;
269 e->origin_vertex->del_edge(e);
270 e->terminal_vertex->add_edge(e);
271 Vertex *t = e->terminal_vertex;
272 e->terminal_vertex = e->origin_vertex;
273 e->origin_vertex = t;
277 // for(Edge *e = _sink->root_edge; e; e = e->next) {
280 // cout << "PATH " << _sink->id;
282 // cout << " " << f->terminal_vertex->id;
283 // for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
289 // int nb_occupied = 0;
290 // for(int e = 0; e < _nb_edges; e++) {
291 // for(int n = 0; n < _nb_vertices; n++) {
292 // Vertex *v = &vertices[n];
293 // for(Edge *e = v->root_edge; e; e = e->next) {
294 // if(e->occupied) nb_occupied++;
299 for(int n = 0; n < _nb_vertices; n++) {
300 Vertex *v = &vertices[n];
301 for(Edge *e = v->root_edge; e; e = e->next) {
302 result_edge_occupation[e->id] = e->occupied;