Seems to work. Lot of debugging garbage still in there.
[mtp.git] / mtp_graph.h
1
2 /*
3  *  mtp is the ``Multi Tracked Paths'', an implementation of the
4  *  k-shortest paths algorithm for multi-target tracking.
5  *
6  *  Copyright (c) 2012 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of mtp.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #ifndef MTP_GRAPH_H
26 #define MTP_GRAPH_H
27
28 #include <iostream>
29 #include <cmath>
30
31 using namespace std;
32
33 #include "misc.h"
34 #include "path.h"
35
36 class Vertex;
37 class Edge;
38
39 class MTPGraph {
40   // Uses the estimated vertex distances to the source to make all the
41   // edge lengths positive, resulting in an identical added value to
42   // all the paths from the same initial node to the same final node
43   // (in particular from source to sink)
44   void update_positivized_lengths();
45
46   // It may happen that numerical errors in update_positivized_lengths
47   // make the resulting lengths negative, albeit very small. The
48   // following method forces all negative lengths to zero, and prints
49   // the total correction when compiled in VERBOSE mode.
50   void force_positivized_lengths();
51
52   void decrease_distance_in_heap(Vertex *v);
53   void increase_distance_in_heap(Vertex *v);
54
55   // Visit the vertices according to _dp_order and simply update their
56   // distance to the source
57   void dp_distance_propagation();
58
59   // Set in every vertex pred_edge_toward_source correspondingly to
60   // the path of shortest length. The current implementation is not
61   // Dijkstra's!
62   void find_shortest_path();
63
64   // Follows the path starting on edge e and returns the number of
65   // nodes to reach the sink. If path is non-null, stores in it the
66   // nodes met along the path, and computes path->length properly.
67   int retrieve_one_path(Edge *e, Path *path);
68
69   // Returns if the graph is a DAG, and set the last_change field of
70   // each vertex to the maximum number of iterations required to reach
71   // it
72   int is_dag();
73
74   int _nb_vertices, _nb_edges;
75   Vertex *_source, *_sink;
76
77   Edge *_edges;
78   Vertex *_vertices;
79
80   // For the shortest path search
81
82   Vertex **_heap;
83   int _heap_size;
84
85   Vertex **_dp_order;
86
87 public:
88
89   // These variables are filled when retrieve_disjoint_paths is called
90   int nb_paths;
91   Path **paths;
92
93   MTPGraph(int nb_vertices, int nb_edges, int *vertex_from, int *vertex_to,
94            int source, int sink);
95
96   ~MTPGraph();
97
98   // Compute the family of path with minimum total length, set the
99   // edge occupied fields accordingly.
100   void find_best_paths(scalar_t *lengths);
101
102   // Retrieve the path corresponding to the occupied edges, and save
103   // the result in the nb_paths and paths fields.
104   void retrieve_disjoint_paths();
105
106   void print(ostream *os);
107   void print_dot(ostream *os);
108 };
109
110 #endif