yliueagle
yliueagle

Reputation: 1201

add vertices to graph

I want to add vertices to a graph object, but it doesn't work.

g = graph.ring(5)
subG = induced.subgraph(g, c(1,2,3)) ##extract sub-graph
v = V(g)[4] ##add vertex '4' to the sub-graph. 
result = subG + v

The result I expected will be a graph with vertices 1,2,3 and 4. With 4 unlinked to 1,2,3. But the output is IGRAPH U--- 7 2 -- Ring graph It seems to have added 4 vertices to subG.

What's the reason for this and how to achieve my goal?

Upvotes: 1

Views: 1838

Answers (1)

tkmckenzie
tkmckenzie

Reputation: 1363

I think you're looking for the add.vertices command:

g <- graph.ring(5)
subG <- induced.subgraph(g, c(1, 2, 3))
subG <- add.vertices(subG, 1)

This gives us

> subG
IGRAPH U--- 4 2 -- Ring graph
+ attr: name (g/c), mutual (g/l), circular (g/l)

> get.adjacency(subG)
4 x 4 sparse Matrix of class "dgCMatrix"

[1,] . 1 . .
[2,] 1 . 1 .
[3,] . 1 . .
[4,] . . . .

Upvotes: 1

Related Questions