3 * mtp is the ``Multi Tracked Paths'', an implementation of the
4 * k-shortest paths algorithm for multi-target tracking.
6 * Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of mtp.
11 * mtp is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * mtp is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 * License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
25 #include "mtp_graph.h"
34 scalar_t length, positivized_length;
35 Vertex *origin_vertex, *terminal_vertex;
37 // These fields are used for the linked list of a vertex's leaving
38 // edge list. We have to do insertions / deletions.
39 Edge *next_leaving_edge, *pred_leaving_edge;
47 scalar_t distance_from_source;
48 Edge *pred_edge_toward_source;
53 inline void add_leaving_edge(Edge *e);
54 inline void del_leaving_edge(Edge *e);
55 inline void decrease_distance_in_heap(Vertex **heap);
56 inline void increase_distance_in_heap(Vertex **heap, int heap_size);
59 //////////////////////////////////////////////////////////////////////
63 positivized_length = - positivized_length;
64 origin_vertex->del_leaving_edge(this);
65 terminal_vertex->add_leaving_edge(this);
66 Vertex *t = terminal_vertex;
67 terminal_vertex = origin_vertex;
71 //////////////////////////////////////////////////////////////////////
77 void Vertex::add_leaving_edge(Edge *e) {
78 e->next_leaving_edge = leaving_edges;
79 e->pred_leaving_edge = 0;
80 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
84 void Vertex::del_leaving_edge(Edge *e) {
85 if(e == leaving_edges) {
86 leaving_edges = e->next_leaving_edge;
88 if(e->pred_leaving_edge) {
89 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
91 if(e->next_leaving_edge) {
92 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
96 void Vertex::decrease_distance_in_heap(Vertex **heap) {
98 // There is some beauty in that
101 (p = heap + (h - heap + 1) / 2 - 1,
102 (*p)->distance_from_source > (*h)->distance_from_source)) {
104 swap((*p)->heap_slot, (*h)->heap_slot);
109 void Vertex::increase_distance_in_heap(Vertex **heap, int heap_size) {
110 Vertex **c1, **c2, **h;
111 // omg, that's beautiful
113 while(c1 = heap + 2 * (h - heap) + 1,
114 c1 < heap + heap_size &&
116 (*c1)->distance_from_source < (*h)->distance_from_source
118 (c2 < heap + heap_size && (*c2)->distance_from_source < (*h)->distance_from_source)
120 if(c2 < heap + heap_size && (*c2)->distance_from_source <= (*c1)->distance_from_source) {
122 swap((*c2)->heap_slot, (*h)->heap_slot);
126 swap((*c1)->heap_slot, (*h)->heap_slot);
132 //////////////////////////////////////////////////////////////////////
134 static int compare_vertex(const void *v1, const void *v2) {
136 (*((Vertex **) v1))->distance_from_source -
137 (*((Vertex **) v2))->distance_from_source;
138 if(delta < 0) return -1;
139 else if(delta > 0) return 1;
143 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
144 int *vertex_from, int *vertex_to,
145 int source, int sink) {
146 _nb_vertices = nb_vertices;
147 _nb_edges = nb_edges;
149 _edges = new Edge[_nb_edges];
150 _vertices = new Vertex[_nb_vertices];
151 _heap = new Vertex *[_nb_vertices];
152 _dp_order = new Vertex *[_nb_vertices];
154 _source = &_vertices[source];
155 _sink = &_vertices[sink];
157 for(int e = 0; e < nb_edges; e++) {
158 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
159 _edges[e].occupied = 0;
160 _edges[e].origin_vertex = _vertices + vertex_from[e];
161 _edges[e].terminal_vertex = _vertices + vertex_to[e];
164 for(int v = 0; v < _nb_vertices; v++) {
165 _heap[v] = &_vertices[v];
166 _vertices[v].heap_slot = &_heap[v];
172 if(compute_dp_distances()) {
173 // Here the distance_from_source field of every vertex is the
174 // number of DP iterations needed to update it. Hence we only have
175 // to process the vertex in that order.
176 for(int v = 0; v < _nb_vertices; v++) { _dp_order[v] = &_vertices[v]; }
177 qsort(_dp_order, _nb_vertices, sizeof(Vertex *), compare_vertex);
179 cerr << __FILE__ << ": This graph is not a DAG." << endl;
184 MTPGraph::~MTPGraph() {
189 for(int p = 0; p < nb_paths; p++) delete paths[p];
193 //////////////////////////////////////////////////////////////////////
195 void MTPGraph::print(ostream *os) {
196 for(int k = 0; k < _nb_edges; k++) {
197 Edge *e = _edges + k;
198 (*os) << e->origin_vertex - _vertices
200 << e->terminal_vertex - _vertices
210 void MTPGraph::print_dot(ostream *os) {
211 (*os) << "digraph {" << endl;
212 (*os) << " rankdir=\"LR\";" << endl;
213 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
214 (*os) << " edge [color=gray,arrowhead=open]" << endl;
215 (*os) << " " << _source - _vertices << " [peripheries=2];" << endl;
216 (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl;
217 for(int k = 0; k < _nb_edges; k++) {
218 Edge *e = _edges + k;
220 << e->origin_vertex - _vertices
222 << e->terminal_vertex - _vertices
225 (*os) << "style=bold,color=black,";
227 (*os) << "label=\"" << e->length << "\"];" << endl;
229 (*os) << "}" << endl;
232 //////////////////////////////////////////////////////////////////////
234 void MTPGraph::update_positivized_lengths() {
235 for(int k = 0; k < _nb_edges; k++) {
236 Edge *e = _edges + k;
237 e->positivized_length +=
238 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
242 void MTPGraph::force_positivized_lengths() {
244 scalar_t residual_error = 0.0;
245 scalar_t max_error = 0.0;
247 for(int k = 0; k < _nb_edges; k++) {
248 Edge *e = _edges + k;
250 if(e->positivized_length < 0) {
253 residual_error -= e->positivized_length;
254 max_error = max(max_error, - e->positivized_length);
256 e->positivized_length = 0.0;
260 cerr << __FILE__ << ": residual_error " << residual_error << " max_error " << residual_error << endl;
264 int MTPGraph::compute_dp_distances() {
268 // This procedure computes for each node the longest link from the
269 // source and abort if the graph is not a DAG. It works by removing
270 // successively nodes without predecessor: At the first iteration it
271 // removes the source, then the nodes with incoming edge only from
272 // the source, etc. If it can remove all the nodes that way, the
273 // graph is a DAG. If at some point it can not remove node anymore
274 // and there are some remaining nodes, the graph is not a DAG.
276 Vertex **active = new Vertex *[_nb_vertices];
278 // All the nodes are active at first
279 for(int k = 0; k < _nb_vertices; k++) {
280 _vertices[k].distance_from_source = 0;
281 active[k] = &_vertices[k];
284 scalar_t nb_iterations = 1;
285 int nb_active = _nb_vertices, pred_nb_active;
288 // We set the distance_from_source field of all the vertices with incoming
289 // edges to the current nb_iterations value
290 for(int f = 0; f < nb_active; f++) {
292 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
293 e->terminal_vertex->distance_from_source = nb_iterations;
297 pred_nb_active = nb_active;
300 // We keep all the vertices with incoming nodes
301 for(int f = 0; f < pred_nb_active; f++) {
303 if(v->distance_from_source == nb_iterations) {
304 active[nb_active++] = v;
309 } while(nb_active < pred_nb_active);
313 return nb_active == 0;
316 void MTPGraph::dp_compute_distances() {
321 for(int k = 0; k < _nb_vertices; k++) {
322 _vertices[k].distance_from_source = FLT_MAX;
323 _vertices[k].pred_edge_toward_source = 0;
326 _source->distance_from_source = 0;
328 for(int k = 0; k < _nb_vertices; k++) {
330 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
331 d = v->distance_from_source + e->positivized_length;
332 tv = e->terminal_vertex;
333 if(d < tv->distance_from_source) {
334 tv->distance_from_source = d;
335 tv->pred_edge_toward_source = e;
336 tv->decrease_distance_in_heap(_heap);
342 // This method does not change the edge occupation. It only sets
343 // properly, for every vertex, the fields distance_from_source and
344 // pred_edge_toward_source.
346 void MTPGraph::find_shortest_path() {
347 Vertex *v, *tv, **a, **b;
351 for(int k = 0; k < _nb_vertices; k++) {
352 _vertices[k].distance_from_source = FLT_MAX;
353 _vertices[k].pred_edge_toward_source = 0;
356 _heap_size = _nb_vertices;
357 _source->distance_from_source = 0;
358 _source->decrease_distance_in_heap(_heap);
361 // Get the closest to the source
364 // Remove it from the heap (swap it with the last in the heap, and
365 // update the distance of that one)
368 b = _heap + _heap_size;
369 swap(*a, *b); swap((*a)->heap_slot, (*b)->heap_slot);
370 _heap[0]->increase_distance_in_heap(_heap, _heap_size);
372 // Now update the neighbors of the currently closest to the source
373 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
374 d = v->distance_from_source + e->positivized_length;
375 tv = e->terminal_vertex;
376 if(d < tv->distance_from_source) {
377 ASSERT(tv->heap_slot - _heap < _heap_size);
378 tv->distance_from_source = d;
379 tv->pred_edge_toward_source = e;
380 tv->decrease_distance_in_heap(_heap);
383 } while(_heap_size > 0);
386 void MTPGraph::find_best_paths(scalar_t *lengths) {
387 scalar_t total_length;
391 for(int e = 0; e < _nb_edges; e++) {
392 _edges[e].length = lengths[e];
393 _edges[e].occupied = 0;
394 _edges[e].positivized_length = _edges[e].length;
397 // Update the distances to the source in "good order"
398 dp_compute_distances();
401 update_positivized_lengths();
402 force_positivized_lengths();
403 find_shortest_path();
407 // Do we reach the sink?
408 if(_sink->pred_edge_toward_source) {
409 // If yes, compute the length of the best path according to the
410 // original edge lengths
412 while(v->pred_edge_toward_source) {
413 total_length += v->pred_edge_toward_source->length;
414 v = v->pred_edge_toward_source->origin_vertex;
416 // If that length is negative
417 if(total_length < 0.0) {
419 cerr << __FILE__ << ": Found a path of length " << total_length << endl;
421 // Invert all the edges along the best path
423 while(v->pred_edge_toward_source) {
424 e = v->pred_edge_toward_source;
425 v = e->origin_vertex;
427 // This is the only place where we change the occupations of
429 e->occupied = 1 - e->occupied;
434 } while(total_length < 0.0);
436 // Put back the graph in its original state (i.e. invert edges which
437 // have been inverted in the process)
438 for(int k = 0; k < _nb_edges; k++) {
440 if(e->occupied) { e->invert(); }
444 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
446 int l = 0, nb_occupied_next;
449 path->nodes[l++] = e->origin_vertex - _vertices;
450 path->length = e->length;
453 while(e->terminal_vertex != _sink) {
455 path->nodes[l++] = e->terminal_vertex - _vertices;
456 path->length += e->length;
459 nb_occupied_next = 0;
460 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
461 if(f->occupied) { nb_occupied_next++; next = f; }
465 if(nb_occupied_next == 0) {
466 cerr << __FILE__ << ": retrieve_one_path: Non-sink end point." << endl;
470 else if(nb_occupied_next > 1) {
471 cerr << __FILE__ << ": retrieve_one_path: Non node-disjoint paths." << endl;
480 path->nodes[l++] = e->terminal_vertex - _vertices;
481 path->length += e->length;
487 void MTPGraph::retrieve_disjoint_paths() {
491 for(int p = 0; p < nb_paths; p++) delete paths[p];
495 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
496 if(e->occupied) { nb_paths++; }
499 paths = new Path *[nb_paths];
502 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
504 l = retrieve_one_path(e, 0);
505 paths[p] = new Path(l);
506 retrieve_one_path(e, paths[p]);