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"
35 scalar_t length, positivized_length;
36 Vertex *origin_vertex, *terminal_vertex;
38 // These fields are used for the linked list of a vertex's leaving
39 // edge list. We have to do insertions / deletions.
40 Edge *next_leaving_edge, *pred_leaving_edge;
47 scalar_t distance_from_source;
48 Edge *pred_edge_toward_source;
50 Edge *leaving_edge_list_root;
55 inline void add_leaving_edge(Edge *e);
56 inline void del_leaving_edge(Edge *e);
57 inline void decrease_distance_in_heap(Vertex **heap);
58 inline void increase_distance_in_heap(Vertex **heap, Vertex **heap_bottom);
61 //////////////////////////////////////////////////////////////////////
65 positivized_length = - positivized_length;
66 origin_vertex->del_leaving_edge(this);
67 terminal_vertex->add_leaving_edge(this);
68 swap(terminal_vertex, origin_vertex);
71 //////////////////////////////////////////////////////////////////////
74 leaving_edge_list_root = 0;
77 void Vertex::add_leaving_edge(Edge *e) {
78 e->next_leaving_edge = leaving_edge_list_root;
79 e->pred_leaving_edge = 0;
80 if(leaving_edge_list_root) {
81 leaving_edge_list_root->pred_leaving_edge = e;
83 leaving_edge_list_root = e;
86 void Vertex::del_leaving_edge(Edge *e) {
87 if(e == leaving_edge_list_root) {
88 leaving_edge_list_root = e->next_leaving_edge;
90 if(e->pred_leaving_edge) {
91 e->pred_leaving_edge->next_leaving_edge = e->next_leaving_edge;
93 if(e->next_leaving_edge) {
94 e->next_leaving_edge->pred_leaving_edge = e->pred_leaving_edge;
98 void Vertex::decrease_distance_in_heap(Vertex **heap) {
100 // There is some beauty in that
103 (p = heap + (h - heap + 1) / 2 - 1,
104 (*p)->distance_from_source > (*h)->distance_from_source)) {
106 swap((*p)->heap_slot, (*h)->heap_slot);
111 void Vertex::increase_distance_in_heap(Vertex **heap, Vertex **heap_bottom) {
112 Vertex **c1, **c2, **h;
113 // omg, that's beautiful
115 while(c1 = heap + 2 * (h - heap) + 1,
118 (*c1)->distance_from_source < (*h)->distance_from_source
120 (c2 < heap_bottom && (*c2)->distance_from_source < (*h)->distance_from_source)
122 if(c2 < heap_bottom && (*c2)->distance_from_source <= (*c1)->distance_from_source) {
124 swap((*c2)->heap_slot, (*h)->heap_slot);
128 swap((*c1)->heap_slot, (*h)->heap_slot);
134 //////////////////////////////////////////////////////////////////////
136 static int compare_vertices_on_distance(const void *v1, const void *v2) {
138 (*((Vertex **) v1))->distance_from_source -
139 (*((Vertex **) v2))->distance_from_source;
140 if(delta < 0) return -1;
141 else if(delta > 0) return 1;
145 //////////////////////////////////////////////////////////////////////
147 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
148 int *vertex_from, int *vertex_to,
149 int source, int sink) {
150 _nb_vertices = nb_vertices;
151 _nb_edges = nb_edges;
153 _edges = new Edge[_nb_edges];
154 _vertices = new Vertex[_nb_vertices];
155 _heap = new Vertex *[_nb_vertices];
156 _dp_order = new Vertex *[_nb_vertices];
158 _source = &_vertices[source];
159 _sink = &_vertices[sink];
161 for(int e = 0; e < nb_edges; e++) {
162 _vertices[vertex_from[e]].add_leaving_edge(&_edges[e]);
163 _edges[e].occupied = 0;
164 _edges[e].origin_vertex = &_vertices[vertex_from[e]];
165 _edges[e].terminal_vertex = &_vertices[vertex_to[e]];
168 for(int v = 0; v < _nb_vertices; v++) {
169 _heap[v] = &_vertices[v];
170 _vertices[v].heap_slot = &_heap[v];
177 for(int v = 0; v < _nb_vertices; v++) { _dp_order[v] = &_vertices[v]; }
178 qsort(_dp_order, _nb_vertices, sizeof(Vertex *), compare_vertices_on_distance);
181 MTPGraph::~MTPGraph() {
186 for(int p = 0; p < nb_paths; p++) delete paths[p];
190 //////////////////////////////////////////////////////////////////////
192 void MTPGraph::compute_dp_ranks() {
197 // This procedure computes for each node the longest link from the
198 // source and abort if the graph is not a DAG. It works by removing
199 // successively nodes without predecessor: At the first iteration it
200 // removes the source, then the nodes with incoming edge only from
201 // the source, etc. If it can remove all the nodes that way, the
202 // graph is a DAG. If at some point it can not remove node anymore
203 // and there are some remaining nodes, the graph is not a DAG. The
204 // rank of a node is the iteration at which is it removed, and we
205 // set the distance_from_source fields to this value.
207 int *nb_predecessors = new int[_nb_vertices];
208 Vertex **bunch = new Vertex *[_nb_vertices];
210 Vertex **already_processed = bunch, **front = bunch, **fresh = bunch;
212 for(int k = 0; k < _nb_vertices; k++) {
213 nb_predecessors[k] = 0;
216 for(int k = 0; k < _nb_vertices; k++) {
218 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
219 tv = int(e->terminal_vertex - _vertices);
220 nb_predecessors[tv]++;
224 for(int k = 0; k < _nb_vertices; k++) {
225 if(nb_predecessors[k] == 0) {
226 *(front++) = _vertices + k;
231 while(already_processed < front) {
233 while(already_processed < front) {
234 v = *(already_processed++);
235 v->distance_from_source = rank;
236 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
237 tv = int(e->terminal_vertex - _vertices);
238 nb_predecessors[tv]--;
239 ASSERT(nb_predecessors[tv] >= 0);
240 if(nb_predecessors[tv] == 0) {
241 *(fresh++) = e->terminal_vertex;
249 if(already_processed < bunch + _nb_vertices) {
250 cerr << __FILE__ << ": The graph is not a DAG." << endl;
254 delete[] nb_predecessors;
258 //////////////////////////////////////////////////////////////////////
260 void MTPGraph::print(ostream *os) {
261 for(int k = 0; k < _nb_edges; k++) {
262 Edge *e = &_edges[k];
263 (*os) << e->origin_vertex - _vertices
265 << e->terminal_vertex - _vertices
266 << " (" << e->length << ")";
267 if(e->occupied) { (*os) << " *"; }
272 void MTPGraph::print_dot(ostream *os) {
273 (*os) << "digraph {" << endl;
274 (*os) << " rankdir=\"LR\";" << endl;
275 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
276 (*os) << " edge [color=gray,arrowhead=open]" << endl;
277 (*os) << " " << _source - _vertices << " [peripheries=2];" << endl;
278 (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl;
279 for(int k = 0; k < _nb_edges; k++) {
280 Edge *e = &_edges[k];
282 << e->origin_vertex - _vertices
284 << e->terminal_vertex - _vertices
287 (*os) << "style=bold,color=black,";
289 (*os) << "label=\"" << e->length << "\"];" << endl;
291 (*os) << "}" << endl;
294 //////////////////////////////////////////////////////////////////////
296 void MTPGraph::update_positivized_lengths() {
297 for(int k = 0; k < _nb_edges; k++) {
298 Edge *e = &_edges[k];
299 e->positivized_length +=
300 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
304 void MTPGraph::force_positivized_lengths() {
306 scalar_t residual_error = 0.0;
307 scalar_t max_error = 0.0;
309 for(int k = 0; k < _nb_edges; k++) {
310 Edge *e = &_edges[k];
312 if(e->positivized_length < 0) {
314 residual_error -= e->positivized_length;
315 max_error = max(max_error, - e->positivized_length);
317 e->positivized_length = 0.0;
321 cerr << __FILE__ << ": residual_error " << residual_error << " max_error " << residual_error << endl;
325 void MTPGraph::dp_compute_distances() {
330 for(int k = 0; k < _nb_vertices; k++) {
331 _vertices[k].distance_from_source = FLT_MAX;
332 _vertices[k].pred_edge_toward_source = 0;
335 _source->distance_from_source = 0;
337 for(int k = 0; k < _nb_vertices; k++) {
339 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
340 d = v->distance_from_source + e->positivized_length;
341 tv = e->terminal_vertex;
342 if(d < tv->distance_from_source) {
343 tv->distance_from_source = d;
344 tv->pred_edge_toward_source = e;
350 // This method does not change the edge occupation. It only sets
351 // properly, for every vertex, the fields distance_from_source and
352 // pred_edge_toward_source.
354 void MTPGraph::find_shortest_path() {
355 Vertex *v, *tv, **last_slot;
359 for(int k = 0; k < _nb_vertices; k++) {
360 _vertices[k].distance_from_source = FLT_MAX;
361 _vertices[k].pred_edge_toward_source = 0;
364 _heap_size = _nb_vertices;
365 _source->distance_from_source = 0;
366 _source->decrease_distance_in_heap(_heap);
369 // Get the closest to the source
372 // Remove it from the heap (swap it with the last_slot in the heap, and
373 // update the distance of that one)
375 last_slot = _heap + _heap_size;
376 swap(*_heap, *last_slot); swap((*_heap)->heap_slot, (*last_slot)->heap_slot);
377 _heap[0]->increase_distance_in_heap(_heap, _heap + _heap_size);
379 // Now update the neighbors of the node currently closest to the
381 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
382 d = v->distance_from_source + e->positivized_length;
383 tv = e->terminal_vertex;
384 if(d < tv->distance_from_source) {
385 ASSERT(tv->heap_slot - _heap < _heap_size);
386 tv->distance_from_source = d;
387 tv->pred_edge_toward_source = e;
388 tv->decrease_distance_in_heap(_heap);
391 } while(_heap_size > 0);
394 void MTPGraph::find_best_paths(scalar_t *lengths) {
395 scalar_t shortest_path_length;
399 for(int e = 0; e < _nb_edges; e++) {
400 _edges[e].length = lengths[e];
401 _edges[e].occupied = 0;
402 _edges[e].positivized_length = _edges[e].length;
405 // Compute the distance of all the nodes from the source by just
406 // visiting them in the proper DAG ordering we computed when
407 // building the graph
408 dp_compute_distances();
411 // Use the current distance from the source to make all edge
413 update_positivized_lengths();
414 // Fix numerical errors
415 force_positivized_lengths();
417 find_shortest_path();
419 shortest_path_length = 0.0;
421 // Do we reach the sink?
422 if(_sink->pred_edge_toward_source) {
423 // If yes, compute the length of the best path according to the
424 // original edge lengths
426 while(v->pred_edge_toward_source) {
427 shortest_path_length += v->pred_edge_toward_source->length;
428 v = v->pred_edge_toward_source->origin_vertex;
430 // If that length is negative
431 if(shortest_path_length < 0.0) {
433 cerr << __FILE__ << ": Found a path of length " << shortest_path_length << endl;
435 // Invert all the edges along the best path
437 while(v->pred_edge_toward_source) {
438 e = v->pred_edge_toward_source;
439 v = e->origin_vertex;
441 // This is the only place where we change the occupations of
443 e->occupied = 1 - e->occupied;
448 } while(shortest_path_length < 0.0);
450 // Put back the graph in its original state (i.e. invert edges which
451 // have been inverted in the process)
452 for(int k = 0; k < _nb_edges; k++) {
454 if(e->occupied) { e->invert(); }
458 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
460 int l = 0, nb_occupied_next;
463 path->nodes[l++] = int(e->origin_vertex - _vertices);
464 path->length = e->length;
467 while(e->terminal_vertex != _sink) {
469 path->nodes[l++] = int(e->terminal_vertex - _vertices);
470 path->length += e->length;
473 nb_occupied_next = 0;
474 for(f = e->terminal_vertex->leaving_edge_list_root; f; f = f->next_leaving_edge) {
475 if(f->occupied) { nb_occupied_next++; next = f; }
479 if(nb_occupied_next == 0) {
480 cerr << __FILE__ << ": retrieve_one_path: Non-sink end point." << endl;
484 else if(nb_occupied_next > 1) {
485 cerr << __FILE__ << ": retrieve_one_path: Non node-disjoint paths." << endl;
494 path->nodes[l++] = int(e->terminal_vertex - _vertices);
495 path->length += e->length;
501 void MTPGraph::retrieve_disjoint_paths() {
505 for(int p = 0; p < nb_paths; p++) delete paths[p];
509 for(e = _source->leaving_edge_list_root; e; e = e->next_leaving_edge) {
510 if(e->occupied) { nb_paths++; }
513 paths = new Path *[nb_paths];
516 for(e = _source->leaving_edge_list_root; e; e = e->next_leaving_edge) {
518 l = retrieve_one_path(e, 0);
519 paths[p] = new Path(l);
520 retrieve_one_path(e, paths[p]);