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);
57 //////////////////////////////////////////////////////////////////////
61 positivized_length = - positivized_length;
62 origin_vertex->del_leaving_edge(this);
63 terminal_vertex->add_leaving_edge(this);
64 Vertex *t = terminal_vertex;
65 terminal_vertex = origin_vertex;
69 //////////////////////////////////////////////////////////////////////
75 void Vertex::add_leaving_edge(Edge *e) {
76 e->next_leaving_edge = leaving_edges;
77 e->pred_leaving_edge = 0;
78 if(leaving_edges) { leaving_edges->pred_leaving_edge = e; }
82 void Vertex::del_leaving_edge(Edge *e) {
83 if(e == leaving_edges) {
84 leaving_edges = e->next_leaving_edge;
86 if(e->pred_leaving_edge) {
87 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
89 if(e->next_leaving_edge) {
90 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
94 //////////////////////////////////////////////////////////////////////
96 static int compare_vertex(const void *v1, const void *v2) {
98 (*((Vertex **) v1))->distance_from_source -
99 (*((Vertex **) v2))->distance_from_source;
100 if(delta < 0) return -1;
101 else if(delta > 0) return 1;
105 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
106 int *vertex_from, int *vertex_to,
107 int source, int sink) {
108 _nb_vertices = nb_vertices;
109 _nb_edges = nb_edges;
111 _edges = new Edge[_nb_edges];
112 _vertices = new Vertex[_nb_vertices];
113 _heap = new Vertex *[_nb_vertices];
114 _dp_order = new Vertex *[_nb_vertices];
116 _source = &_vertices[source];
117 _sink = &_vertices[sink];
119 for(int e = 0; e < nb_edges; e++) {
120 _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
121 _edges[e].occupied = 0;
122 _edges[e].origin_vertex = _vertices + vertex_from[e];
123 _edges[e].terminal_vertex = _vertices + vertex_to[e];
126 for(int v = 0; v < _nb_vertices; v++) {
127 _heap[v] = &_vertices[v];
128 _vertices[v].heap_slot = &_heap[v];
134 if(compute_dp_distances()) {
135 // Here the distance_from_source field of every vertex is the
136 // number of DP iterations needed to update it. Hence we only have
137 // to process the vertex in that order.
138 for(int v = 0; v < _nb_vertices; v++) { _dp_order[v] = &_vertices[v]; }
139 qsort(_dp_order, _nb_vertices, sizeof(Vertex *), compare_vertex);
141 cerr << __FILE__ << ": This graph is not a DAG." << endl;
146 MTPGraph::~MTPGraph() {
151 for(int p = 0; p < nb_paths; p++) delete paths[p];
155 //////////////////////////////////////////////////////////////////////
157 void MTPGraph::print(ostream *os) {
158 for(int k = 0; k < _nb_edges; k++) {
159 Edge *e = _edges + k;
160 (*os) << e->origin_vertex - _vertices
162 << e->terminal_vertex - _vertices
172 void MTPGraph::print_dot(ostream *os) {
173 (*os) << "digraph {" << endl;
174 (*os) << " rankdir=\"LR\";" << endl;
175 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
176 (*os) << " edge [color=gray,arrowhead=open]" << endl;
177 (*os) << " " << _source - _vertices << " [peripheries=2];" << endl;
178 (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl;
179 for(int k = 0; k < _nb_edges; k++) {
180 Edge *e = _edges + k;
182 << e->origin_vertex - _vertices
184 << e->terminal_vertex - _vertices
187 (*os) << "style=bold,color=black,";
189 (*os) << "label=\"" << e->length << "\"];" << endl;
191 (*os) << "}" << endl;
194 //////////////////////////////////////////////////////////////////////
196 void MTPGraph::update_positivized_lengths() {
197 for(int k = 0; k < _nb_edges; k++) {
198 Edge *e = _edges + k;
199 e->positivized_length +=
200 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
204 void MTPGraph::force_positivized_lengths() {
206 scalar_t residual_error = 0.0;
207 scalar_t max_error = 0.0;
209 for(int k = 0; k < _nb_edges; k++) {
210 Edge *e = _edges + k;
212 if(e->positivized_length < 0) {
215 residual_error -= e->positivized_length;
216 max_error = max(max_error, - e->positivized_length);
218 e->positivized_length = 0.0;
222 cerr << __FILE__ << ": residual_error " << residual_error << " max_error " << residual_error << endl;
226 int MTPGraph::compute_dp_distances() {
230 // This procedure computes for each node the longest link from the
231 // source and abort if the graph is not a DAG. It works by removing
232 // successively nodes without predecessor: At the first iteration it
233 // removes the source, then the nodes with incoming edge only from
234 // the source, etc. If it can remove all the nodes that way, the
235 // graph is a DAG. If at some point it can not remove node anymore
236 // and there are some remaining nodes, the graph is not a DAG.
238 Vertex **active = new Vertex *[_nb_vertices];
240 // All the nodes are active at first
241 for(int k = 0; k < _nb_vertices; k++) {
242 _vertices[k].distance_from_source = 0;
243 active[k] = &_vertices[k];
246 scalar_t nb_iterations = 1;
247 int nb_active = _nb_vertices, pred_nb_active;
250 // We set the distance_from_source field of all the vertices with incoming
251 // edges to the current nb_iterations value
252 for(int f = 0; f < nb_active; f++) {
254 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
255 e->terminal_vertex->distance_from_source = nb_iterations;
259 pred_nb_active = nb_active;
262 // We keep all the vertices with incoming nodes
263 for(int f = 0; f < pred_nb_active; f++) {
265 if(v->distance_from_source == nb_iterations) {
266 active[nb_active++] = v;
271 } while(nb_active < pred_nb_active);
275 return nb_active == 0;
278 void MTPGraph::decrease_distance_in_heap(Vertex *v) {
280 // There is some beauty in that
283 (p = _heap + (h - _heap + 1) / 2 - 1,
284 (*p)->distance_from_source > (*h)->distance_from_source)) {
286 swap((*p)->heap_slot, (*h)->heap_slot);
291 void MTPGraph::increase_distance_in_heap(Vertex *v) {
292 Vertex **c1, **c2, **h;
293 // omg, that's beautiful
295 while(c1 = _heap + 2 * (h - _heap + 1) - 1, c2 = c1 + 1,
296 (c1 < _heap + _heap_size && (*c1)->distance_from_source < (*h)->distance_from_source)
298 (c2 < _heap + _heap_size && (*c2)->distance_from_source < (*h)->distance_from_source)
300 if(c1 < _heap + _heap_size &&
301 !(c2 < _heap + _heap_size && (*c2)->distance_from_source < (*c1)->distance_from_source)){
303 swap((*c1)->heap_slot, (*h)->heap_slot);
307 swap((*c2)->heap_slot, (*h)->heap_slot);
313 void MTPGraph::dp_compute_distances() {
318 for(int k = 0; k < _nb_vertices; k++) {
319 _vertices[k].distance_from_source = FLT_MAX;
320 _vertices[k].pred_edge_toward_source = 0;
323 _source->distance_from_source = 0;
325 for(int k = 0; k < _nb_vertices; k++) {
327 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
328 d = v->distance_from_source + e->positivized_length;
329 tv = e->terminal_vertex;
330 if(d < tv->distance_from_source) {
331 tv->distance_from_source = d;
332 tv->pred_edge_toward_source = e;
333 decrease_distance_in_heap(tv);
339 // This method does not change the edge occupation. It only sets
340 // properly, for every vertex, the fields distance_from_source and
341 // pred_edge_toward_source.
343 void MTPGraph::find_shortest_path() {
344 Vertex *v, *tv, **a, **b;
348 for(int k = 0; k < _nb_vertices; k++) {
349 _vertices[k].distance_from_source = FLT_MAX;
350 _vertices[k].pred_edge_toward_source = 0;
353 _heap_size = _nb_vertices;
354 _source->distance_from_source = 0;
355 decrease_distance_in_heap(_source);
358 // Get the closest to the source
361 // Remove it from the heap (swap it with the last in the heap, and
362 // update the distance of that one)
365 b = _heap + _heap_size;
366 swap(*a, *b); swap((*a)->heap_slot, (*b)->heap_slot);
367 increase_distance_in_heap(_heap[0]);
369 // Now update the neighbors of the currently closest to the source
370 for(e = v->leaving_edges; e; e = e->next_leaving_edge) {
371 d = v->distance_from_source + e->positivized_length;
372 tv = e->terminal_vertex;
373 if(d < tv->distance_from_source) {
374 ASSERT(tv->heap_slot - _heap < _heap_size);
375 tv->distance_from_source = d;
376 tv->pred_edge_toward_source = e;
377 decrease_distance_in_heap(tv);
380 } while(_heap_size > 0);
383 void MTPGraph::find_best_paths(scalar_t *lengths) {
384 scalar_t total_length;
388 for(int e = 0; e < _nb_edges; e++) {
389 _edges[e].length = lengths[e];
390 _edges[e].occupied = 0;
391 _edges[e].positivized_length = _edges[e].length;
394 // Update the distance to the source in "good order"
396 dp_compute_distances();
399 update_positivized_lengths();
400 force_positivized_lengths();
401 find_shortest_path();
405 // Do we reach the sink?
406 if(_sink->pred_edge_toward_source) {
407 // If yes, compute the length of the best path according to the
408 // original edge lengths
410 while(v->pred_edge_toward_source) {
411 total_length += v->pred_edge_toward_source->length;
412 v = v->pred_edge_toward_source->origin_vertex;
414 // If that length is negative
415 if(total_length < 0.0) {
417 cerr << __FILE__ << ": Found a path of length " << total_length << endl;
419 // Invert all the edges along the best path
421 while(v->pred_edge_toward_source) {
422 e = v->pred_edge_toward_source;
423 v = e->origin_vertex;
425 // This is the only place where we change the occupations of
427 e->occupied = 1 - e->occupied;
432 } while(total_length < 0.0);
434 // Put back the graph in its original state (i.e. invert edges which
435 // have been inverted in the process)
436 for(int k = 0; k < _nb_edges; k++) {
438 if(e->occupied) { e->invert(); }
442 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
444 int l = 0, nb_occupied_next;
447 path->nodes[l++] = e->origin_vertex - _vertices;
448 path->length = e->length;
451 while(e->terminal_vertex != _sink) {
453 path->nodes[l++] = e->terminal_vertex - _vertices;
454 path->length += e->length;
457 nb_occupied_next = 0;
458 for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) {
459 if(f->occupied) { nb_occupied_next++; next = f; }
463 if(nb_occupied_next == 0) {
464 cerr << __FILE__ << ": retrieve_one_path: Non-sink end point." << endl;
468 else if(nb_occupied_next > 1) {
469 cerr << __FILE__ << ": retrieve_one_path: Non node-disjoint paths." << endl;
478 path->nodes[l++] = e->terminal_vertex - _vertices;
479 path->length += e->length;
485 void MTPGraph::retrieve_disjoint_paths() {
489 for(int p = 0; p < nb_paths; p++) delete paths[p];
493 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
494 if(e->occupied) { nb_paths++; }
497 paths = new Path *[nb_paths];
500 for(e = _source->leaving_edges; e; e = e->next_leaving_edge) {
502 l = retrieve_one_path(e, 0);
503 paths[p] = new Path(l);
504 retrieve_one_path(e, paths[p]);