File indexing completed on 2025-01-18 09:37:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
0011 #define BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
0012
0013 #include <vector>
0014 #include <boost/graph/graph_traits.hpp>
0015 #include <boost/tuple/tuple.hpp>
0016 #include <boost/property_map/property_map.hpp>
0017 #include <boost/limits.hpp>
0018
0019 #ifdef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
0020 #include <iterator>
0021 #endif
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 namespace boost
0041 {
0042 template < class VertexListGraph, class OrderPA, class ColorMap >
0043 typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
0044 const VertexListGraph& G, OrderPA order, ColorMap color)
0045 {
0046 typedef graph_traits< VertexListGraph > GraphTraits;
0047 typedef typename GraphTraits::vertex_descriptor Vertex;
0048 typedef typename property_traits< ColorMap >::value_type size_type;
0049
0050 size_type max_color = 0;
0051 const size_type V = num_vertices(G);
0052
0053
0054
0055
0056
0057
0058
0059 std::vector< size_type > mark(V,
0060 std::numeric_limits< size_type >::max
0061 BOOST_PREVENT_MACRO_SUBSTITUTION());
0062
0063
0064 typename GraphTraits::vertex_iterator v, vend;
0065 for (boost::tie(v, vend) = vertices(G); v != vend; ++v)
0066 put(color, *v, V - 1);
0067
0068
0069 for (size_type i = 0; i < V; i++)
0070 {
0071 Vertex current = get(order, i);
0072 typename GraphTraits::adjacency_iterator v, vend;
0073
0074
0075
0076 for (boost::tie(v, vend) = adjacent_vertices(current, G); v != vend;
0077 ++v)
0078 mark[get(color, *v)] = i;
0079
0080
0081
0082 size_type j = 0;
0083
0084
0085
0086
0087
0088 while (j < max_color && mark[j] == i)
0089 ++j;
0090
0091 if (j == max_color)
0092 ++max_color;
0093
0094
0095 put(color, current, j);
0096 }
0097
0098 return max_color;
0099 }
0100
0101 template < class VertexListGraph, class ColorMap >
0102 typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
0103 const VertexListGraph& G, ColorMap color)
0104 {
0105 typedef typename graph_traits< VertexListGraph >::vertex_descriptor
0106 vertex_descriptor;
0107 typedef typename graph_traits< VertexListGraph >::vertex_iterator
0108 vertex_iterator;
0109
0110 std::pair< vertex_iterator, vertex_iterator > v = vertices(G);
0111 #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
0112 std::vector< vertex_descriptor > order(v.first, v.second);
0113 #else
0114 std::vector< vertex_descriptor > order;
0115 order.reserve(std::distance(v.first, v.second));
0116 while (v.first != v.second)
0117 order.push_back(*v.first++);
0118 #endif
0119 return sequential_vertex_coloring(G,
0120 make_iterator_property_map(order.begin(), identity_property_map(),
0121 graph_traits< VertexListGraph >::null_vertex()),
0122 color);
0123 }
0124 }
0125
0126 #endif