I have been trying to construct a problem weighted Graph for Minimum Spanning Tree from a list or string of words as, given a string of words to list of named-tuple with every possible combinations of words as head and tail with additional head "root"
Input:
['john', 'saw', 'mary', 'root']
Required Output:
[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')]
Code:
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
I need the output to be like mentioned above