igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] Reading Symbolic Adjacency


From: Lorenzo Isella
Subject: Re: [igraph] Reading Symbolic Adjacency
Date: Wed, 21 Apr 2010 11:33:40 +0200
User-agent: Thunderbird 2.0.0.24 (X11/20100411)


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".



Hi Tamas,
And thanks for the prompt reply.
My only question before trying to implement your suggestion is the following: what if you already have a numpy array containing the data in the file "your_graph.txt"? In that case I do not need to load anything, but I still have to convert that array, let us call it A, into a graph object.
Cheers

Lorenzo





> 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




reply via email to

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