def load_map(mapFilename): """ Parses the map file and constructs a directed graph Parameters: mapFilename : name of the map file Assumes: Each entry in the map file consists of the following four positive integers, separated by a blank space: From To TotalDistance DistanceOutdoors e.g. 32 76 54 23 This entry would become an edge from 32 to 76. Returns: a directed graph representing the map """ print "Loading map from file..." mitMap = WeightedDigraph() f = open('mit_map.txt', 'r') for l in f: line = l.split() mitMap.addNode(line[0]) mitMap.addNode(line[1]) newEdge = WeightedEdge(line[0], line[1], line[2], line[3]) mitMap.addEdge(newEdge) return mitMap
def load_map(mapFilename): """ Parses the map file and constructs a directed graph Parameters: mapFilename : name of the map file Assumes: Each entry in the map file consists of the following four positive integers, separated by a blank space: From To TotalDistance DistanceOutdoors e.g. 32 76 54 23 This entry would become an edge from 32 to 76. Returns: a directed graph representing the map """ # TODO print "Loading map from file..." infile = open('./' + str(mapFilename), 'r') graph = WeightedDigraph() # add nodes for line in infile: # each line in file line = line.strip() # strip "\n" from line line = line.split(' ') node1, node2 = line[0], line[1] # take nodes #print node1, node2, type(node1), type(node2) try: graph.addNode(Node(node1)) # note: add a node, not a string! except ValueError as e: #node 1 already exist pass try: graph.addNode(Node(node2)) except ValueError as e: # node 2 already exist pass infile.close() # add edges infile = open('./' + str(mapFilename), 'r') for line in infile: # each line in file line = line.strip() # strip "\n" from line line = line.split(' ') node1, node2 = Node(line[0]), Node(line[1]) # take nodes totDist, outDist = int(line[2]), int(line[3]) edge = WeightedEdge(Node(node1), Node(node2), totDist, outDist) graph.addEdge(edge) return graph
def load_map(mapFilename): """ Parses the map file and constructs a directed graph Parameters: mapFilename : name of the map file Assumes: Each entry in the map file consists of the following four positive integers, separated by a blank space: From To TotalDistance DistanceOutdoors e.g. 32 76 54 23 This entry would become an edge from 32 to 76. Returns: a directed graph representing the map """ # TODO print "Loading map from file..." result = WeightedDigraph() with open(mapFilename, 'r') as inFile: for line in inFile.readlines(): print 'line:', line s, toNode, totalDist, distOut = line.split() fromNode = int(s) #print 'type', type(fromNode) node1 = W_Node(int(fromNode)) node2 = W_Node(int(toNode)) weightedEdge = WeightedEdge(node1, node2, int(totalDist), int(distOut)) print 'weightedEdge', weightedEdge if node1 not in result.nodes: result.addNode(node1) if node2 not in result.nodes: result.addNode(node2) result.addWeightedEdge(weightedEdge) print result
import string from graph import Digraph, Edge, Node, \ WeightedEdge, WeightedDigraph, \ v2_WeightedDigraph # import the helper functions for Prob3 and 4 from utils import * # test for problem 1 # see the graph implementation normal_g = Digraph() weighted_g = WeightedDigraph() # with self.edges as complex tuple v2_weighted_g = v2_WeightedDigraph() # with self.weights A = Node("A") B = Node("B") C = Node("C") D = Node("D") for node in [A,B,C,D]: normal_g.addNode(node) weighted_g.addNode(node) v2_weighted_g.addNode(node) # for normal digraph normal_g.addEdge(Edge(A, B)) normal_g.addEdge(Edge(A, C)) normal_g.addEdge(Edge(B, C)) normal_g.addEdge(Edge(C, A)) normal_g.addEdge(Edge(C, D))