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_;
 }
 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 #4
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);
    }