[英]Given the number of total nodes and degrees of each node, is it possible to construct a graph?
[英]Terminal nodes for each Node in a graph
我想列出关于图中节点的所有终端节点,我的代码确实可以工作,但是在复杂的图中它不能有效地工作。 如您所见,我使用的递归在处理复杂图形时效率不高。
def neighbourt(T,item,st1,list1,list2):
for n1 in T.neighbors(item):
if n1 > item and T.degree(n1)> 1:
#print(n1)
st1=st1+neighbourt(T,n1,st1,list1,list2)
elif n1 > item and T.degree(n1)==1:
fruit_dictionary1 = dict(zip(list1,list2))
ab=fruit_dictionary1[n1]
st1=st1+str(ab)+"_"
#return st1
return st1
代码用 python 编写,item 是节点,T 是无向图。
任何建议都会有所帮助。
下面的代码应该做同样的事情,但没有递归:
def neighbourT(graph):
for node in graph.nodes():
for n in graph.neighbors(node):
if n > node and graph.degree(n) == 1:
print(node,n)
例如对于图 T:
T = nx.Graph()
T.add_edge("A","B")
T.add_edge("A","C")
T.add_edge("B","D")
T.add_edge("B","F")
T.add_edge("C","E")
neighbourT(T)
# prints
B D
B F
C E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.