[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] Methods for getting disconnected pairs and for common neigh
From: |
Tamas Nepusz |
Subject: |
Re: [igraph] Methods for getting disconnected pairs and for common neighbors |
Date: |
Mon, 24 Nov 2014 14:39:04 +0100 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
Hi,
Just answered this on Stack Overflow but here it is again so people can find
the answer on the mailing list as well:
Re the first question (listing pairs of disconnected nodes): yes, you have to
do this manually, but it is fairly easy:
from itertools import product
all_nodes = set(range(g.vcount())
disconnected_pairs = [list(product(cluster,all_nodes.difference(cluster))) \
for cluster in g.clusters()]
But beware, this could be a fairly large list if your graph is large and
consists of a lot of disconnected components.
Re the second question (listing common neighbors): again, you have to do this
manually but it only takes a single set intersection operation in Python:
set(g.neighbors(v1)).intersection(set(g.neighbors(v2)))
If you find that you need to do this for many pairs of nodes, you should
probably create the neighbor sets first:
neighbor_sets = [set(neis) for neis in g.get_adjlist()]
Then you can simply write `neighbor_sets[i]` instead of `set(g.neighbors(i))`.
T.
On 11/24, Merton Lister wrote:
> Hi everyone,
>
> I have two questions about methods available in the igraph python package.
>
> Is there a method that would return all the unconnected pairs of nodes in
> an undirected graph? Ideally, there is no duplicate as (1, 2) and (2, 1)
> are the same in this context.
>
> In addition, given two nodes in an undirected graph, is there a method that
> returns their common neighbors?
>
> Many thanks for your help!
>
> Best regards,
> Merton
> _______________________________________________
> igraph-help mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/igraph-help
--
T.