igraph-help
[Top][All Lists]

## [igraph] question re adding edges

 From: Carl Pearson Subject: [igraph] question re adding edges Date: Fri, 7 Sep 2012 11:14:41 -0400

```I am trying add some edges to existing graph, but I seem to be getting
the wrong results.

Essentially, I want to generate a graph with one function to create
the edges, pick out a few members of that graph, and then add edges
between those members based on a different generating function.

a code snippet
p <- 5000
f_p <- 0.001
rg<-random.graph.game(p,p*5,type="gnm",directed=T) # make the initial graph
rg\$faction<-sample(p, rbinom(1,p-1,f_p)+1) # pick the special vertices

e.g.
> rg\$faction
[1] 4939 3833 2425 2973 3748 2664 4581 4297

el<-get.edgelist(graph.tree(length(rg\$faction), mode="out")) # get a
new graph to overlay onto original

e.g.
> el
[,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    4
[4,]    2    5
[5,]    3    6
[6,]    3    7
[7,]    4    8

> edges(array(rg\$faction[el], dim=dim(el))) ## looks sensible
[[1]]
[,1] [,2]
[1,] 4939 3833
[2,] 4939 2425
[3,] 3833 2973
[4,] 3833 3748
[5,] 2425 2664
[6,] 2425 4581
[7,] 2973 4297

attr(,"class")
[1] "igraph.edge"

## here's where things seem to go awry ##

rg <- rg + edges(array(rg\$faction[el], dim=dim(el)), faction=T) ##
also tried with add.edges(rg, array(rg\$faction[el], dim=dim(el)),
faction=T), same result
> E(rg)[faction]
Edge sequence:

[25001] 4939 -> 4939
[25002] 3833 -> 3833
[25003] 2425 -> 2425
[25004] 2973 -> 3833
[25005] 2425 -> 2973
[25006] 3748 -> 2664
[25007] 4581 -> 4297

basically, looks like the edges feeding in by column.  Now, I've
managed to massage the code to do what I want by using

rg <- add.edges(rg, t(array(rg\$faction[el], dim=dim(el))), faction=T)
## transposing

But I feel like this isn't the "right" way; it doesn't seem to embrace
the syntax provided by the library.  Am I trying to use the syntax
incorrectly?  Is there a more "igraph"y way to do what I'm attempting
here?

```