X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mtp_graph.cc;h=33d5a4ff0d8bdd191467b5cdaf8fc607108fb5a3;hb=86c860c0f43a9130e4121072620961fc892ed83f;hp=12017c20a1b9039e1e62ebd5a96ed9ee0a90ac92;hpb=2b3a2e10ec226f1610b9c39abd20f0899a34a652;p=mtp.git diff --git a/mtp_graph.cc b/mtp_graph.cc index 12017c2..33d5a4f 100644 --- a/mtp_graph.cc +++ b/mtp_graph.cc @@ -46,16 +46,14 @@ public: Edge *leaving_edges; scalar_t distance_from_source; Edge *pred_edge_toward_source; - - int last_change; // Used to mark which edges have already been - // processed in some methods - - Vertex **heap_position; + Vertex **heap_slot; Vertex(); inline void add_leaving_edge(Edge *e); inline void del_leaving_edge(Edge *e); + inline void decrease_distance_in_heap(Vertex **heap); + inline void increase_distance_in_heap(Vertex **heap, int heap_size); }; ////////////////////////////////////////////////////////////////////// @@ -95,10 +93,50 @@ void Vertex::del_leaving_edge(Edge *e) { } } +void Vertex::decrease_distance_in_heap(Vertex **heap) { + Vertex **p, **h; + // There is some beauty in that + h = heap_slot; + while(h > heap && + (p = heap + (h - heap + 1) / 2 - 1, + (*p)->distance_from_source > (*h)->distance_from_source)) { + swap(*p, *h); + swap((*p)->heap_slot, (*h)->heap_slot); + h = p; + } +} + +void Vertex::increase_distance_in_heap(Vertex **heap, int heap_size) { + Vertex **c1, **c2, **h; + // omg, that's beautiful + h = heap_slot; + while(c1 = heap + 2 * (h - heap + 1) - 1, c2 = c1 + 1, + (c1 < heap + heap_size && (*c1)->distance_from_source < (*h)->distance_from_source) + || + (c2 < heap + heap_size && (*c2)->distance_from_source < (*h)->distance_from_source) + ) { + if(c1 < heap + heap_size && + !(c2 < heap + heap_size && (*c2)->distance_from_source < (*c1)->distance_from_source)){ + swap(*c1, *h); + swap((*c1)->heap_slot, (*h)->heap_slot); + h = c1; + } else { + swap(*c2, *h); + swap((*c2)->heap_slot, (*h)->heap_slot); + h = c2; + } + } +} + ////////////////////////////////////////////////////////////////////// static int compare_vertex(const void *v1, const void *v2) { - return (*((Vertex **) v1))->last_change - (*((Vertex **) v2))->last_change; + scalar_t delta = + (*((Vertex **) v1))->distance_from_source - + (*((Vertex **) v2))->distance_from_source; + if(delta < 0) return -1; + else if(delta > 0) return 1; + else return 0; } MTPGraph::MTPGraph(int nb_vertices, int nb_edges, @@ -124,16 +162,16 @@ MTPGraph::MTPGraph(int nb_vertices, int nb_edges, for(int v = 0; v < _nb_vertices; v++) { _heap[v] = &_vertices[v]; - _vertices[v].heap_position = &_heap[v]; + _vertices[v].heap_slot = &_heap[v]; } paths = 0; nb_paths = 0; - if(is_dag()) { - // Here the last_change field of every vertex tells us how many - // iterations of DP we need to reach it. Hence we only have to - // process the vertex in that order. + if(compute_dp_distances()) { + // Here the distance_from_source field of every vertex is the + // number of DP iterations needed to update it. Hence we only have + // to process the vertex in that order. for(int v = 0; v < _nb_vertices; v++) { _dp_order[v] = &_vertices[v]; } qsort(_dp_order, _nb_vertices, sizeof(Vertex *), compare_vertex); } else { @@ -211,48 +249,47 @@ void MTPGraph::force_positivized_lengths() { 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); - } + residual_error -= e->positivized_length; + max_error = max(max_error, - e->positivized_length); #endif e->positivized_length = 0.0; } } #ifdef VERBOSE - cerr << "residual_error " << residual_error << " max_error " << residual_error << endl; + cerr << __FILE__ << ": residual_error " << residual_error << " max_error " << residual_error << endl; #endif } -int MTPGraph::is_dag() { +int MTPGraph::compute_dp_distances() { Vertex *v; Edge *e; + // This procedure computes for each node the longest link from the + // source and abort if the graph is not a DAG. It works by removing + // successively nodes without predecessor: At the first iteration it + // removes the source, then the nodes with incoming edge only from + // the source, etc. If it can remove all the nodes that way, the + // graph is a DAG. If at some point it can not remove node anymore + // and there are some remaining nodes, the graph is not a DAG. + Vertex **active = new Vertex *[_nb_vertices]; - // We put everybody in the active + // All the nodes are active at first for(int k = 0; k < _nb_vertices; k++) { - _vertices[k].last_change = 0; + _vertices[k].distance_from_source = 0; active[k] = &_vertices[k]; } - int iteration = 1; + scalar_t nb_iterations = 1; int nb_active = _nb_vertices, pred_nb_active; do { - // We set the last_change field of all the vertices with incoming - // edges to the current iteration value + // We set the distance_from_source field of all the vertices with incoming + // edges to the current nb_iterations value for(int f = 0; f < nb_active; f++) { v = active[f]; for(e = v->leaving_edges; e; e = e->next_leaving_edge) { - e->terminal_vertex->last_change = iteration; + e->terminal_vertex->distance_from_source = nb_iterations; } } @@ -262,12 +299,12 @@ int MTPGraph::is_dag() { // We keep all the vertices with incoming nodes for(int f = 0; f < pred_nb_active; f++) { v = active[f]; - if(v->last_change == iteration) { + if(v->distance_from_source == nb_iterations) { active[nb_active++] = v; } } - iteration++; + nb_iterations++; } while(nb_active < pred_nb_active); delete[] active; @@ -275,101 +312,7 @@ int MTPGraph::is_dag() { return nb_active == 0; } -#ifdef DEBUG -void print_heap(int first, int heap_size, Vertex **heap, int depth, Vertex *vertices) { - // if(depth == 0) cout << "** START_HEAP ********************************************************" << endl; - // if(first < heap_size) { - // print_heap((first + 1) * 2 - 1, heap_size, heap, depth + 1, vertices); - // for(int d = 0; d < depth; d++) cout << " "; - // cout << "[" << heap[first] - vertices << "] "; - // if(heap[first]->distance_from_source == FLT_MAX) cout << "inf"; - // else cout << heap[first]->distance_from_source; - // cout << endl; - // print_heap((first + 1) * 2, heap_size, heap, depth + 1, vertices); - // } - // if(depth == 0) cout << "** END_HEAP **********************************************************" << endl; -} - -void check_heap(Vertex **heap, int heap_size, Vertex *vertices, int nb_vertices) { - Vertex **p, **h; - int *used = new int[nb_vertices]; - for(int v = 0; v < nb_vertices; v++) used[v] = 0; - for(int k = 0; k < heap_size; k++) { - used[heap[k] - vertices]++; - h = heap + k; - if(h != (*h)->heap_position) abort(); - if(k > 0) { - p = heap + (h - heap + 1) / 2 - 1; - if((*h)->distance_from_source < (*p)->distance_from_source) abort(); - } - } - for(int v = 0; v < nb_vertices; v++) { - cout << used[v]; - if(used[v] > 1) abort(); - } - cout << endl; - delete[] used; -} -#endif - -void MTPGraph::decrease_distance_in_heap(Vertex *v) { -#ifdef DEBUG - // cout << "START decrease_distance_in_heap" << endl; - // print_heap(0, _heap_size, _heap, 0, _vertices); -#endif - Vertex **p, **h; - // There is some beauty in that - h = v->heap_position; - while(h > _heap && - (p = _heap + (h - _heap + 1) / 2 - 1, - (*p)->distance_from_source > (*h)->distance_from_source)) { -// #warning REMOVE - // cout << "SWAP [" << (*p) - _vertices << " | " << (*h) - _vertices << "]" << endl; - // if((*p) - _vertices == 6 && (*h) - _vertices == 96) abort(); - swap(*p, *h); - swap((*p)->heap_position, (*h)->heap_position); - h = p; - } -#ifdef DEBUG - // check_heap(_heap, _heap_size, _vertices, _nb_vertices); - // print_heap(0, _heap_size, _heap, 0, _vertices); -#endif -} - -void MTPGraph::increase_distance_in_heap(Vertex *v) { -#ifdef DEBUG - // cout << "START increase_distance_in_heap" << endl; - // print_heap(0, _heap_size, _heap, 0, _vertices); -#endif - Vertex **c1, **c2, **h; - // There is some beauty in that - h = v->heap_position; - while(c1 = _heap + 2 * (h - _heap + 1) - 1, c2 = c1 + 1, - (c1 < _heap + _heap_size && - (*c1)->distance_from_source < (*h)->distance_from_source) - || - (c2 < _heap + _heap_size && - (*c2)->distance_from_source < (*h)->distance_from_source) - ) { - if(c1 < _heap + _heap_size && - !(c2 < _heap + _heap_size && - (*c2)->distance_from_source < (*c1)->distance_from_source)){ - swap(*c1, *h); - swap((*c1)->heap_position, (*h)->heap_position); - h = c1; - } else { - swap(*c2, *h); - swap((*c2)->heap_position, (*h)->heap_position); - h = c2; - } - } -#ifdef DEBUG - // check_heap(_heap, _heap_size, _vertices, _nb_vertices); - // print_heap(0, _heap_size, _heap, 0, _vertices); -#endif -} - -void MTPGraph::dp_distance_propagation() { +void MTPGraph::dp_compute_distances() { Vertex *v, *tv; Edge *e; scalar_t d; @@ -389,38 +332,21 @@ void MTPGraph::dp_distance_propagation() { if(d < tv->distance_from_source) { tv->distance_from_source = d; tv->pred_edge_toward_source = e; - decrease_distance_in_heap(tv); + tv->decrease_distance_in_heap(_heap); } } } - -#ifdef DEBUG - for(int k = 0; k < _nb_vertices; k++) { - if(_vertices[k].distance_from_source == FLT_MAX) abort(); - } -#endif } -// This method does not change the edge occupation. It only set +// This method does not change the edge occupation. It only sets // properly, for every vertex, the fields distance_from_source and // pred_edge_toward_source. void MTPGraph::find_shortest_path() { - Vertex *v, *tv; + Vertex *v, *tv, **a, **b; Edge *e; scalar_t d; -#ifdef DEBUG - if(is_dag()) { - cout << "find_shortest_path: DAG -> ok" << endl; - } else { - for(int e = 0; e < _nb_edges; e++) { - if(_edges[e].positivized_length < 0) abort(); - } - cout << "find_shortest_path: All positivized_length are positive -> ok" << endl; - } -#endif - for(int k = 0; k < _nb_vertices; k++) { _vertices[k].distance_from_source = FLT_MAX; _vertices[k].pred_edge_toward_source = 0; @@ -428,50 +354,32 @@ void MTPGraph::find_shortest_path() { _heap_size = _nb_vertices; _source->distance_from_source = 0; - decrease_distance_in_heap(_source); + _source->decrease_distance_in_heap(_heap); do { -#ifdef DEBUG - for(int k = 0; k < _heap_size; k++) { - if(_heap[0]->distance_from_source > _heap[k]->distance_from_source) abort(); - } - // cout << "OK!" << endl; -#endif - + // Get the closest to the source v = _heap[0]; + + // Remove it from the heap (swap it with the last in the heap, and + // update the distance of that one) _heap_size--; - Vertex **a = _heap, **b = _heap + _heap_size; - swap(*a, *b); swap((*a)->heap_position, (*b)->heap_position); - increase_distance_in_heap(_heap[0]); + a = _heap; + b = _heap + _heap_size; + swap(*a, *b); swap((*a)->heap_slot, (*b)->heap_slot); + _heap[0]->increase_distance_in_heap(_heap, _heap_size); + // Now update the neighbors of the currently closest to the source for(e = v->leaving_edges; e; e = e->next_leaving_edge) { d = v->distance_from_source + e->positivized_length; tv = e->terminal_vertex; if(d < tv->distance_from_source) { - ASSERT(tv->heap_position - _heap < _heap_size); + ASSERT(tv->heap_slot - _heap < _heap_size); tv->distance_from_source = d; tv->pred_edge_toward_source = e; - decrease_distance_in_heap(tv); - } -#ifdef DEBUG - for(int k = 0; k < _heap_size; k++) { - if(_heap[0]->distance_from_source > _heap[k]->distance_from_source) abort(); + tv->decrease_distance_in_heap(_heap); } -#endif } } while(_heap_size > 0); - -#ifdef DEBUG - for(int k = 0; k < _nb_vertices; k++) { - v = &_vertices[k]; - for(e = v->leaving_edges; e; e = e->next_leaving_edge) { - d = v->distance_from_source + e->positivized_length; - tv = e->terminal_vertex; - if(d < tv->distance_from_source) abort(); - } - } -#endif - cout << "DONE!" << endl; } void MTPGraph::find_best_paths(scalar_t *lengths) { @@ -485,12 +393,8 @@ void MTPGraph::find_best_paths(scalar_t *lengths) { _edges[e].positivized_length = _edges[e].length; } - // // We call find_shortest_path here to set properly the distances to - // // the source, so that we can make all the edge lengths positive at - // // the first iteration. - // find_shortest_path(); - - dp_distance_propagation(); + // Update the distances to the source in "good order" + dp_compute_distances(); do { update_positivized_lengths(); @@ -511,7 +415,7 @@ void MTPGraph::find_best_paths(scalar_t *lengths) { // If that length is negative if(total_length < 0.0) { #ifdef VERBOSE - cerr << "Found a path of length " << total_length << endl; + cerr << __FILE__ << ": Found a path of length " << total_length << endl; #endif // Invert all the edges along the best path v = _sink; @@ -558,12 +462,12 @@ int MTPGraph::retrieve_one_path(Edge *e, Path *path) { #ifdef DEBUG if(nb_occupied_next == 0) { - cerr << "retrieve_one_path: Non-sink end point." << endl; + cerr << __FILE__ << ": retrieve_one_path: Non-sink end point." << endl; abort(); } else if(nb_occupied_next > 1) { - cerr << "retrieve_one_path: Non node-disjoint paths." << endl; + cerr << __FILE__ << ": retrieve_one_path: Non node-disjoint paths." << endl; abort(); } #endif