Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::filtered_graph filtering on vertex color... if I only had a colormap
From: Alex HighViz (alexhighviz_at_[hidden])
Date: 2017-11-18 04:56:41




> From: Boost-users [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Brian Davis via Boost-users
>Lets say I have a graph g (of type graph_t)
> and a color map (boy I wish I had a colormap):

There is not one single color_map class, it is a concept. Any object that has the following defined will do as a color map:

Color c = get(my_color_map, vertex); // get the color of a vertex
put(my_color_map, vertex, c); // set the color of a vertex
my_color_map[vertex] = c; // get the color of a vertex (for an LValue property map)
c = my_color_map[vertex]; // set the color of a vertex (for an LValue property map)


> typedef boost::property_map<graph_t, boost::vertex_color_t>::type color_map_t;
> or is it?

That would only work if your graph type have a vertex_color property embedded in it, which is probably doesn't. If It did you could simply write

auto colormap = boost::get(boost::vertex_color, g);


The following that you propose look perfectly fine

> auto indexmap = boost::get(boost::vertex_index, g);
> auto colors = boost::make_vector_property_map<boost::default_color_type>(indexmap);

This looks good and would the preferred approach if you do not know the number of vertices when you construct the map, why did it not work?

If you do know the number of vertices, use shared_array_property_map instead.

Your code example is a bit garbled, but I think the problem is that you are constructing the filter with only two arguments (graph,vertex predicate), whereas the constructor expects (graph, edge predicate, vertex predicate) or (graph, edge predicate).

It is a pity that BGL does not have the enthusiastic support anymore that it used to have. Once you get used to it, it is very powerful.

The following compiles and runs correctly for me:
 
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graph_utility.hpp>

#include <boost/property_map/property_map.hpp>

#include <iostream>

template<class ColorMap>
struct is_white
{
  is_white() {}
  is_white(ColorMap cm) : m_cm(cm)
  {}

  using key_type = typename boost::property_traits<ColorMap>::key_type;

  bool operator()(const key_type& v) const
  {
    return get(m_cm, v) != boost::default_color_type::white_color;
  }

  ColorMap m_cm;
};

int main()
{
  using graph_type = boost::adjacency_list<>;
  graph_type g(5);

  add_edge(0, 1, g);
  add_edge(0, 2, g);
  add_edge(2, 3, g);
  add_edge(2, 4, g);
  add_edge(3, 1, g);
  add_edge(4, 2, g);

  auto index_map = boost::get(boost::vertex_index, g);
  auto color_map = boost::make_vector_property_map<boost::default_color_type>(index_map);
 
  using color_map_type = decltype(color_map);
  using edge_predicate = boost::keep_all;
  using vertex_predicate = is_white<color_map_type>;

  color_map[0] = boost::default_color_type::black_color;
  color_map[1] = boost::default_color_type::white_color;
  color_map[2] = boost::default_color_type::black_color;
  color_map[3] = boost::default_color_type::black_color;
  color_map[4] = boost::default_color_type::black_color;

  using filtered_graph_type = boost::filtered_graph<graph_type, edge_predicate, vertex_predicate>;

  filtered_graph_type fg(g, edge_predicate(), vertex_predicate(color_map));

  std::cout << "unfiltered edges: " << std::endl;
  boost::print_graph(g, "01234");

  std::cout << "filtered edges:" << std::endl;
  boost::print_graph(fg, "012345");

  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net