I'm creating a complete graph with 50 randomly created nodes. Each node is defined as a Cartesian coordinate as follows:
n = 50
V = []
V=range(n)
random.seed()
pos = {i:(random.randint(0,500),random.randint(0,500)) for i in V}
I need to assign the Euclidean distance between each node as the edge weight corresponds to that pair of nodes.
I have already defined the Euclidean distance between two nodes as follows:
points = []
positions = []
for i in pos:
points.append(pos[i])
positions.append(i)
positions.append(pos[i])
def distance(points, i, j):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
return math.sqrt(dx*dx + dy*dy)
I tried assigning Euclidean distance as their weight as follows:
for u in V:
for v in V:
weights = distance(points, u,v)
G = nx.complete_graph(n)
for e in G.edges():
weights[e] = distance(points, e[0],e[1])
But this returns the following error.
weights[e] = distance(points, e[0],e[1])
TypeError: 'float' object does not support item assignment
Can someone suggest me a way to do that?