OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be found | |
3 // in the LICENSE file. | |
4 | |
5 #include "webgtt/graph.h" | |
6 | |
7 #include <boost/concept_check.hpp> | |
8 #include <boost/graph/adjacency_matrix.hpp> | |
9 #include <boost/graph/graph_concepts.hpp> | |
10 #include <boost/graph/graph_traits.hpp> | |
11 #include <boost/property_map/property_map.hpp> | |
12 #include <boost/smart_ptr.hpp> | |
13 #include <boost/type_traits.hpp> | |
14 | |
15 #include <limits> | |
16 #include <map> | |
17 #include <utility> | |
18 #include <vector> | |
19 | |
20 namespace graph { | |
21 | |
22 template <class VertexListGraph, class Order, class Color> | |
23 typename boost::graph_traits<VertexListGraph>::vertices_size_type | |
24 SequentialVertexColoring(const VertexListGraph& graph, | |
25 Order order, | |
26 const Color& color) { | |
27 typedef boost::graph_traits<VertexListGraph> GraphTraits; | |
28 typedef typename GraphTraits::vertex_descriptor vertex_descriptor; | |
29 typedef typename GraphTraits::vertices_size_type size_type; | |
30 typedef typename boost::property_traits<Color>::value_type ColorType; | |
31 typedef typename boost::property_traits<Order>::value_type OrderType; | |
32 | |
33 boost::function_requires< boost::VertexListGraphConcept<VertexListGraph> >(); | |
34 boost::function_requires< boost::ReadWritePropertyMapConcept<Color, | |
35 vertex_descriptor> >(); | |
36 boost::function_requires< boost::IntegerConcept<ColorType> >(); | |
37 boost::function_requires< boost::ReadablePropertyMapConcept<Order, | |
38 size_type> >(); | |
39 BOOST_STATIC_ASSERT((boost::is_same<OrderType, vertex_descriptor>::value)); | |
40 | |
41 size_type max_color = 0; | |
42 const size_type number_of_vertices = boost::num_vertices(graph); | |
43 std::vector<size_type> mark(number_of_vertices, | |
44 std::numeric_limits<size_type>::max()); | |
45 | |
46 typename GraphTraits::vertex_iterator vertex, last_vertex; | |
47 for (tie(vertex, last_vertex) = boost::vertices(graph); vertex != last_vertex; | |
48 ++vertex) { | |
49 color[*vertex] = number_of_vertices - 1; // which means "not colored". | |
50 } | |
51 | |
52 for (size_type i = 0; i < number_of_vertices; ++i) { | |
53 vertex_descriptor current_vertex = order[i]; | |
54 // Mark all the colors of the adjacent vertices. | |
55 typename GraphTraits::adjacency_iterator adjacent_vertex, | |
56 last_adjacent_vertex; | |
57 for (tie(adjacent_vertex, last_adjacent_vertex) = | |
58 boost::adjacent_vertices(current_vertex, graph); | |
59 adjacent_vertex != last_adjacent_vertex; ++adjacent_vertex) { | |
60 mark[color[*adjacent_vertex]] = i; | |
61 } | |
62 // Find the smallest color unused by the adjacent vertices. | |
63 size_type smallest_color = 0; | |
64 while (smallest_color < max_color && mark[smallest_color] == i) { | |
65 ++smallest_color; | |
66 } | |
67 // If all the colors are used up, increase the number of colors. | |
68 if (smallest_color == max_color) { | |
69 ++max_color; | |
70 } | |
71 color[current_vertex] = smallest_color; | |
72 } | |
73 return max_color; | |
74 } | |
75 | |
76 Graph::Graph(int number_of_vertices, | |
77 std::vector< std::vector<int> > adjacency_matrix) | |
78 : number_of_vertices_(number_of_vertices), | |
79 adjacency_matrix_(adjacency_matrix) {} | |
80 | |
81 std::vector<int> Graph::GetColoring() const { | |
82 // Construct the boost graph. | |
83 typedef boost::adjacency_matrix<boost::undirectedS> UndirectedGraph; | |
84 typedef boost::graph_traits<UndirectedGraph> GraphTraits; | |
85 typedef GraphTraits::vertex_descriptor vertex_descriptor; | |
86 typedef GraphTraits::vertex_iterator vertex_iterator; | |
87 UndirectedGraph undirected_graph(number_of_vertices_); | |
88 for (int i = 0; i < number_of_vertices_; ++i) { | |
89 for (int j = i + 1; j < number_of_vertices_; ++j) { | |
90 if (adjacency_matrix_[i][j] == 1) { | |
91 boost::add_edge(i, j, undirected_graph); | |
92 } | |
93 } | |
94 } | |
95 // Create order as an externally stored property. | |
96 boost::scoped_array<vertex_descriptor> | |
97 order(new vertex_descriptor[number_of_vertices_]); | |
98 std::pair<vertex_iterator, vertex_iterator> first_last_vertex = | |
99 boost::vertices(undirected_graph); | |
100 for (int i = 0; i < number_of_vertices_; ++i) { | |
101 order[i] = first_last_vertex.first[i]; | |
102 } | |
103 // Create color as an externally stored property. | |
104 std::map<vertex_descriptor, int> color; | |
105 for (int i = 0; i < number_of_vertices_; ++i) { | |
106 color.insert(std::make_pair(vertex_descriptor(first_last_vertex.first[i]), | |
107 0)); | |
108 } | |
109 // Get the coloring. | |
110 SequentialVertexColoring(undirected_graph, order.get(), | |
111 boost::make_assoc_property_map(color)); | |
112 // Return the coloring. | |
113 std::vector<int> vertex_colors(number_of_vertices_); | |
114 for (int i = 0; i < number_of_vertices_; ++i) { | |
115 vertex_colors[i] = color[first_last_vertex.first[i]]; | |
116 } | |
117 return vertex_colors; | |
118 } | |
119 | |
120 } // namespace graph | |
OLD | NEW |