igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] going from a directed to an undirected graph


From: Gabor Csardi
Subject: Re: [igraph] going from a directed to an undirected graph
Date: Sun, 9 Mar 2008 22:34:10 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

Yeah, but originally the question was to create an undirected
graph, in which every mutual pair of directed edges should be 
replaced by an undirected edge. At least this is how i understand
it. 

This is not easy to do with igraph, but R can help:

## create a small sample graph
g <- graph( c(0,1,0,1,0,1, 1,0,1,0,1,0, 0,2, 2,0, 1,2, 2,1, 3,1, 1,4) )

## count multiplicity of edges
E(g)$no <- count.multiple(g)

## remove multiple edges, keep multiplicity as an edge attribute
g2 <- delete.edges(g, which(is.multiple(g))-1)

## create edge list, then two edge lists for the two directions
el <- cbind(get.edgelist(g2), E(g2)$no)
el1 <- el[ el[,1] < el[,2], ]
el2 <- el[ el[,1] > el[,2], ]
el2 <- cbind(el2[,2], el2[,1], el2[,3])

## merge the two edge lists, only edges appearing in both are kept
new.el <- as.matrix(merge(el1, el2, by=1:2))

## choose the minimum multiplicity
mult <-  pmin(new.el[,3], new.el[,4])
new.el <- new.el[,1:2]

## create a new graph with the desired multiplicity
edges <- sapply(seq(length=nrow(new.el)), function(x) rep(new.el[x,], mult[x]))
edges <- unlist(unname(edges))

new.g <- graph(edges, dir=FALSE, n=vcount(g))

Alisa, is this what you wanted?

I think i'll add a function called "is.mutual" that gives you the 
mutual edges to make this easier. 

Gabor

ps. if you don't join the list (or send email from a different address),
then your messages might be delayed if i'm offline, like it happened
with this message.

On Sun, Mar 09, 2008 at 09:44:25PM +0100, Tamas Nepusz wrote:
> Dear Alisa,
> 
> >My problem is that when I used the as.undirected function with the  
> >"collapse" parameter, I wiped out the secondary connection. Is there  
> >any way to replace each pair of directed edges with one undirected  
> >edge (i.e. not the "each" parameter) without removing multiple  
> >edges? Suggestions?
> I assume you are using the R interface of igraph. Try utilizing the  
> simplify() and count.multiple() calls. A possible solution is listed  
> in the documentation:
> 
> E(g)$weight <- count.multiple(g)
> g <- simplify(g)
> E(g)$weight
> 
> simplify eliminates all multiple edges, so you'll have at most one  
> edge from a->b or b->a. However, its associated weight attribute will  
> contain the number of edges that were collapsed into this single edge.
> 
> -- 
> T.
> 
> 
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help

-- 
Csardi Gabor <address@hidden>    UNIL DGM




reply via email to

[Prev in Thread] Current Thread [Next in Thread]