Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:36

0001 //=======================================================================
0002 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
0003 // Copyright 2004 The Trustees of Indiana University
0004 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
0005 //
0006 // Distributed under the Boost Software License, Version 1.0. (See
0007 // accompanying file LICENSE_1_0.txt or copy at
0008 // http://www.boost.org/LICENSE_1_0.txt)
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 /* This algorithm is to find coloring of a graph
0024 
0025    Algorithm:
0026    Let G = (V,E) be a graph with vertices (somehow) ordered v_1, v_2, ...,
0027    v_n. For k = 1, 2, ..., n the sequential algorithm assigns v_k to the
0028    smallest possible color.
0029 
0030    Reference:
0031 
0032    Thomas F. Coleman and Jorge J. More, Estimation of sparse Jacobian
0033    matrices and graph coloring problems. J. Numer. Anal. V20, P187-209, 1983
0034 
0035    v_k is stored as o[k] here.
0036 
0037    The color of the vertex v will be stored in color[v].
0038    i.e., vertex v belongs to coloring color[v] */
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     // We need to keep track of which colors are used by
0054     // adjacent vertices. We do this by marking the colors
0055     // that are used. The mark array contains the mark
0056     // for each color. The length of mark is the
0057     // number of vertices since the maximum possible number of colors
0058     // is the number of vertices.
0059     std::vector< size_type > mark(V,
0060         std::numeric_limits< size_type >::max
0061             BOOST_PREVENT_MACRO_SUBSTITUTION());
0062 
0063     // Initialize colors
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     // Determine the color for every vertex one by one
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         // Mark the colors of vertices adjacent to current.
0075         // i can be the value for marking since i increases successively
0076         for (boost::tie(v, vend) = adjacent_vertices(current, G); v != vend;
0077              ++v)
0078             mark[get(color, *v)] = i;
0079 
0080         // Next step is to assign the smallest un-marked color
0081         // to the current vertex.
0082         size_type j = 0;
0083 
0084         // Scan through all useable colors, find the smallest possible
0085         // color that is not used by neighbors.  Note that if mark[j]
0086         // is equal to i, color j is used by one of the current vertex's
0087         // neighbors.
0088         while (j < max_color && mark[j] == i)
0089             ++j;
0090 
0091         if (j == max_color) // All colors are used up. Add one more color
0092             ++max_color;
0093 
0094         // At this point, j is the smallest possible color
0095         put(color, current, j); // Save the color of vertex current
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