+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation. //
+// //
+// This program is distributed in the hope that it will be useful, but //
+// WITHOUT ANY WARRANTY; without even the implied warranty of //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
+// General Public License for more details. //
+// //
+// You should have received a copy of the GNU General Public License //
+// along with this program. If not, see <http://www.gnu.org/licenses/>. //
+// //
+// Written by and Copyright (C) Francois Fleuret //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports //
+///////////////////////////////////////////////////////////////////////////
+
+#include "mtp_graph.h"
+
+#include <iostream>
+#include <float.h>
+
+using namespace std;
+
+class Edge {
+public:
+ int id, occupied;
+ scalar_t length, work_length;
+ Vertex *terminal_vertex;
+ Edge *next, *pred;
+};
+
+class Vertex {
+public:
+ int id, iteration;
+ Edge *root_edge;
+ scalar_t distance_from_source;
+ Vertex *pred_vertex;
+ Edge *pred_edge;
+
+ Vertex() { root_edge = 0; }
+
+ inline void add_edge(Edge *e) {
+ e->next = root_edge;
+ e->pred = 0;
+ if(root_edge) { root_edge->pred = e; }
+ root_edge = e;
+ }
+
+ inline void del_edge(Edge *e) {
+ if(e == root_edge) { root_edge = e->next; }
+ if(e->pred) { e->pred->next = e->next; }
+ if(e->next) { e->next->pred = e->pred; }
+ }
+};
+
+void MTPGraph::print() {
+ for(int n = 0; n < _nb_vertices; n++) {
+ for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+ cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
+ if(e->occupied) {
+ cout << " *";
+ }
+ cout << endl;
+ }
+ }
+}
+
+MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
+ int *from, int *to,
+ int src, int snk) {
+ _nb_vertices = nb_vertices;
+ _nb_edges = nb_edges;
+
+ edge_heap = new Edge[_nb_edges];
+ vertices = new Vertex[_nb_vertices];
+
+ source = &vertices[src];
+ sink = &vertices[snk];
+
+ for(int v = 0; v < _nb_vertices; v++) {
+ vertices[v].id = v;
+ }
+
+ for(int e = 0; e < nb_edges; e++) {
+ vertices[from[e]].add_edge(&edge_heap[e]);
+ edge_heap[e].occupied = 0;
+ edge_heap[e].id = e;
+ edge_heap[e].terminal_vertex = &vertices[to[e]];
+ }
+}
+
+MTPGraph::~MTPGraph() {
+ delete[] vertices;
+ delete[] edge_heap;
+}
+
+void MTPGraph::initialize_work_lengths() {
+ scalar_t length_min = 0;
+ for(int n = 0; n < _nb_vertices; n++) {
+ for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+ length_min = min(e->length, length_min);
+ }
+ }
+ for(int n = 0; n < _nb_vertices; n++) {
+ for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+ e->work_length = e->length - length_min;
+ }
+ }
+}
+
+void MTPGraph::update_work_length() {
+ for(int n = 0; n < _nb_vertices; n++) {
+ scalar_t d = vertices[n].distance_from_source;
+ for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+ e->work_length += d - e->terminal_vertex->distance_from_source;
+ }
+ }
+}
+
+void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
+ Vertex **tmp_front;
+ int tmp_front_size;
+ Vertex *v, *tv;
+ scalar_t d;
+
+#ifdef VERBOSE
+ scalar_t residual_error = 0.0;
+#endif
+ for(int n = 0; n < _nb_vertices; n++) {
+ for(Edge *e = vertices[n].root_edge; e; e = e->next) {
+ if(e->work_length < 0) {
+#ifdef VERBOSE
+ residual_error -= e->work_length;
+#endif
+ e->work_length = 0.0;
+ }
+ }
+ }
+#ifdef VERBOSE
+ cerr << "residual_error " << residual_error << endl;
+#endif
+
+ for(int v = 0; v < _nb_vertices; v++) {
+ vertices[v].distance_from_source = FLT_MAX;
+ vertices[v].pred_vertex = 0;
+ vertices[v].pred_edge = 0;
+ vertices[v].iteration = 0;
+ }
+
+ int iteration = 0;
+
+ int front_size = 0, new_front_size;
+ front[front_size++] = source;
+ source->distance_from_source = 0;
+
+ do {
+ new_front_size = 0;
+ iteration++;
+ for(int f = 0; f < front_size; f++) {
+ v = front[f];
+ for(Edge *e = v->root_edge; e; e = e->next) {
+ d = v->distance_from_source + e->work_length;
+ tv = e->terminal_vertex;
+ if(d < tv->distance_from_source) {
+ tv->distance_from_source = d;
+ tv->pred_vertex = v;
+ tv->pred_edge = e;
+ if(tv->iteration < iteration) {
+ new_front[new_front_size++] = tv;
+ tv->iteration = iteration;
+ }
+ }
+ }
+ }
+
+ tmp_front = new_front;
+ new_front = front;
+ front = tmp_front;
+
+ tmp_front_size = new_front_size;
+ new_front_size = front_size;
+ front_size = tmp_front_size;
+ } while(front_size > 0);
+}
+
+void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
+ Vertex **front = new Vertex *[_nb_vertices];
+ Vertex **new_front = new Vertex *[_nb_vertices];
+
+ scalar_t total_length;
+
+ for(int e = 0; e < _nb_edges; e++) {
+ edge_heap[e].length = lengths[e];
+ }
+
+ initialize_work_lengths();
+
+ do {
+ total_length = 0.0;
+ find_shortest_path(front, new_front);
+ update_work_length();
+
+ // Do we reach the sink?
+ if(sink->pred_edge) {
+
+ // If yes, compute the length of the best path
+ for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+ total_length += v->pred_edge->length;
+ }
+
+ // If that length is negative
+ if(total_length < 0.0) {
+ // Invert all the edges along the best path
+ for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+ Edge *e = v->pred_edge;
+ e->terminal_vertex = v->pred_vertex;
+ e->occupied = 1 - e->occupied;
+ e->length = - e->length;
+ e->work_length = - e->work_length;
+ v->pred_vertex->del_edge(e);
+ v->add_edge(e);
+ }
+ }
+ }
+ } while(total_length < 0.0);
+
+ delete[] front;
+ delete[] new_front;
+
+ for(int n = 0; n < _nb_vertices; n++) {
+ Vertex *v = &vertices[n];
+ for(Edge *e = v->root_edge; e; e = e->next) {
+ result_edge_occupation[e->id] = e->occupied;
+ }
+ }
+}