DOLFIN
DOLFIN C++ interface
BoostGraphColoring.h
1 // Copyright (C) 2010 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // First added: 2010-11-24
19 // Last changed:
20 
21 #ifndef __DOLFIN_BOOST_GRAPH_INTERFACE_H
22 #define __DOLFIN_BOOST_GRAPH_INTERFACE_H
23 
24 #include <iostream>
25 #include <vector>
26 
27 #include <boost/graph/adjacency_list.hpp>
28 #include <boost/graph/compressed_sparse_row_graph.hpp>
29 #include <boost/graph/sequential_vertex_coloring.hpp>
30 #include <dolfin/common/Timer.h>
31 #include "Graph.h"
32 
33 namespace dolfin
34 {
35 
36  class Mesh;
37 
39 
41  {
42 
43  public:
44 
46  template<typename ColorType>
47  static std::size_t compute_local_vertex_coloring(const Graph& graph,
48  std::vector<ColorType>& colors)
49  {
50  Timer timer("Boost graph coloring (from dolfin::Graph)");
51 
52  // Typedef for Boost compressed sparse row graph
53  typedef boost::compressed_sparse_row_graph<boost::directedS,
54  boost::property<boost::vertex_color_t, ColorType> > BoostGraph;
55 
56  // Number of vertices
57  const std::size_t n = graph.size();
58 
59  // Count number of edges
60  Graph::const_iterator vertex;
61  std::size_t num_edges = 0;
62  for (vertex = graph.begin(); vertex != graph.end(); ++vertex)
63  num_edges += vertex->size();
64 
65  // Build list of graph edges
66  std::vector<std::pair<std::size_t, std::size_t> > edges;
67  edges.reserve(num_edges);
69  for (vertex = graph.begin(); vertex != graph.end(); ++vertex)
70  {
71  for (edge = vertex->begin(); edge != vertex->end(); ++edge)
72  {
73  const std::size_t vertex_index = vertex - graph.begin();
74  if (vertex_index != (std::size_t) *edge)
75  edges.push_back(std::make_pair(vertex_index, *edge));
76  }
77  }
78  // Build Boost graph
79  const BoostGraph g(boost::edges_are_unsorted_multi_pass,
80  edges.begin(), edges.end(), n);
81 
82  // Resize vector to hold colors
83  colors.resize(n);
84 
85  // Perform coloring
86  return compute_local_vertex_coloring(g, colors);
87  }
88 
90  template<typename T, typename ColorType>
91  static std::size_t compute_local_vertex_coloring(const T& graph,
92  std::vector<ColorType>& colors)
93  {
94  Timer timer("Boost graph coloring");
95 
96  // Number of vertices in graph
97  const std::size_t num_vertices = boost::num_vertices(graph);
98  dolfin_assert(num_vertices == colors.size());
99 
100  typedef typename boost::graph_traits<T>::vertices_size_type
101  vert_size_type;
102  typedef typename boost::property_map<T,
103  boost::vertex_index_t>::const_type vert_index_map;
104 
105  // Resize to hold colors
106  colors.resize(num_vertices);
107 
108  // Color vertices
109  std::vector<vert_size_type> _colors(num_vertices);
110  boost::iterator_property_map<vert_size_type*, vert_index_map>
111  color(&_colors.front(), get(boost::vertex_index, graph));
112  const vert_size_type num_colors = sequential_vertex_coloring(graph,
113  color);
114 
115  // Copy colors and return
116  std::copy(_colors.begin(), _colors.end(), colors.begin());
117  return num_colors;
118  }
119 
120  };
121 }
122 
123 #endif
static std::size_t compute_local_vertex_coloring(const T &graph, std::vector< ColorType > &colors)
Compute vertex colors.
Definition: BoostGraphColoring.h:91
This class colors a graph using the Boost Graph Library.
Definition: BoostGraphColoring.h:40
std::vector< T >::const_iterator const_iterator
Const iterator.
Definition: Set.h:43
Definition: adapt.h:29
Definition: Timer.h:47
std::vector< graph_set_type > Graph
Vector of unordered Sets.
Definition: Graph.h:39
static std::size_t compute_local_vertex_coloring(const Graph &graph, std::vector< ColorType > &colors)
Compute vertex colors.
Definition: BoostGraphColoring.h:47