Noel
2008/8/15 Dan <danie...@gmail.com>:
G=graph you are starting with.
I=XGraph()
C=XGraph()
I.add_nodes_from(G)
C.add_nodes_from(G)
for u in G:
unbrs=set(G[u])
for v in G:
if u==v:
I.add_edge(u,v,len(unbrs))
C.add_edge(u,v,0)
continue
vnbrs=set(G[v])
intercept=unbrs & vnbrs # intersection of two sets
complement=unbrs^vnbrs # symmetric difference of two sets
I.add_edge(u,v,len(intersection))
C.add_edge(u,v,len(complement))
Then I[n1][n2] yields the number of common neighbors
and C[n1][n2] yields the number of neighbors not in common.
(I haven't tested this code...)
Dan
Potentially - there is no simple way to represent multiple edges with
an adjacency matrix. Though maybe in your case you can reduce
the graph to one without multiple edges by summing the weights
of the parallel edges to make a single edge?
Then the numpy suggestion might be the fastest.
Or maybe scipy sparse matrices.
> One additional piece of information : I need to be able to retrieve
> the weight of the edges to the shared and non shared nodes e.g.
>
> A has edges to B and C
>
> D has edges to C and E
>
> I need to discover that A and D share 1 neighbor (C) and what the
> weights are for A<->C and D<->C and also what the weights of their
> unique neighbors are (A<->B and D<->E).
>
Maybe it is two steps. First find the neighbor overlaps then
look up the weights in the graph?
Aric