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