scalar_t length, work_length;
Vertex *origin_vertex, *terminal_vertex;
Edge *next, *pred;
+
+ inline void revert();
};
class Vertex {
int id, iteration;
Edge *root_edge;
scalar_t distance_from_source;
- Vertex *pred_vertex;
Edge *pred_edge;
- Vertex() { root_edge = 0; }
+ Vertex();
+ inline void add_edge(Edge *e);
+ inline void del_edge(Edge *e);
+};
- inline void add_edge(Edge *e) {
- e->next = root_edge;
- e->pred = 0;
- if(root_edge) { root_edge->pred = e; }
- root_edge = e;
- }
+//////////////////////////////////////////////////////////////////////
- inline void del_edge(Edge *e) {
- if(e == root_edge) { root_edge = e->next; }
- if(e->pred) { e->pred->next = e->next; }
- if(e->next) { e->next->pred = e->pred; }
- }
-};
+void Edge::revert() {
+ length = - length;
+ work_length = 0;
+ origin_vertex->del_edge(this);
+ terminal_vertex->add_edge(this);
+ Vertex *t = terminal_vertex;
+ terminal_vertex = origin_vertex;
+ origin_vertex = t;
+}
+
+//////////////////////////////////////////////////////////////////////
+
+Vertex::Vertex() {
+ root_edge = 0;
+}
+
+void Vertex::add_edge(Edge *e) {
+ e->next = root_edge;
+ e->pred = 0;
+ if(root_edge) { root_edge->pred = e; }
+ root_edge = e;
+}
+
+void Vertex::del_edge(Edge *e) {
+ if(e == root_edge) { root_edge = e->next; }
+ if(e->pred) { e->pred->next = e->next; }
+ if(e->next) { e->next->pred = e->pred; }
+}
+
+//////////////////////////////////////////////////////////////////////
void MTPGraph::print() {
for(int k = 0; k < _nb_edges; k++) {
for(int v = 0; v < _nb_vertices; v++) {
vertices[v].distance_from_source = FLT_MAX;
- vertices[v].pred_vertex = 0;
vertices[v].pred_edge = 0;
vertices[v].iteration = 0;
}
tv = e->terminal_vertex;
if(d < tv->distance_from_source) {
tv->distance_from_source = d;
- tv->pred_vertex = v;
tv->pred_edge = e;
if(tv->iteration < iteration) {
_new_front[_new_front_size++] = tv;
void MTPGraph::find_best_paths(scalar_t *lengths, int *result_edge_occupation) {
scalar_t total_length;
+ Vertex *v;
+ Edge *e;
for(int e = 0; e < _nb_edges; e++) {
edges[e].length = lengths[e];
edges[e].work_length = edges[e].length;
}
-#warning
- // find_shortest_path(_front, _new_front);
- // update_work_lengths();
+ find_shortest_path(_front, _new_front);
+ update_work_lengths();
- initialize_work_lengths();
+ // #warning
+ // initialize_work_lengths();
do {
force_positive_work_lengths();
// Do we reach the _sink?
if(_sink->pred_edge) {
-
// If yes, compute the length of the best path
- for(Vertex *v = _sink; v->pred_vertex; v = v->pred_vertex) {
+ v = _sink;
+ while(v->pred_edge) {
total_length += v->pred_edge->length;
+ v = v->pred_edge->origin_vertex;
}
-
// 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) {
- Edge *e = v->pred_edge;
+ v = _sink;
+ while(v->pred_edge) {
+ e = v->pred_edge;
+ v = e->origin_vertex;
+ e->revert();
e->occupied = 1 - e->occupied;
- e->length = - e->length;
- e->work_length = - e->work_length;
- 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(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;
- }
+ if(e->occupied) { e->revert(); }
}
- // 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];
for(Edge *e = v->root_edge; e; e = e->next) {