Chris@16: //======================================================================= Chris@16: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. Chris@16: // Copyright 2004, 2005 Trustees of Indiana University Chris@16: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Chris@16: // Doug Gregor, D. Kevin McGrath Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //=======================================================================// Chris@16: #ifndef BOOST_GRAPH_KING_HPP Chris@16: #define BOOST_GRAPH_KING_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /* Chris@16: King Algorithm for matrix reordering Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: namespace detail { Chris@16: template Chris@16: class bfs_king_visitor:public default_bfs_visitor Chris@16: { Chris@16: public: Chris@16: bfs_king_visitor(OutputIterator *iter, Buffer *b, Compare compare, Chris@16: PseudoDegreeMap deg, std::vector loc, VecMap color, Chris@16: VertexIndexMap vertices): Chris@16: permutation(iter), Qptr(b), degree(deg), comp(compare), Chris@16: Qlocation(loc), colors(color), vertex_map(vertices) { } Chris@16: Chris@16: template Chris@16: void finish_vertex(Vertex, Graph& g) { Chris@16: typename graph_traits::out_edge_iterator ei, ei_end; Chris@16: Vertex v, w; Chris@16: Chris@16: typedef typename std::deque::reverse_iterator reverse_iterator; Chris@16: Chris@16: reverse_iterator rend = Qptr->rend()-index_begin; Chris@16: reverse_iterator rbegin = Qptr->rbegin(); Chris@16: Chris@16: Chris@16: //heap the vertices already there Chris@16: std::make_heap(rbegin, rend, boost::bind(comp, _2, _1)); Chris@16: Chris@16: unsigned i = 0; Chris@16: Chris@16: for(i = index_begin; i != Qptr->size(); ++i){ Chris@16: colors[get(vertex_map, (*Qptr)[i])] = 1; Chris@16: Qlocation[get(vertex_map, (*Qptr)[i])] = i; Chris@16: } Chris@16: Chris@16: i = 0; Chris@16: Chris@16: for( ; rbegin != rend; rend--){ Chris@16: percolate_down(i); Chris@16: w = (*Qptr)[index_begin+i]; Chris@16: for (boost::tie(ei, ei_end) = out_edges(w, g); ei != ei_end; ++ei) { Chris@16: v = target(*ei, g); Chris@16: put(degree, v, get(degree, v) - 1); Chris@16: Chris@16: if (colors[get(vertex_map, v)] == 1) { Chris@16: percolate_up(get(vertex_map, v), i); Chris@16: } Chris@16: } Chris@16: Chris@16: colors[get(vertex_map, w)] = 0; Chris@16: i++; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void examine_vertex(Vertex u, const Graph&) { Chris@16: Chris@16: *(*permutation)++ = u; Chris@16: index_begin = Qptr->size(); Chris@16: Chris@16: } Chris@16: protected: Chris@16: Chris@16: Chris@16: //this function replaces pop_heap, and tracks state information Chris@16: template Chris@16: void percolate_down(int offset){ Chris@16: int heap_last = index_begin + offset; Chris@16: int heap_first = Qptr->size() - 1; Chris@16: Chris@16: //pop_heap functionality: Chris@16: //swap first, last Chris@16: std::swap((*Qptr)[heap_last], (*Qptr)[heap_first]); Chris@16: Chris@16: //swap in the location queue Chris@16: std::swap(Qlocation[heap_first], Qlocation[heap_last]); Chris@16: Chris@16: //set drifter, children Chris@16: int drifter = heap_first; Chris@16: int drifter_heap = Qptr->size() - drifter; Chris@16: Chris@16: int right_child_heap = drifter_heap * 2 + 1; Chris@16: int right_child = Qptr->size() - right_child_heap; Chris@16: Chris@16: int left_child_heap = drifter_heap * 2; Chris@16: int left_child = Qptr->size() - left_child_heap; Chris@16: Chris@16: //check that we are staying in the heap Chris@16: bool valid = (right_child < heap_last) ? false : true; Chris@16: Chris@16: //pick smallest child of drifter, and keep in mind there might only be left child Chris@16: int smallest_child = (valid && get(degree, (*Qptr)[left_child]) > get(degree,(*Qptr)[right_child])) ? Chris@16: right_child : left_child; Chris@16: Chris@16: while(valid && smallest_child < heap_last && comp((*Qptr)[drifter], (*Qptr)[smallest_child])){ Chris@16: Chris@16: //if smallest child smaller than drifter, swap them Chris@16: std::swap((*Qptr)[smallest_child], (*Qptr)[drifter]); Chris@16: std::swap(Qlocation[drifter], Qlocation[smallest_child]); Chris@16: Chris@16: //update the values, run again, as necessary Chris@16: drifter = smallest_child; Chris@16: drifter_heap = Qptr->size() - drifter; Chris@16: Chris@16: right_child_heap = drifter_heap * 2 + 1; Chris@16: right_child = Qptr->size() - right_child_heap; Chris@16: Chris@16: left_child_heap = drifter_heap * 2; Chris@16: left_child = Qptr->size() - left_child_heap; Chris@16: Chris@16: valid = (right_child < heap_last) ? false : true; Chris@16: Chris@16: smallest_child = (valid && get(degree, (*Qptr)[left_child]) > get(degree,(*Qptr)[right_child])) ? Chris@16: right_child : left_child; Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: Chris@16: Chris@16: // this is like percolate down, but we always compare against the Chris@16: // parent, as there is only a single choice Chris@16: template Chris@16: void percolate_up(int vertex, int offset){ Chris@16: Chris@16: int child_location = Qlocation[vertex]; Chris@16: int heap_child_location = Qptr->size() - child_location; Chris@16: int heap_parent_location = (int)(heap_child_location/2); Chris@16: unsigned parent_location = Qptr->size() - heap_parent_location; Chris@16: Chris@16: bool valid = (heap_parent_location != 0 && child_location > index_begin + offset && Chris@16: parent_location < Qptr->size()); Chris@16: Chris@16: while(valid && comp((*Qptr)[child_location], (*Qptr)[parent_location])){ Chris@16: Chris@16: //swap in the heap Chris@16: std::swap((*Qptr)[child_location], (*Qptr)[parent_location]); Chris@16: Chris@16: //swap in the location queue Chris@16: std::swap(Qlocation[child_location], Qlocation[parent_location]); Chris@16: Chris@16: child_location = parent_location; Chris@16: heap_child_location = heap_parent_location; Chris@16: heap_parent_location = (int)(heap_child_location/2); Chris@16: parent_location = Qptr->size() - heap_parent_location; Chris@16: valid = (heap_parent_location != 0 && child_location > index_begin + offset); Chris@16: } Chris@16: } Chris@16: Chris@16: OutputIterator *permutation; Chris@16: int index_begin; Chris@16: Buffer *Qptr; Chris@16: PseudoDegreeMap degree; Chris@16: Compare comp; Chris@16: std::vector Qlocation; Chris@16: VecMap colors; Chris@16: VertexIndexMap vertex_map; Chris@16: }; Chris@16: Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: template Chris@16: OutputIterator Chris@16: king_ordering(const Graph& g, Chris@16: std::deque< typename graph_traits::vertex_descriptor > Chris@16: vertex_queue, Chris@16: OutputIterator permutation, Chris@16: ColorMap color, DegreeMap degree, Chris@16: VertexIndexMap index_map) Chris@16: { Chris@16: typedef typename property_traits::value_type ds_type; Chris@16: typedef typename property_traits::value_type ColorValue; Chris@16: typedef color_traits Color; Chris@16: typedef typename graph_traits::vertex_descriptor Vertex; Chris@16: typedef iterator_property_map::iterator, VertexIndexMap, ds_type, ds_type&> PseudoDegreeMap; Chris@16: typedef indirect_cmp > Compare; Chris@16: typedef typename boost::sparse::sparse_ordering_queue queue; Chris@16: typedef typename detail::bfs_king_visitor, VertexIndexMap > Visitor; Chris@16: typedef typename graph_traits::vertices_size_type Chris@16: vertices_size_type; Chris@16: std::vector pseudo_degree_vec(num_vertices(g)); Chris@16: PseudoDegreeMap pseudo_degree(pseudo_degree_vec.begin(), index_map); Chris@16: Chris@16: typename graph_traits::vertex_iterator ui, ui_end; Chris@16: queue Q; Chris@16: // Copy degree to pseudo_degree Chris@16: // initialize the color map Chris@16: for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui){ Chris@16: put(pseudo_degree, *ui, get(degree, *ui)); Chris@16: put(color, *ui, Color::white()); Chris@16: } Chris@16: Chris@16: Compare comp(pseudo_degree); Chris@16: std::vector colors(num_vertices(g)); Chris@16: Chris@16: for(vertices_size_type i = 0; i < num_vertices(g); i++) Chris@16: colors[i] = 0; Chris@16: Chris@16: std::vector loc(num_vertices(g)); Chris@16: Chris@16: //create the visitor Chris@16: Visitor vis(&permutation, &Q, comp, pseudo_degree, loc, colors, index_map); Chris@16: Chris@16: while( !vertex_queue.empty() ) { Chris@16: Vertex s = vertex_queue.front(); Chris@16: vertex_queue.pop_front(); Chris@16: Chris@16: //call BFS with visitor Chris@16: breadth_first_visit(g, s, Q, vis, color); Chris@16: } Chris@16: Chris@16: return permutation; Chris@16: } Chris@16: Chris@16: Chris@16: // This is the case where only a single starting vertex is supplied. Chris@16: template Chris@16: OutputIterator Chris@16: king_ordering(const Graph& g, Chris@16: typename graph_traits::vertex_descriptor s, Chris@16: OutputIterator permutation, Chris@16: ColorMap color, DegreeMap degree, VertexIndexMap index_map) Chris@16: { Chris@16: Chris@16: std::deque< typename graph_traits::vertex_descriptor > vertex_queue; Chris@16: vertex_queue.push_front( s ); Chris@16: return king_ordering(g, vertex_queue, permutation, color, degree, Chris@16: index_map); Chris@16: } Chris@16: Chris@16: Chris@16: template < class Graph, class OutputIterator, Chris@16: class ColorMap, class DegreeMap, class VertexIndexMap> Chris@16: OutputIterator Chris@16: king_ordering(const Graph& G, OutputIterator permutation, Chris@16: ColorMap color, DegreeMap degree, VertexIndexMap index_map) Chris@16: { Chris@16: if (has_no_vertices(G)) Chris@16: return permutation; Chris@16: Chris@16: typedef typename boost::graph_traits::vertex_descriptor Vertex; Chris@16: typedef typename property_traits::value_type ColorValue; Chris@16: typedef color_traits Color; Chris@16: Chris@16: std::deque vertex_queue; Chris@16: Chris@16: // Mark everything white Chris@16: BGL_FORALL_VERTICES_T(v, G, Graph) put(color, v, Color::white()); Chris@16: Chris@16: // Find one vertex from each connected component Chris@16: BGL_FORALL_VERTICES_T(v, G, Graph) { Chris@16: if (get(color, v) == Color::white()) { Chris@16: depth_first_visit(G, v, dfs_visitor<>(), color); Chris@16: vertex_queue.push_back(v); Chris@16: } Chris@16: } Chris@16: Chris@16: // Find starting nodes for all vertices Chris@16: // TBD: How to do this with a directed graph? Chris@16: for (typename std::deque::iterator i = vertex_queue.begin(); Chris@16: i != vertex_queue.end(); ++i) Chris@16: *i = find_starting_node(G, *i, color, degree); Chris@16: Chris@16: return king_ordering(G, vertex_queue, permutation, color, degree, Chris@16: index_map); Chris@16: } Chris@16: Chris@16: template Chris@16: OutputIterator Chris@16: king_ordering(const Graph& G, OutputIterator permutation, Chris@16: VertexIndexMap index_map) Chris@16: { Chris@16: if (has_no_vertices(G)) Chris@16: return permutation; Chris@16: Chris@16: std::vector colors(num_vertices(G)); Chris@16: return king_ordering(G, permutation, Chris@16: make_iterator_property_map(&colors[0], index_map, Chris@16: colors[0]), Chris@16: make_out_degree_map(G), index_map); Chris@16: } Chris@16: Chris@16: template Chris@16: inline OutputIterator Chris@16: king_ordering(const Graph& G, OutputIterator permutation) Chris@16: { return king_ordering(G, permutation, get(vertex_index, G)); } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_GRAPH_KING_HPP