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) {
103 p = heap + ((h - heap + 1) >> 1) - 1;
104 if((*p)->distance_from_source <= distance_from_source) break;
105 swap((*p)->heap_slot, heap_slot);
111 void Vertex::increase_distance_in_heap(Vertex **heap, Vertex **heap_bottom) {
112 Vertex **c1, **c2, **h;
115 c1 = heap + 2 * (h - heap) + 1;
116 if(c1 >= heap_bottom) break;
118 if((*c1)->distance_from_source < distance_from_source) {
119 if(c2 < heap_bottom && (*c2)->distance_from_source < (*c1)->distance_from_source) {
120 swap((*c2)->heap_slot, heap_slot);
124 swap((*c1)->heap_slot, heap_slot);
129 if(c2 < heap_bottom && (*c2)->distance_from_source < distance_from_source) {
130 swap((*c2)->heap_slot, heap_slot);
138 //////////////////////////////////////////////////////////////////////
140 MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
141 int *vertex_from, int *vertex_to,
142 int source, int sink) {
143 _nb_vertices = nb_vertices;
144 _nb_edges = nb_edges;
146 _edges = new Edge[_nb_edges];
147 _vertices = new Vertex[_nb_vertices];
148 _heap = new Vertex *[_nb_vertices];
149 _dp_order = new Vertex *[_nb_vertices];
151 _source = &_vertices[source];
152 _sink = &_vertices[sink];
154 for(int e = 0; e < nb_edges; e++) {
155 _vertices[vertex_from[e]].add_leaving_edge(&_edges[e]);
156 _edges[e].occupied = 0;
157 _edges[e].origin_vertex = &_vertices[vertex_from[e]];
158 _edges[e].terminal_vertex = &_vertices[vertex_to[e]];
161 for(int v = 0; v < _nb_vertices; v++) {
162 _heap[v] = &_vertices[v];
163 _vertices[v].heap_slot = &_heap[v];
169 compute_dp_ordering();
172 MTPGraph::~MTPGraph() {
177 for(int p = 0; p < nb_paths; p++) delete paths[p];
181 //////////////////////////////////////////////////////////////////////
183 void MTPGraph::print(ostream *os) {
184 for(int k = 0; k < _nb_edges; k++) {
185 Edge *e = &_edges[k];
186 (*os) << e->origin_vertex - _vertices
188 << e->terminal_vertex - _vertices
189 << " (" << e->length << ")";
190 if(e->occupied) { (*os) << " *"; }
195 void MTPGraph::print_dot(ostream *os) {
196 (*os) << "digraph {" << endl;
197 (*os) << " rankdir=\"LR\";" << endl;
198 (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl;
199 (*os) << " edge [color=gray,arrowhead=open]" << endl;
200 (*os) << " " << _source - _vertices << " [peripheries=2];" << endl;
201 (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl;
202 for(int k = 0; k < _nb_edges; k++) {
203 Edge *e = &_edges[k];
205 << e->origin_vertex - _vertices
207 << e->terminal_vertex - _vertices
210 (*os) << "style=bold,color=black,";
212 (*os) << "label=\"" << e->length << "\"];" << endl;
214 (*os) << "}" << endl;
217 //////////////////////////////////////////////////////////////////////
219 void MTPGraph::update_positivized_lengths() {
220 for(int k = 0; k < _nb_edges; k++) {
221 Edge *e = &_edges[k];
222 e->positivized_length +=
223 e->origin_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
227 void MTPGraph::force_positivized_lengths() {
229 scalar_t residual_error = 0.0;
230 scalar_t max_error = 0.0;
232 for(int k = 0; k < _nb_edges; k++) {
233 Edge *e = &_edges[k];
235 if(e->positivized_length < 0) {
237 residual_error -= e->positivized_length;
238 max_error = max(max_error, - e->positivized_length);
240 e->positivized_length = 0.0;
244 cerr << __FILE__ << ": residual_error " << residual_error << " max_error " << residual_error << endl;
248 void MTPGraph::dp_compute_distances() {
253 for(int k = 0; k < _nb_vertices; k++) {
254 _vertices[k].distance_from_source = FLT_MAX;
255 _vertices[k].pred_edge_toward_source = 0;
258 _source->distance_from_source = 0;
260 for(int k = 0; k < _nb_vertices; k++) {
262 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
263 d = v->distance_from_source + e->positivized_length;
264 tv = e->terminal_vertex;
265 if(d < tv->distance_from_source) {
266 tv->distance_from_source = d;
267 tv->pred_edge_toward_source = e;
273 // This method does not change the edge occupation. It only sets
274 // properly, for every vertex, the fields distance_from_source and
275 // pred_edge_toward_source.
277 void MTPGraph::find_shortest_path() {
278 Vertex *v, *tv, **last_slot;
282 for(int k = 0; k < _nb_vertices; k++) {
283 _vertices[k].distance_from_source = FLT_MAX;
284 _vertices[k].pred_edge_toward_source = 0;
287 int heap_size = _nb_vertices;
288 _source->distance_from_source = 0;
289 _source->decrease_distance_in_heap(_heap);
291 while(heap_size > 0) {
292 // Get the closest to the source
295 // Remove it from the heap (swap it with the last_slot in the heap, and
296 // update the distance of that one)
298 last_slot = _heap + heap_size;
299 swap(*_heap, *last_slot); swap((*_heap)->heap_slot, (*last_slot)->heap_slot);
300 _heap[0]->increase_distance_in_heap(_heap, last_slot);
302 // Now update the neighbors of the node currently closest to the
304 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
305 d = v->distance_from_source + e->positivized_length;
306 tv = e->terminal_vertex;
307 if(d < tv->distance_from_source) {
308 ASSERT(tv->heap_slot < last_slot);
309 tv->distance_from_source = d;
310 tv->pred_edge_toward_source = e;
311 tv->decrease_distance_in_heap(_heap);
317 void MTPGraph::find_best_paths(scalar_t *lengths) {
318 scalar_t shortest_path_length;
322 for(int e = 0; e < _nb_edges; e++) {
323 _edges[e].length = lengths[e];
324 _edges[e].occupied = 0;
325 _edges[e].positivized_length = _edges[e].length;
328 // Compute the distance of all the nodes from the source by just
329 // visiting them in the proper DAG ordering we computed when
330 // building the graph
331 dp_compute_distances();
334 // Use the current distance from the source to make all edge
336 update_positivized_lengths();
337 // Fix numerical errors
338 force_positivized_lengths();
340 find_shortest_path();
342 shortest_path_length = 0.0;
344 // Do we reach the sink?
345 if(_sink->pred_edge_toward_source) {
346 // If yes, compute the length of the best path according to the
347 // original edge lengths
349 while(v->pred_edge_toward_source) {
350 shortest_path_length += v->pred_edge_toward_source->length;
351 v = v->pred_edge_toward_source->origin_vertex;
353 // If that length is negative
354 if(shortest_path_length < 0.0) {
356 cerr << __FILE__ << ": Found a path of length " << shortest_path_length << endl;
358 // Invert all the edges along the best path
360 while(v->pred_edge_toward_source) {
361 e = v->pred_edge_toward_source;
362 v = e->origin_vertex;
364 // This is the only place where we change the occupations of
366 e->occupied = 1 - e->occupied;
371 } while(shortest_path_length < 0.0);
373 // Put back the graph in its original state (i.e. invert edges which
374 // have been inverted in the process)
375 for(int k = 0; k < _nb_edges; k++) {
377 if(e->occupied) { e->invert(); }
381 int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
383 int l = 0, nb_occupied_next;
386 path->nodes[l++] = int(e->origin_vertex - _vertices);
387 path->length = e->length;
390 while(e->terminal_vertex != _sink) {
392 path->nodes[l++] = int(e->terminal_vertex - _vertices);
393 path->length += e->length;
396 nb_occupied_next = 0;
397 for(f = e->terminal_vertex->leaving_edge_list_root; f; f = f->next_leaving_edge) {
398 if(f->occupied) { nb_occupied_next++; next = f; }
402 if(nb_occupied_next == 0) {
403 cerr << __FILE__ << ": retrieve_one_path: Non-sink end point." << endl;
407 else if(nb_occupied_next > 1) {
408 cerr << __FILE__ << ": retrieve_one_path: Non node-disjoint paths." << endl;
417 path->nodes[l++] = int(e->terminal_vertex - _vertices);
418 path->length += e->length;
424 //////////////////////////////////////////////////////////////////////
426 static int compare_vertices_on_distance(const void *v1, const void *v2) {
428 (*((Vertex **) v1))->distance_from_source -
429 (*((Vertex **) v2))->distance_from_source;
430 if(delta < 0) return -1;
431 else if(delta > 0) return 1;
435 void MTPGraph::compute_dp_ordering() {
440 // This method computes for each node the length of the longest link
441 // from the source, and orders the node in _dp_order according to
442 // it. It aborts if the graph is not a DAG.
444 int *nb_predecessors = new int[_nb_vertices];
446 Vertex **already_processed = _dp_order, **front = _dp_order, **new_front = _dp_order;
448 for(int k = 0; k < _nb_vertices; k++) {
449 nb_predecessors[k] = 0;
452 for(int k = 0; k < _nb_vertices; k++) {
454 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
455 ntv = int(e->terminal_vertex - _vertices);
456 nb_predecessors[ntv]++;
460 for(int k = 0; k < _nb_vertices; k++) {
461 if(nb_predecessors[k] == 0) {
462 *(front++) = _vertices + k;
467 while(already_processed < front) {
469 while(already_processed < front) {
470 v = *(already_processed++);
471 v->distance_from_source = rank;
472 for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
473 ntv = int(e->terminal_vertex - _vertices);
474 nb_predecessors[ntv]--;
475 ASSERT(nb_predecessors[ntv] >= 0);
476 if(nb_predecessors[ntv] == 0) {
477 *(new_front++) = e->terminal_vertex;
485 if(already_processed < _dp_order + _nb_vertices) {
486 cerr << __FILE__ << ": The graph is not a DAG." << endl;
490 delete[] nb_predecessors;
492 for(int v = 0; v < _nb_vertices; v++) { _dp_order[v] = &_vertices[v]; }
493 qsort(_dp_order, _nb_vertices, sizeof(Vertex *), compare_vertices_on_distance);
496 //////////////////////////////////////////////////////////////////////
498 void MTPGraph::retrieve_disjoint_paths() {
502 for(int p = 0; p < nb_paths; p++) delete paths[p];
506 for(e = _source->leaving_edge_list_root; e; e = e->next_leaving_edge) {
507 if(e->occupied) { nb_paths++; }
510 paths = new Path *[nb_paths];
513 for(e = _source->leaving_edge_list_root; e; e = e->next_leaving_edge) {
515 l = retrieve_one_path(e, 0);
516 paths[p] = new Path(l);
517 retrieve_one_path(e, paths[p]);