Nodes ordered in a particular order

65 views
Skip to first unread message

Aditya Tandon

unread,
Jun 28, 2014, 6:23:32 PM6/28/14
to networkx...@googlegroups.com
Hey,

I'm new to using networkx and Python...would be great if someone can help me with this -

So, basically I want the nodes of a Graph arranged in a list in a particular order. Something like this - {firstnode, (all neighbours of firstnode), (....all neighbours of neighbours...) and so on}

Can anyone help me do this?

Moritz Emanuel Beber

unread,
Jun 29, 2014, 5:17:19 AM6/29/14
to networkx...@googlegroups.com
Hi,
>
> So, basically I want the nodes of a Graph arranged in a list in a
> particular order. Something like this - {firstnode, (all neighbours of
> firstnode), (....all neighbours of neighbours...) and so on}
Check out the end of this section:

http://networkx.github.io/documentation/latest/tutorial/tutorial.html#accessing-edges

to see how to use the adjacency_iter.

HTH,
Moritz

Aditya Tandon

unread,
Jun 29, 2014, 8:25:33 AM6/29/14
to networkx...@googlegroups.com
Hi,
I checked the section out and it seems it iterates in increase numeric order for the nodes and not in the particular way I mentioned. 

Aric Hagberg

unread,
Jun 29, 2014, 3:11:15 PM6/29/14
to networkx...@googlegroups.com
You can use the 'neighbors' method like this:

In [1]: import networkx as nx

In [2]: G = nx.path_graph(4)

In [3]: G.edges()
Out[3]: [(0, 1), (1, 2), (2, 3)]

In [4]: nbr = G.neighbors(0)

In [5]: nbr
Out[5]: [1]

Then find the neighbors of all of those nodes (in this case only node
1 is in the list).

Aric
> --
> You received this message because you are subscribed to the Google Groups
> "networkx-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to networkx-discu...@googlegroups.com.
> To post to this group, send email to networkx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/networkx-discuss.
> For more options, visit https://groups.google.com/d/optout.

Moritz Emanuel Beber

unread,
Jun 29, 2014, 3:43:31 PM6/29/14
to networkx...@googlegroups.com
Hi,
> I checked the section out and it seems it iterates in increase numeric
> order for the nodes and not in the particular way I mentioned.
>
On a second read, what you describe also sounds like a breadth-first
search starting at some root node. Or as Aric described just using
neighbors and their neighbors and so on.

Moritz
Reply all
Reply to author
Forward
0 new messages