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::find_shortest_path(Vertex **_front, Vertex **_new_front) {
151 scalar_t residual_error = 0.0;
153 for(int n = 0; n < _nb_vertices; n++) {
154 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
155 if(e->work_length < 0) {
157 residual_error -= e->work_length;
159 e->work_length = 0.0;
164 cerr << "residual_error " << residual_error << endl;
167 for(int v = 0; v < _nb_vertices; v++) {
168 vertices[v].distance_from_source = FLT_MAX;
169 vertices[v].pred_vertex = 0;
170 vertices[v].pred_edge = 0;
171 vertices[v].iteration = 0;
176 int _front_size = 0, _new_front_size;
177 _front[_front_size++] = _source;
178 _source->distance_from_source = 0;
183 for(int f = 0; f < _front_size; f++) {
185 for(Edge *e = v->root_edge; e; e = e->next) {
186 d = v->distance_from_source + e->work_length;
187 tv = e->terminal_vertex;
188 if(d < tv->distance_from_source) {
189 tv->distance_from_source = d;
192 if(tv->iteration < iteration) {
193 _new_front[_new_front_size++] = tv;
194 tv->iteration = iteration;
200 tmp_front = _new_front;
204 tmp_front_size = _new_front_size;
205 _new_front_size = _front_size;
206 _front_size = tmp_front_size;
207 } while(_front_size > 0);
210 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
211 scalar_t total_length;
213 for(int e = 0; e < _nb_edges; e++) {
214 edges[e].length = lengths[e];
217 initialize_work_lengths();
221 find_shortest_path(_front, _new_front);
222 update_work_lengths();
224 // Do we reach the _sink?
225 if(_sink->pred_edge) {
227 // If yes, compute the length of the best path
228 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
229 total_length += v->pred_edge->length;
232 // If that length is negative
233 if(total_length < 0.0) {
234 // Invert all the edges along the best path
235 for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
236 Edge *e = v->pred_edge;
237 e->terminal_vertex = v->pred_vertex;
238 e->occupied = 1 - e->occupied;
239 e->length = - e->length;
240 e->work_length = - e->work_length;
241 v->pred_vertex->del_edge(e);
246 } while(total_length < 0.0);
248 for(int n = 0; n < _nb_vertices; n++) {
249 Vertex *v = &vertices[n];
250 for(Edge *e = v->root_edge; e; e = e->next) {
251 result_edge_occupation[e->id] = e->occupied;