2 ///////////////////////////////////////////////////////////////////////////
5 // This program is free software: you can redistribute it and/or modify //
6 // it under the terms of the version 3 of the GNU General Public License //
7 // as published by the Free Software Foundation. //
9 // This program is distributed in the hope that it will be useful, but //
10 // WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
12 // General Public License for more details. //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 // Written by and Copyright (C) Francois Fleuret //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
21 ///////////////////////////////////////////////////////////////////////////
34 typedef float scalar_t;
37 #define ASSERT(x) if(!(x)) { \
38 std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
50 scalar_t length, fixed_length;
51 Vertex *terminal_vertex;
58 // These are the leaving edges
65 Vertex() { first_edge = 0; }
67 inline void add_edge(Edge *e) {
68 if(first_edge) { first_edge->pred = e; }
74 inline void del_edge(Edge *e) {
75 if(e == first_edge) { first_edge = e->next; }
76 if(e->pred) { e->pred->next = e->next; }
77 if(e->next) { e->next->pred = e->pred; }
86 Vertex *source, *sink;
88 Graph(int nb_vertices, int nb_edges, int *from, int *to, scalar_t *lengths,
89 int source, int sink);
92 void initialize_fixed_lengths();
93 void update_fixed_length();
94 void find_shortest_path();
95 void find_best_paths();
97 void print_occupied_edges();
100 void Graph::print() {
101 for(int n = 0; n < nb_vertices; n++) {
102 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
103 cout << n << " -> " << e->terminal_vertex->id << " " << e->length << endl;
108 void Graph::print_occupied_edges() {
109 for(int n = 0; n < nb_vertices; n++) {
110 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
112 int a = n, b = e->terminal_vertex->id;
113 if(a > b) { int c = a; a = b; b = c; }
114 cout << a << " " << b << endl;
120 Graph::Graph(int nb_vrt, int nb_edges,
121 int *from, int *to, scalar_t *lengths,
123 nb_vertices = nb_vrt;
125 edge_heap = new Edge[nb_edges];
126 vertices = new Vertex[nb_vertices];
128 source = &vertices[src];
129 sink = &vertices[snk];
131 for(int v = 0; v < nb_vertices; v++) {
135 for(int e = 0; e < nb_edges; e++) {
136 vertices[from[e]].add_edge(&edge_heap[e]);
137 edge_heap[e].occupied = 0;
138 edge_heap[e].length = lengths[e];
139 edge_heap[e].terminal_vertex = &vertices[to[e]];
148 void Graph::initialize_fixed_lengths() {
149 scalar_t length_min = 0;
150 for(int n = 0; n < nb_vertices; n++) {
151 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
152 length_min = min(e->length, length_min);
155 for(int n = 0; n < nb_vertices; n++) {
156 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
157 e->fixed_length = e->length - length_min;
162 void Graph::update_fixed_length() {
163 for(int n = 0; n < nb_vertices; n++) {
164 scalar_t d = vertices[n].distance;
165 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
166 e->fixed_length += d - e->terminal_vertex->distance;
171 void Graph::find_shortest_path() {
172 Vertex **front = new Vertex *[nb_vertices];
173 Vertex **new_front = new Vertex *[nb_vertices];
180 for(int n = 0; n < nb_vertices; n++) {
181 for(Edge *e = vertices[n].first_edge; e; e = e->next) {
182 if(e->fixed_length < 0) {
183 cerr << "DEBUG error in find_shortest_path: Edge fixed lengths have to be positive."
191 for(int v = 0; v < nb_vertices; v++) {
192 vertices[v].distance = FLT_MAX;
193 vertices[v].pred_vertex = 0;
194 vertices[v].pred_edge = 0;
197 int front_size = 0, new_front_size;
198 front[front_size++] = source;
199 source->distance = 0;
203 for(int f = 0; f < front_size; f++) {
205 for(Edge *e = v->first_edge; e; e = e->next) {
206 d = v->distance + e->fixed_length;
207 tv = e->terminal_vertex;
208 if(d < tv->distance) {
212 new_front[new_front_size++] = tv;
217 tmp_front = new_front;
221 tmp_front_size = new_front_size;
222 new_front_size = front_size;
223 front_size = tmp_front_size;
224 } while(front_size > 0);
230 void Graph::find_best_paths() {
231 scalar_t total_length;
233 initialize_fixed_lengths();
241 find_shortest_path();
242 update_fixed_length();
244 // Do we reach the sink?
245 if(sink->pred_edge) {
248 cout << "VERBOSE there is a path reaching the sink" << endl;
251 // If yes, compute the length of the best path
252 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
253 total_length += v->pred_edge->length;
257 cout << "VERBOSE total_length " << total_length << endl;
260 // If that length is negative
261 if(total_length < 0.0) {
262 // Invert all the edges along the best path
263 for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
264 Edge *e = v->pred_edge;
265 e->terminal_vertex = v->pred_vertex;
266 e->occupied = 1 - e->occupied;
267 e->length = - e->length;
268 e->fixed_length = - e->fixed_length;
269 v->pred_vertex->del_edge(e);
274 } while(total_length < 0.0);
277 //////////////////////////////////////////////////////////////////////
279 int main(int argc, char **argv) {
282 cerr << argv[0] << " <graph file>" << endl;
286 ifstream *file = new ifstream(argv[1]);
288 int nb_edges, nb_vertices;
293 (*file) >> nb_vertices >> nb_edges;
294 (*file) >> source >> sink;
296 cout << "INPUT nb_edges " << nb_edges << endl;
297 cout << "INPUT nb_vertices " << nb_vertices << endl;
298 cout << "INPUT source " << source << endl;
299 cout << "INPUT sink " << sink << endl;
301 scalar_t *el = new scalar_t[nb_edges];
302 int *ea = new int[nb_edges];
303 int *eb = new int[nb_edges];
305 for(int e = 0; e < nb_edges; e++) {
306 (*file) >> ea[e] >> eb[e] >> el[e];
307 cout << "INPUT_EDGE " << ea[e] << " " << eb[e] << " " << el[e] << endl;
310 Graph graph(nb_vertices, nb_edges, ea, eb, el, source, sink);
312 graph.find_best_paths();
313 graph.print_occupied_edges();
321 cerr << "Can not open " << argv[1] << endl;