igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] Weighted graphs in Python interface


From: Szabolcs Feczak
Subject: Re: [igraph] Weighted graphs in Python interface
Date: Mon, 2 Jun 2008 21:08:41 +1000

On Mon, Jun 2, 2008 at 7:15 PM, Jonathan Donges <address@hidden> wrote:
Hi igraphers!

I've some questions about weighted networks in igraph:

1. Working with the Python interface, I would like to generate a directed weighted graph (without multiple edges and self-loops) from an adjacency matrix, where the elements of the matrix store the weight if an edge exists and are otherwise zero. The matrix is a numpy array or list of lists. How can this be achieved in a most direct way?

HI Jonathan,

Unfortunately there is no direct method to this according to my knowledge you are better of converting your matrix into the ncol format.
which is a normal text list of
     node-from node-to weight

its not to hard to convert adjacency matrix into ncol
assuming you have a matrix in a coma separated value file
without node name headers, input.csv:
0,1,1
1,0,0
2,1,0


#!/usr/bin/env python

import csv
m = []
r = csv.reader(open(r'input.csv','rb'))
for row in r:
        m.append( [int(i) for i in row] )

of = open('ncol','w')
for i in range(1,len(m)):
     for j in range(0,i):
            of.write ("%d %d %s\n" % (i,j,m[i][j]))
            of.write ("%d %d %s\n" % (j,i,m[j][i]))

of.close()
#then you can read it easily with
from igraph import *
g=Graph.Read_Ncol("ncol", names=True, weights=True, directed=True)
# print g would give in case of the above example
# Directed graph (|V| = 3, |E| = 6)


Cheers

address@hidden Uni




reply via email to

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