Is there any faster, nicer way of building a Networkx tree. Currently, my code is
for numb in range(0,len(previous)):
nodos = list(chunks(current,3))
for i in range(0,3):
G.add_edge(previous[numb],nodos[numb][i])
This works in the following way: 1. The tree has 3 branches (or edges). I have two arrays:
previous = [x,y,z] #These nodes have already been added to the graph
current = [xx,xy,xz, xy,yy,yz, xz,yz,zz] #This nodes need to be added.
Ideally, I should do the following:
1. Start with x in previous:
1.1 Pick the first 3 nodes in current (i.e. xx,xy,xz)
1.1.1 Add the nodes-edges: x->xx, x->xy, x->xz
So far my codes does:
1. Start with x in previous
2. Partition current into chunks of 3 items: [[xx,xy,xz], [xy,yy,yz], [xz,yz,zz]]
3. Loop through all the nodes in these chunks:
4. Add x->xx, loop again, add x->xy... etc.
My implementation is extremely inefficient. How would you do this efficiently? Thank you