]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/graph/sequential_vertex_coloring.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / graph / sequential_vertex_coloring.hpp
CommitLineData
7c673cae
FG
1//=======================================================================
2// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3// Copyright 2004 The Trustees of Indiana University
4// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9//=======================================================================
10#ifndef BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
11#define BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
12
13#include <vector>
14#include <boost/graph/graph_traits.hpp>
15#include <boost/tuple/tuple.hpp>
16#include <boost/property_map/property_map.hpp>
17#include <boost/limits.hpp>
18
19#ifdef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
f67539c2 20#include <iterator>
7c673cae
FG
21#endif
22
23/* This algorithm is to find coloring of a graph
24
f67539c2 25 Algorithm:
7c673cae
FG
26 Let G = (V,E) be a graph with vertices (somehow) ordered v_1, v_2, ...,
27 v_n. For k = 1, 2, ..., n the sequential algorithm assigns v_k to the
f67539c2 28 smallest possible color.
7c673cae
FG
29
30 Reference:
31
32 Thomas F. Coleman and Jorge J. More, Estimation of sparse Jacobian
33 matrices and graph coloring problems. J. Numer. Anal. V20, P187-209, 1983
34
f67539c2 35 v_k is stored as o[k] here.
7c673cae
FG
36
37 The color of the vertex v will be stored in color[v].
38 i.e., vertex v belongs to coloring color[v] */
39
f67539c2
TL
40namespace boost
41{
42template < class VertexListGraph, class OrderPA, class ColorMap >
43typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
44 const VertexListGraph& G, OrderPA order, ColorMap color)
45{
46 typedef graph_traits< VertexListGraph > GraphTraits;
7c673cae 47 typedef typename GraphTraits::vertex_descriptor Vertex;
f67539c2
TL
48 typedef typename property_traits< ColorMap >::value_type size_type;
49
7c673cae
FG
50 size_type max_color = 0;
51 const size_type V = num_vertices(G);
52
53 // We need to keep track of which colors are used by
54 // adjacent vertices. We do this by marking the colors
55 // that are used. The mark array contains the mark
56 // for each color. The length of mark is the
57 // number of vertices since the maximum possible number of colors
58 // is the number of vertices.
f67539c2
TL
59 std::vector< size_type > mark(V,
60 std::numeric_limits< size_type >::max
61 BOOST_PREVENT_MACRO_SUBSTITUTION());
62
63 // Initialize colors
7c673cae
FG
64 typename GraphTraits::vertex_iterator v, vend;
65 for (boost::tie(v, vend) = vertices(G); v != vend; ++v)
f67539c2
TL
66 put(color, *v, V - 1);
67
68 // Determine the color for every vertex one by one
69 for (size_type i = 0; i < V; i++)
70 {
71 Vertex current = get(order, i);
72 typename GraphTraits::adjacency_iterator v, vend;
73
74 // Mark the colors of vertices adjacent to current.
75 // i can be the value for marking since i increases successively
76 for (boost::tie(v, vend) = adjacent_vertices(current, G); v != vend;
77 ++v)
78 mark[get(color, *v)] = i;
79
80 // Next step is to assign the smallest un-marked color
81 // to the current vertex.
82 size_type j = 0;
83
84 // Scan through all useable colors, find the smallest possible
85 // color that is not used by neighbors. Note that if mark[j]
86 // is equal to i, color j is used by one of the current vertex's
87 // neighbors.
88 while (j < max_color && mark[j] == i)
89 ++j;
90
91 if (j == max_color) // All colors are used up. Add one more color
92 ++max_color;
93
94 // At this point, j is the smallest possible color
95 put(color, current, j); // Save the color of vertex current
7c673cae 96 }
f67539c2 97
7c673cae 98 return max_color;
f67539c2
TL
99}
100
101template < class VertexListGraph, class ColorMap >
102typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
103 const VertexListGraph& G, ColorMap color)
104{
105 typedef typename graph_traits< VertexListGraph >::vertex_descriptor
106 vertex_descriptor;
107 typedef typename graph_traits< VertexListGraph >::vertex_iterator
108 vertex_iterator;
109
110 std::pair< vertex_iterator, vertex_iterator > v = vertices(G);
7c673cae 111#ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
f67539c2 112 std::vector< vertex_descriptor > order(v.first, v.second);
7c673cae 113#else
f67539c2 114 std::vector< vertex_descriptor > order;
7c673cae 115 order.reserve(std::distance(v.first, v.second));
f67539c2
TL
116 while (v.first != v.second)
117 order.push_back(*v.first++);
7c673cae 118#endif
f67539c2
TL
119 return sequential_vertex_coloring(G,
120 make_iterator_property_map(order.begin(), identity_property_map(),
121 graph_traits< VertexListGraph >::null_vertex()),
122 color);
123}
7c673cae
FG
124}
125
126#endif