Ticket #8317: 0001-Added-edge-coloring-algorithm.2.patch

File 0001-Added-edge-coloring-algorithm.2.patch, 13.6 KB (added by Maciej Piechotka <uzytkownik2@…>, 9 years ago)

0001-Added-edge-coloring-algorithm.patch

  • new file oost/graph/edge_coloring.hpp

    From 890e078f7e1139ab39715496d5df9aed0fb09d8f Mon Sep 17 00:00:00 2001
    From: Maciej Piechotka <uzytkownik2@gmail.com>
    Date: Wed, 20 Mar 2013 18:02:18 +0100
    Subject: [PATCH] Added edge coloring algorithm
    
    ---
     boost/graph/edge_coloring.hpp         | 196 ++++++++++++++++++++++++++++++++++
     libs/graph/doc/edge_coloring.html     |  98 +++++++++++++++++
     libs/graph/doc/table_of_contents.html |   1 +
     libs/graph/example/Jamfile.v2         |   2 +-
     libs/graph/example/edge_coloring.cpp  |  70 ++++++++++++
     5 files changed, 366 insertions(+), 1 deletion(-)
     create mode 100644 boost/graph/edge_coloring.hpp
     create mode 100644 libs/graph/doc/edge_coloring.html
     create mode 100644 libs/graph/example/edge_coloring.cpp
    
    diff --git a/boost/graph/edge_coloring.hpp b/boost/graph/edge_coloring.hpp
    new file mode 100644
    index 0000000..6bb6246
    - +  
     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/graph/graph_traits.hpp>
     13#include <boost/graph/iteration_macros.hpp>
     14#include <boost/graph/properties.hpp>
     15#include <algorithm>
     16#include <limits>
     17#include <vector>
     18
     19/* This algorithm is to find coloring of an edges
     20
     21   Reference:
     22
     23   Misra, J., & Gries, D. (1992). A constructive proof of Vizing’s
     24   theorem. In Information Processing Letters.
     25*/
     26
     27namespace boost {
     28  namespace detail {
     29    template<typename Graph, typename ColorMap>
     30    bool
     31    is_free(const Graph &g,
     32            ColorMap color,
     33            typename boost::graph_traits<Graph>::vertex_descriptor u,
     34            typename boost::property_traits<ColorMap>::value_type free_color)
     35    {
     36      typedef typename boost::property_traits<ColorMap>::value_type color_t;
     37      if (free_color == std::numeric_limits<color_t>::max())
     38        return false;
     39      BGL_FORALL_OUTEDGES_T(u, e, g, Graph) {
     40        if (get(color, e) == free_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,
     51                typename boost::graph_traits<Graph>::vertex_descriptor x,
     52                typename boost::graph_traits<Graph>::vertex_descriptor y)
     53    {
     54      typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_t;
     55      std::vector<vertex_t> fan;
     56      fan.push_back(y);
     57      bool extended;
     58      do {
     59        extended = false;
     60        BGL_FORALL_OUTEDGES_T(x, e, g, Graph) {
     61          vertex_t v = target(e, g);
     62          if (is_free(g, color, fan.back(), get(color, e)) &&
     63              std::find(fan.begin(), fan.end(), v) == fan.end()) {
     64            fan.push_back(v);
     65            extended = true;
     66          }
     67        }
     68      } while(extended);
     69      return fan;
     70    }
     71    template<typename Graph, typename ColorMap>
     72    typename boost::property_traits<ColorMap>::value_type
     73    find_free_color(const Graph &g,
     74                    ColorMap color,
     75                    typename boost::graph_traits<Graph>::vertex_descriptor u)
     76    {
     77      typename boost::property_traits<ColorMap>::value_type c = 0;
     78      while (!is_free(g, color, u, c)) c++;
     79      return c;
     80    }
     81
     82    template<typename Graph, typename ColorMap>
     83    void
     84    invert_cd_path(const Graph &g,
     85                   ColorMap color,
     86                   typename boost::graph_traits<Graph>::vertex_descriptor x,
     87                   typename boost::graph_traits<Graph>::edge_descriptor eold,
     88                   typename boost::property_traits<ColorMap>::value_type c,
     89                   typename boost::property_traits<ColorMap>::value_type d)
     90    {
     91      put(color, eold, d);
     92      BGL_FORALL_OUTEDGES_T(x, e, g, Graph) {
     93        if (get(color, e) == d && e != eold) {
     94          invert_cd_path(g, color, target(e, g), e, d, c);
     95          return;
     96        }
     97      }
     98    }
     99
     100    template<typename Graph, typename ColorMap>
     101    void
     102    invert_cd_path(const Graph &g,
     103                   ColorMap color,
     104                   typename boost::graph_traits<Graph>::vertex_descriptor x,
     105                   typename boost::property_traits<ColorMap>::value_type c,
     106                   typename boost::property_traits<ColorMap>::value_type d)
     107    {
     108      BGL_FORALL_OUTEDGES_T(x, e, g, Graph) {
     109        if (get(color, e) == d) {
     110          invert_cd_path(g, color, target(e, g), e, d, c);
     111          return;
     112        }
     113      }
     114    }
     115   
     116    template<typename Graph, typename ColorMap, typename ForwardIterator>
     117    void
     118    rotate_fan(const Graph &g,
     119               ColorMap color,
     120               typename boost::graph_traits<Graph>::vertex_descriptor x,
     121               ForwardIterator begin,
     122               ForwardIterator end)
     123    {
     124      typedef typename boost::graph_traits<Graph>::edge_descriptor edge_t;
     125      if (begin == end) {
     126        return;
     127      }
     128      edge_t previous = edge(x, *begin, g).first;
     129      for (begin++; begin != end; begin++) {
     130        edge_t current = edge(x, *begin, g).first;
     131        put(color, previous, get(color, current));
     132        previous = current;
     133      }
     134    }
     135
     136    template<typename Graph, typename ColorMap>
     137    class find_free_in_fan
     138    {
     139    public:
     140      find_free_in_fan(const Graph &graph,
     141                       const ColorMap color,
     142                       typename boost::property_traits<ColorMap>::value_type d)
     143        : graph(graph),
     144          color(color),
     145          d(d) {}
     146      bool operator()(const typename boost::graph_traits<Graph>::vertex_descriptor u) const {
     147        return is_free(graph, color, u, d);
     148      }
     149    private:
     150      const Graph &graph;
     151      const ColorMap color;
     152      const typename boost::property_traits<ColorMap>::value_type d;
     153    };
     154  }
     155
     156  template<typename Graph, typename ColorMap>
     157  typename boost::property_traits<ColorMap>::value_type
     158  color_edge(const Graph &g,
     159             ColorMap color,
     160             typename boost::graph_traits<Graph>::edge_descriptor e)
     161  {
     162    typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_t;
     163    typedef typename boost::property_traits<ColorMap>::value_type color_t;
     164    typedef typename std::vector<vertex_t>::iterator fan_iterator;
     165    using namespace detail;
     166    vertex_t x = source(e, g), y = target(e, g);
     167    std::vector<vertex_t> fan = maximal_fan(g, color, x, y);
     168    color_t c = find_free_color(g, color, x);
     169    color_t d = find_free_color(g, color, fan.back());
     170    invert_cd_path(g, color, x, c, d);
     171    fan_iterator w = std::find_if(fan.begin(),
     172                                  fan.end(),
     173                                  find_free_in_fan<Graph, ColorMap>(g, color, d));
     174    rotate_fan(g, color, x, fan.begin(), w + 1);
     175    put(color, edge(x, *w, g).first, d);
     176    return std::max(c, d);
     177  }
     178
     179  template<typename Graph, typename ColorMap>
     180  typename boost::property_traits<ColorMap>::value_type
     181  edge_coloring(const Graph &g,
     182                ColorMap color)
     183  {
     184    typedef typename boost::property_traits<ColorMap>::value_type color_t;
     185    BGL_FORALL_EDGES_T(e, g, Graph) {
     186      put(color, e, std::numeric_limits<color_t>::max());
     187    }
     188    color_t colors = 0;
     189    BGL_FORALL_EDGES_T(e, g, Graph) {
     190      colors = std::max(colors, color_edge(g, color, e) + 1);
     191    }
     192    return colors;
     193  }
     194}
     195
     196#endif
  • new file libs/graph/doc/edge_coloring.html

    diff --git a/libs/graph/doc/edge_coloring.html b/libs/graph/doc/edge_coloring.html
    new file mode 100644
    index 0000000..5f6436d
    - +  
     1<HTML>
     2<!--
     3  ~~ Copyright (c) Maciej Piechotka 2013
     4  ~~
     5     Distributed under the Boost Software License, Version 1.0.
     6     (See accompanying file LICENSE_1_0.txt or copy at
     7     http://www.boost.org/LICENSE_1_0.txt)
     8-->
     9 
     10 
     11<Head>
     12<Title>Boost Graph Library: Edge Coloring</Title>
     13<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
     14        ALINK="#ff0000">
     15<IMG SRC="../../../boost.png"
     16     ALT="C++ Boost" width="277" height="86">
     17
     18<BR Clear>
     19
     20<H1>
     21<img src="figs/python.gif" alt="(Python)"/>
     22<TT>edge_coloring</TT>
     23</H1>
     24
     25
     26<P>
     27<DIV ALIGN="LEFT">
     28<TABLE CELLPADDING=3 border>
     29<TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
     30<TD ALIGN="LEFT">undirected, loop-free</TD>
     31</TR>
     32<TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
     33<TD ALIGN="LEFT">color</TD>
     34</TR>
     35<TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
     36<TD ALIGN="LEFT">time: <i>O(|E| |V|)</i> </TD>
     37</TR>
     38</TABLE>
     39</DIV>
     40
     41
     42<pre>
     43  template &lt;class Graph, class ColorMap&gt;
     44  typename boost::property_traits<ColorMap>::value_type
     45  edge_coloring(const Graph &amp;g, ColorMap color);
     46</pre>
     47
     48<p>Computes an edge coloring for the vertices in the graph, using
     49an algorithm proposed by Mista et al. []. Given edges ordered
     50e<sub>1</sub>, e<sub>2</sub>, ..., e<sub>n</sub> it assignes a
     51colors c<sub>1</sub>, c<sub>2</sub>, ..., c<sub>n</sub> in a way
     52that no vertex connects with 2 edges of the same color. Furthermore
     53at most m + 1 colors are used.
     54
     55<!-- King, I.P. An automatic reordering scheme for simultaneous equations derived from network analysis. Int. J. Numer. Methods Engrg. 2 (1970), 523-533 -->
     56
     57<h3>Where defined</h3>
     58<a href="../../../boost/graph/edge_coloring.hpp"><tt>boost/graph/edge_coloring.hpp</tt></a>
     59
     60<h3>Parameters</h3>
     61
     62IN: <tt>const Graph&amp; g</tt>
     63<blockquote>
     64  The graph object on which the algorithm will be applied. The type
     65  <tt>Graph</tt> must be a model of <a href="EdgeListGraph.html">
     66  Edge List Graph</a> and <a href="IncidenceGraph.html">Incidence
     67  Graph</a>.
     68</blockquote>
     69
     70OUT: <tt>ColorMap color</tt>
     71<blockquote>
     72  This property map records the colors of each edges. It must be a
     73  model of <A HREF="../../property_map/doc/ReadWritePropertyMap.html">
     74  Read/Write Property Map</A> whose key type is the same as the edge
     75  descriptor type of the graph and whose value type is an integral type
     76  that can store all values smaller or equal to m.
     77</blockquote>
     78
     79
     80<h3>Example</h3>
     81
     82See <A
     83href="../example/edge_coloring.cpp"><tt>example/king_ordering.cpp</tt></A>.
     84
     85<h3>See Also</h3>
     86
     87<A href="./sequential_vertex_coloring.html">sequential vertex ordering</tt></A>.
     88
     89<br>
     90<HR>
     91<TABLE>
     92<TR valign=top>
     93<TD nowrap>Copyright &copy; 2013</TD><TD>
     94Maciej Piechotka (<A HREF="mailto:uzytkownik2@gmail.com">uzytkownik2@gmail.com</A>)
     95</TD></TR></TABLE>
     96
     97</BODY>
     98</HTML>
  • libs/graph/doc/table_of_contents.html

    diff --git a/libs/graph/doc/table_of_contents.html b/libs/graph/doc/table_of_contents.html
    index 0fd36e8..f2131c9 100644
    a b  
    285285                  <ol>
    286286                      <li><a href="metric_tsp_approx.html"><tt>metric_tsp_approx</tt></a></li>
    287287                      <LI><A href="sequential_vertex_coloring.html"><tt>sequential_vertex_coloring</tt></A>
     288                      <LI><A href="edge_coloring.html"><tt>edge_coloring</tt></A>
    288289                      <LI><A href="is_bipartite.html"><tt>is_bipartite</tt></A> (including two-coloring of bipartite graphs)
    289290                      <LI><A href="find_odd_cycle.html"><tt>find_odd_cycle</tt></A>
    290291                      <LI><A href="maximum_adjacency_search.html"><tt>maximum_adjacency_search</tt></A>
  • libs/graph/example/Jamfile.v2

    diff --git a/libs/graph/example/Jamfile.v2 b/libs/graph/example/Jamfile.v2
    index ce155c6..8998182 100644
    a b exe subgraph_properties : subgraph_properties.cpp ;  
    5050exe vf2_sub_graph_iso_example : vf2_sub_graph_iso_example.cpp ;
    5151exe vf2_sub_graph_iso_multi_example : vf2_sub_graph_iso_multi_example.cpp ;
    5252exe sloan_ordering : sloan_ordering.cpp ;
    53 
     53exe edge_coloring : edge_coloring.cpp ;
  • new file libs/graph/example/edge_coloring.cpp

    diff --git a/libs/graph/example/edge_coloring.cpp b/libs/graph/example/edge_coloring.cpp
    new file mode 100644
    index 0000000..81a8a38
    - +  
     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
     10#include <boost/config.hpp>
     11#include <boost/graph/adjacency_list.hpp>
     12#include <boost/graph/edge_coloring.hpp>
     13#include <boost/graph/properties.hpp>
     14
     15/*
     16  Sample output
     17  Colored using 5 colors
     18    a-d: 4
     19    a-f: 0
     20    b-c: 2
     21    b-e: 3
     22    b-g: 1
     23    b-j: 0
     24    c-d: 0
     25    c-e: 1
     26    d-f: 2
     27    d-i: 1
     28    e-g: 4
     29    f-g: 3
     30    f-h: 1
     31    g-h: 0
     32*/
     33
     34int main(int, char *[])
     35{
     36  using namespace boost;
     37  using namespace std;
     38  typedef adjacency_list<vecS, vecS, undirectedS, no_property, size_t, no_property> Graph;
     39
     40  typedef std::pair<std::size_t, std::size_t> Pair;
     41  Pair edges[14] = { Pair(0,3), //a-d
     42                     Pair(0,5),  //a-f
     43                     Pair(1,2),  //b-c
     44                     Pair(1,4),  //b-e
     45                     Pair(1,6),  //b-g
     46                     Pair(1,9),  //b-j
     47                     Pair(2,3),  //c-d
     48                     Pair(2,4),  //c-e
     49                     Pair(3,5),  //d-f
     50                     Pair(3,8),  //d-i
     51                     Pair(4,6),  //e-g
     52                     Pair(5,6),  //f-g
     53                     Pair(5,7),  //f-h
     54                     Pair(6,7) }; //g-h
     55
     56  Graph G(10);
     57
     58  for (size_t i = 0; i < sizeof(edges)/sizeof(edges[0]); i++)
     59    add_edge(edges[i].first, edges[i].second, G).first;
     60
     61  size_t colors = edge_coloring(G, get(edge_bundle, G));
     62
     63  cout << "Colored using " << colors << " colors" << endl;
     64  for (size_t i = 0; i < sizeof(edges)/sizeof(edges[0]); i++) {
     65    cout << "  " << (char)('a' + edges[i].first) << "-" << (char)('a' + edges[i].second) << ": " << G[edge(edges[i].first, edges[i].second, G).first] << endl;
     66  }
     67
     68  return 0;
     69}
     70