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 """ map_graph = Digraph() print("Loading map from file...") with open(mapFilename) as f: lines = f.readlines() for line in lines: node1 = Node(line[0]) node2 = Node(line[1]) edge1 = Edge(node1, node2) map_graph.addNode(node1) map_graph.addNode(node2) map_graph.addEdge(edge1) return map_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 """ print "Loading map from file..." # Open File inFile = open(mapFilename, 'r', 0) # MIT: Digraph representing the MIT Map MIT = Digraph(); # Generate list of map entries mapList = [] for line in inFile: mapList.append(line.split()) # Generate nodes nodes = [] for entry in mapList: if entry[0] not in nodes: nodes.append(entry[0]) for node in nodes: MIT.addNode(node) # Generate edges edges = [] for entry in mapList: singleEdge = Edge(entry[0], entry[1], int(entry[2]), int(entry[3])) edges.append(singleEdge) for edge in edges: MIT.addEdge(edge) # Print report print " ", len(nodes), "nodes loaded." print " ", len(edges), "edges loaded." inFile.close() return MIT
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..." # Open File inFile = open(mapFilename, 'r', 0) # MIT: Digraph representing the MIT Map MIT = Digraph() # Generate list of map entries mapList = [] for line in inFile: mapList.append(line.split()) # Generate nodes nodes = [] for entry in mapList: if entry[0] not in nodes: nodes.append(entry[0]) for node in nodes: MIT.addNode(node) # Generate edges edges = [] for entry in mapList: singleEdge = Edge(entry[0], entry[1], int(entry[2]), int(entry[3])) edges.append(singleEdge) for edge in edges: MIT.addEdge(edge) # Print report print " ", len(nodes), "nodes loaded." print " ", len(edges), "edges loaded." inFile.close() return MIT
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..." dataFile = open(mapFilename, "r") g = Digraph() for line in dataFile: if len(line) == 0 or line[0] == "#": continue dataLine = string.split(line) fromBuidling = g.getNode(dataLine[0]) toBuilding = g.getNode(dataLine[1]) totalDistance = int(dataLine[2]) outdoorDistance = int(dataLine[3]) if fromBuidling is None: fromBuidling = Node(dataLine[0]) g.addNode(fromBuidling) if toBuilding is None: toBuilding = Node(dataLine[1]) g.addNode(toBuilding) p = Path(fromBuidling, toBuilding, totalDistance, outdoorDistance) g.addEdge(p) return g
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..." f = open (mapFilename,"r") map_dict = {} l = [] for line in open(mapFilename,"r"): name, score, weight1, weight2 = line.split() l = [score, weight1, weight2] map_dict[name] = l graph = Digraph() list_of_nodes = [] for i in map_dict: if not (i in list_of_nodes): graph.addNode(i) if not (map_dict[i][0] in list_of_nodes): graph.addNode(map_dict[i][0]) list_of_nodes.append(i) list_of_nodes.append(map_dict[i][0]) edge = Edge(i, map_dict[i][0], map_dict[i][1], map_dict[i][2]) #print edge 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 """ mapGraph = Digraph() print "Loading map from file..." inputFile = open(mapFilename) for line in inputFile: line = line.rstrip() splitLine = line.split(' ') # First, add start node if it doesn't already exist if not mapGraph.hasNode(splitLine[0]): mapGraph.addNode(splitLine[0]) inputFile.seek(0) for line in inputFile: line = line.rstrip() splitLine = line.split(' ') # Then, add edge between two nodes lineEdge = FeatureEdge(splitLine[0],splitLine[1],splitLine[2],splitLine[3]) mapGraph.addEdge(lineEdge) return mapGraph
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..." campusDigraph = Digraph() with open(mapFilename) as f: read_file = f.readlines() for line in read_file: edge = line.split() node1 = Node(edge[0]) node2 = Node(edge[1]) try: campusDigraph.addNode(node1) except ValueError: pass try: campusDigraph.addNode(node2) except ValueError: pass campusDigraph.addEdge(weightedEdge(node1, node2, int(edge[2]), int(edge[3]))) return campusDigraph
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)) v2_weighted_g.addEdge(WeightedEdge(A, B, 4, 2)) v2_weighted_g.addEdge(WeightedEdge(A, C, 5, 2)) v2_weighted_g.addEdge(WeightedEdge(B, C, 3, 1))
def main(): source = 'Chicago' destination = 'Phoenix' g = Digraph() for name in ('Boston', 'Providence', 'New York', 'Chicago', 'Denver', 'Phoenix', 'Los Angeles'): #Create 7 nodes g.addNode(Node(name)) g.addEdge(Edge(g.getNode('Boston'), g.getNode('Providence'))) g.addEdge(Edge(g.getNode('Boston'), g.getNode('New York'))) g.addEdge(Edge(g.getNode('Providence'), g.getNode('Boston'))) g.addEdge(Edge(g.getNode('Providence'), g.getNode('New York'))) g.addEdge(Edge(g.getNode('New York'), g.getNode('Chicago'))) g.addEdge(Edge(g.getNode('Chicago'), g.getNode('Denver'))) g.addEdge(Edge(g.getNode('Chicago'), g.getNode('Phoenix'))) g.addEdge(Edge(g.getNode('Denver'), g.getNode('Phoenix'))) g.addEdge(Edge(g.getNode('Denver'), g.getNode('New York'))) g.addEdge(Edge(g.getNode('Los Angeles'), g.getNode('Boston'))) sp = DFS(g, g.getNode(source), g.getNode(destination), [], None, toPrint=True) if sp != None: print('Shortest path from', source, 'to', destination, 'is', printPath(sp)) else: print('There is no path from', source, 'to', destination)