including isolates/singleton nodes

118 views
Skip to first unread message

plikarish

unread,
Jul 12, 2010, 7:35:27 PM7/12/10
to networkx-discuss
Hello all,

Networkx is great. Here's a question or two for the list though.

I have a weighted graph stored in edgelist format. I've prepended the
edgelist with a list of *all* nodes in the graph (including isolated
nodes) but these nodes are not included in G.nodes() after using
nx.read_edgelist(...). I have to add them manually by looping through
the file again and use G.add_node() if I want the isolates included.
Is this behavior desired? How is one supposed to include isolates in
the graph? (Note, I haven't gone to the trouble of checking other file
formats).

This also led me to examine networkx's behavior with an empty graph,
I've found some funky behavior on this edge case, using some methods
on an empty graph throw an error, seems like maybe this should be
either it's own exception or should be handled differently?

I claim neither expertise as a Social Network Analyst nor as a
developer. Just food for thought.

Thanks for the very useful software,

Peter

Aric Hagberg

unread,
Jul 12, 2010, 8:40:10 PM7/12/10
to networkx...@googlegroups.com
On Mon, Jul 12, 2010 at 5:35 PM, plikarish <plik...@gmail.com> wrote:
> Hello all,
>
> Networkx is great. Here's a question or two for the list though.
>
> I have a weighted graph stored in edgelist format. I've prepended the
> edgelist with a list of *all* nodes in the graph (including isolated
> nodes) but these nodes are not included in G.nodes() after using
> nx.read_edgelist(...). I have to add them manually by looping through
> the file again and use G.add_node() if I want the isolates included.
> Is this behavior desired? How is one supposed to include isolates in
> the graph? (Note, I haven't gone to the trouble of checking other file
> formats).

You'll have to use some other data file format since "edgelist"
doesn't handle isolated nodes (only edges).

"multiline_adjlist" should work:

In [1]: import networkx as nx

In [2]: G=nx.Graph()

In [3]: G.add_weighted_edges_from([('a','b',1),('c','d',2)])

In [4]: G.add_node('singleton')

In [5]: G.nodes()
Out[5]: ['a', 'c', 'b', 'd', 'singleton']

In [6]: nx.write_multiline_adjlist(G,'test.adj')

In [7]: H=nx.read_multiline_adjlist('test.adj')

In [8]: H.nodes()
Out[8]: ['a', 'c', 'b', 'd', 'singleton']


>
> This also led me to examine networkx's behavior with an empty graph,
> I've found some funky behavior on this edge case, using some methods
> on an empty graph throw an error, seems like maybe this should be
> either it's own exception or should be handled differently?

Many graph properties are not well defined for empty graphs. But
I'd like the failures in NetworkX to be obvious. So if there are cases
where either an error should be produced or special case should be provided
post those here or open a ticket at https://networkx.lanl.gov/trac/newticket
and we'll get them fixed.

Aric

Reply all
Reply to author
Forward
0 new messages