Example #1
0
my_graph create_my_graph()
{
  using boost::add_edge;
  using boost::add_vertex;
  using my_vertex_descriptor = my_graph::vertex_descriptor;
  my_graph g;

  //All vertex names
  //Note: cannot use spaces
  std::vector<std::string> names;
  names.push_back("Mr_A");
  names.push_back("Mrs_B");
  names.push_back("Dr_C");
  names.push_back("Prof_D");

  const my_vertex_descriptor v0 = add_vertex(names[0],g);
  const my_vertex_descriptor v1 = add_vertex(names[1],g);
  const my_vertex_descriptor v2 = add_vertex(names[2],g);
  const my_vertex_descriptor v3 = add_vertex(names[3],g);

  std::vector<double> frequencies;
  frequencies.push_back(0.9);
  frequencies.push_back(0.5);
  frequencies.push_back(0.6);
  frequencies.push_back(0.1);

  add_edge(v0,v1,frequencies[0],g);
  add_edge(v1,v2,frequencies[1],g);
  add_edge(v2,v3,frequencies[2],g);
  add_edge(v0,v3,frequencies[3],g);

  return g;
}
Example #2
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));
      }
    }
  void SetUp() override {
    using boost::add_edge;
    this->g = GraphType(3);

    // Add edge with an associated SpatialEdge at construction.
    SG::PointType p0{{0, 0, 0}};
    SG::PointType e1{{1, 0, 0}};
    SG::PointType e2{{2, 0, 0}};
    g[0].pos = p0;
    g[1].pos = e1;
    g[2].pos = e2;

    SG::PointType e05{{0.5, 0, 0}};
    SG::PointType e15{{1.5, 0, 0}};
    SG::SpatialEdge se05;
    se05.edge_points.insert(std::end(se05.edge_points), {e05});
    add_edge(0, 1, se05, g);
    SG::SpatialEdge se15;
    se15.edge_points.insert(std::end(se15.edge_points), {e15});
    add_edge(1, 2, se15, g);
  };
Example #4
0
    void readFromAdjacencyList( const string& fname, GraphT& G ) {
        typedef typename boost::graph_traits< GraphT >::vertex_descriptor Vertex;
        typedef typename boost::graph_traits< GraphT >::edge_descriptor Edge;
        typedef unordered_map<string,Vertex> svMap;

        svMap namePosMap;
        bool inserted;
        Vertex u,v;
        typename unordered_map<string,Vertex>::iterator pos;

        string line;
        typedef vector< string > splitVectorT;
        ifstream gfile(fname);
        size_t numInsertedVerts = 0;
        if ( gfile.is_open() ) {
            while( gfile.good() ) {
                getline( gfile, line, '\n' );
                if ( gfile.eof() ) { break; }
                boost::algorithm::trim(line);
                auto vline = line.substr( 0, line.find_first_of('#') );
                splitVectorT splitVec;
                split( splitVec, vline, is_any_of(" \t"), token_compress_on );

                if ( splitVec.size() > 0  and vline.size() > 0 ) {
                    auto fromVert = splitVec[0];
                    boost::tie( pos, inserted ) = namePosMap.insert( std::make_pair(fromVert,Vertex()) );
                    if (inserted) {
                        ++numInsertedVerts;
                        u = add_vertex(G);
                        G[u].name = fromVert;
                        // This will happen later
                        // G[u].idx = nameMap[fromVert];
                        pos->second = u;
                    } else {
                        u = pos->second;
                    }

                    for( auto tgtIt = splitVec.begin() + 1; tgtIt != splitVec.end(); ++tgtIt ) {
                        auto& tgt = *tgtIt;
                        boost::tie(pos, inserted) = namePosMap.insert(std::make_pair(tgt, Vertex()));
                        if (inserted) {
                            ++numInsertedVerts;
                            v = add_vertex(G);
                            G[v].name = tgt;
                            // This will happen later
                            // G[v].idx = nameMap[tgt];
                            pos->second = v;
                        } else {
                            v = pos->second;
                        }

                        Edge e; bool i;
                        boost::tie(e,i) = add_edge(u,v,G); 
                        G[e].weight = 1.0;
                    } 
                }

            }
            
            if ( namePosMap.size() != boost::num_vertices(G) ) {
                std::cerr << "(namePosMap.size() = " << namePosMap.size() << ") != ("
                          << "(order(G) = " << boost::num_vertices(G) << ") : Error building the graph, aborting\n";
                std::abort();
            }
        }
        gfile.close();

    }
Example #5
0
    void readFromMultilineAdjacencyList( const string& fname, GraphT& G ) {
        typedef typename boost::graph_traits< GraphT >::vertex_descriptor Vertex;
        typedef typename boost::graph_traits< GraphT >::edge_descriptor Edge;

        typedef unordered_map<string,Vertex> svMap;

        svMap namePosMap;
        bool inserted;
        Vertex u,v;
        typename unordered_map<string,Vertex>::iterator pos;

        bool headLine = false;
        size_t remEdgeLine = 0;
        string line;
        typedef vector< string > splitVectorT;
        ifstream gfile(fname);
        if ( gfile.is_open() ) {
            while( gfile.good() ) {
                getline( gfile, line, '\n' );
                if ( gfile.eof() ) { break; }
                boost::algorithm::trim(line);
                auto vline = line.substr( 0, line.find_first_of('#') );

                if (vline.length() == 0) { continue; }

                splitVectorT splitVec;
                split( splitVec, vline, is_any_of(" \t"), token_compress_on );

                if ( splitVec.size() > 0  and vline.size() > 0 ) {
                    auto fromVert = splitVec[0];
                    remEdgeLine = lexical_cast<size_t>(splitVec[1]);

                    boost::tie( pos, inserted ) = namePosMap.insert( std::make_pair(fromVert,Vertex()) );
                    if (inserted) {
                        u = add_vertex(G);
                        G[u].name = fromVert;
                        // This will happen later
                        // G[u].idx = nameMap[fromVert];
                        pos->second = u;
                    } else {
                        u = pos->second;
                    }

                    while ( remEdgeLine > 0 ) {

                        getline( gfile, line, '\n' );
                        boost::algorithm::trim(line);
                        vline = line.substr( 0, line.find_first_of('#') );
                        split( splitVec, vline, is_any_of(" \t"), token_compress_on );

                        auto toVert = splitVec[0];
                        double weight = lexical_cast<double>(splitVec[1]);


                        boost::tie(pos, inserted) = namePosMap.insert(std::make_pair(toVert, Vertex()));
                        if (inserted) {
                            v = add_vertex(G);
                            G[v].name = toVert;
                            // This will happen later
                            // G[v].idx = nameMap[toVert];
                            pos->second = v;
                        } else {
                            v = pos->second;
                        }

                        Edge e; bool i;
                        boost::tie(e,i) = add_edge(u,v,G);
                        G[e].weight = weight;
                        remEdgeLine--;
                    }
                }

            }

            if ( namePosMap.size() != boost::num_vertices(G) ) {
                std::cerr << "(namePosMap.size() = " << namePosMap.size() << ") != ("
                          << "(order(G) = " << boost::num_vertices(G) << ") : Error building the graph, aborting\n";
                std::abort();
            }
        }
        gfile.close();

    }