简体   繁体   English

NetworkX:如何返回 Bool 判断节点是否具有边缘?

[英]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.我有一个要连接的 nx 个节点的列表,并且需要断言父节点只能绘制一条边,而子节点可以绘制多条边。 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):只是它下面的下一个逻辑节点(超集),即 (1,4) -> (1,4,8) 和 (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.我想如果我可以确保任何节点只能在父节点 (i) 没有子节点 (j) 连接到它的情况下才能继续执行 for 循环,那么我的想法应该可行。 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.供将来参考:使用 networkx 属性和标记节点。 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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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