-1

I have a boost graph:

Graph g(2);

indices of which I access like:

g[0]; g[1];

I draw an edge

add_edge(0, 1, g);

Problem:

I want to add a vertex in between 0 and 1 such that there exists an edge from 0 to 0.5 (the new vertex, say) and 0.5 to 1.

What should be my approach?

sehe
  • 374,641
  • 47
  • 450
  • 633
Aviral Srivastava
  • 137
  • 1
  • 2
  • 6

1 Answers1

1

Given an edge-descriptor of the original edge:

template <typename Edge, typename Graph>
auto split_edge(Edge edge, Graph& g) {
    auto v = source(edge, g);
    auto u = target(edge, g);

    auto n = add_vertex(g);

    add_edge(v, n, g);
    add_edge(n, u, g);
    return n;
}

Returns the descriptor for the newly added vertex.

Sample Program

Live On Coliru

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;

template <typename Edge, typename Graph>
auto split_edge(Edge edge, Graph& g) -> typename Graph::vertex_descriptor {
    auto v = source(edge, g);
    auto u = target(edge, g);

    auto n = add_vertex(g);

    add_edge(v, n, g);
    add_edge(n, u, g);
    return n;
}

int main() {
    Graph g(2);
    auto original = add_edge(0, 1, g).first;
    print_graph(g);

    auto v_half = split_edge(original, g);

    print_graph(g);
}

Prints

0 --> 1 
1 --> 
0 --> 1 2 
1 --> 
2 --> 1 
sehe
  • 374,641
  • 47
  • 450
  • 633