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 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
73 _nb_vertices = nb_vertices;
76 edge_heap = new Edge[_nb_edges];
77 vertices = new Vertex[_nb_vertices];
79 source = &vertices[src];
80 sink = &vertices[snk];
82 for(int v = 0; v < _nb_vertices; v++) {
86 for(int e = 0; e < nb_edges; e++) {
87 vertices[from[e]].add_edge(&edge_heap[e]);
88 edge_heap[e].occupied = 0;
90 edge_heap[e].terminal_vertex = &vertices[to[e]];
94 MTPGraph::~MTPGraph() {
99 void MTPGraph::initialize_work_lengths() {
100 scalar_t length_min = 0;
101 for(int n = 0; n < _nb_vertices; n++) {
102 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
103 length_min = min(e->length, length_min);
106 for(int n = 0; n < _nb_vertices; n++) {
107 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
108 e->work_length = e->length - length_min;
113 void MTPGraph::update_work_length() {
114 for(int n = 0; n < _nb_vertices; n++) {
115 scalar_t d = vertices[n].distance_from_source;
116 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
117 e->work_length += d - e->terminal_vertex->distance_from_source;
122 void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
129 scalar_t residual_error = 0.0;
131 for(int n = 0; n < _nb_vertices; n++) {
132 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
133 if(e->work_length < 0) {
135 residual_error -= e->work_length;
137 e->work_length = 0.0;
142 cerr << "residual_error " << residual_error << endl;
145 for(int v = 0; v < _nb_vertices; v++) {
146 vertices[v].distance_from_source = FLT_MAX;
147 vertices[v].pred_vertex = 0;
148 vertices[v].pred_edge = 0;
149 vertices[v].iteration = 0;
154 int front_size = 0, new_front_size;
155 front[front_size++] = source;
156 source->distance_from_source = 0;
161 for(int f = 0; f < front_size; f++) {
163 for(Edge *e = v->root_edge; e; e = e->next) {
164 d = v->distance_from_source + e->work_length;
165 tv = e->terminal_vertex;
166 if(d < tv->distance_from_source) {
167 tv->distance_from_source = d;
170 if(tv->iteration < iteration) {
171 new_front[new_front_size++] = tv;
172 tv->iteration = iteration;
178 tmp_front = new_front;
182 tmp_front_size = new_front_size;
183 new_front_size = front_size;
184 front_size = tmp_front_size;
185 } while(front_size > 0);
188 void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
189 Vertex **front = new Vertex *[_nb_vertices];
190 Vertex **new_front = new Vertex *[_nb_vertices];
192 scalar_t total_length;
194 for(int e = 0; e < _nb_edges; e++) {
195 edge_heap[e].length = lengths[e];
198 initialize_work_lengths();
202 find_shortest_path(front, new_front);
203 update_work_length();
205 // Do we reach the sink?
206 if(sink->pred_edge) {
208 // If yes, compute the length of the best path
209 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
210 total_length += v->pred_edge->length;
213 // If that length is negative
214 if(total_length < 0.0) {
215 // Invert all the edges along the best path
216 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
217 Edge *e = v->pred_edge;
218 e->terminal_vertex = v->pred_vertex;
219 e->occupied = 1 - e->occupied;
220 e->length = - e->length;
221 e->work_length = - e->work_length;
222 v->pred_vertex->del_edge(e);
227 } while(total_length < 0.0);
232 for(int n = 0; n < _nb_vertices; n++) {
233 Vertex *v = &vertices[n];
234 for(Edge *e = v->root_edge; e; e = e->next) {
235 result_edge_occupation[e->id] = e->occupied;