0

I have been trying to construct a problem weighted Graph for Minimum Spanning Tree Algorithm from a list of words with namedtupled data type. But its not working with Digraphs here is the code i`ve tried

Input = ['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

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')]

0 Answers0