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 scalar_t residual_error = 0.0;
173 for(int n = 0; n < nb_vertices; n++) {
174 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
175 if(e->work_length < 0) {
177 residual_error -= e->work_length;
179 e->work_length = 0.0;
184 cout << "residual_error " << residual_error << endl;
187 for(int v = 0; v < nb_vertices; v++) {
188 vertices[v].distance_from_source = FLT_MAX;
189 vertices[v].pred_vertex = 0;
190 vertices[v].pred_edge = 0;
191 vertices[v].iteration = 0;
196 int front_size = 0, new_front_size;
197 front[front_size++] = source;
198 source->distance_from_source = 0;
203 for(int f = 0; f < front_size; f++) {
205 for(Edge *e = v->root_edge; e; e = e->next) {
206 d = v->distance_from_source + e->work_length;
207 tv = e->terminal_vertex;
208 if(d < tv->distance_from_source) {
209 tv->distance_from_source = d;
212 if(tv->iteration < iteration) {
213 new_front[new_front_size++] = tv;
214 tv->iteration = iteration;
220 tmp_front = new_front;
224 tmp_front_size = new_front_size;
225 new_front_size = front_size;
226 front_size = tmp_front_size;
227 } while(front_size > 0);
230 void Graph::find_best_paths(int *result_edge_occupation) {
231 Vertex **front = new Vertex *[nb_vertices];
232 Vertex **new_front = new Vertex *[nb_vertices];
234 scalar_t total_length;
236 initialize_work_lengths();
240 find_shortest_path(front, new_front);
241 update_work_length();
243 // Do we reach the sink?
244 if(sink->pred_edge) {
246 // If yes, compute the length of the best path
247 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
248 total_length += v->pred_edge->length;
251 // If that length is negative
252 if(total_length < 0.0) {
253 // Invert all the edges along the best path
254 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
255 Edge *e = v->pred_edge;
256 e->terminal_vertex = v->pred_vertex;
257 e->occupied = 1 - e->occupied;
258 e->length = - e->length;
259 e->work_length = - e->work_length;
260 v->pred_vertex->del_edge(e);
265 } while(total_length < 0.0);
270 for(int n = 0; n < nb_vertices; n++) {
271 Vertex *v = &vertices[n];
272 for(Edge *e = v->root_edge; e; e = e->next) {
273 result_edge_occupation[e->id] = e->occupied;
278 void find_best_paths(int nb_vertices,
279 int nb_edges, int *ea, int *eb, scalar_t *el,
280 int source, int sink,
281 int *result_edge_occupation) {
282 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
283 graph.find_best_paths(result_edge_occupation);
286 void dot_print(int nb_vertices,
287 int nb_edges, int *ea, int *eb, scalar_t *el,
288 int source, int sink,
289 int *edge_occupation) {
290 cout << "digraph {" << endl;
291 cout << " node[shape=circle];" << endl;
292 for(int e = 0; e < nb_edges; e++) {
293 if(edge_occupation[e]) {
294 cout << " " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
296 cout << " " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
302 //////////////////////////////////////////////////////////////////////
304 int main(int argc, char **argv) {
307 cerr << argv[0] << " <graph file>" << endl;
311 ifstream *file = new ifstream(argv[1]);
313 int nb_edges, nb_vertices;
318 (*file) >> nb_vertices >> nb_edges;
319 (*file) >> source >> sink;
321 scalar_t *edge_lengths = new scalar_t[nb_edges];
322 int *vertex_from = new int[nb_edges];
323 int *vertex_to = new int[nb_edges];
324 int *result_edge_occupation = new int[nb_edges];
326 for(int e = 0; e < nb_edges; e++) {
327 (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
330 find_best_paths(nb_vertices, nb_edges,
331 vertex_from, vertex_to, edge_lengths,
333 result_edge_occupation);
335 dot_print(nb_vertices, nb_edges,
336 vertex_from, vertex_to, edge_lengths,
338 result_edge_occupation);
340 delete[] result_edge_occupation;
341 delete[] edge_lengths;
342 delete[] vertex_from;
347 cerr << "Can not open " << argv[1] << endl;