def instantiate_graph(self): graph = Graph() try: with open(self.__file_name) as f: for line in f: # check if the line defines nodes or edges if line[1] is ':': nodes = line.split(' ') for node in nodes: name = node.split(':')[0] x, y = node.split(':')[1].split(',') graph.add_node(name.strip(), { 'X': x.strip(), 'Y': y.strip() }) elif line[1] is ',': edges = line.split(' ') for edge in edges: source = edge.split(',')[0] destination, weight = edge.split(',')[1].split(':') graph.add_edge(source.strip(), destination.strip(), weight.strip()) else: raise SyntaxError("Unknown line type in %s" % (this.file_name)) except Exception as e: raise e finally: f.close() return graph
def add_edge(self, s, t): WeightedGraph.add_edge(self, s, t, 1)
def add_edge(self, s, t, wgt): WeightedGraph.add_edge(self, s, t, wgt) WeightedGraph.add_edge(self, t, s, wgt)