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 for(int n = 0; n < nb_vertices; n++) {
172 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
173 if(e->work_length < 0) {
174 cerr << "DEBUG error in find_shortest_path: Edge work lengths have to be positive."
182 for(int v = 0; v < nb_vertices; v++) {
183 vertices[v].distance_from_source = FLT_MAX;
184 vertices[v].pred_vertex = 0;
185 vertices[v].pred_edge = 0;
186 vertices[v].iteration = 0;
191 int front_size = 0, new_front_size;
192 front[front_size++] = source;
193 source->distance_from_source = 0;
198 for(int f = 0; f < front_size; f++) {
200 for(Edge *e = v->root_edge; e; e = e->next) {
201 d = v->distance_from_source + e->work_length;
202 tv = e->terminal_vertex;
203 if(d < tv->distance_from_source) {
204 tv->distance_from_source = d;
207 if(tv->iteration < iteration) {
208 new_front[new_front_size++] = tv;
209 tv->iteration = iteration;
215 tmp_front = new_front;
219 tmp_front_size = new_front_size;
220 new_front_size = front_size;
221 front_size = tmp_front_size;
222 } while(front_size > 0);
225 void Graph::find_best_paths(int *result_edge_occupation) {
226 Vertex **front = new Vertex *[nb_vertices];
227 Vertex **new_front = new Vertex *[nb_vertices];
229 scalar_t total_length;
231 initialize_work_lengths();
235 find_shortest_path(front, new_front);
236 update_work_length();
238 // Do we reach the sink?
239 if(sink->pred_edge) {
241 // If yes, compute the length of the best path
242 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
243 total_length += v->pred_edge->length;
246 // If that length is negative
247 if(total_length < 0.0) {
248 // Invert all the edges along the best path
249 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
250 Edge *e = v->pred_edge;
251 e->terminal_vertex = v->pred_vertex;
252 e->occupied = 1 - e->occupied;
253 e->length = - e->length;
254 e->work_length = - e->work_length;
255 v->pred_vertex->del_edge(e);
260 } while(total_length < 0.0);
265 for(int n = 0; n < nb_vertices; n++) {
266 Vertex *v = &vertices[n];
267 for(Edge *e = v->root_edge; e; e = e->next) {
268 result_edge_occupation[e->id] = e->occupied;
273 void find_best_paths(int nb_vertices,
274 int nb_edges, int *ea, int *eb, scalar_t *el,
275 int source, int sink,
276 int *result_edge_occupation) {
277 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
278 graph.find_best_paths(result_edge_occupation);
281 void dot_print(int nb_vertices,
282 int nb_edges, int *ea, int *eb, scalar_t *el,
283 int source, int sink,
284 int *edge_occupation) {
285 cout << "digraph {" << endl;
286 cout << " node[shape=circle];" << endl;
287 for(int e = 0; e < nb_edges; e++) {
288 if(edge_occupation[e]) {
289 cout << " " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
291 cout << " " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
297 //////////////////////////////////////////////////////////////////////
299 int main(int argc, char **argv) {
302 cerr << argv[0] << " <graph file>" << endl;
306 ifstream *file = new ifstream(argv[1]);
308 int nb_edges, nb_vertices;
313 (*file) >> nb_vertices >> nb_edges;
314 (*file) >> source >> sink;
316 scalar_t *edge_lengths = new scalar_t[nb_edges];
317 int *vertex_from = new int[nb_edges];
318 int *vertex_to = new int[nb_edges];
319 int *result_edge_occupation = new int[nb_edges];
321 for(int e = 0; e < nb_edges; e++) {
322 (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
325 find_best_paths(nb_vertices, nb_edges,
326 vertex_from, vertex_to, edge_lengths,
328 result_edge_occupation);
330 dot_print(nb_vertices, nb_edges,
331 vertex_from, vertex_to, edge_lengths,
333 result_edge_occupation);
335 delete[] result_edge_occupation;
336 delete[] edge_lengths;
337 delete[] vertex_from;
342 cerr << "Can not open " << argv[1] << endl;