1

Say I have a networkx graph:

g=nx.gnm_random_graph(5,5)
for u,v,w in g.edges(data=True):
    w = np.random.randint(0,10)

I'd like to represent this as if each edge has a fixed length (its weight value). The networks I am looking at are radial in nature - I know in advance that this is possible (I don't have the restriction raised in unutbu's comment here).

I want to randomly compute co-ordinates for the nodes (with the fixed edge length constraints) and plot them. I then want to tabulate the distance between all the nodes that are not linked by edges.

From what I've read so far, it seems as though networkx draws nodes without consideration of edge lengths. But the node positions can be explicitly defined. So I'd need to write a function to compute the node positions.

Does anyone know of a networkx, or other, function that might help with this?

More generally, is networkx suited to this task?

Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170
  • Can you explain what you mean by "radial in nature"? – Joel Sep 14 '15 at 11:18
  • @Joel I mean that there is a central node and the other nodes branch out from this in a tree like fashion, never forming a loop. – Lee Sep 14 '15 at 12:33

1 Answers1

0

I stumbled over this and even though its fairly old I will try to give an answer:

First, I tried your code and it doesnt seem to work.

In [60]:  for u,v,w in g.edges(data=True):
    ...:      w = np.random.randint(0,10)

gives me

In [61]:  list(g.edges_iter(data='weight',default = 1))
Out[61]:  [(0, 4, 1), (0, 2, 1), (1, 2, 1), (2, 3, 1), (2, 4, 1)]

So the weights all remain '1'. Thats logical, since you are only altering 'w' and not the edge's parameter 'weight'. You can achieve that by using the following:

In [62]:  for u,v in g.edges():
    ...:      g[u][v]['weight'] = np.random.randint(0,10)

which gives you

In [63]:  list(g.edges_iter(data='weight',default = 1))
Out[64]:  [(0, 4, 1), (0, 2, 8), (1, 2, 7), (2, 3, 3), (2, 4, 8)]

The Placement of the nodes is done by passing a dictonary with x and y coordinates for each node. Such a dictonary can be obtained by using the networkx placing algorithms, which can be foud here networkx.drawing.layout. Using the networkx.circular_layout as a basis, you could use numpy to subsequently build your graph-coordinates outwards from center. That would of cause require a little bit of simple trigonometrics and would only work, if your system is not over-determined.

Rantanplan
  • 11
  • 3