0

Say that I have a regular grid of points with a given set of coordinates, like this:

Node IDs            Coordinates
0   1   2           (0,0)  (1,0)  (2,0)
3   4   5           (0,1)  (1,1)  (2,1)
6   7   8           (0,2)  (1,2)  (2,2)

In my case, the regular grid is a 2D graph generated with NetworkX.

How can I create a dict of lists where the key represents each node ID in the network and the list contains the distances from one specific node to those that are connected to it?

Example: node (0,0) is connected to nodes (1,0) and (0,1). Considering the squared distance to avoid the computational costs of sqrt, the dict I want would feature mydict={0:[1,1],...], since the squared distance from node (0,0) to its connected neighbors is in both cases 1. Each list is therefore as long as there are neighbors to the each node.

I failed to provide some code as I don't understand how to solve the problem. Many thanks to those who will help.

EDIT

This is how the regular network is created:

import networkx as nx
N=3
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds.sort()
vals.sort()
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=False, node_size = 15)

This is how one can get the neighbors of each node:

for node in G.nodes():
        list_of_neigh=G.neighbors(node) #The connected neighbors of node n

And this is how one can access each node in the graph:

G.nodes()
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • You need to provide a little bit of surrounding code. How is your data stored in the python program? Are nodes just tuples or are they instances of a custom class? How can we access the neighbors given a node? – Lucas Mar 31 '16 at 11:12
  • 1
    If you're running around bulding dynamically allocated 2-item lists stuffed into a dictionary in Python, I think the additional cost of a square root is neglible. :) – unwind Mar 31 '16 at 11:12
  • Even if the number of lists is `900`? – FaCoffee Mar 31 '16 at 11:18
  • 1
    @FC84 `sum(math.sqrt(n) for n in range(1000000))` takes less than a second to evaluate, so, yes, the cost is somewhat neglible. – John Coleman Mar 31 '16 at 11:42
  • Ok, let's use the `sqrt` then. But this is not the reason why I asked this question – FaCoffee Mar 31 '16 at 11:43

1 Answers1

1

Supposing that nodes are tuples (a,b), something like this (not tested)? :

def dist(n1, n2):
    x = n2[0]-n1[0]
    y = n2[1]-n1[1]
    return math.sqrt(x*x + y*y)   # or math.hypot(x, y)

distances = {}
for node in G.nodes():
    list_of_neigh = G.neighbors(node)
    distances[node] = [dist(node, x) for x in list_of_neigh]
JulienD
  • 7,102
  • 9
  • 50
  • 84
  • I get a `TypeError: 'int' object has no attribute '__getitem__' ` pointing at `x=n2[0]-n1[0]`. What is the problem? – FaCoffee Mar 31 '16 at 13:13
  • The problem is that I assumed that your nodes were tuples of ints, whereas apparently they are only ints (cannot use `[]` on an int, probably `n2` is an int). Make it so that `node`, `x`, `n1`, `n2` are all couples of coordinates like you described. – JulienD Mar 31 '16 at 13:15
  • Well I don't know how to do that :) – FaCoffee Mar 31 '16 at 13:16
  • What does `node` look like ? Can you print it ? It may be an object `Node`, and you access its properties like `node.x`, `node.y` instead of `node[0]`, `node[1]`. – JulienD Mar 31 '16 at 13:17
  • If you type `G.nodes()` that'll give you a `list`, so the nodes are the elements of that list. The properties you might be referring to are the coordinates, which are stored in the `pos2` dict. – FaCoffee Mar 31 '16 at 13:19
  • So I have amended your `x = n2[0]-n1[0]` line to be `x = pos2[n1][0]-pos2[n2][0]` since `pos2` stores the coordinates. Similarly I have amended the `y` line. – FaCoffee Mar 31 '16 at 13:22
  • Yes, that should work. What is `G.nodes()[0]` ? I doubt you have to use your own dict for that. – JulienD Mar 31 '16 at 13:47