def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") graph = Digraph() with open(map_filename, 'r') as f1: list1 = f1.readlines() for item in list1: src, dest, total, outdoors = item[0:-1].split(' ') if (graph.has_node(Node(src)) == 0): graph.add_node(Node(src)) if (graph.has_node(Node(dest)) == 0): graph.add_node(Node(dest)) graph.add_edge(WeightedEdge(Node(src), Node(dest), total, outdoors)) # TODO return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ g = Digraph() # Create a digraph object print("Loading map from file...") with open(map_filename, "r") as f: for passage in f: # For each entry, store four info From, To, TotalD, OutD = passage.split() node_src, node_dest = Node(From), Node(To) if not g.has_node(node_src): g.add_node(node_src) if not g.has_node(node_dest): g.add_node(node_dest) g.add_edge( WeightedEdge(node_src, node_dest, int(TotalD), int(OutD))) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ digraph = Digraph() count = 0 with open(map_filename, 'r') as file: for line in file: line_list = line.split(' ') count += 1 src = Node(line_list[0]) dest = Node(line_list[1]) if not digraph.has_node(src): digraph.add_node(src) if not digraph.has_node(dest): digraph.add_node(dest) weighted_edge = WeightedEdge(src, dest, line_list[2], line_list[3]) digraph.add_edge(weighted_edge) return digraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ #print("Loading map from file...") campus_graph = Digraph() # 实例化Digraph with open(map_filename) as file_object: lines = file_object.readlines() for line in lines: list = line.split() if not campus_graph.has_node(Node(list[0])): campus_graph.add_node(Node(list[0])) # 若不在即加入 if not campus_graph.has_node(Node(list[1])): campus_graph.add_node(Node(list[1])) campus_graph.add_edge( WeightedEdge( # 将该边加入 Node(list[0]), Node(list[1]), list[2], list[3])) return campus_graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ g = Digraph() with open(map_filename) as f: data = f.read().strip() dataList = data.split('\n') for mapEdge in dataList: edgeList = mapEdge.split(' ') # from to TD, DO fromN = Node(edgeList[0]) toN = Node(edgeList[1]) if not g.has_node(fromN): g.add_node(fromN) if not g.has_node(toN): g.add_node(toN) g.add_edge(WeightedEdge(fromN, toN, edgeList[2], edgeList[3])) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") # MY_CODE map_graph = Digraph() with open(map_filename, 'r') as f: for line in f: src, dest, total_dist, outdoor_dist = line.split(' ') src = Node(src) dest = Node(dest) if not map_graph.has_node(src): map_graph.add_node(src) if not map_graph.has_node(dest): map_graph.add_node(dest) edge = WeightedEdge(src, dest, int(total_dist), int(outdoor_dist)) map_graph.add_edge(edge) return map_graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ digraph = Digraph() print("Loading map from file...") with open(map_filename, 'r') as file: for line in file: parts = line.split() src = Node(parts[0]) dest = Node(parts[1]) edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3])) edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3])) if not digraph.has_node(src): digraph.add_node(src) if not digraph.has_node(dest): digraph.add_node(dest) digraph.add_edge(edge) return digraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") campus_map = Digraph() with open(map_filename, 'r') as file_handle: for line in file_handle: # 32 36 70 0 info = line.strip().split( ) # info = ["32","36","70","0"] strip返回一个去掉前后端的line的副本 from_node = Node(info[0]) to_node = Node(info[1]) if not campus_map.has_node(from_node): campus_map.add_node(from_node) if not campus_map.has_node(to_node): campus_map.add_node(to_node) cur_edge = WeightedEdge(from_node, to_node, int(info[2]), int(info[3])) # 假设cur_edge不存在重复的情况,也就是说mit_map.txt没有提供重复的数据 campus_map.add_edge(cur_edge) return campus_map
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ d = Digraph() with open(map_filename) as f: for line in f: data = line.split(' ') assert (len(data) == 4) src = Node(data[0]) dest = Node(data[1]) edge = WeightedEdge(src, dest, data[2], data[3]) if not d.has_node(src): d.add_node(src) if not d.has_node(dest): d.add_node(dest) d.add_edge(edge) f.close() return d
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") inFile = open(map_filename, 'r') digraph = Digraph() for line in inFile: mapEntry = line.split() #mapEntry(list) src = Node(mapEntry[0]) dest = Node(mapEntry[1]) total_distance = int(mapEntry[2]) outdoor_distance = int(mapEntry[3]) edge = WeightedEdge(src, dest, total_distance, outdoor_distance) if not digraph.has_node(src): digraph.add_node(src) if not digraph.has_node(dest): digraph.add_node(dest) digraph.add_edge(edge) return digraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") f=open(map_filename,'r') g=Digraph() for line in f: src, dest, total_distance, outdoor_distance = line.split() src_node=Node(src) dest_node=Node(dest) src_dest=WeightedEdge(src_node, dest_node, total_distance, outdoor_distance) if not g.has_node(src_node): g.add_node(src_node) if not g.has_node(dest_node): g.add_node(dest_node) g.add_edge(src_dest) f.close() return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") g = Digraph() with open(map_filename, "r") as file: for line in file: (src, dst, tot_dist, outdoor_dist) = line.split(' ') tot_dist = int(tot_dist) outdoor_dist = int(outdoor_dist) if not g.has_node(Node(src)): g.add_node(Node(src)) if not g.has_node(Node(dst)): g.add_node(Node(dst)) g.add_edge(WeightedEdge(src, dst, tot_dist, outdoor_dist)) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") graph = Digraph() # 创建空图 with open(map_filename) as file: for line in file: elements = line.split() # 按空格分割成list src = Node(elements[0]) dest = Node(elements[1]) total_distance = int(elements[2]) # 数字类型 outdoor_distance = int(elements[3]) # 数字类型 if not graph.has_node(src): graph.add_node(src) if not graph.has_node(dest): graph.add_node(dest) graph.add_edge(WeightedEdge(src, dest, total_distance, outdoor_distance)) return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ # TODO print("Loading map from file...") # Format for the graph file: # Starting node, destination node, total weight of edge (distance), total distance spent outside # So steps: # 1. Create graph object # For every line: # 0. Save all numbers in variables (source, destination, total_distance, outdoor_distance) # 1. Create source_node object # 2.Create destination node object # 3. If graph has not(source_node) object: # add_node to graph # else get node from graph and save in src variable # Do the same for destination object # 4. Create weightedEdge object from variables and objects # 5. Add edge to graph # Problem 2c: Testing load_map # Include the lines used to test load_map below, but comment them out g = Digraph() with open(map_filename) as f: for line in f: input = line.split() source = input[0] destination = input[1] total_distance = input[2] outdoor_distance = input[3] src_object = Node(source) dest_object = Node(destination) if not (g.has_node(src_object)): g.add_node(src_object) if not (g.has_node(dest_object)): g.add_node(dest_object) edge_object = WeightedEdge(src_object, dest_object, total_distance, outdoor_distance) if edge_object not in g.get_edges_for_node(src_object): g.add_edge(edge_object) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") # Open map_filename for reading and parse each line to get the edge parameters # Assumes map_filename is properly formatted newdigraph = Digraph() # Initialize new digraph with open(map_filename, mode='r') as fID: for tline in fID: # Split on space edge_params = tline.split(' ') # edge_params will have the following format: # edge_params[0]: Source Node, string # edge_params[1]: Destination Node, string # edge_params[2]: Total Distance, int # edge_params[3]: Distance Outdoors, int for idx, param in enumerate(edge_params): if idx < 2: # Node names, staying as strings. Strip newline edge_params[idx] = edge_params[idx].replace('/n', '') else: # Distances, cast as integers. This strips the newline edge_params[idx] = int(edge_params[idx]) # Check to see if our source node is in the digraph we're generating, # add if it's not if not newdigraph.has_node(edge_params[0]): newdigraph.add_node(edge_params[0]) # Check to see if our destination node is in the digraph we're # generating, add if it's not if not newdigraph.has_node(edge_params[1]): newdigraph.add_node(edge_params[1]) newdigraph.add_edge( WeightedEdge(edge_params[0], edge_params[1], edge_params[2], edge_params[3])) return newdigraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") # Create a new graph graph = Digraph() # Read in the data from the file with open(map_filename, 'r') as map_data: for line in map_data: # Split the line by '' to isolate the 4 variables of interest line_data = line.split() src = Node(line_data[0]) dest = Node(line_data[1]) total_distance = int(line_data[2]) outdoor_distance = int(line_data[3]) # Create a weighted edge from src to dest new_wedge = WeightedEdge(src, dest, total_distance, outdoor_distance) #If src or dest nodes not graph, then create them if not graph.has_node(src): graph.add_node(src) if not graph.has_node(dest): graph.add_node(dest) # Add the new edge to the grapg graph.edges[src].append(new_wedge) return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") # initialize a digraph g representing the map g = Digraph() # open the file contains map data with open(map_filename) as file: # read data per line and store it into a list read_data = file.read().split("\n") # the list contains an unnecessary newline character at the end read_data = read_data[:-1] # loop through each entry in data list for entry in read_data: # split each entry into source, destination nodes, total_distance and outdoor_distance # and store them into a list respectively raw_data = entry.split(" ") # src = source node, dest = destination, total = total_distance, outdoor = outdoor_distance src = Node(raw_data[0]) dest = Node(raw_data[1]) total = int(raw_data[2]) outdoor = int(raw_data[3]) # check if g already has node src or not if not g.has_node(src): g.add_node(src) # check if g already has node dest or not if not g.has_node(dest): g.add_node(dest) # initialize a weighted edge edge = WeightedEdge(src, dest, total, outdoor) # add above edge into g g.add_edge(edge) # close file file.close() # return the desired directed graph return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ graph = Digraph() with open(map_filename) as file: for line in file.readlines(): el = line.strip('\n').split(' ') # Add nodes if they're not already in the graph for node in el[:2]: if not graph.has_node(node): graph.add_node(node) graph.add_edge(WeightedEdge(*el)) return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") graph = Digraph() f = open(map_filename, 'r') for line in f: line = line.split() src = Node(line[0]) dst = Node(line[1]) for node in [src, dst]: if not graph.has_node(node): graph.add_node(node) edge = WeightedEdge(src, dst, int(line[2]), int(line[3])) graph.add_edge(edge) f.close() return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ # TODO # print("Loading map from file...") file = open(map_filename, 'r') # open the file g = Digraph() for line in file: # read each line of the file line = line.strip('\n') # remove the \n character line = line.split(' ') for i in range(0, 2): nod = Node(line[i]) if not g.has_node(nod): g.add_node(nod) wei_edge = WeightedEdge(Node(line[0]), Node(line[1]), int(line[2]), int(line[3])) g.add_edge(wei_edge) file.close() return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("\n\nLoading map from file...") g = Digraph() with open(map_filename) as file: for line in file: s, t, total, outdoor = line.split() s, t = map(Node, (s, t)) if not g.has_node(s): g.add_node(s) if not g.has_node(t): g.add_node(t) edge = WeightedEdge(s, t, *map(int, (total, outdoor))) g.add_edge(edge) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ file = open("mit_map.txt") content = file.read() file.close() #split the data into a list of lists contentList = content.splitlines() mitData = [] for lines in contentList: lineSplit = lines.split(' ') mitData.append(lineSplit) #print(mitData) #create digrapgh diG = Digraph() #loop through list of list, add nodes and edges to graphs for datum in mitData: #create node node1 = Node(datum[0]) node2 = Node(datum[1]) #create edge wEdge = WeightedEdge(node1, node2, int(datum[2]), int(datum[3])) #add nodes to graph if not diG.has_node(node1): diG.add_node(node1) if not diG.has_node(node2): diG.add_node(node2) #add weighted edge to graphs diG.add_edge(wEdge)
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") map_data = open(map_filename) graph = Digraph() for row in map_data: node_info = row[:-1].split() src_name = node_info[0] dest_name = node_info[1] total_dist = node_info[2] outdoor_dist = node_info[3] src = Node(src_name) dest = Node(dest_name) edge = WeightedEdge(src, dest, total_dist, outdoor_dist) if graph.has_node(src) == False: graph.add_node(src) if graph.has_node(dest) == False: graph.add_node(dest) graph.add_edge(edge) map_data.close() return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map Graph’s nodes represent the numbered buildings Graph’s edges represent the distance (total and outdoor) of each route The distances are asigned as object attributes to instances of WeightedEdge class """ print("Loading map from file...") file = open(map_filename, mode='r') g = Digraph() for line in file: # convert each line (string) to a list of the values in that line line_lst = line.split() src = Node(line_lst[0]) dest = Node(line_lst[1]) tot_dist = int(line_lst[2]) out_dist = int(line_lst[3]) if not g.has_node(src): g.add_node(src) if not g.has_node(dest): g.add_node(dest) edge = WeightedEdge(src, dest, tot_dist, out_dist) g.add_edge(edge) return g
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") graph = Digraph() filepath = str( Path(__file__).resolve().parents[0] / map_filename ) map_file = open(filepath,'r') for line in map_file: from_vert, to_vert, tot_dist, dist_out = line.split() from_node = Node(from_vert) if not graph.has_node(from_node): graph.add_node(from_node) to_node = Node(to_vert) if not graph.has_node(to_node): graph.add_node(to_node) edge = WeightedEdge(from_node,to_node,tot_dist,dist_out) graph.add_edge(edge) map_file.close() return graph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ # TODO print("Loading map from file...") data = open(map_filename, "r") map = Digraph() while True: dataLine = data.readline() if dataLine == "": break dataLine = dataLine.replace("\n", "").split( " ") # Refining the data from the txt file # Creating a weightedEdge Object currentEdge = WeightedEdge(Node(dataLine[0]), Node(dataLine[1]), int(dataLine[2]), int(dataLine[3])) if not (map.has_node(Node(dataLine[0]))): map.add_node(Node(dataLine[0])) if not (map.has_node(Node(dataLine[1]))): map.add_node(Node(dataLine[1])) map.add_edge(currentEdge) return map
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ # TODO print("Loading map from file...") Map = Digraph() datafile = open(map_filename, 'r') #loop over each line to collect data # source destination total_dist outdoor_dist for line in datafile: map_info = line.split() #add source as a node source = Node(map_info[0]) if not Map.has_node(source): Map.add_node(source) #add destination as a node destination = Node(map_info[1]) if not Map.has_node(destination): Map.add_node(destination) total_dist = int(map_info[2]) #want the distances to be integers outdoor_dist = int(map_info[3]) #create the edge from source to destination map_edge = WeightedEdge(source, destination, total_dist, outdoor_dist) Map.add_edge(map_edge) datafile.close() return Map
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print('Loading map from a file..') digraph = Digraph() file = open(map_filename, 'r') for line in file: src_name, dest_name, total_distance, outdoor_distance = line.rstrip( ).split(' ') src_node = Node(src_name) dest_node = Node(dest_name) edge = WeightedEdge(src_node, dest_node, int(total_distance), int(outdoor_distance)) if not digraph.has_node(src_node): digraph.add_node(src_node) if not digraph.has_node(dest_node): digraph.add_node(dest_node) digraph.add_edge(edge) return digraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 Digraph representing the map """ print("Loading map from file...") file = map_filename.strip() # remove apostrophes or quotations MIT_map = open(file) # open file MIT_map_digraph = Digraph() # empty digraph for line in MIT_map: # for each line in the file map_info = line.split(' ') # separate the elements source = Node(map_info[0]) # source node is first element dest = Node(map_info[1]) # destination node is second element if not MIT_map_digraph.has_node(source): # check if the source node is # already a node, if it isn't MIT_map_digraph.add_node(source) # add it as one if not MIT_map_digraph.has_node(dest): # check if the destination node # is already a node, if it isn't MIT_map_digraph.add_node(dest) # add it as one totalDistance = int(map_info[2]) # total distance is third element outdoorDistance = int(map_info[3]) # outdoor distance is fourth element weightedEdge = WeightedEdge(source, dest, totalDistance, outdoorDistance) # make a weighted edge # from the information in that line MIT_map_digraph.add_edge(weightedEdge) # add the weighted edge to the # digraph MIT_map.close() # close file return MIT_map_digraph # return digraph
def load_map(map_filename): """ Parses the map file and constructs a directed graph Parameters: map_filename : 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 e.g. 32 76 54 This entry would become an edge from 32 to 76. Returns: a directed graph representing the map """ print("Loading map from file...") with open(map_filename) as file: graph = Digraph() for line in file: # take out the \n from each line in the file line = line[:-1] # elements represents a list containing [source, destination, distance] elements = line.split(" ") node = Node(elements[0]) dest = Node(elements[1]) edge = WeightedEdge(Node(elements[0]), Node(elements[1]), int(elements[2])) # add the source and destination nodes to the graph if they are not already there and once they are, # add the edge if not graph.has_node(node): graph.add_node(node) if not graph.has_node(dest): graph.add_node(dest) if graph.has_node(node) and graph.has_node(dest): graph.add_edge(edge) return graph