2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by and Copyright (C) Francois Fleuret //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
21 // Takes the graph description file as input and produces a dot file.
23 // EXAMPLE: ./mtp ./graph2.txt | dot -T pdf -o- | xpdf -
36 typedef float scalar_t;
39 #define ASSERT(x) if(!(x)) { \
40 std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
52 scalar_t length, work_length;
53 Vertex *terminal_vertex;
61 scalar_t distance_from_source;
65 Vertex() { root_edge = 0; }
67 inline void add_edge(Edge *e) {
70 if(root_edge) { root_edge->pred = e; }
74 inline void del_edge(Edge *e) {
75 if(e == root_edge) { root_edge = e->next; }
76 if(e->pred) { e->pred->next = e->next; }
77 if(e->next) { e->next->pred = e->pred; }
82 void initialize_work_lengths();
83 void update_work_length();
84 void find_shortest_path(Vertex **front, Vertex **new_front);
89 Vertex *source, *sink;
91 Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
92 int source, int sink);
96 void find_best_paths(int *result_edge_occupation);
100 void Graph::print() {
101 for(int n = 0; n < nb_vertices; n++) {
102 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
103 cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
112 Graph::Graph(int nb_vrt, int nb_edges,
113 int *from, int *to, scalar_t *lengths,
115 nb_vertices = nb_vrt;
117 edge_heap = new Edge[nb_edges];
118 vertices = new Vertex[nb_vertices];
120 source = &vertices[src];
121 sink = &vertices[snk];
123 for(int v = 0; v < nb_vertices; v++) {
127 for(int e = 0; e < nb_edges; e++) {
128 vertices[from[e]].add_edge(&edge_heap[e]);
129 edge_heap[e].occupied = 0;
131 edge_heap[e].length = lengths[e];
132 edge_heap[e].terminal_vertex = &vertices[to[e]];
141 void Graph::initialize_work_lengths() {
142 scalar_t length_min = 0;
143 for(int n = 0; n < nb_vertices; n++) {
144 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
145 length_min = min(e->length, length_min);
148 for(int n = 0; n < nb_vertices; n++) {
149 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
150 e->work_length = e->length - length_min;
155 void Graph::update_work_length() {
156 for(int n = 0; n < nb_vertices; n++) {
157 scalar_t d = vertices[n].distance_from_source;
158 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
159 e->work_length += d - e->terminal_vertex->distance_from_source;
164 void Graph::find_shortest_path(Vertex **front, Vertex **new_front) {
171 cout << "find_shortest_path" << endl;
175 scalar_t residual_error = 0.0;
177 for(int n = 0; n < nb_vertices; n++) {
178 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
179 if(e->work_length < 0) {
181 residual_error -= e->work_length;
183 e->work_length = 0.0;
188 cout << "DEBUG residual_error " << residual_error << endl;
191 for(int v = 0; v < nb_vertices; v++) {
192 vertices[v].distance_from_source = FLT_MAX;
193 vertices[v].pred_vertex = 0;
194 vertices[v].pred_edge = 0;
195 vertices[v].iteration = 0;
200 int front_size = 0, new_front_size;
201 front[front_size++] = source;
202 source->distance_from_source = 0;
207 for(int f = 0; f < front_size; f++) {
209 for(Edge *e = v->root_edge; e; e = e->next) {
210 d = v->distance_from_source + e->work_length;
211 tv = e->terminal_vertex;
212 if(d < tv->distance_from_source) {
213 tv->distance_from_source = d;
216 if(tv->iteration < iteration) {
217 new_front[new_front_size++] = tv;
218 tv->iteration = iteration;
224 tmp_front = new_front;
228 tmp_front_size = new_front_size;
229 new_front_size = front_size;
230 front_size = tmp_front_size;
231 } while(front_size > 0);
234 void Graph::find_best_paths(int *result_edge_occupation) {
235 Vertex **front = new Vertex *[nb_vertices];
236 Vertex **new_front = new Vertex *[nb_vertices];
238 scalar_t total_length;
240 initialize_work_lengths();
244 find_shortest_path(front, new_front);
245 update_work_length();
247 // Do we reach the sink?
248 if(sink->pred_edge) {
250 // If yes, compute the length of the best path
251 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
252 total_length += v->pred_edge->length;
255 // If that length is negative
256 if(total_length < 0.0) {
257 // Invert all the edges along the best path
258 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
259 Edge *e = v->pred_edge;
260 e->terminal_vertex = v->pred_vertex;
261 e->occupied = 1 - e->occupied;
262 e->length = - e->length;
263 e->work_length = - e->work_length;
264 v->pred_vertex->del_edge(e);
269 } while(total_length < 0.0);
274 for(int n = 0; n < nb_vertices; n++) {
275 Vertex *v = &vertices[n];
276 for(Edge *e = v->root_edge; e; e = e->next) {
277 result_edge_occupation[e->id] = e->occupied;
282 void find_best_paths(int nb_vertices,
283 int nb_edges, int *ea, int *eb, scalar_t *el,
284 int source, int sink,
285 int *result_edge_occupation) {
286 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
287 graph.find_best_paths(result_edge_occupation);
290 void dot_print(int nb_vertices,
291 int nb_edges, int *ea, int *eb, scalar_t *el,
292 int source, int sink,
293 int *edge_occupation) {
294 cout << "digraph {" << endl;
295 cout << " node[shape=circle];" << endl;
296 for(int e = 0; e < nb_edges; e++) {
297 if(edge_occupation[e]) {
298 cout << " " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
300 cout << " " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
306 //////////////////////////////////////////////////////////////////////
308 int main(int argc, char **argv) {
311 cerr << argv[0] << " <graph file>" << endl;
315 ifstream *file = new ifstream(argv[1]);
317 int nb_edges, nb_vertices;
322 (*file) >> nb_vertices >> nb_edges;
323 (*file) >> source >> sink;
325 scalar_t *edge_lengths = new scalar_t[nb_edges];
326 int *vertex_from = new int[nb_edges];
327 int *vertex_to = new int[nb_edges];
328 int *result_edge_occupation = new int[nb_edges];
330 for(int e = 0; e < nb_edges; e++) {
331 (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
334 find_best_paths(nb_vertices, nb_edges,
335 vertex_from, vertex_to, edge_lengths,
337 result_edge_occupation);
339 // dot_print(nb_vertices, nb_edges,
340 // vertex_from, vertex_to, edge_lengths,
342 // result_edge_occupation);
344 delete[] result_edge_occupation;
345 delete[] edge_lengths;
346 delete[] vertex_from;
351 cerr << "Can not open " << argv[1] << endl;