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..." campus = WeightedDigraph() with open(mapFilename) as infile: for line in infile: data = line.split() for d in data[:2]: try: campus.addNode(d) except ValueError: pass campus.addEdge(Edge(data[0], data[1])) campus.addWeights(data[0], data[1], map(int, data[2:])) return campus
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(mapFname): #TODO print "Loading map from file..." dataFile = open(mapFname, 'r') #open mit_map.txt in read-only mode, assign it to dataFile variable travel_map = WD() #create empty weighted directed graph node_dic = {} edge_list = [] for line in dataFile: if len(line) == 0 or line[0] == '#': #skip null or comment lines continue src, dest, distance, outside_distance = line.split() #pull relevant data from line if src not in node_dic: node_dic[src] = Node(src) if dest not in node_dic: node_dic[dest] = Node(dest) edge_list.append((src, dest, distance, outside_distance)) for node in node_dic.values(): travel_map.addNode(node) for edge in edge_list: start = node_dic[edge[0]] end = node_dic[edge[1]] WEdge = WE(start, end, edge[2], edge[3]) travel_map.addEdge(WEdge) dataFile.close() return travel_map
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
class TestWeightedDigraph(TestCase): def setUp(self): # set up nodes self.na = Node('a') self.nb = Node('b') self.nc = Node('c') # set up weighted edges self.e1 = WeightedEdge(self.na, self.nb, 15, 10) self.e2 = WeightedEdge(self.na, self.nc, 14, 6) self.e3 = WeightedEdge(self.nb, self.nc, 3, 1) self.g = WeightedDigraph() self.g.addNode(self.na) self.g.addNode(self.nb) self.g.addNode(self.nc) self.g.addEdge(self.e1) self.g.addEdge(self.e2) self.g.addEdge(self.e3) self.print_strings = ["a->b (15.0, 10.0)", 'a->c (14.0, 6.0)', 'b->c (3.0, 1.0)'] def test_addEdge(self): self.assertEquals(self.g.hasNode(self.e1.getDestination()), True) self.assertEquals(self.g.hasNode(self.e1.getSource()), True) self.assertEquals(self.g.hasNode(self.e2.getDestination()), True) self.assertEquals(self.g.hasNode(self.e2.getSource()), True) self.assertEquals(self.g.hasNode(self.e3.getDestination()), True) self.assertEquals(self.g.hasNode(self.e3.getSource()), True) def test_childrenOf(self): for node in self.g.nodes: children = self.g.childrenOf(node) for child in children: self.assertIsInstance(child, Node) def test___str__(self): my_str = str(self.g) test_list = split(str(self.g), '\n') print('my_str is: {0}'.format(my_str)) print('test_list is: {0}'.format(test_list)) self.assertEqual(test_list[0], self.print_strings[0]) self.assertEqual(test_list[1], self.print_strings[1]) self.assertEqual(test_list[2], self.print_strings[2])
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
# 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)) # for two versions of weightedDigrap weighted_g.addEdge(WeightedEdge(A, B, 4, 2)) weighted_g.addEdge(WeightedEdge(A, C, 5, 2)) weighted_g.addEdge(WeightedEdge(B, C, 3, 1)) weighted_g.addEdge(WeightedEdge(C, A, 5, 2)) weighted_g.addEdge(WeightedEdge(C, D, 7, 6))