X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mtp_graph.cc;h=2dd145d31c0ff612159bab431e939b5c7cfb933c;hb=5b566c29eac9896a002c8081e760fd3c7d3a4fc3;hp=d3b921b791e6acfc2728702b0b593d0dc2cacdf0;hpb=abbaf6b4b4b8d10ee8e9054a3b8d262a2f242920;p=mtp.git diff --git a/mtp_graph.cc b/mtp_graph.cc index d3b921b..2dd145d 100644 --- a/mtp_graph.cc +++ b/mtp_graph.cc @@ -24,9 +24,8 @@ #include "mtp_graph.h" -#include +// #include #include -#include using namespace std; @@ -49,9 +48,11 @@ public: scalar_t distance_from_source; Edge *pred_edge_toward_source; - int iteration; // Used in find_shortest_path to know if we already - // added this vertex to the front + int last_change; // Used to mark which edges have already been + // processed in some methods + Vertex(); + inline void add_leaving_edge(Edge *e); inline void del_leaving_edge(Edge *e); }; @@ -60,7 +61,7 @@ public: void Edge::invert() { length = - length; - positivized_length = 0; + positivized_length = - positivized_length; origin_vertex->del_leaving_edge(this); terminal_vertex->add_leaving_edge(this); Vertex *t = terminal_vertex; @@ -188,15 +189,14 @@ void MTPGraph::force_positivized_lengths() { scalar_t residual_error = 0.0; scalar_t max_error = 0.0; #endif - for(int n = 0; n < _nb_vertices; n++) { - for(Edge *e = _vertices[n].leaving_edges; e; e = e->next_leaving_edge) { - if(e->positivized_length < 0) { + for(int k = 0; k < _nb_edges; k++) { + Edge *e = _edges + k; + if(e->positivized_length < 0) { #ifdef VERBOSE - residual_error -= e->positivized_length; - max_error = max(max_error, - e->positivized_length); + residual_error -= e->positivized_length; + max_error = max(max_error, - e->positivized_length); #endif - e->positivized_length = 0.0; - } + e->positivized_length = 0.0; } } #ifdef VERBOSE @@ -210,7 +210,7 @@ int MTPGraph::is_dag() { // We put everybody in the front for(int k = 0; k < _nb_vertices; k++) { - _vertices[k].iteration = 0; + _vertices[k].last_change = -1; _front[k] = &_vertices[k]; } @@ -218,14 +218,12 @@ int MTPGraph::is_dag() { int front_size = _nb_vertices, pred_front_size; do { - iteration++; - // We set the iteration field of all vertex with incoming edges to // the current iteration value for(int f = 0; f < front_size; f++) { v = _front[f]; for(e = v->leaving_edges; e; e = e->next_leaving_edge) { - e->terminal_vertex->iteration = iteration; + e->terminal_vertex->last_change = iteration; } } @@ -235,10 +233,12 @@ int MTPGraph::is_dag() { // We remove all the vertices without incoming edge for(int f = 0; f < pred_front_size; f++) { v = _front[f]; - if(v->iteration == iteration) { + if(v->last_change == iteration) { _front[front_size++] = v; } } + + iteration++; } while(front_size < pred_front_size); return front_size == 0; @@ -268,7 +268,7 @@ void MTPGraph::find_shortest_path() { for(int k = 0; k < _nb_vertices; k++) { _vertices[k].distance_from_source = FLT_MAX; _vertices[k].pred_edge_toward_source = 0; - _vertices[k].iteration = 0; + _vertices[k].last_change = -1; } int iteration = 0; @@ -279,7 +279,6 @@ void MTPGraph::find_shortest_path() { do { new_front_size = 0; - iteration++; for(int f = 0; f < front_size; f++) { v = _front[f]; @@ -289,9 +288,9 @@ void MTPGraph::find_shortest_path() { if(d < tv->distance_from_source) { tv->distance_from_source = d; tv->pred_edge_toward_source = e; - if(tv->iteration < iteration) { + if(tv->last_change < iteration) { _new_front[new_front_size++] = tv; - tv->iteration = iteration; + tv->last_change = iteration; } } } @@ -300,6 +299,8 @@ void MTPGraph::find_shortest_path() { tmp_front = _new_front; _new_front = _front; _front = tmp_front; front_size = new_front_size; + + iteration++; } while(front_size > 0); } @@ -328,7 +329,8 @@ void MTPGraph::find_best_paths(scalar_t *lengths) { // Do we reach the sink? if(_sink->pred_edge_toward_source) { - // If yes, compute the length of the best path + // If yes, compute the length of the best path according to the + // original edge lengths v = _sink; while(v->pred_edge_toward_source) { total_length += v->pred_edge_toward_source->length;