我一直在尝试从具有 namedtupled 数据类型的单词列表中构建最小生成树算法的问题加权图。但它不适用于 Digraphs 是我尝试过的代码
输入 = ['john', 'saw', 'mary', 'root']
from collections import defaultdict, namedtuple
Arc = namedtuple('Arc', ('head', 'weight', 'tail'))
def Constrain_graph(sentence):
Arc = namedtuple('Arc', ('head', 'weight', 'tail'))
C_graph=[]
for wordindex in range(1, len(sentence)):
G = nx.DiGraph()
G.nodes(sentence[wordindex])
#G.add_node(nod)
G.add_nodes_from(range(1,len(sentence)))
C_graph=nx.Graph()
return C_graph
所需输出:
[Arc('root',weight,'saw'),Arc('root',weight,'john'),Arc('root', weight,'mary'),Arc('saw', weight,'john '), Arc('john', weight, 'saw'), Arc('saw', weight,'mary'),Arc('john', weight,'mary'), Arc('mary', weight, 'john'),Arc('saw', weight,'mary'),Arc('mary', weight,'saw')]