def map_parser(filename): edges = dict() nodes = dict() def add_edge(corr1, corr2): dist = int(sqrt((corr2[0] - corr1[0])**2 + (corr2[1] - corr1[1])**2)) label1 = nameify(corr1) label2 = nameify(corr2) d = edges.get(label1) if d: d[label2] = dist else: edges[label1] = {label2: dist} def add_node(corr): nodes[nameify(corr)] = corr def nameify(corr): return '(' + str(corr[0]) + ',' + str(corr[1]) + ')' with open(os.path.join(DIR_PATH, filename), 'r') as csvfile: reader = csv.reader(csvfile, delimiter=' ') for row in reader: corr1 = int(row[0]), int(row[1]) corr2 = int(row[3]), int(row[4]) add_node(corr1) add_node(corr2) add_edge(corr1, corr2) graph = Graph(edges, directed=True) graph.locations = nodes return graph
def create_graph_from_points(cord_list: List[Tuple[float, float]]) -> Graph: graph_dict: Dict[str, Dict[str, float]] = {} for index, cord in enumerate(cord_list): tmp_cord_dict = dict() for sec_index, sec_cord in enumerate(cord_list): if index != sec_index: tmp_cord_dict[str(sec_index + 1)] = distance(cord, sec_cord) graph_dict[str(index + 1)] = tmp_cord_dict return Graph(graph_dict, False)
def setUp(self): nodes = { 0: City(0, "Zion0", Position(1.5352765135104984, -25.407051236792057)), 1: City(1, "Amsterdam1", Position(-80.89942735555584, 102.4463541142299)), 2: City(2, "Phoenix2", Position(24.713383034928697, 178.20745931034742)), 3: City(3, "State College3", Position(-78.057464912123, -120.7319949406601)), 4: City(4, "Reno4", Position(-49.415337519725654, 4.901625295542724)) } graph = {0: [2], 1: [0, 2], 2: [3, 0], 3: [4], 4: [3, 2]} self.graph = Graph(nodes, graph) self.start = list(nodes)[0] self.finish = list(nodes)[-1] self.solution = [0, 2, 3, 4]
def simpleGraphCreation(): ''' UUUU US U U U U GU UUUU ''' g = Graph( { (1, 1): { (1, 2): 1, (2, 1): 1 }, (1, 2): { (2, 2): 1 }, (2, 1): { (2, 2): 1, (3, 1): 1 }, (2, 2): { (3, 2): 1 }, (3, 1): { (3, 2): 1 }, }, directed=False) print(g.get((2, 2))) print(g.nodes()) g.locations = Dict() for i in g.nodes(): g.locations.update({i: i}) print(euclidean(g.locations[(1, 1)], g.locations[(3, 2)]))
V: List[str] = list(graph_dict.keys()) T: Set[Tuple[str, str]] = set() X: Set[str] = set() sum_of_MST: float = 0.0 X.add(V[0]) while len(X) != graph.number_of_vertices: crossing: Set[Tuple[str, str]] = set() for x in X: for k in V: if k not in X and graph_dict.get(x).get(k) > 0.0: crossing.add((x, k)) edge = sorted(crossing, key=lambda e: graph_dict.get(e[0]).get(e[1]))[0] T.add(edge) X.add(edge[1]) sum_of_MST += graph_dict.get(edge[0]).get(edge[1]) # for edge in T: # print(str(edge)) return sum_of_MST if __name__ == '__main__': romania_map = Graph(dict( Arad=dict(Bucharest=75.0, Craiova=140.0), Bucharest=dict(Arad=75.0, Craiova=101.0), Craiova=dict(Arad=140.0, Bucharest=101.0)), False) print(primMST(romania_map))
def create_graph(self, graph_file=None): map = '' if graph_file == None: map = open(self.graph_file, 'r') else: map = open(graph_file, 'r') # index of the row for graph_arr index = 0 # temp array to hold the row of numbers arr = [] # temp array to hold the 2D array of numbers graph_arr = [] # dictionary to hold the vertices and paths dict = Dict() start_coord = map.readlines()[0:1] map.seek(0) goal_coord = map.readlines()[1:2] map.seek(0) # set the coordinates start_coord = re.sub('[^0-9,.]', '', str(start_coord)).split(",") self.start_coordinates = (int(start_coord[0]), int(start_coord[1])) goal_coord = re.sub('[^0-9,.]', '', str(goal_coord)).split(",") self.end_coordinates = (int(goal_coord[0]), int(goal_coord[1])) for line in map.readlines()[2:]: for i in range(0, len(line)): if line[i] != '\n': # avoid new line characters arr.append(int(line[i])) index = index + 1 graph_arr.insert(index, arr) arr = [] self.graph_arr = graph_arr for row in range(0, len(graph_arr) - 1): for col in range(0, len(graph_arr[row])): if graph_arr[row][col] == 0: continue else: # if both right and bottom are free and not = 0, make a path. if col + 1 < len(graph_arr[row]) and row + 1 < len( graph_arr) and graph_arr[row][ col + 1] != 0 and graph_arr[row + 1][col] != 0: dict.update({ (row, col): { (row, col + 1): int(graph_arr[row][col + 1]), (row + 1, col): int(graph_arr[row + 1][col]) } }) # if the bottom is free, but not the right and the bottom isn't = 0, make a path. elif graph_arr[row][col + 1] == 0 and graph_arr[ row + 1][col] != 0 and row + 1 < len(graph_arr): dict.update({ (row, col): { (row + 1, col): int(graph_arr[row + 1][col]) } }) # if the right is free, but not the bottom and the right isn't = 0, make a path. elif graph_arr[row + 1][col] == 0 and graph_arr[row][ col + 1] != 0 and col + 1 < len(graph_arr[row]): dict.update({ (row, col): { (row, col + 1): int(graph_arr[row][col + 1]) } }) # you reached a point without an right or bottom opening else: dict.update({(row, col): {}}) # self.manhattan_distances.update({(row, col): (row, col)}) # self.get_manhattan_distance((row, col)) # graph created self.graph = Graph(dict, directed=False) # self.graph.locations = self.manhattan_distances self.graph.locations = Dict() for i in self.graph.nodes(): self.graph.locations.update({i: i}) map.close()
MY_GRAPH = Graph( edgesdict=[{ NAME: '1', VAL: 10, NODE1: 'S', NODE2: 'A' }, { NAME: '2', VAL: 8, NODE1: 'S', NODE2: 'C' }, { NAME: '3', VAL: 3, NODE1: 'S', NODE2: 'B' }, { NAME: '4', VAL: 3, NODE1: 'A', NODE2: 'C' }, { NAME: '5', VAL: 3, NODE1: 'B', NODE2: 'C' }, { NAME: '6', VAL: 3, NODE1: 'C', NODE2: 'D' }, { NAME: '7', VAL: 12, NODE1: 'C', NODE2: 'G' }, { NAME: '8', VAL: 4, NODE1: 'C', NODE2: 'E' }, { NAME: '9', VAL: 10, NODE1: 'D', NODE2: 'G' }, { NAME: '10', VAL: 4, NODE1: 'E', NODE2: 'G' }], # Heuristic is not admissible nor consistent heuristic={ 'G': { 'S': 0, 'A': 5, 'B': 10, 'C': 7, 'D': 0, 'E': 9, 'G': 0 } })
NEWGRAPH1 = Graph( edgesdict=[{ 'NAME': 'e1', 'LENGTH': 6, 'NODE1': 'S', 'NODE2': 'A' }, { 'NAME': 'e2', 'LENGTH': 4, 'NODE1': 'A', 'NODE2': 'B' }, { 'NAME': 'e3', 'LENGTH': 7, 'NODE1': 'B', 'NODE2': 'F' }, { 'NAME': 'e4', 'LENGTH': 6, 'NODE1': 'C', 'NODE2': 'D' }, { 'NAME': 'e5', 'LENGTH': 3, 'NODE1': 'C', 'NODE2': 'A' }, { 'NAME': 'e6', 'LENGTH': 7, 'NODE1': 'E', 'NODE2': 'D' }, { 'NAME': 'e7', 'LENGTH': 6, 'NODE1': 'D', 'NODE2': 'H' }, { 'NAME': 'e8', 'LENGTH': 2, 'NODE1': 'S', 'NODE2': 'C' }, { 'NAME': 'e9', 'LENGTH': 2, 'NODE1': 'B', 'NODE2': 'D' }, { 'NAME': 'e10', 'LENGTH': 25, 'NODE1': 'E', 'NODE2': 'G' }, { 'NAME': 'e11', 'LENGTH': 5, 'NODE1': 'E', 'NODE2': 'C' }], heuristic={ "G": { 'S': 11, 'A': 9, 'B': 6, 'C': 12, 'D': 8, 'E': 15, 'F': 1, 'H': 2 }, "H": { 'S': 11, 'A': 9, 'B': 6, 'D': 12, 'E': 8, 'F': 15, 'G': 14 }, 'A': { 'S': 5, # admissible "B": 1, # h(d) > h(b)+c(d->b) ... 6 > 1 + 2 "C": 3, "D": 6, "E": 8, "F": 11, "G": 33, "H": 12 }, 'C': { "S": 2, # consistent "A": 3, "B": 7, "D": 6, "E": 5, "F": 14, "G": 30, "H": 12 }, "D": { "D": 3 }, # dumb "E": {} # empty })