def test_is_route(self): G = Graph(6, [(1, 2), (2, 3), (1, 4), (5, 6), (6, 5)]) for u in [1, 2, 3, 4]: for v in [5, 6]: with self.subTest(u=u, v=v): self.assertFalse(is_route(G, u, v)) self.assertFalse(is_route(G, v, u)) for (u, v) in [(1, 2), (2, 3), (1, 4)]: with self.subTest(u=u, v=v): self.assertTrue(is_route(G, u, v)) self.assertFalse(is_route(G, v, u)) self.assertTrue(is_route(G, 5, 6)) self.assertTrue(is_route(G, 6, 5))
def main(domain, path_set): """Read domain and graph files to generate the problem, call general_search function """ G = Graph() read_graph(G, path_set["GRAPH_FILE"]) coords = get_coords(path_set["COORDS_FILE"]) if domain not in ["route", "tsp"]: # print "Wrong Domain!" return 0 if domain == "route": # Q2: Get domain and search method from route.txt route_info = [] for line in open(path_set["DOMAIN_ROUTE_FILE"]): route_info.append(line.strip('\n')) start, goal, method = route_info[:3] problem = { 'domain': domain, 'method': method, 'graph': G, 'coords': coords, 'start': start, 'goal': goal} elif domain == "tsp": # Q3: Get domain and search method from tsp.txt tsp_info = [] for line in open(path_set["DOMAIN_TSP_FILE"]): tsp_info.append(line.strip('\n')) start, method = tsp_info[:2] problem = { 'domain': domain, 'method': method, 'graph': G, 'coords': coords, 'start': start} result = general_search(problem) # my search algorithm if result: path, cost, count = result print("------------------------------") print("domain: \t %s" % domain) print("method: \t %s" % method) print("node expanded: \t %s" % count) print("solution path: \t %s" % " -> ".join(path)) print("total cost: \t %s" % cost) else: print("no path!")
def make_graph_from_maze(coordinate, maze, graph=Graph()): x, y = coordinate path = maze[y][x] cur_vertex = Vertex(coordinate) if path == '+': return (None, graph) if coordinate in graph.data: return graph.get_vertex(coordinate), graph if x < 0 or y < 0 or x > len(maze[0]) or y > len(maze): return Vertex('GOAAAAAAL'), graph graph.add_vertex(cur_vertex) for x_offset, y_offset in DIRECTION: other_vertex, _ = make_graph_from_maze((x + x_offset, y + y_offset), maze, graph) if other_vertex is not None: graph.add_edge(cur_vertex, other_vertex, 1) return cur_vertex, graph
def test_topological_sort(self): G = Graph(6, [(1, 4), (6, 2), (2, 4), (6, 1), (4, 3)]) print(topological_sort(G)) G = Graph(8, [(1, 3), (1, 4), (2, 4), (3, 4), (3, 5), (5, 8), (6, 5), (6, 7), (7, 8)]) print(topological_sort(G))
from mygraph import Graph from myalgorithm import prims_algorithm with open('graph_10.txt') as f: l = f.readline() m = [] for line in f: m.append([int(x) for x in line.split()]) cnt = 0 g = Graph() weight = [] for i in range(len(m)): weight.append(m[i][len(m[i]) - 1]) for i in range(len(m[0]) - 1): g.add_vertex(i) for j in range(len(m)): x = None y = None cnt = 0 for i in range(len(m[0]) - 1): if cnt == 0 and m[j][i] == 1: x = i cnt = 1 continue if m[j][i] == 1 and cnt == 1: y = i cnt = 2 if cnt == 2:
from mygraph import Node from mygraph import Graph g = Graph() g.add_node('a') g.add_node('b') g.add_node('c') g.add_node('d') g.add_node('e') g.add_node('f') g.add_edge('a', 'b', 7) g.add_edge('a', 'c', 9) g.add_edge('a', 'f', 14) g.add_edge('b', 'c', 10) g.add_edge('b', 'd', 15) g.add_edge('c', 'd', 11) g.add_edge('c', 'f', 2) g.add_edge('d', 'e', 6) g.add_edge('e', 'f', 9) print("Nodes") for n in g: print(g.nodes_dict[n.name]) print("Connections") for n in g: for w in n.get_connections(): print('( %s , %s, %3d)' % (n.id, w.id, n.get_weight(w)))
from mygraph import Graph f=open('graph_18.txt','r') line = f.readline() mymatrix = [] weights = [] graph = Graph() new_graph = {} count=0 for line in f: mymatrix.append([int(x) for x in line.split()]) graph.add_vertex(count) new_graph[count] = {} count += 1 for i in range(len(mymatrix)): weights.append(mymatrix[i][len(mymatrix[i]) - 1]) for j in range(len(mymatrix)): col = 0 row = 0 counter = 0 for i in range(len(mymatrix[0]) - 1): if counter == 0 and mymatrix[j][i] == 1: col = i counter = 1 continue if mymatrix[j][i] == 1 and counter == 1: row = i
from mygraph import Graph from myalgorithm import dijkstra with open('graph_36.txt') as file: line1 = file.readline() incidence_matrix = [] for line in file: incidence_matrix.append([int(x) for x in line.split()]) counter = 0 graph = Graph() new_graph = {} weight = [] for i in range(len(incidence_matrix)): weight.append(incidence_matrix[i][len(incidence_matrix[i]) - 1]) for i in range(len(incidence_matrix[0]) - 1): graph.add_vertex(i) new_graph[i] = {} for j in range(len(incidence_matrix)): first_one = None second_one = None counter = 0 for i in range(len(incidence_matrix[0]) - 1): if counter == 0 and incidence_matrix[j][i] == 1: first_one = i counter = 1 continue if incidence_matrix[j][i] == 1 and counter == 1: second_one = i
from mygraph import Graph from myalgorithm import kruskalsalg with open('graph_17.txt') as f: s = f.readline() t = [] for line in f: t.append([int(i) for i in line.split()]) cnt = 0 g = Graph() weight = [] for i in range(len(t)): weight.append(t[i][len(t[i]) - 1]) for i in range(len(t[0]) - 1): g.add_vertex(i) for j in range(len(t)): x = None y = None cnt = 0 for i in range(len(t[0]) - 1): if cnt == 0 and t[j][i] == 1: x = i cnt = 1 continue if t[j][i] == 1 and cnt == 1: y = i cnt = 2