X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mtp_graph.cc;h=5371732a03e9c85e3b357f16464e36773535a0b7;hb=6c727bc27f1b594b5f3e5a756a0a01c3cb9b1cec;hp=052de17884763a1a32747e149b0f047e3ee23f20;hpb=e03634d09a9049c7ae4fb426c67ad513cc84185a;p=mtp.git diff --git a/mtp_graph.cc b/mtp_graph.cc index 052de17..5371732 100644 --- a/mtp_graph.cc +++ b/mtp_graph.cc @@ -24,19 +24,18 @@ #include "mtp_graph.h" -#include #include -#include using namespace std; class Edge { public: - int id, occupied; + int occupied; scalar_t length, positivized_length; Vertex *origin_vertex, *terminal_vertex; - // These are the links in the origin_vertex leaving edge list + // These fields are used for the linked list of a vertex's leaving + // edge list. We have to do insertions / deletions. Edge *next_leaving_edge, *pred_leaving_edge; inline void invert(); @@ -44,7 +43,6 @@ public: class Vertex { public: - int id; Edge *leaving_edges; scalar_t distance_from_source; Edge *pred_edge_toward_source; @@ -111,14 +109,9 @@ MTPGraph::MTPGraph(int nb_vertices, int nb_edges, _source = &_vertices[source]; _sink = &_vertices[sink]; - for(int k = 0; k < _nb_vertices; k++) { - _vertices[k].id = k; - } - for(int e = 0; e < nb_edges; e++) { _vertices[vertex_from[e]].add_leaving_edge(_edges + e); _edges[e].occupied = 0; - _edges[e].id = e; _edges[e].origin_vertex = _vertices + vertex_from[e]; _edges[e].terminal_vertex = _vertices + vertex_to[e]; } @@ -141,9 +134,9 @@ MTPGraph::~MTPGraph() { void MTPGraph::print(ostream *os) { for(int k = 0; k < _nb_edges; k++) { Edge *e = _edges + k; - (*os) << e->origin_vertex->id + (*os) << e->origin_vertex - _vertices << " -> " - << e->terminal_vertex->id + << e->terminal_vertex - _vertices << " " << e->length; if(e->occupied) { @@ -158,14 +151,14 @@ void MTPGraph::print_dot(ostream *os) { (*os) << " rankdir=\"LR\";" << endl; (*os) << " node [shape=circle,width=0.75,fixedsize=true];" << endl; (*os) << " edge [color=gray,arrowhead=open]" << endl; - (*os) << " " << _source->id << " [peripheries=2];" << endl; - (*os) << " " << _sink->id << " [peripheries=2];" << endl; + (*os) << " " << _source - _vertices << " [peripheries=2];" << endl; + (*os) << " " << _sink - _vertices << " [peripheries=2];" << endl; for(int k = 0; k < _nb_edges; k++) { Edge *e = _edges + k; - // (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id - // << ";" - // << endl; - (*os) << " " << e->origin_vertex->id << " -> " << e->terminal_vertex->id + (*os) << " " + << e->origin_vertex - _vertices + << " -> " + << e->terminal_vertex - _vertices << " ["; if(e->occupied) { (*os) << "style=bold,color=black,"; @@ -190,15 +183,25 @@ 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 + if((e->origin_vertex->last_change < 0 && e->terminal_vertex->last_change >= 0) || + (e->origin_vertex->last_change >= 0 && e->terminal_vertex->last_change < 0)) { + cout << "Inconsistent non-connexity (this should never happen)." << endl; + abort(); + } + if(e->origin_vertex->last_change >= 0 && + e->terminal_vertex->last_change >= 0 && + e->positivized_length < 0) { residual_error -= e->positivized_length; max_error = max(max_error, - e->positivized_length); -#endif - e->positivized_length = 0.0; } +#endif + e->positivized_length = 0.0; } } #ifdef VERBOSE @@ -220,8 +223,8 @@ int MTPGraph::is_dag() { int front_size = _nb_vertices, pred_front_size; do { - // We set the iteration field of all vertex with incoming edges to - // the current iteration value + // We set the last_change field of all the vertices 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) { @@ -232,7 +235,7 @@ int MTPGraph::is_dag() { pred_front_size = front_size; front_size = 0; - // We remove all the vertices without incoming edge + // We keep all the vertices with incoming nodes for(int f = 0; f < pred_front_size; f++) { v = _front[f]; if(v->last_change == iteration) { @@ -247,7 +250,7 @@ int MTPGraph::is_dag() { } // This method does not change the edge occupation. It only set -// properly for every vertex the fields distance_from_source and +// properly, for every vertex, the fields distance_from_source and // pred_edge_toward_source. void MTPGraph::find_shortest_path() { @@ -361,42 +364,48 @@ void MTPGraph::find_best_paths(scalar_t *lengths) { // Put back the graph in its original state (i.e. invert edges which // have been inverted in the process) for(int k = 0; k < _nb_edges; k++) { - Edge *e = _edges + k; + e = _edges + k; if(e->occupied) { e->invert(); } } } int MTPGraph::retrieve_one_path(Edge *e, Path *path) { Edge *f, *next = 0; - int l = 0; + int l = 0, nb_occupied_next; if(path) { - path->nodes[l++] = e->origin_vertex->id; + path->nodes[l++] = e->origin_vertex - _vertices; path->length = e->length; } else l++; while(e->terminal_vertex != _sink) { if(path) { - path->nodes[l++] = e->terminal_vertex->id; + path->nodes[l++] = e->terminal_vertex - _vertices; path->length += e->length; } else l++; - int nb_choices = 0; + + nb_occupied_next = 0; for(f = e->terminal_vertex->leaving_edges; f; f = f->next_leaving_edge) { - if(f->occupied) { nb_choices++; next = f; } - if(nb_choices == 0) { - cerr << "retrieve_one_path: Non-sink end point." << endl; - abort(); - } - if(nb_choices > 1) { - cerr << "retrieve_one_path: Non node-disjoint paths." << endl; - abort(); - } + if(f->occupied) { nb_occupied_next++; next = f; } + } + +#ifdef DEBUG + if(nb_occupied_next == 0) { + cerr << "retrieve_one_path: Non-sink end point." << endl; + abort(); + } + + else if(nb_occupied_next > 1) { + cerr << "retrieve_one_path: Non node-disjoint paths." << endl; + abort(); } +#endif + e = next; } if(path) { - path->nodes[l++] = e->terminal_vertex->id; + path->nodes[l++] = e->terminal_vertex - _vertices; path->length += e->length; } else l++; @@ -405,6 +414,7 @@ int MTPGraph::retrieve_one_path(Edge *e, Path *path) { void MTPGraph::retrieve_disjoint_paths() { Edge *e; + int p, l; for(int p = 0; p < nb_paths; p++) delete paths[p]; delete[] paths; @@ -416,10 +426,10 @@ void MTPGraph::retrieve_disjoint_paths() { paths = new Path *[nb_paths]; - int p = 0; + p = 0; for(e = _source->leaving_edges; e; e = e->next_leaving_edge) { if(e->occupied) { - int l = retrieve_one_path(e, 0); + l = retrieve_one_path(e, 0); paths[p] = new Path(l); retrieve_one_path(e, paths[p]); p++;