Date: Mon, 19 Apr 2010 18:06:57 +0100
From: Tamas Nepusz <address@hidden>
Subject: Re: [igraph] Reading Symbolic Adjacency
To: Help for igraph users <address@hidden>
Message-ID: <address@hidden>
Content-Type: text/plain; charset=us-ascii
> Suppose you are give an adjacency list like the one pasted at the end of the
email as a Numpy array.
> Each row stands for a link (each long number is a node ID).
> Now, you would like to turn that into an undirected graph object without
multiple links/self interactions (hence interactions like 73400443 73400443 are
not admitted).
Up to this point, it is easy:
from igraph import load
g = load("your_graph.txt", format="ncol")
Numeric node IDs will be assigned to an attribute called "name".
> On top of that, some links appear more than once and I would like to and you
would like to weight them simply by counting how many times they are repeated.
This is the complicated part. igraph 0.6 will have a generic solution for this,
see the following function in the development branch:
http://bazaar.launchpad.net/%7Eigraph/igraph/0.6-main/annotate/head%3A/interfaces/python/igraph/__init__.py#L438
The link above shows you an improved version of the simplify() function that
lets you specify what to do with edge attributes when multiple edges are
collapsed into a single one. This would help you solve the problem by doing the
following:
g.es["weight"] = 1.
g.simplify(reduce_attributes=sum)
This tells igraph to assign unit weight to every edge, then collapse the edges
by summing up the weights. So, you can simply copy the method shown on the
above link to do the simplification task.
Here is another solution using igraph's existing tools (untested, but it should
work):
from igraph import UniqueIdGenerator, Graph
from collections import defaultdict
vertex_ids = UniqueIdGenerator()
weights = defaultdict(int)
for line in f:
source, target = line.strip().split()
edge = vertex_ids[source], vertex_ids[target]
if edge[0] > edge[1]:
edge = edge[1], edge[0]
weights[edge] += 1
g = Graph(weights.keys())
g.es["weight"] = weights.values()
-- Tamas