I have a graph extracted from an image of several tree-like structures. The graph has a vertex for every articulation point, even if that point is not a branch or end (i.e. the node order is 2). I'd like to remove these order-2 vertices, but maintain connectivity so that the branch point or end vertices that would have been connected through these intermediates are now connected by a single edge. I can do this for small graphs by removing vertices and connecting edges one-at-a-time, but this is slow when working with 10,000+ edges.
This is an example starting graph. I'd like to remove (for example) vertices 8 and 6, while inserting an edge connecting 9 and 4. Similarly, I'd like to remove vertex 5 while inserting an edge between 7 and 4.
edge_matrix = cbind(
c(1,2,3,4,4,5,6,8,9,9,10,11),
c(2,3,4,5,6,7,8,9,10,11,12,13))
example_graph = graph.data.frame(edge_matrix, directed=F)
structure(list(13, FALSE, c(1, 2, 3, 4, 5, 10, 6, 7, 8, 9, 11,
12), c(0, 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9), c(0, 1, 2, 3, 4,
6, 7, 8, 9, 5, 10, 11), c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
), c(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), c(0, 1, 2,
3, 5, 6, 7, 8, 10, 11, 12, 12, 12, 12), list(c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("1", "2", "3", "4", "5", "6", "8",
"9", "10", "11", "7", "12", "13")), .Names = "name"), list()),
<environment>), class = "igraph")