Commit 979e9680 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Implement sequential_vertex_coloring()

parent 3a7ae957
......@@ -28,6 +28,7 @@ libgraph_tool_topology_la_SOURCES = \
graph_random_matching.cc \
graph_random_spanning_tree.cc \
graph_reciprocity.cc \
graph_sequential_color.cc \
graph_similarity.cc \
graph_subgraph_isomorphism.cc \
graph_topological_sort.cc \
......
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007-2012 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "graph_filtering.hh"
#include "graph.hh"
#include "graph_properties.hh"
#include <boost/graph/sequential_vertex_coloring.hpp>
using namespace std;
using namespace boost;
using namespace graph_tool;
struct get_coloring
{
template <class Graph, class OrderMap, class ColorMap>
void operator()(Graph& g, OrderMap order, ColorMap color, size_t& nc) const
{
nc = sequential_vertex_coloring(g, order, color);
}
};
typedef property_map_types::apply<mpl::vector<int32_t, int64_t>,
GraphInterface::vertex_index_map_t,
mpl::bool_<false> >::type
int_properties;
size_t sequential_coloring(GraphInterface& gi, boost::any order,
boost::any color)
{
size_t nc = 0;
run_action<>()
(gi, bind<void>(get_coloring(), _1, _2, _3, ref(nc)),
vertex_integer_properties(), int_properties())(order, color);
return nc;
}
......@@ -38,10 +38,13 @@ void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
python::list vmapping, python::list emapping,
size_t n_max, size_t seed);
double reciprocity(GraphInterface& gi);
size_t sequential_coloring(GraphInterface& gi, boost::any order,
boost::any color);
bool is_bipartite(GraphInterface& gi, boost::any part_map);
void get_random_spanning_tree(GraphInterface& gi, size_t root,
boost::any weight_map, boost::any tree_map,
size_t seed);
vector<int32_t> get_tsp(GraphInterface& gi, size_t src, boost::any weight_map);
void export_components();
void export_similarity();
......@@ -63,8 +66,10 @@ BOOST_PYTHON_MODULE(libgraph_tool_topology)
def("transitive_closure", &transitive_closure);
def("is_planar", &is_planar);
def("reciprocity", &reciprocity);
def("sequential_coloring", &sequential_coloring);
def("is_bipartite", &is_bipartite);
def("random_spanning_tree", &get_random_spanning_tree);
def("get_tsp", &get_tsp);
export_components();
export_similarity();
export_dists();
......
......@@ -43,6 +43,7 @@ Summary
topological_sort
transitive_closure
tsp_tour
sequential_vertex_coloring
label_components
label_biconnected_components
label_largest_component
......@@ -68,10 +69,11 @@ __all__ = ["isomorphism", "subgraph_isomorphism", "mark_subgraph",
"max_cardinality_matching", "max_independent_vertex_set",
"min_spanning_tree", "random_spanning_tree", "dominator_tree",
"topological_sort", "transitive_closure", "tsp_tour",
"label_components", "label_largest_component",
"label_biconnected_components", "label_out_component",
"shortest_distance", "shortest_path", "pseudo_diameter",
"is_bipartite", "is_planar", "similarity", "edge_reciprocity"]
"sequential_vertex_coloring", "label_components",
"label_largest_component", "label_biconnected_components",
"label_out_component", "shortest_distance", "shortest_path",
"pseudo_diameter", "is_bipartite", "is_planar", "similarity",
"edge_reciprocity"]
def similarity(g1, g2, label1=None, label2=None, norm=True):
......@@ -1539,12 +1541,56 @@ def tsp_tour(g, src, weight=None):
"""
if g.is_directed():
raise ValueError("Graph must be undirected.")
def sequential_vertex_coloring(g, order=None, color=None):
"""Returns a vertex coloring of the graph.
Parameters
----------
g : :class:`~graph_tool.Graph`
Graph to be used.
order : :class:`~graph_tool.PropertyMap` (optional, default: None)
Order with which the vertices will be colored.
color : :class:`~graph_tool.PropertyMap` (optional, default: None)
Integer-valued vertex property map to store the colors.
Returns
-------
color : :class:`~graph_tool.PropertyMap`
Integer-valued vertex property map with the vertex colors.
Notes
-----
The time complexity is :math:`O(V(d+k))`, where :math:`V` is the number of
vertices, :math:`d` is the maximum degree of the vertices in the graph, and
:math:`k` is the number of colors used.
Examples
--------
>>> g = gt.lattice([20, 20])
>>> colors = gt.sequential_vertex_coloring(g, g.vertex(0))
>>> print(colors.a)
[0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1
0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0
1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0]
References
----------
.. [sgc-bgl] http://www.boost.org/libs/graph/doc/sequential_vertex_coloring.html
.. [graph-coloring] http://en.wikipedia.org/wiki/Graph_coloring
"""
if order is None:
order = g.vertex_index
if color is None:
color = g.new_vertex_property("int")
tour = libgraph_tool_topology.\
get_tsp(g._Graph__graph, int(src),
_prop("e", g, weight))
return tour.a.copy()
sequential_coloring(g._Graph__graph,
_prop("v", g, order),
_prop("v", g, color))
return color
from .. flow import libgraph_tool_flow
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment