public:
int id, occupied;
scalar_t length, work_length;
- Vertex *terminal_vertex;
+ Vertex *origin_vertex, *terminal_vertex;
Edge *next, *pred;
};
};
void MTPGraph::print() {
- for(int n = 0; n < _nb_vertices; n++) {
- for(Edge *e = vertices[n].root_edge; e; e = e->next) {
- cout << n << " -> " << e->terminal_vertex->id << " " << e->length;
- if(e->occupied) {
- cout << " *";
- }
- cout << endl;
+ for(int k = 0; k < _nb_edges; k++) {
+ Edge *e = edges + k;
+ cout << e->origin_vertex->id
+ << " -> "
+ << e->terminal_vertex->id
+ << " "
+ << e->length;
+ if(e->occupied) {
+ cout << " *";
+ }
+ cout << endl;
+ }
+}
+
+void MTPGraph::print_dot() {
+ cout << "digraph {" << endl;
+ cout << " node[shape=circle];" << endl;
+ for(int k = 0; k < _nb_edges; k++) {
+ Edge *e = edges + k;
+ if(e->occupied) {
+ cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
+ << " [style=bold,color=black,label=\"" << -e->length << "\"];" << endl;
+ } else {
+ cout << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
+ << " [color=gray,label=\"" << e->length << "\"];" << endl;
}
}
+ cout << "}" << endl;
}
MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
_nb_vertices = nb_vertices;
_nb_edges = nb_edges;
- edge_heap = new Edge[_nb_edges];
+ edges = new Edge[_nb_edges];
vertices = new Vertex[_nb_vertices];
+ _front = new Vertex *[_nb_vertices];
+ _new_front = new Vertex *[_nb_vertices];
- source = &vertices[src];
- sink = &vertices[snk];
+ _source = &vertices[src];
+ _sink = &vertices[snk];
for(int v = 0; v < _nb_vertices; v++) {
vertices[v].id = v;
}
for(int e = 0; e < nb_edges; e++) {
- vertices[from[e]].add_edge(&edge_heap[e]);
- edge_heap[e].occupied = 0;
- edge_heap[e].id = e;
- edge_heap[e].terminal_vertex = &vertices[to[e]];
+ vertices[from[e]].add_edge(&edges[e]);
+ edges[e].occupied = 0;
+ edges[e].id = e;
+ edges[e].origin_vertex = &vertices[from[e]];
+ edges[e].terminal_vertex = &vertices[to[e]];
}
+
}
MTPGraph::~MTPGraph() {
delete[] vertices;
- delete[] edge_heap;
+ delete[] edges;
+ delete[] _front;
+ delete[] _new_front;
}
void MTPGraph::initialize_work_lengths() {
}
}
-void MTPGraph::update_work_length() {
- for(int n = 0; n < _nb_vertices; n++) {
- scalar_t d = vertices[n].distance_from_source;
- for(Edge *e = vertices[n].root_edge; e; e = e->next) {
- e->work_length += d - e->terminal_vertex->distance_from_source;
- }
+void MTPGraph::update_work_lengths() {
+ for(int k = 0; k < _nb_edges; k++) {
+ Edge *e = edges + k;
+ e->work_length += e->terminal_vertex->distance_from_source - e->terminal_vertex->distance_from_source;
}
}
-void MTPGraph::find_shortest_path(Vertex **front, Vertex **new_front) {
- Vertex **tmp_front;
- int tmp_front_size;
- Vertex *v, *tv;
- scalar_t d;
-
+void MTPGraph::force_positive_work_lengths() {
#ifdef VERBOSE
scalar_t residual_error = 0.0;
#endif
#ifdef VERBOSE
cerr << "residual_error " << residual_error << endl;
#endif
+}
+
+void MTPGraph::find_shortest_path(Vertex **_front, Vertex **_new_front) {
+ Vertex **tmp_front;
+ int tmp_front_size;
+ Vertex *v, *tv;
+ scalar_t d;
for(int v = 0; v < _nb_vertices; v++) {
vertices[v].distance_from_source = FLT_MAX;
int iteration = 0;
- int front_size = 0, new_front_size;
- front[front_size++] = source;
- source->distance_from_source = 0;
+ int _front_size = 0, _new_front_size;
+ _front[_front_size++] = _source;
+ _source->distance_from_source = 0;
do {
- new_front_size = 0;
+ _new_front_size = 0;
iteration++;
- for(int f = 0; f < front_size; f++) {
- v = front[f];
+ for(int f = 0; f < _front_size; f++) {
+ v = _front[f];
for(Edge *e = v->root_edge; e; e = e->next) {
d = v->distance_from_source + e->work_length;
tv = e->terminal_vertex;
tv->pred_vertex = v;
tv->pred_edge = e;
if(tv->iteration < iteration) {
- new_front[new_front_size++] = tv;
+ _new_front[_new_front_size++] = tv;
tv->iteration = iteration;
}
}
}
}
- tmp_front = new_front;
- new_front = front;
- front = tmp_front;
+ tmp_front = _new_front;
+ _new_front = _front;
+ _front = tmp_front;
- tmp_front_size = new_front_size;
- new_front_size = front_size;
- front_size = tmp_front_size;
- } while(front_size > 0);
+ tmp_front_size = _new_front_size;
+ _new_front_size = _front_size;
+ _front_size = tmp_front_size;
+ } while(_front_size > 0);
}
void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
- Vertex **front = new Vertex *[_nb_vertices];
- Vertex **new_front = new Vertex *[_nb_vertices];
-
scalar_t total_length;
for(int e = 0; e < _nb_edges; e++) {
- edge_heap[e].length = lengths[e];
+ edges[e].length = lengths[e];
+ edges[e].work_length = edges[e].length;
}
+#warning
+ // find_shortest_path(_front, _new_front);
+ // update_work_lengths();
+
initialize_work_lengths();
do {
+ force_positive_work_lengths();
+ find_shortest_path(_front, _new_front);
+ update_work_lengths();
+
total_length = 0.0;
- find_shortest_path(front, new_front);
- update_work_length();
- // Do we reach the sink?
- if(sink->pred_edge) {
+ // Do we reach the _sink?
+ if(_sink->pred_edge) {
// If yes, compute the length of the best path
- for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+ for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
total_length += v->pred_edge->length;
}
// If that length is negative
if(total_length < 0.0) {
+#ifdef VERBOSE
+ cout << "Found a path of length " << total_length << endl;
+#endif
// Invert all the edges along the best path
- for(Vertex *v = sink; v->pred_edge; v = v->pred_vertex) {
+ for(Vertex *v = _sink; v->pred_edge; v = v->pred_vertex) {
Edge *e = v->pred_edge;
- e->terminal_vertex = v->pred_vertex;
e->occupied = 1 - e->occupied;
e->length = - e->length;
e->work_length = - e->work_length;
- v->pred_vertex->del_edge(e);
- v->add_edge(e);
+ e->origin_vertex->del_edge(e);
+ e->terminal_vertex->add_edge(e);
+ Vertex *t = e->terminal_vertex;
+ e->terminal_vertex = e->origin_vertex;
+ e->origin_vertex = t;
}
}
}
+
} while(total_length < 0.0);
- delete[] front;
- delete[] new_front;
+ for(int k = 0; k < _nb_edges; k++) {
+ Edge *e = edges + k;
+ if(e->occupied) {
+ e->length = - e->length;
+ e->work_length = 0;
+ e->origin_vertex->del_edge(e);
+ e->terminal_vertex->add_edge(e);
+ Vertex *t = e->terminal_vertex;
+ e->terminal_vertex = e->origin_vertex;
+ e->origin_vertex = t;
+ }
+ }
+
+ // for(Edge *e = _sink->root_edge; e; e = e->next) {
+ // if(e->occupied) {
+ // Edge *f = e;
+ // cout << "PATH " << _sink->id;
+ // while(f) {
+ // cout << " " << f->terminal_vertex->id;
+ // for(f = f->terminal_vertex->root_edge; f && !f->occupied; f = f->next);
+ // }
+ // cout << endl;
+ // }
+ // }
+
+ // int nb_occupied = 0;
+ // for(int e = 0; e < _nb_edges; e++) {
+ // for(int n = 0; n < _nb_vertices; n++) {
+ // Vertex *v = &vertices[n];
+ // for(Edge *e = v->root_edge; e; e = e->next) {
+ // if(e->occupied) nb_occupied++;
+ // }
+ // }
+ // }
for(int n = 0; n < _nb_vertices; n++) {
Vertex *v = &vertices[n];