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;
88 void dot_print(int nb_vertices,
89 int nb_edges, int *ea, int *eb, scalar_t *el,
90 int _source, int _sink,
91 int *edge_occupation) {
92 for(int e = 0; e < nb_edges; e++) {
96 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
99 _nb_vertices = nb_vertices;
100 _nb_edges = nb_edges;
102 edges = new Edge[_nb_edges];
103 vertices = new Vertex[_nb_vertices];
105 _source = &vertices[src];
106 _sink = &vertices[snk];
108 for(int v = 0; v < _nb_vertices; v++) {
112 for(int e = 0; e < nb_edges; e++) {
113 vertices[from[e]].add_edge(&edges[e]);
114 edges[e].occupied = 0;
116 edges[e].terminal_vertex = &vertices[to[e]];
119 _front = new Vertex *[_nb_vertices];
120 _new_front = new Vertex *[_nb_vertices];
123 MTPGraph::~MTPGraph() {
130 void MTPGraph::initialize_work_lengths() {
131 scalar_t length_min = 0;
132 for(int n = 0; n < _nb_vertices; n++) {
133 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
134 length_min = min(e->length, length_min);
137 for(int n = 0; n < _nb_vertices; n++) {
138 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
139 e->work_length = e->length - length_min;
144 void MTPGraph::update_work_length() {
145 for(int n = 0; n < _nb_vertices; n++) {
146 scalar_t d = vertices[n].distance_from_source;
147 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
148 e->work_length += d - e->terminal_vertex->distance_from_source;
153 void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
160 scalar_t residual_error = 0.0;
162 for(int n = 0; n < _nb_vertices; n++) {
163 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
164 if(e->work_length < 0) {
166 residual_error -= e->work_length;
168 e->work_length = 0.0;
173 cerr << "residual_error " << residual_error << endl;
176 for(int v = 0; v < _nb_vertices; v++) {
177 vertices[v].distance_from_source = FLT_MAX;
178 vertices[v].pred_vertex = 0;
179 vertices[v].pred_edge = 0;
180 vertices[v].iteration = 0;
185 int _front_size = 0, _new_front_size;
186 _front[_front_size++] = _source;
187 _source->distance_from_source = 0;
192 for(int f = 0; f < _front_size; f++) {
194 for(Edge *e = v->root_edge; e; e = e->next) {
195 d = v->distance_from_source + e->work_length;
196 tv = e->terminal_vertex;
197 if(d < tv->distance_from_source) {
198 tv->distance_from_source = d;
201 if(tv->iteration < iteration) {
202 _new_front[_new_front_size++] = tv;
203 tv->iteration = iteration;
209 tmp_front = _new_front;
213 tmp_front_size = _new_front_size;
214 _new_front_size = _front_size;
215 _front_size = tmp_front_size;
216 } while(_front_size > 0);
219 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
220 scalar_t total_length;
222 for(int e = 0; e < _nb_edges; e++) {
223 edges[e].length = lengths[e];
226 initialize_work_lengths();
230 find_shortest_path(_front, _new_front);
231 update_work_length();
233 // Do we reach the _sink?
234 if(_sink->pred_edge) {
236 // If yes, compute the length of the best path
237 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
238 total_length += v->pred_edge->length;
241 // If that length is negative
242 if(total_length < 0.0) {
243 // Invert all the edges along the best path
244 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
245 Edge *e = v->pred_edge;
246 e->terminal_vertex = v->pred_vertex;
247 e->occupied = 1 - e->occupied;
248 e->length = - e->length;
249 e->work_length = - e->work_length;
250 v->pred_vertex->del_edge(e);
255 } while(total_length < 0.0);
257 for(int n = 0; n < _nb_vertices; n++) {
258 Vertex *v = &vertices[n];
259 for(Edge *e = v->root_edge; e; e = e->next) {
260 result_edge_occupation[e->id] = e->occupied;