Search code examples
rigraph

Remove edges by specifying endpoints


How can I delete edges from a graph by naming their endpoints?

delete_edges expects edge numbers, and it's not clear to me the mapping between endpoints and edge numbers.

library(igraph)
g = make_ring(10)

Say I wanted to remove the vertices between nodes 7&8 and nodes 9&10.

A hackish way to do so is:

g = delete_edges(g, c(7, 9))

But I had to inspect the output of E(g) closely before figuring out that those edges are numbered 7 & 9.

I tried looking for how the print methods assign the node mapping to E(g) but it looks like quite the rabbit hole.


Solution

  • It looks like you can do this with a string argument -- see the second example in ?delete_edges.

    g = delete_edges(g, c("7|8", "9|10"))
    g
    # IGRAPH U--- 10 8 -- Ring graph
    # + attr: name (g/c), mutual (g/l), circular (g/l)
    # + edges:
    # [1] 1-- 2 2-- 3 3-- 4 4-- 5 5-- 6 6-- 7 8-- 9 1--10
    

    Apparently c("7|8", "9|10") also counts as an "edge sequence" as described in the edges argument.