I am 2 days old with python and coding in general, I have been working on making a random geometric graph that is 2mode. To do this I have been looking at the code in networkx here
I started using the following logic
import networkx as nx
def my_bipartite_geom_graph(a, b, radius, dim):
G=nx.Graph()
A=nx.Graph()
A.name="a node set"
A.add_nodes_from(range(a))
for n in A:
A.node[n]['pos']=[random.random() for i in range(0,dim)]
B=nx.Graph()
B.name="b node set"
B.add_nodes_from(range(b))
for n in B:
B.node[n]['pos']=[random.random() for i in range(0,dim)]
G=nx.disjoint_union(A,B)
nodesa = A.nodes(data=True)
nodesb = B.nodes(data=True)
while nodesa:
u,du = nodesa.pop()
pu = du['pos']
for v,dv in nodesb:
pv = dv['pos']
d = sum(((a-b)**2 for a,b in zip(pu,pv)))
if d <= radius**2:
G.add_edge(u,v)
return G
This returns a graph but clearly not what i was hoping for. Any pointers on how better to approach this problem would be greatly appreciated.
Best Wishes
The problem is
nx.disjoint_unionrenumbers your nodes so that they are distinct. That meansB[n] != G[n]. So the node labels inBcontains nodes fromAin theGgraph.Here is one way to solve it: