igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] two graphs, two noncomparable sets of node IDs


From: Gabor Csardi
Subject: Re: [igraph] two graphs, two noncomparable sets of node IDs
Date: Sat, 25 Aug 2007 23:36:46 +0200
User-agent: Mutt/1.5.13 (2006-08-11)

Arthur,

there are some ways to do this, they might be considered as hacks though.
First you need to tell us whether you use R, Python or plain C. 
R is easier of course, maybe Python too, i don't know that. C is not that
easy but possible too.

Suppose you have two files, g1.el:

foo bar
bar foobar
foobar foo
rab foo

and g2.el:

foobar foo
oof bar
foo oof

This is how to handle them in R. You just read in the graphs with
some method that keeps the vertex names, eg:

> g1 <- read.graph("g1.el", format="ncol")
> g2 <- read.graph("g2.el", format="ncol")

Then when comparing some vertex statistics, you assign the vertex names 
as names to the result:

> d1 <- degree(g1) ; names(d1) <- V(g1)$names
> d2 <- degree(g2) ; names(d2) <- V(g2)$names

> d1
   foo    bar foobar    rab 
     3      2      2      1 
> d2
foobar    foo    oof    bar 
     1      2      2      1 

Or if you want to order by names:

> d1[ sort(names(d1)) ]
   bar    foo foobar    rab 
     2      3      2      1 
> d2[ sort(names(d2)) ]
   bar    foo foobar    oof 
     1      2      1      2 

For the intersection you can read in the graphs in a way which
assigns the same numeric ids to the same symbolic ids. The drawback is
that all symbolic ids must appear in both graphs as isolates. First
we need a list of symbolic names, and then use this list to assign the 
vertex ids:

> el1 <- scan("g1.el", what="")
> el2 <- scan("g2.el", what="")
> symb <- unique(c(el1,el2))
> symb
[1] "foo"    "bar"    "foobar" "rab"    "oof"   

Then create graphs with the appropriate numeric ids:

> ids <- seq(symb)-1
> names(ids) <- symb
> ids
   foo    bar foobar    rab    oof 
     0      1      2      3      4 
> g1 <- graph(ids[el1], n=length(symb))
> g2 <- graph(ids[el2], n=length(symb))

You can also assign the symbolic names as attributes:

> V(g1)$name <- V(g2)$name <- symb
> g1
Vertices: 5 
Edges: 4 
Directed: TRUE 
Edges:
                    
[0] foo    -> bar   
[1] bar    -> foobar
[2] foobar -> foo   
[3] rab    -> foo   
> g2
Vertices: 5 
Edges: 3 
Directed: TRUE 
Edges:
                    
[0] foobar -> foo   
[1] oof    -> bar   
[2] foo    -> oof   

And the intersection, names are sadly not preserved:

> g3 <- g1 %s% g2
> V(g3)$name <- symb
> g3
Vertices: 5 
Edges: 1 
Directed: TRUE 
Edges:

[0] foobar -> foo   

Hmmm, quite nasty, there should be a way to handle vertex names
better.... if you need to do this in C i can show you some examples,
but right now it is much easier to use R (or perhaps Python) if
you have vertex names. 

Best,
Gabor

On Fri, Aug 24, 2007 at 07:42:48PM +0100, Arthur Wuster wrote:
> 
> Hi there!
> 
> I have read a number of graphs into the igraph format. These graphs share
> most of their nodes, just the edges differ. In the course reading in the
> graphs, igraph converts my node names into numerical IDs, and that causes
> some trouble when I want to compare graphs. For example, I would like to see
> whether the degrees of nodes are correlated between my graphs. However I
> can't compare nodes between graphs, because identical numerical IDs don't
> refer to identical nodes in two different graphs.  
> 
> Also If I want to get for example the intersection between two graphs I
> can't do this, again because of the same problem: The numerical ID which has
> been assigned to each node name differs. The igraph documentation says about
> this: 
> 
> " These functions do not handle vertex and edge attributes, the new graph
> will have no attributes at all. Yes, this is considered to be a bug, so will
> likely change in the near future."
> 
> I have studied the igraph documentation (which is otherwise excellent) the
> whole afternoon and I didn't find a way around this yet. Can you help?
> 
> Best wishes, 
> 
> Arthur Wuster
> Theoretical and Computational Biology
> MRC Laboratory of Molecular Biology
> Cambridge, UK
> 
> 
> 
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help

-- 
Csardi Gabor <address@hidden>    MTA RMKI, ELTE TTK




reply via email to

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