I am new to coding python. I am hoping to modify this code to develop a bipartite two mode version. It is code from networkx used to make a geometrci random graph. I have got to grips with most of this function but I am having trouble understanding exactly what lines 94 to 99 are doing. I understand while, zip and nodes.pop() but other parts are confusing for a newbie. Can anyone help with an explanation for what this part of the code is doing more than the general # description given ?
G=nx.Graph()
G.name="Random Geometric Graph"
G.add_nodes_from(range(n))
if pos is None:
# random positions
for n in G:
G.node[n]['pos']=[random.random() for i in range(0,dim)]
else:
nx.set_node_attributes(G,'pos',pos)
# connect nodes within "radius" of each other
# n^2 algorithm, could use a k-d tree implementation
nodes = G.nodes(data=True)
while nodes: #line94
u,du = nodes.pop()
pu = du['pos']
for v,dv in nodes:
pv = dv['pos']
d = sum(((a-b)**2 for a,b in zip(pu,pv))) #line99
if d <= radius**2:
G.add_edge(u,v)
return G
This piece of code is a quite frequently seen idiom to combine every node with every other node exactly once (so the order of
aandbshouldn’t matter for the operation performed in the# do somethingpart).It works because the empty list is considered a falsy value in the condition of the
whileloop, while non-empty lists are considered boolean true.This line computes the square of the Euclidean distance of the two vectors
puandpv. This is best demonstrated by taking it apart:In the last step, we don’t use a list comprehension any more, because we don’t need a list.
sumjust needs the values in some iterable form, so we use a generator expression instead.