From: Francois Fleuret Date: Sat, 29 Dec 2012 22:05:59 +0000 (+0100) Subject: Simplified compute_dp_ranks. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=a68c02957279f6e68f71f104c7f8761a5b2240a7;p=mtp.git Simplified compute_dp_ranks. --- diff --git a/mtp_graph.cc b/mtp_graph.cc index 7cd7e52..80f9f05 100644 --- a/mtp_graph.cc +++ b/mtp_graph.cc @@ -205,9 +205,9 @@ void MTPGraph::compute_dp_ranks() { // set the distance_from_source fields to this value. int *nb_predecessors = new int[_nb_vertices]; - int *without_predecessor = new int[_nb_vertices]; - int *new_without_predecessor = new int[_nb_vertices]; - int nb_without_predecessor, new_nb_without_predecessor; + Vertex **bunch = new Vertex *[_nb_vertices]; + + Vertex **already_processed = bunch, **front = bunch, **fresh = bunch; for(int k = 0; k < _nb_vertices; k++) { nb_predecessors[k] = 0; @@ -221,44 +221,38 @@ void MTPGraph::compute_dp_ranks() { } } - nb_without_predecessor = 0; for(int k = 0; k < _nb_vertices; k++) { if(nb_predecessors[k] == 0) { - without_predecessor[nb_without_predecessor++] = k; + *(front++) = _vertices + k; } } scalar_t rank = 1; - while(nb_without_predecessor > 0) { - new_nb_without_predecessor = 0; - for(int l = 0; l < nb_without_predecessor; l++) { - v = &_vertices[without_predecessor[l]]; + while(already_processed < front) { + fresh = front; + while(already_processed < front) { + v = *(already_processed++); v->distance_from_source = rank; for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) { tv = int(e->terminal_vertex - _vertices); nb_predecessors[tv]--; ASSERT(nb_predecessors[tv] >= 0); if(nb_predecessors[tv] == 0) { - new_without_predecessor[new_nb_without_predecessor++] = tv; + *(fresh++) = e->terminal_vertex; } } } - - swap(without_predecessor, new_without_predecessor); - nb_without_predecessor = new_nb_without_predecessor; + front = fresh; rank++; } - for(int k = 0; k < _nb_vertices; k++) { - if(nb_predecessors[k] > 0) { - cerr << __FILE__ << ": The graph is not a DAG." << endl; - abort(); - } + if(already_processed < bunch + _nb_vertices) { + cerr << __FILE__ << ": The graph is not a DAG." << endl; + abort(); } delete[] nb_predecessors; - delete[] without_predecessor; - delete[] new_without_predecessor; + delete[] bunch; } //////////////////////////////////////////////////////////////////////