Example #1
0
    void add_middle_vertex_to_edges
    (const InputGraph& in_g, OutputGraph& out_g, WeightTag w_tag,
     IndexMap i_map, Orig2CopyMap orig_to_copy_map)
    {
      using boost::vertices;
      using boost::edges;
      using boost::num_edges;
      using boost::source;
      using boost::target;
      using boost::clear_vertex;
      using boost::add_vertex;
      using boost::add_edge;

      typedef typename boost::graph_traits<OutputGraph>::vertex_descriptor
        Vertex;
      typedef typename boost::property_map<OutputGraph, boost::edge_all_t>::type
        EdgePropertyMap;
      typedef typename boost::property_traits<EdgePropertyMap>::value_type
        EdgePropertyValue;
      typedef typename boost::property_map<OutputGraph, WeightTag>::type
        WeightMap;
      typedef typename boost::property_traits<WeightMap>::value_type
        WeightValue;
      typedef boost::tuple<Vertex, Vertex, EdgePropertyValue, WeightValue>
        EdgeTuple;
      typedef std::vector<EdgeTuple> EdgeTuples;

      boost::copy_graph(in_g, out_g,
          boost::vertex_index_map(i_map). orig_to_copy(orig_to_copy_map));

      WeightMap w_map = get(w_tag, out_g);
      EdgePropertyMap e_all_map = get(boost::edge_all, out_g);
      EdgeTuples edge_tuples;
      edge_tuples.reserve(num_edges(out_g));

      typename boost::graph_traits<OutputGraph>::edge_iterator ei, ei_end;
      for (boost::tie(ei, ei_end) = edges(out_g); ei != ei_end; ++ei) {
        edge_tuples.push_back
          (make_tuple(source(*ei, out_g), target(*ei, out_g),
                      get(e_all_map, *ei), get(w_map, *ei) / 2));
      }

      typename boost::graph_traits<OutputGraph>::vertex_iterator vi, vi_end;
      for (boost::tie(vi, vi_end) = vertices(out_g); vi != vi_end; ++vi) {
        clear_vertex(*vi, out_g);
      }

      // add_vertex must not invalid vertex_descriptor
      for (typename EdgeTuples::const_iterator eti = edge_tuples.begin(),
          eti_end = edge_tuples.end(); eti != eti_end; ++eti) {
        const Vertex new_v = add_vertex(out_g);
        put(w_map,
            add_edge(get<0>(*eti), new_v, get<2>(*eti), out_g).first,
            get<3>(*eti));
        put(w_map,
            add_edge(new_v, get<1>(*eti), get<2>(*eti), out_g).first,
            get<3>(*eti));
      }
    }
 reference operator[](key_type const& e) const
 {
   using boost::source;
   using boost::target;
   reference color1 = get(vertex_color_map_, source(e, *graph_));
   reference color2 = get(vertex_color_map_, target(e, *graph_));
   if (color1 == color2) {
     return color1;
   }
   return default_color_;
 }
    typename AcyclicGraph<LinearGraph, WeightMap>::type
    convert_linear_graph_to_acyclic_graph
    (const LinearGraph& g, WeightMap weight, CandidateMap candidate,
     Combine combine, Multiple multiple)
    {
      using boost::vertices;
      using boost::out_edges;
      using boost::out_degree;
      using boost::target;
      typedef boost::graph_traits<LinearGraph> Traits;
      typedef typename Traits::vertex_descriptor Vertex;
      typedef typename Traits::vertex_iterator VIter;
      typedef
        typename boost::property_traits<WeightMap>::value_type
        WeightValue;
      typedef typename AcyclicGraph<LinearGraph, WeightMap>::type AcyclicGraph;

      const typename std::iterator_traits<VIter>::difference_type
        num_v = boost::distance(vertices(g));
      const Vertex s = detail::get_start_vertex(g);

      std::vector<Vertex> new_vertices;
      new_vertices.reserve(num_v);
      std::vector<WeightValue> e_weight;
      e_weight.reserve(num_v - 1);

      // Collect info about a linear graph
      new_vertices.push_back(s);
      Vertex prev = s, now = s;
      typename Traits::out_edge_iterator ei, ei_end;
      do {
        boost::tie(ei, ei_end) = out_edges(now, g);
        if (target(*ei, g) == prev) {
          ++ei;
        }
        const Vertex next = target(*ei, g);
        if (get(candidate, next) || out_degree(next, g) == 1) {
          new_vertices.push_back(next);
        }

        if (get(candidate, now) || out_degree(now, g) == 1) {
          e_weight.push_back(get(weight, *ei));
        }
        else {
          e_weight.back() = combine(e_weight.back(), get(weight, *ei));
        }
        prev = now;
        now = next;
      } while (out_degree(now, g) != 1);

      // create acyclic graph's edge info
      const std::vector< std::pair<std::size_t, std::size_t> >
        new_edges(detail::create_new_edges(new_vertices.size()));
      const std::vector<WeightValue>
        new_weight(detail::convert_edge_weight(e_weight, combine, multiple));

      AcyclicGraph acyclic_g(
          boost::edges_are_sorted,
          new_edges.begin(), new_edges.end(),
          new_weight.begin(), new_vertices.size());

      // set vertex prop;
      typename boost::graph_traits<AcyclicGraph>::vertex_iterator vi, vi_end;
      for (boost::tie(vi, vi_end) = vertices(acyclic_g); vi != vi_end; ++vi) {
        acyclic_g[*vi] = new_vertices[get(boost::vertex_index, acyclic_g, *vi)];
      }

      return acyclic_g;
    }
 void operator()(Edge e, const Graph& g) {
   using boost::source;
   using boost::target;
   put(m_generator, target(e, g), get(m_generator, source(e, g)));
 }
Example #5
0
    typename boost::property_traits<WeightMap>::value_type
    uneven_dist_center
    (const Graph& g, std::size_t k, CenterMap center_map,
     WeightMap weight_map, EdgeColorMap color_map,
     ExistedCenterMap existed_center_map, CandidateMap candidate_map,
     Compare compare, Combine combine, Multiple multiple, DistZero zero)
    {
      using boost::vertices;
      using boost::source;
      using boost::target;
      typedef linear_graphs<Graph> LinearGraphs;
      typedef typename LinearGraphs::value_type LinearGraph;
      typedef typename AcyclicGraph<LinearGraph, WeightMap>::type AcyclicGraph;
      typedef
        typename boost::property_map<AcyclicGraph, boost::edge_bundle_t>::type
        AcyclicWeightMap;
      typedef
        detail::graph_info<AcyclicGraph, AcyclicWeightMap, Compare, Combine>
        GraphInfo;
      typedef std::vector<GraphInfo> GraphInfos;

      const LinearGraphs lgraphs
        = split_to_linear_graphs(g, color_map, existed_center_map);

      /* TODO
      if (std::accumulate(lgraphs.begin(), lgraphs.end(), 0) < k) {
        throw std::runtime_error("The number of center is too large");
      }
      */

      // convert linear graph to acyclic graph
      GraphInfos graph_infos;
      graph_infos.reserve(lgraphs.size());
      typename GraphInfo::OptimalSolutions optimal_solutions;
      typename GraphInfo::OptimalResouceContainers optimal_resource_containers;
      std::priority_queue<
          GraphInfo*, std::vector<GraphInfo*>, indirected_less<GraphInfo>
        > que;
      for (typename LinearGraphs::const_iterator
          it = lgraphs.begin(); it != lgraphs.end(); ++it) {
#ifndef __GXX_EXPERIMENTAL_CXX0X__
        graph_infos.push_back(
            GraphInfo(
              convert_linear_graph_to_acyclic_graph(
                *it, weight_map, candidate_map, combine, multiple),
              compare, combine, zero,
              optimal_solutions, optimal_resource_containers));
#else
        graph_infos.emplace_back(
            convert_linear_graph_to_acyclic_graph(
              *it, weight_map, candidate_map, combine, multiple),
            compare, combine, zero,
            optimal_solutions, optimal_resource_containers);
#endif
        if (graph_infos.back().is_allocable()) {
          que.push(&graph_infos.back());
        }
      }

      // allocate center number
      for (std::size_t i = 0; i < k; ++i) {
        if (que.empty()) {
          throw std::runtime_error("The number of center is too large");
        }
        GraphInfo& info = *(que.top());
        que.pop();
        info.update();
        if (info.is_allocable()) {
          que.push(&info);
        }
      }

      // set center_map
      // TODO Exclude existed center?
      typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
      typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
      boost::fill(
          vertices(g) | Canard::adaptors::mapped(center_map), false);
      for (typename GraphInfos::const_iterator
          it = graph_infos.begin(); it != graph_infos.end(); ++it) {
        const AcyclicGraph& graph = it->graph;
        typename GraphInfo::EdgeVector::const_iterator
          first = it->prev_solution.begin(), last = it->prev_solution.end();
        put(center_map, graph[target(*first, graph)], true);
        for (; first != last; ++first) {
          put(center_map, graph[source(*first, graph)], true);
        }
      }

      return boost::accumulate(
          graph_infos | boost::adaptors::transformed(
            std::mem_fun_ref(&GraphInfo::get_value)),
          zero);
    }