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;
62 scalar_t distance_from_source;
67 Vertex() { root_edge = 0; }
69 inline void add_edge(Edge *e) {
72 if(root_edge) { root_edge->pred = e; }
76 inline void del_edge(Edge *e) {
77 if(e == root_edge) { root_edge = e->next; }
78 if(e->pred) { e->pred->next = e->next; }
79 if(e->next) { e->next->pred = e->pred; }
84 void initialize_work_lengths();
85 void update_work_length();
86 void find_shortest_path(Vertex **front, Vertex **new_front);
91 Vertex *source, *sink;
94 Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
95 int source, int sink);
99 void find_best_paths(int *result_edge_occupation);
103 void Graph::print() {
104 for(int n = 0; n < nb_vertices; n++) {
105 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
106 cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
115 Graph::Graph(int nb_vrt, int nb_edges,
116 int *from, int *to, scalar_t *lengths,
118 nb_vertices = nb_vrt;
120 edge_heap = new Edge[nb_edges];
121 vertices = new Vertex[nb_vertices];
123 source = &vertices[src];
124 sink = &vertices[snk];
126 for(int v = 0; v < nb_vertices; v++) {
130 for(int e = 0; e < nb_edges; e++) {
131 vertices[from[e]].add_edge(&edge_heap[e]);
132 edge_heap[e].occupied = 0;
134 edge_heap[e].length = lengths[e];
135 edge_heap[e].terminal_vertex = &vertices[to[e]];
144 void Graph::initialize_work_lengths() {
145 scalar_t length_min = 0;
146 for(int n = 0; n < nb_vertices; n++) {
147 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
148 length_min = min(e->length, length_min);
151 for(int n = 0; n < nb_vertices; n++) {
152 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
153 e->work_length = e->length - length_min;
158 void Graph::update_work_length() {
159 for(int n = 0; n < nb_vertices; n++) {
160 scalar_t d = vertices[n].distance_from_source;
161 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
162 e->work_length += d - e->terminal_vertex->distance_from_source;
167 void Graph::find_shortest_path(Vertex **front, Vertex **new_front) {
174 for(int n = 0; n < nb_vertices; n++) {
175 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
176 if(e->work_length < 0) {
177 cerr << "DEBUG error in find_shortest_path: Edge fixed lengths have to be positive."
185 for(int v = 0; v < nb_vertices; v++) {
186 vertices[v].distance_from_source = FLT_MAX;
187 vertices[v].pred_vertex = 0;
188 vertices[v].pred_edge = 0;
191 int front_size = 0, new_front_size;
192 front[front_size++] = source;
193 source->distance_from_source = 0;
197 for(int f = 0; f < front_size; f++) {
199 for(Edge *e = v->root_edge; e; e = e->next) {
200 d = v->distance_from_source + e->work_length;
201 tv = e->terminal_vertex;
202 if(d < tv->distance_from_source) {
203 tv->distance_from_source = d;
206 new_front[new_front_size++] = tv;
211 tmp_front = new_front;
215 tmp_front_size = new_front_size;
216 new_front_size = front_size;
217 front_size = tmp_front_size;
218 } while(front_size > 0);
221 void Graph::find_best_paths(int *result_edge_occupation) {
222 Vertex **front = new Vertex *[nb_vertices];
223 Vertex **new_front = new Vertex *[nb_vertices];
225 scalar_t total_length;
227 initialize_work_lengths();
231 find_shortest_path(front, new_front);
232 update_work_length();
234 // Do we reach the sink?
235 if(sink->pred_edge) {
237 // If yes, compute the length of the best path
238 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
239 total_length += v->pred_edge->length;
242 // If that length is negative
243 if(total_length < 0.0) {
244 // Invert all the edges along the best path
245 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
246 Edge *e = v->pred_edge;
247 e->terminal_vertex = v->pred_vertex;
248 e->occupied = 1 - e->occupied;
249 e->length = - e->length;
250 e->work_length = - e->work_length;
251 v->pred_vertex->del_edge(e);
256 } while(total_length < 0.0);
261 for(int n = 0; n < nb_vertices; n++) {
262 Vertex *v = &vertices[n];
263 for(Edge *e = v->root_edge; e; e = e->next) {
264 result_edge_occupation[e->id] = e->occupied;
269 void find_best_paths(int nb_vertices,
270 int nb_edges, int *ea, int *eb, scalar_t *el,
271 int source, int sink,
272 int *result_edge_occupation) {
273 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
274 graph.find_best_paths(result_edge_occupation);
277 void dot_print(int nb_vertices,
278 int nb_edges, int *ea, int *eb, scalar_t *el,
279 int source, int sink,
280 int *edge_occupation) {
281 cout << "digraph {" << endl;
282 cout << " node[shape=circle];" << endl;
283 for(int e = 0; e < nb_edges; e++) {
284 if(edge_occupation[e]) {
285 cout << " " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
287 cout << " " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
293 //////////////////////////////////////////////////////////////////////
295 int main(int argc, char **argv) {
298 cerr << argv[0] << " <graph file>" << endl;
302 ifstream *file = new ifstream(argv[1]);
304 int nb_edges, nb_vertices;
309 (*file) >> nb_vertices >> nb_edges;
310 (*file) >> source >> sink;
312 scalar_t *edge_lengths = new scalar_t[nb_edges];
313 int *vertex_from = new int[nb_edges];
314 int *vertex_to = new int[nb_edges];
315 int *result_edge_occupation = new int[nb_edges];
317 for(int e = 0; e < nb_edges; e++) {
318 (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
321 find_best_paths(nb_vertices, nb_edges,
322 vertex_from, vertex_to, edge_lengths,
324 result_edge_occupation);
326 dot_print(nb_vertices, nb_edges,
327 vertex_from, vertex_to, edge_lengths,
329 result_edge_occupation);
331 delete[] result_edge_occupation;
332 delete[] edge_lengths;
333 delete[] vertex_from;
338 cerr << "Can not open " << argv[1] << endl;