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 ///////////////////////////////////////////////////////////////////////////
32 typedef float scalar_t;
35 #define ASSERT(x) if(!(x)) { \
36 std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
48 scalar_t length, work_length;
49 Vertex *terminal_vertex;
58 scalar_t distance_from_source;
63 Vertex() { root_edge = 0; }
65 inline void add_edge(Edge *e) {
68 if(root_edge) { root_edge->pred = e; }
72 inline void del_edge(Edge *e) {
73 if(e == root_edge) { root_edge = e->next; }
74 if(e->pred) { e->pred->next = e->next; }
75 if(e->next) { e->next->pred = e->pred; }
80 void initialize_work_lengths();
81 void update_work_length();
82 void find_shortest_path(Vertex **front, Vertex **new_front);
87 Vertex *source, *sink;
90 Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
91 int source, int sink);
95 void find_best_paths(int *result_edge_occupation);
100 for(int n = 0; n < nb_vertices; n++) {
101 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
102 cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
111 Graph::Graph(int nb_vrt, int nb_edges,
112 int *from, int *to, scalar_t *lengths,
114 nb_vertices = nb_vrt;
116 edge_heap = new Edge[nb_edges];
117 vertices = new Vertex[nb_vertices];
119 source = &vertices[src];
120 sink = &vertices[snk];
122 for(int v = 0; v < nb_vertices; v++) {
126 for(int e = 0; e < nb_edges; e++) {
127 vertices[from[e]].add_edge(&edge_heap[e]);
128 edge_heap[e].occupied = 0;
130 edge_heap[e].length = lengths[e];
131 edge_heap[e].terminal_vertex = &vertices[to[e]];
140 void Graph::initialize_work_lengths() {
141 scalar_t length_min = 0;
142 for(int n = 0; n < nb_vertices; n++) {
143 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
144 length_min = min(e->length, length_min);
147 for(int n = 0; n < nb_vertices; n++) {
148 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
149 e->work_length = e->length - length_min;
154 void Graph::update_work_length() {
155 for(int n = 0; n < nb_vertices; n++) {
156 scalar_t d = vertices[n].distance_from_source;
157 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
158 e->work_length += d - e->terminal_vertex->distance_from_source;
163 void Graph::find_shortest_path(Vertex **front, Vertex **new_front) {
170 for(int n = 0; n < nb_vertices; n++) {
171 for(Edge *e = vertices[n].root_edge; e; e = e->next) {
172 if(e->work_length < 0) {
173 cerr << "DEBUG error in find_shortest_path: Edge fixed lengths have to be positive."
181 for(int v = 0; v < nb_vertices; v++) {
182 vertices[v].distance_from_source = FLT_MAX;
183 vertices[v].pred_vertex = 0;
184 vertices[v].pred_edge = 0;
187 int front_size = 0, new_front_size;
188 front[front_size++] = source;
189 source->distance_from_source = 0;
193 for(int f = 0; f < front_size; f++) {
195 for(Edge *e = v->root_edge; e; e = e->next) {
196 d = v->distance_from_source + e->work_length;
197 tv = e->terminal_vertex;
198 if(d < tv->distance_from_source) {
199 tv->distance_from_source = d;
202 new_front[new_front_size++] = tv;
207 tmp_front = new_front;
211 tmp_front_size = new_front_size;
212 new_front_size = front_size;
213 front_size = tmp_front_size;
214 } while(front_size > 0);
217 void Graph::find_best_paths(int *result_edge_occupation) {
218 Vertex **front = new Vertex *[nb_vertices];
219 Vertex **new_front = new Vertex *[nb_vertices];
221 scalar_t total_length;
223 initialize_work_lengths();
227 find_shortest_path(front, new_front);
228 update_work_length();
230 // Do we reach the sink?
231 if(sink->pred_edge) {
233 // If yes, compute the length of the best path
234 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
235 total_length += v->pred_edge->length;
238 // If that length is negative
239 if(total_length < 0.0) {
240 // Invert all the edges along the best path
241 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
242 Edge *e = v->pred_edge;
243 e->terminal_vertex = v->pred_vertex;
244 e->occupied = 1 - e->occupied;
245 e->length = - e->length;
246 e->work_length = - e->work_length;
247 v->pred_vertex->del_edge(e);
252 } while(total_length < 0.0);
257 for(int n = 0; n < nb_vertices; n++) {
258 Vertex *v = &vertices[n];
259 for(Edge *e = v->root_edge; e; e = e->next) {
260 result_edge_occupation[e->id] = e->occupied;
265 void find_best_paths(int nb_vertices,
266 int nb_edges, int *ea, int *eb, scalar_t *el,
267 int source, int sink,
268 int *result_edge_occupation) {
269 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
270 graph.find_best_paths(result_edge_occupation);
273 void dot_print(int nb_vertices,
274 int nb_edges, int *ea, int *eb, scalar_t *el,
275 int source, int sink,
276 int *edge_occupation) {
277 cout << "digraph {" << endl;
278 cout << " node[shape=circle];" << endl;
279 for(int e = 0; e < nb_edges; e++) {
280 if(edge_occupation[e]) {
281 cout << " " << ea[e] << " -> " << eb[e] << " [style=bold,color=black,label=\"" << el[e] << "\"];" << endl;
283 cout << " " << ea[e] << " -> " << eb[e] << " [color=gray,label=\"" << el[e] << "\"];" << endl;
289 //////////////////////////////////////////////////////////////////////
291 int main(int argc, char **argv) {
294 cerr << argv[0] << " <graph file>" << endl;
298 ifstream *file = new ifstream(argv[1]);
300 int nb_edges, nb_vertices;
305 (*file) >> nb_vertices >> nb_edges;
306 (*file) >> source >> sink;
308 scalar_t *edge_lengths = new scalar_t[nb_edges];
309 int *vertex_from = new int[nb_edges];
310 int *vertex_to = new int[nb_edges];
311 int *result_edge_occupation = new int[nb_edges];
313 for(int e = 0; e < nb_edges; e++) {
314 (*file) >> vertex_from[e] >> vertex_to[e] >> edge_lengths[e];
317 find_best_paths(nb_vertices, nb_edges,
318 vertex_from, vertex_to, edge_lengths,
320 result_edge_occupation);
322 dot_print(nb_vertices, nb_edges,
323 vertex_from, vertex_to, edge_lengths,
325 result_edge_occupation);
327 delete[] result_edge_occupation;
328 delete[] edge_lengths;
329 delete[] vertex_from;
334 cerr << "Can not open " << argv[1] << endl;