Zubin
Zubin

Reputation: 167

Bipartite Graphs in R's igraph Package

I'm trying to create a bipartite graph w/ R's igraph package, but having a devil of a time.

Can anyone tell me why this works:

g <- graph.bipartite( rep(0:1,length=10), c(0,1,2,3,4,5,6,7,8,9))

But this gives me an error:

g <- graph.bipartite( rep(0:1,length=10), c(10,11,12,13,14,15,16,17,18,19))
Error in graph.bipartite(rep(0:1, length = 10), c(10, 11, 12, 13, 14,  : 
  At bipartite.c:438 : Invalid (negative) vertex id, Invalid vertex id

Upvotes: 0

Views: 2049

Answers (2)

Tam&#225;s
Tam&#225;s

Reputation: 48061

The first argument of graph.bipartite implicitly specifies the number of vertices. In both cases, you will have 10 vertices in your graph. However, since vertices have consecutive numeric IDs starting from zero in igraph, you cannot use 10, 11 etc as vertex IDs.

Upvotes: 1

Henry
Henry

Reputation: 6784

With your graph.bipartite( rep(0:1, length=10), ...) you have told graph.bipartite that there are ten vertices in the graph, and it treats them as 0,1,2,...9.

You could have written

graph.bipartite( c(0,0,1,0), c(0,2,1,2,2,3))

with four vertices 0, 1, 2, and 3 (with 2 in one part and 0, 1 and 3 in the other) but not

graph.bipartite( c(0,0,1,0), c(0,2,1,2,2,4))

because there is no vertex 4, nor

graph.bipartite( c(0,0,1,0), c(0,2,1,2,1,3))

because the attempted edge (1,3) joins two vertices in the same part.

Upvotes: 1

Related Questions