igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] Re: igraph-help Digest, Vol 14, Issue 7


From: Tamas Nepusz
Subject: Re: [igraph] Re: igraph-help Digest, Vol 14, Issue 7
Date: Thu, 13 Sep 2007 16:45:35 +0200

> Reading the on-line documentation I have got a newbie question:  I'm trying
> to get how I can create a new network from one stored in a .txt file.
>
> Perhaps in Networkx package (I'm sure you know it), I do it:
>
> >G=Graph()
> >#I open the file in order to read it
> >while there_is_data:
> >     G.add_edge(node1,node2,weight)
>
> How can I do it (or something like it) using igraph package?
It's almost the same, but it's faster to store the edge list and the
weights in a list first and then create the graph in one run:

from igraph import *
edges, weights = []
while there_is_data_in_the_file:
  edges.append((node1, node2))  # note that I'm appending tuples here
  weights.append(float(weight))
g = Graph(edges)
g.vs["weight"] = weights

I'm not sure that igraph 0.4.3 supports omitting the number of
vertices when creating the graph (igraph 0.5 surely does, but that has
not been released yet), so you might have to supply the number of
vertices when creating the graph:

g = Graph(no_of_vertices, edges)

If you want to create a directed graph, use:

g = Graph(no_of_vertices, edges, directed=True)

An even easier way is to use the NCOL format loader:

g = Graph.Read_Ncol("graph.ncol", weights=True)

As far as I understood, the format you are using is something similar
to the NCOL format (see http://bioinformatics.icmb.utexas.edu/lgl/ for
the details of the format).

-- 
T.




reply via email to

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