def test_shortest_path(self): graph = DiGraph() graph_algo = GraphAlgo(graph) for i in range(0, 11): graph.add_node(i) graph.add_edge(1, 2, 1) graph.add_edge(2, 3, 1) graph.add_edge(3, 4, 1) graph.add_edge(4, 5, 1) graph.add_edge(5, 10, 1) graph.add_edge(1, 6, 10) graph.add_edge(6, 7, 20) graph.add_edge(7, 8, 30) graph.add_edge(8, 9, 40) graph.add_edge(9, 10, 50) check = (float('inf'), []) check2 = (0, [0]) check3 = (10, [1, 6]) check4 = (5, [1, 2, 3, 4, 5, 10]) self.assertEqual(graph_algo.shortest_path(2, 6), check) self.assertEqual(graph_algo.shortest_path(0, 0), check2) self.assertEqual(graph_algo.shortest_path(1, 6), check3) self.assertEqual(graph_algo.shortest_path(1, 10), check4)
def test_shortest_path(self): g = DiGraph() for i in range(0, 5): g.add_node(i) g.add_edge(0, 1, 10) g.add_edge(1, 2, 10) g.add_edge(0, 2, 5) g.add_edge(2, 3, 10) g.add_edge(3, 4, 10) graph = GraphAlgo(g) (path, path_list) = (25, [0, 2, 3, 4]) (p, pl) = graph.shortest_path(0, 4) print(p, pl) self.assertEqual(path, p) self.assertEqual(path_list, pl) (path0, path_list0) = (float('inf'), []) (p0, pl0) = graph.shortest_path(4, 0) self.assertEqual(path0, p0) self.assertEqual(path_list0, pl0) (p0, pl0) = graph.shortest_path(7, 0) # nodes does not exist self.assertEqual(path0, p0) self.assertEqual(path_list0, pl0)
def test_connected_component(self): graph = DiGraph() graph.add_node(0) graph.add_node(1) graph.add_node(2) graph.add_node(3) graph.add_node(4) graph.add_node(5) graph.add_node(6) graph.add_node(7) graph.add_edge(0, 1, 2) graph.add_edge(1, 2, 2) graph.add_edge(2, 0, 2) graph.add_edge(5, 0, 2) graph.add_edge(5, 6, 2) graph.add_edge(6, 2, 2) graph.add_edge(6, 0, 2) graph.add_edge(6, 4, 2) graph.add_edge(4, 5, 2) graph.add_edge(3, 4, 2) graph.add_edge(3, 7, 2) graph.add_edge(7, 3, 2) graph.add_edge(7, 5, 2) g_algo = GraphAlgo(graph) answer = [0, 1, 2] self.assertEqual(g_algo.connected_component(1), answer)
def setUpClass(self): self.g2 = DiGraph() # creates an empty directed graph for n in range(5): self.g2.add_node(n) self.g2.add_edge(0, 1, 1) self.g2.add_edge(1, 0, 1.1) self.g2.add_edge(1, 2, 1.3) self.g2.add_edge(2, 3, 1.1) self.g2.add_edge(1, 3, 1.9) self.g2.remove_edge(1, 3) self.g2.add_edge(1, 3, 10) self.g2.add_edge(0, 4, 10) self.g2.add_edge(4, 0, 10) self.g_algo = GraphAlgo(self.g2) self.aaa = DiGraph() self.aaa.add_node(1) self.aaa.add_node(2) self.aaa.add_node(3) self.aaa.add_edge(1, 2, 3) self.aaa.add_edge(1, 3, 3) self.aaa.add_edge(2, 3, 3) self.z = GraphAlgo() self.z.my_graph = DiGraph() self.z.my_graph.add_node(1) self.z.my_graph.add_node(2) self.z.my_graph.add_node(3) self.z.my_graph.add_node(4) self.z.my_graph.add_edge(1, 2, 9) self.z.my_graph.add_edge(1, 3, 1) self.z.my_graph.add_edge(2, 3, 1) self.z.my_graph.add_edge(3, 2, 1) self.z.my_graph.add_edge(1, 4, 1) self.z.my_graph.add_edge(4, 2, 1)
def test_get_graph(self): self.graph = DiGraph() for i in range(10): self.graph.add_node(i) graph_algo = GraphAlgo(self.graph) self.assertEqual(self.graph.__str__(), graph_algo.get_graph().__str__())
def test_connected_components(self): d1 = DiGraph() d1.add_node(0, (1, 2, 0)) d1.add_node(1, (4, 7, 0)) d1.add_node(2, (0, 1, 4)) d1.add_node(3, (0, 9, 8)) d1.add_node(4, (4, 4, 0)) d1.add_node(5, (5, 15, 0)) d1.add_node(6, (21, 2, 0)) d1.add_node(7, (13, 13, 0)) d1.add_edge(0, 1, 1) d1.add_edge(1, 2, 2) d1.add_edge(2, 3, 3) d1.add_edge(2, 0, 3) d1.add_edge(3, 4, 0.5) d1.add_edge(4, 5, 0.5) d1.add_edge(4, 7, 0.5) d1.add_edge(5, 6, 0.5) d1.add_edge(6, 4, 0.5) d1.add_edge(6, 7, 0.5) d1.add_node(8, (6, 8, 0)) d1.add_node(9, (7, 8, 0)) d1.add_edge(8, 9, 1) d1.add_edge(9, 8, 1) d1.add_edge(8, 0, 1) algo = GraphAlgo(d1) self.assertTrue([[0, 2, 1], [3], [4, 6, 5], [7], [9, 8]] == algo.connected_components())
def test_connected_components(self): graph = DiGraph() for i in range(1, 9): graph.add_node(i) graph.add_edge(1, 2, 1) graph.add_edge(2, 3, 1) graph.add_edge(3, 4, 1) graph.add_edge(4, 1, 1) graph.add_edge(3, 5, 1) graph.add_edge(5, 6, 1) graph.add_edge(6, 7, 1) graph.add_edge(7, 5, 1) graph.add_edge(7, 8, 1) algo = GraphAlgo(graph) self.assertEqual([[1, 2, 3, 4], [5, 6, 7], [8]], algo.connected_components()) graph.add_edge(5, 3, 1) self.assertEqual([[1, 2, 3, 4, 5, 6, 7], [8]], algo.connected_components()) algo = GraphAlgo() self.assertEqual([], algo.connected_components()) algo = GraphAlgo(None) self.assertEqual([], algo.connected_components())
def test_shortestPath(self): """ checks the correctness of the shortest path """ g = graphCreator(6) g.add_edge(0, 1, 0.5) g.add_edge(0, 2, 16.6) g.add_edge(1, 3, 1.2) g.add_edge(1, 2, 1.0) g.add_edge(2, 1, 5.5) g.add_edge(2, 3, 8.3) g.add_edge(4, 2, 7.0) g.add_edge(5, 4, 2.0) ga = GraphAlgo(g) path1 = [0, 1, 2] path2 = [5, 4, 2, 1, 3] path3 = [2, 1, 3] path11 = ga.shortest_path(0, 2) path22 = ga.shortest_path(5, 3) path33 = ga.shortest_path(2, 3) path44 = ga.shortest_path(4, 0) self.assertEqual(1.5, path11[0]) self.assertEqual(15.7, path22[0]) self.assertEqual(6.7, path33[0]) self.assertEqual(inf, path44[0]) self.assertEqual(True, (path11[1] == path1)) self.assertEqual(True, (path22[1] == path2)) self.assertEqual(True, (path33[1] == path3)) self.assertEqual(0, len(path44[1])) self.assertEqual(inf, path44[0])
def test_save_and_load_no_pos(self): # add nodes self.graph = DiGraph() self.graph.add_node(1) self.graph.add_node(2) self.graph.add_node(3) self.graph.add_node(4) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 1, 6) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # save as JSON file self.algo.save_to_json("../data/test.json") self.graph1 = self.algo.get_graph() # load JSON file self.algo.load_from_json("../data/test.json") self.graph2 = self.algo.get_graph() # check if Graph's.toString are equal self.assertEqual(self.graph1.__repr__(), self.graph2.__repr__())
def test_extremeCases(self): graph = DiGraph() graph.add_node(1) graph.add_node(2) graph.add_node(3) graph.add_node(4) graph.add_node(5) graph.add_node(6) graph.add_edge(1, 3, 6) graph.add_edge(3, 4, 10) graph.add_edge(4, 2, 5) graph.add_edge(2, 1, 10) graph.add_edge(2, 1, 10) graph.add_edge(2, 1, 10.9) algo = GraphAlgo(graph) self.assertEquals(11, graph.get_mc()) graph.remove_node(2) self.assertEquals(5, graph.v_size()) self.assertEquals(2, graph.e_size()) self.assertFalse(graph.remove_node(2)) self.assertEquals(16, algo.shortest_path(1, 4)[0]) self.assertEquals((float('inf'), []), algo.shortest_path(1, 2)) self.assertEquals(12, graph.get_mc())
def test_shortest_path_list(self): """Creating a graph and checking the shortestPathDist on it""" print("Checking shortest_path_list... ") g1 = DiGraph() for i in range(0, 5): g1.add_node(i) g1.add_edge(0, 1, 2.5) g1.add_edge(0, 1, 20) g1.add_edge(1, 2, 6.7) g1.add_edge(2, 3, 40) g1.add_edge(3, 4, 10) g1.add_edge(4, 3, 1) g1.add_edge(3, 2, 16) g1.add_edge(2, 1, 0.5) g1.add_edge(1, 0, 20) g1.add_edge(4, 2, 70) g1.add_edge(2, 4, 70) g1.add_edge(1, 2, 100) g1.add_edge(3, 1, 80) g1.add_edge(1, 3, 80) operator = GraphAlgo(g1) self.assertEqual([1], operator.shortest_path(1, 1)[1]) self.assertEqual([0, 1, 2], operator.shortest_path(0, 2)[1]) self.assertEqual([0, 1, 2, 3], operator.shortest_path(0, 3)[1]) self.assertEqual([0, 1, 2, 3, 4], operator.shortest_path(0, 4)[1]) self.assertEqual([4, 3], operator.shortest_path(4, 3)[1]) self.assertEqual([4, 3, 2], operator.shortest_path(4, 2)[1]) self.assertEqual([4, 3, 2, 1], operator.shortest_path(4, 1)[1]) self.assertEqual([4, 3, 2, 1, 0], operator.shortest_path(4, 0)[1])
def check0(): """ This function tests the naming (main methods of the DiGraph class, as defined in GraphInterface. :return: """ g = DiGraph() # creates an empty directed graph for n in range(4): g.add_node(n) g.add_edge(0, 1, 1) g.add_edge(1, 0, 1.1) g.add_edge(1, 2, 1.3) g.add_edge(0, 3, 1) g.add_edge(2, 3, 1.1) g.add_edge(1, 3, 1.9) g.remove_edge(1, 3) g.add_edge(1, 3, 10) print(g.e_size()) print(g) # prints the __repr__ (func output) print(g.get_all_v()) # prints a dict with all the graph's vertices. print(g.all_in_edges_of_node(1)) print(g.all_out_edges_of_node(1)) g_algo = GraphAlgo(g) print(g_algo.shortest_path(1, 0)) g_algo.plot_graph()
def test_connected_components(self): self.assertEqual(self.ga.connected_components(), [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.ga.Graph.add_edge(1, 0, 5) self.assertEqual(self.ga.connected_components(), [[0, 1], [2], [3], [4], [5], [6], [7], [8], [9]]) self.assertEqual(self.ga.connected_component(0), [0, 1]) empty_graph = GraphAlgo() self.assertEqual(empty_graph.connected_components(), [])
def run_time(file_name: str, src, dest): graph_algo = GraphAlgo() dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.load_from_json(file_name) stop = timeit.default_timer() dt = dt + (stop - start) print(graph_algo.get_graph()) print("run time of read from json file and build the graph: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() connecteds = graph_algo.connected_components() stop = timeit.default_timer() dt = dt + (stop - start) # print(connecteds) print("run time of connected_components: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.connected_component(1) stop = timeit.default_timer() dt = dt + (stop - start) print("run time of connected_component: ", dt / 100) dt = 0 for i in range(100): start = timeit.default_timer() graph_algo.shortest_path(src, dest) stop = timeit.default_timer() dt = dt + (stop - start) print("run time of shortest_path: ", dt / 100)
def test_shortest_path(self, SCC: list = None, graph: object = None): V = r.randint(20, 30) if SCC is None: if graph is not None: SCC = self.get_max_scc(graph) else: graph = self.make_graph(V, V * 4) SCC = self.get_max_scc(graph) elif graph is None: graph = self.make_graph(V, V * 4) if SCC is not None and graph is not None: try: algo = GraphAlgo(graph) first_node = SCC[0] for node in SCC: if node.key != first_node.key: sp1 = algo.shortest_path(first_node.key, node.key) assert len(sp1[1]) > 1 dist1 = sp1[1][len(sp1[1]) - 1].weight assert dist1 == sp1[0] sp2 = algo.shortest_path(node.key, first_node.key) assert len(sp2[1]) > 1 dist2 = sp2[1][len(sp2[1]) - 1].weight assert dist2 == sp2[0] # algo.get_graph().add_node(-10) # sp = algo.shortest_path(first_node.key, algo.get_graph().get_node(-10)) # assert sp[0] == float('inf') and len(sp[1]) == 0 except AssertionError as e: print(e) self.fail("shortest_path_test failed with graph {}".format( algo.get_graph().__str__()))
def test_big_dijkstra(self): # Checking if there is an exception at some scenario algo = GraphAlgo() algo.load_from_json("data/A5") for i in algo.graph.nodes.keys(): for j in algo.graph.nodes.keys(): x = algo.shortest_path(i, j)
def test_save_and_load(self): file_path = "unittest.json" graph = DiGraph() for i in range(9): graph.add_node(i) graph.add_edge(0, 1, 2.3) graph.add_edge(0, 3, 1.2) graph.add_edge(0, 2, 1.0) graph.add_edge(1, 4, 4.5) graph.add_edge(1, 3, 4.9) graph.add_edge(1, 7, 8.5) graph.add_edge(2, 5, 6.1) graph.add_edge(3, 2, 1.34) graph.add_edge(4, 3, 4.5) graph.add_edge(4, 2, 3.7) graph.add_edge(4, 5, 0.73) graph.add_edge(4, 6, 4.9) graph.add_edge(5, 2, 6.1) graph.add_edge(6, 5, 1.34) graph.add_edge(6, 7, 2.9) graph.add_edge(7, 6, 2.9) graph.add_edge(7, 1, 8.5) graph.add_edge(7, 8, 2.35) graph.add_edge(7, 4, 5.0) graph.add_edge(8, 7, 4.5) graph_algo = GraphAlgo(graph) graph_algo.save_to_json(file_path) graph_algo = GraphAlgo() graph_algo.load_from_json(file_path) self.assertEqual(graph, graph_algo.get_graph()) graph.remove_edge(3, 2) self.assertNotEqual(graph, graph_algo.get_graph())
def test_save_and_load(self): # add nodes self.graph = DiGraph() self.graph.add_node(1, (1.44, 2.43, 4.477)) self.graph.add_node(2, (2.789, 4.3331, 6.41457)) self.graph.add_node(3, (3.142, 4.1376, 2.1376)) self.graph.add_node(4, (5.137, 6.2, 12.137)) # add edges self.graph.add_edge(1, 2, 2) self.graph.add_edge(2, 3, 3) self.graph.add_edge(3, 4, 4) self.graph.add_edge(4, 1, 6) # init graph algo self.algo = GraphAlgo() self.algo.__init__(self.graph) # save as JSON file self.algo.save_to_json("../data/test.json") self.graph1 = self.algo.get_graph() # load JSON file self.algo.load_from_json("../data/test.json") self.graph2 = self.algo.get_graph() # check if Graph's.toString are equal self.assertEqual(self.graph1.__repr__(), self.graph2.__repr__())
def test_shortest_path(self): g = DiGraph() for i in range(10): g.add_node(i) k = GraphAlgo(g) self.assertEqual((float('inf'), []), k.shortest_path(0, 9)) g.add_edge(0, 1, 2) g.add_edge(1, 2, 1) g.add_edge(1, 3, 6.2) g.add_edge(3, 2, 1) g.add_edge(0, 3, 3) g.add_edge(0, 8, 16) g.add_edge(0, 4, 14) g.add_edge(0, 5, 3) g.add_edge(5, 6, 15) g.add_edge(5, 7, 7) g.add_edge(4, 7, 2.5) g.add_edge(7, 9, 3) g.add_edge(7, 8, 4) g.add_edge(8, 1, 9) dis, l = k.shortest_path(0, 9) self.assertEqual(l, [0, 5, 7, 9]) self.assertEqual(dis, 13) g.remove_node(9) g.add_node(9) self.assertEqual((float('inf'), []), k.shortest_path(0, 9))
def test_connected_components_none_graph(self): ga1 = GraphAlgo(None) self.assertEqual( "[]", ga1.connected_components().__str__(), "connected_components returns " "uncorrected list for none graph")
def setUp(self) -> None: self.graph = DiGraph() for i in range(10): self.graph.add_node(i) self.graph.add_edge(0, 1, 1.0) self.graph.add_edge(0, 2, 3.1) self.graph.add_edge(2, 0, 2.4) self.graph.add_edge(1, 3, 4.2) self.graph.add_edge(1, 4, 3.2) self.graph.add_edge(2, 3, 1.5) self.graph.add_edge(3, 1, 3.5) self.graph.add_edge(3, 4, 5.6) self.graph.add_edge(4, 0, 2.7) self.graph.add_edge(4, 1, 4.8) self.graph.add_edge(4, 3, 1.9) self.graph.add_edge(5, 4, 7.2) self.graph.add_edge(6, 5, 2.2) self.graph.add_edge(6, 4, 5.2) self.graph.add_edge(4, 6, 1.2) self.graph.add_edge(7, 5, 4.1) self.graph.add_edge(6, 7, 2.8) self.graph.add_edge(8, 0, 9.2) self.graph.add_edge(0, 8, 5.2) self.graph.add_edge(9, 8, 2.6) self.graph.add_edge(8, 9, 7.4) self._g = GraphAlgo(self.graph)
def test_connected_component(self): graph = GraphAlgo(create_small_graph()) self.assertEqual(graph.connected_component(3), [2, 3]) self.assertEqual(graph.connected_component(0), [0, 1]) self.assertEqual(graph.connected_component(1), [0, 1]) self.assertEqual(graph.connected_component(9), [])
def setUp(self) -> None: graph = DiGraph() for i in range(10): graph.add_node(i) for i in range(9): graph.add_edge(i, i + 1, 3) self.ga = GraphAlgo(graph)
def test_scc2(self): g = graph_builder(100) algo = GraphAlgo(g) for n in g.get_all_v().keys(): self.assertEqual(1, len(algo.connected_component(n))) self.assertEqual(0, len(algo.connected_component(200))) # un-existed node self.assertEqual(100, len(algo.connected_components()))
def test_helper_scc(self): g = DiGraph() g.add_node(0) g.add_node(1) g.add_edge(0, 1, 3) g.add_edge(1, 0, 3) algo = GraphAlgo(g) algo.bfs(0, 0)
def test_plot_graph(self): # random positions self._g.plot_graph() # known positions self.g5 = DiGraph() self._g2 = GraphAlgo(self.g5) self._g2.load_from_json('../data/A1') self._g2.plot_graph()
def test_get_graph(self): """ test the return of the graph algo function """ graph = create_graph1() graph_algo = GraphAlgo(graph) g = graph_algo.get_graph self.assertEqual(graph_algo.get_graph(), graph)
def my_scc(file_path: str, id1: int, ) -> float: algo = GraphAlgo(None) algo.load_from_json(file_path) start_time = time.perf_counter() sccs = algo.connected_component(id1) end_time = time.perf_counter() # print("scc in g for specific node:", scc, "\n") return end_time-start_time
def test_shortest_path(self): algo = GraphAlgo() algo.load_from_json('../data/A0') path, pathList = algo.shortest_path(3, 9) pathDist = len(pathList) - 1 pathList2 = [3, 2, 1, 0, 10, 9] self.assertEqual(pathDist, 5) self.assertEqual(pathList, pathList2)
def test_load_from_json(self): g_algo = GraphAlgo() file = '../data/T0.json' g_algo.load_from_json(file) self.assertEqual(g_algo.get_graph().v_size(), 4) self.assertEqual(g_algo.get_graph().e_size(), 5)