简体   繁体   中英

NetworkX: How to Return Bool over whether a node has an edge?

I have a list of nx nodes I'm trying to connect and need to assert that a parent node can only draw one edge, while a child can have many edges drawn to it. To help explain, below is my rendered dot file. I don't want the top node to draw an edge to every instance where it is found. Just the next logical node (superset) below it, ie (1,4) -> (1,4,8) and (1,4,8)->(1,4,8,13):

在此处输入图片说明

I think if I can ensure any node can only proceed through the for loops if the parent (i) doesn't have a child (j) already connected to it, my idea should work. Any help or doc references would be greatly appreciated.

My code:

for i in G.nodes:
    # Possible (if i does not have edge):
    for j in G.nodes:
        if i != j and set(i).issubset(set(j)):
            G.add_edge(i, j)

Solved! for future reference: Use networkx attributes and tag nodes. You can then make a parent condition and turn it on or off when an edge is drawn:

for i in G.nodes:
    G.nodes[i]['parent'] = False

for i in node_list:
    G.add_node(i, parent=False)
for i in G.nodes:
    if not G.nodes[i]['parent']:
        for j in G.nodes:
            if i != j and set(i).issubset(set(j)):
                G.add_edge(i, j)
                G.nodes[i]['parent'] = True

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM