0

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

  • How do you call this function? The function does not return anything, so what do you consider the "output"? Why does your expected output have a duplicate, and not "john"-"saw"? Why do you have `continue` as the final line in the loop, as there is nothing to skip? – trincot Jul 27 '19 at 12:03
  • i am out of logic thats why the code is incomplete and i need to consider all the head tail combinations hence the duplicates . – Asiis Pradhan Jul 29 '19 at 04:10
  • I don't see where the minimum spanning tree comes in to play. It looks more like a complete graph, but even then... I don't understand what you are after. Even less I understand why this is tagged with `parsing`, or with `weighted-graph`. – trincot Jul 29 '19 at 07:15
  • the output would be the starting problem graph for the minimum spanning tree Algorithm. – Asiis Pradhan Jul 29 '19 at 07:49
  • So then your question has nothing to do with it. Are you looking for a complete graph? – trincot Jul 29 '19 at 08:50
  • No i need a directed weighted Graph as given Output because my spanning tree algorithm works only for such namedtuple data structure – Asiis Pradhan Jul 29 '19 at 08:59
  • Sorry, I don't understand the logic of the required output you have provided. – trincot Jul 29 '19 at 09:16
  • Any relevant information should be in the question, so an SO member looking at your question can understand it and possibly post an answer. – trincot Aug 07 '19 at 07:03

0 Answers0