[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] how to combine subgraphs with attributes
From: |
Gabor Csardi |
Subject: |
Re: [igraph] how to combine subgraphs with attributes |
Date: |
Tue, 3 Jul 2007 11:00:15 +0200 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
Hi, ideally graph.union should handle edge attributes, it's a pity
it doesn't. The quick and dirty solution for your example would be something
like:
g <- graph.empty(n=11, directed=FALSE)
g <- add.edges(g, e1, color="red", sv=1)
g <- add.edges(g, e2, color="blue", sv=2)
g <- add.edges(g, e3, color="grey", sv=3)
A somewhat better solution is to write a function which steps through
all the graphs and their edge attributes to create the union graph:
my.union <- function(...) {
graphs <- list(...)
if (length(graphs)==0) {
stop("Please supply at least one graph")
}
if (any(!sapply(graphs, is.igraph))) {
stop("Not a graph object")
}
if (unique(sapply(graphs, is.directed))) {
stop("You can't mix directed and undirected graphs")
}
v <- max(sapply(graphs, vcount))
g <- graph.empty(n=v, directed=is.directed(graphs[[1]]))
for (i in seq(along=graphs)) {
attrnames <- list.edge.attributes(graphs[[i]])
attrs <- list()
for (j in attrnames) {
attrs[[j]] <- get.edge.attribute(graphs[[i]], j)
}
g <- add.edges(g, t(get.edgelist(graphs[[i]])), attr=attrs)
}
g
}
and then you can write
g <- my.union(g1,g2,g3)
I haven't tested it too much....
Btw, for creating undirected graphs
g1 <- graph(e1, directed=FALSE)
is better (clearer and faster) than
g1 <- as.undirected(graph(e1))
Best,
Gabor
On Tue, Jul 03, 2007 at 12:01:14PM +0900, MATSUDA, Noriyuki wrote:
> Hello:
> Can anyone help me combine subgraphs with specific attributes.
> The following
> is a pilot script:
> -----------------------------------------------------------------------------------
> # BSJ07: Varying edge attribues by sigma
> #
>
> e1<- c(0,1,1,2,2,3,3,4,4,10)) # sv1
> e2<- c(0,4,4,5,5,7,7,2) # sv2
> e3<- c(3,8,8,4,4,0) # sv3
> g1<-as.undirected(graph(e1))
> g2<-as.undirected(graph(e2))
> g3<-as.undirected(graph(e3))
> E(g1)$color <-"red"
> E(g2)$color <-"blue"
> E(g3)$color <-"grey"
> E(g1)$sv<-1
> E(g2)$sv<-2
> E(g3)$sv<-3
>
> # g<- ??(g1,g2,g2)
> -----------------------------------------------------------------------------------
>
> An alternative is to create g out of e1,e2,e3 by
> g<- as.undirected(graph( c(e1,e2,e3) )
> But, I need help in specifying its edge attributes depending on whether they
> originated from e1, e2 or e3.
>
> Thanks in advance
> --
>
> MATSUDA, Noriyuki, Ph.D. Univ. of Tsukuba
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
--
Csardi Gabor <address@hidden> MTA RMKI, ELTE TTK