| 1 | //======================================================================= |
| 2 | // Copyright 2013 Maciej Piechotka |
| 3 | // Authors: Maciej Piechotka |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. (See |
| 6 | // accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | //======================================================================= |
| 9 | #ifndef BOOST_GRAPH_EDGE_COLORING_HPP |
| 10 | #define BOOST_GRAPH_EDGE_COLORING_HPP |
| 11 | |
| 12 | #include <boost/foreach.hpp> |
| 13 | #include <boost/phoenix.hpp> |
| 14 | #include <algorithm> |
| 15 | #include <limits> |
| 16 | #include <vector> |
| 17 | |
| 18 | /* This algorithm is to find coloring of an edges |
| 19 | |
| 20 | Reference: |
| 21 | |
| 22 | Misra, J., & Gries, D. (1992). A constructive proof of Vizing’s |
| 23 | theorem. In Information Processing Letters. |
| 24 | */ |
| 25 | |
| 26 | namespace boost { |
| 27 | namespace detail { |
| 28 | template<typename Graph, typename ColorMap> |
| 29 | bool |
| 30 | is_free(const Graph &g, |
| 31 | ColorMap color_map, |
| 32 | typename boost::graph_traits<Graph>::vertex_descriptor u, |
| 33 | typename boost::property_traits<ColorMap>::value_type color) |
| 34 | { |
| 35 | typedef typename boost::property_traits<ColorMap>::value_type color_t; |
| 36 | if (color == std::numeric_limits<color_t>::max()) |
| 37 | return false; |
| 38 | BOOST_FOREACH(typename boost::graph_traits<Graph>::edge_descriptor e, |
| 39 | boost::out_edges(u, g)) { |
| 40 | if (boost::get(color_map, e) == color) { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | template<typename Graph, typename ColorMap> |
| 48 | std::vector<typename boost::graph_traits<Graph>::vertex_descriptor> |
| 49 | maximal_fan(const Graph &g, |
| 50 | ColorMap color_map, |
| 51 | typename boost::graph_traits<Graph>::vertex_descriptor x, |
| 52 | typename boost::graph_traits<Graph>::vertex_descriptor y) |
| 53 | { |
| 54 | typedef typename boost::property_traits<ColorMap>::value_type color_t; |
| 55 | typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_t; |
| 56 | std::vector<vertex_t> fan; |
| 57 | fan.push_back(y); |
| 58 | bool extended; |
| 59 | do { |
| 60 | extended = false; |
| 61 | BOOST_FOREACH(typename boost::graph_traits<Graph>::edge_descriptor e, |
| 62 | boost::out_edges(x, g)) { |
| 63 | vertex_t v = boost::target(e, g); |
| 64 | if (is_free(g, color_map, fan.back(), boost::get(color_map, e)) && |
| 65 | std::find(fan.begin(), fan.end(), v) == fan.end()) { |
| 66 | fan.push_back(v); |
| 67 | extended = true; |
| 68 | } |
| 69 | } |
| 70 | } while(extended); |
| 71 | return fan; |
| 72 | } |
| 73 | template<typename Graph, typename ColorMap> |
| 74 | typename boost::property_traits<ColorMap>::value_type |
| 75 | find_free_color(const Graph &g, |
| 76 | ColorMap color_map, |
| 77 | typename boost::graph_traits<Graph>::vertex_descriptor u) |
| 78 | { |
| 79 | typename boost::property_traits<ColorMap>::value_type c = 0; |
| 80 | while (!is_free(g, color_map, u, c)) c++; |
| 81 | return c; |
| 82 | } |
| 83 | |
| 84 | template<typename Graph, typename ColorMap> |
| 85 | void |
| 86 | invert_cd_path(const Graph &g, |
| 87 | ColorMap color_map, |
| 88 | typename boost::graph_traits<Graph>::vertex_descriptor x, |
| 89 | typename boost::graph_traits<Graph>::edge_descriptor eold, |
| 90 | typename boost::property_traits<ColorMap>::value_type c, |
| 91 | typename boost::property_traits<ColorMap>::value_type d) |
| 92 | { |
| 93 | typedef typename boost::graph_traits<Graph>::edge_descriptor edge_t; |
| 94 | boost::put(color_map, eold, d); |
| 95 | BOOST_FOREACH(edge_t e, boost::out_edges(x, g)) { |
| 96 | if (boost::get(color_map, e) == d && e != eold) { |
| 97 | invert_cd_path(g, color_map, boost::target(e, g), e, d, c); |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | template<typename Graph, typename ColorMap> |
| 104 | void |
| 105 | invert_cd_path(const Graph &g, |
| 106 | ColorMap color_map, |
| 107 | typename boost::graph_traits<Graph>::vertex_descriptor x, |
| 108 | typename boost::property_traits<ColorMap>::value_type c, |
| 109 | typename boost::property_traits<ColorMap>::value_type d) |
| 110 | { |
| 111 | typedef typename boost::graph_traits<Graph>::edge_descriptor edge_t; |
| 112 | BOOST_FOREACH(edge_t e, boost::out_edges(x, g)) { |
| 113 | if (boost::get(color_map, e) == d) { |
| 114 | invert_cd_path(g, color_map, boost::target(e, g), e, d, c); |
| 115 | return; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | template<typename Graph, typename ColorMap, typename ForwardIterator> |
| 121 | void |
| 122 | rotate_fan(const Graph &g, |
| 123 | ColorMap color_map, |
| 124 | typename boost::graph_traits<Graph>::vertex_descriptor x, |
| 125 | ForwardIterator begin, |
| 126 | ForwardIterator end) |
| 127 | { |
| 128 | typedef typename boost::graph_traits<Graph>::edge_descriptor edge_t; |
| 129 | typedef typename boost::property_traits<ColorMap>::value_type color_t; |
| 130 | if (begin == end) { |
| 131 | return; |
| 132 | } |
| 133 | edge_t previous = boost::edge(x, *begin, g).first; |
| 134 | for (begin++; begin != end; begin++) { |
| 135 | edge_t current = boost::edge(x, *begin, g).first; |
| 136 | boost::put(color_map, previous, boost::get(color_map, current)); |
| 137 | previous = current; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | template<typename Graph, typename ColorMap> |
| 143 | typename boost::property_traits<ColorMap>::value_type |
| 144 | color_edge(const Graph &g, |
| 145 | ColorMap color_map, |
| 146 | typename boost::graph_traits<Graph>::edge_descriptor e) |
| 147 | { |
| 148 | typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_t; |
| 149 | typedef typename boost::property_traits<ColorMap>::value_type color_t; |
| 150 | typedef typename std::vector<vertex_t>::iterator fan_iterator; |
| 151 | using namespace detail; |
| 152 | namespace ph = boost::phoenix; |
| 153 | vertex_t x = boost::source(e, g), y = boost::target(e, g); |
| 154 | std::vector<vertex_t> fan = maximal_fan(g, color_map, x, y); |
| 155 | color_t c = find_free_color(g, color_map, x); |
| 156 | color_t d = find_free_color(g, color_map, fan.back()); |
| 157 | invert_cd_path(g, color_map, x, c, d); |
| 158 | fan_iterator w = std::find_if(fan.begin(), |
| 159 | fan.end(), |
| 160 | ph::bind(&is_free<Graph, ColorMap>, |
| 161 | ph::val(g), |
| 162 | ph::val(color_map), |
| 163 | ph::arg_names::arg1, |
| 164 | ph::val(d))); |
| 165 | rotate_fan(g, color_map, x, fan.begin(), w + 1); |
| 166 | boost::put(color_map, boost::edge(x, *w, g).first, d); |
| 167 | return std::max(c, d); |
| 168 | } |
| 169 | |
| 170 | template<typename Graph, typename ColorMap> |
| 171 | typename boost::property_traits<ColorMap>::value_type |
| 172 | edge_coloring(const Graph &g, |
| 173 | ColorMap color_map) |
| 174 | { |
| 175 | typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_t; |
| 176 | typedef typename boost::graph_traits<Graph>::edge_descriptor edge_t; |
| 177 | typedef typename boost::property_traits<ColorMap>::value_type color_t; |
| 178 | BOOST_FOREACH(edge_t e, boost::edges(g)) { |
| 179 | boost::put(color_map, e, std::numeric_limits<color_t>::max()); |
| 180 | } |
| 181 | color_t colors = 0; |
| 182 | BOOST_FOREACH(edge_t e, boost::edges(g)) { |
| 183 | colors = std::max(colors, color_edge(g, color_map, e) + 1); |
| 184 | } |
| 185 | return colors; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | #endif |