1

I have a list with 7 elements:

elements = ['A','B','C','D','E','F','G']

and I want to make a network starting from A in the center, and going down all the possible paths to each other element, so it would look something like this:

## A->[B1,C1,D1,E1,F1,G1]
## B1 -> [C2,D2,E2,F2,G2] for each of [B1,C1,...,G1]
## C2 -> [D3,E3,F3,G3] for each of the above, etc.

I'm currently trying with the networkx package, the end goal would then be to make it a circular tree graph, maybe color the nodes by the letter or assign some weights etc.

For now though I'm kinda stuck on the aforementioned problem, I've tried many things and it feels like it shouldn't be too tough but I'm not too experienced in these algorithm problems. It looks like it should be some kind of recursion problem. Here's one of the things I've tried if it helps:

def add_edges(network, edge_list,i,previous_ele):
    edge_list1 = edge_list.copy()
    for ele in edge_list:
        network.add_edge(previous_ele+str(i),ele+str(i+1))
        edge_list1.remove(ele)
        add_edges(network, edge_list1, i+1, ele)
N = nx.DiGraph()
elements = ['A','B','C','D','E','F','G']
elements.remove('A')
for ele in elements:
    N.add_edge('A',ele+'1')
for i in range(len(elements)):
    add_edges(N, elements, 1, elements[i])

Thanks in advance for any tips!

1
  • I'm not sure what you mean by ## B1 -> [C2,D2,E2,F2,G2] for each of [B1,C1,...,G1] Commented Oct 3, 2022 at 20:53

1 Answer 1

2

I'm not sure if I completely understood what you're trying to do, but here's a script that does my best guess. The same script works for any number of elements, but it becomes difficult to see the graph clearly.

import networkx as nx
from itertools import combinations

elements = ['A','B','C','D']
G = nx.DiGraph()

for c in elements[1:]:
    G.add_edge(elements[0], c+'1')

for i in range(1,len(elements)-1):
    for a,b in combinations(elements[i:],2):
        G.add_edge(a+str(i),b+str(i+1))

The resulting graph, drawn using nx.draw(G, with_labels = True, node_color = "orange"):

enter image description here


As a bonus, here's a way to draw the graph so that the hierarchy is clear. After the first block of code, add the following:

pos = {elements[0]:(0,0)}
for i in range(1,len(elements)):
    for j in range(1,len(elements)):
        pos[elements[j]+str(i)] = (j-1,-i)

nx.draw(G, pos = pos, with_labels = True, node_color = "orange")

The resulting drawing for your original list of elements:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! Unfortunately it's not what I meant, and I apologise if my question was misleading. My question started from this publication: doi.org/10.3390/w11020306 (I am not affiliated), you don't need to read it, just look at Figure 2 for an example. I wanted to make an algorithm to create such graphs for any combination of nodes, with a different central node each time.
@Froggster Can you clarify what exactly I misunderstood? I see now that the “circular tree” presentation is an important thing for you, but did I interpret the tree structure (ie the nature of the connections in your graph) correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.