def test_CreationAdjacencyListGraph(self): """ This test is used to test creation of one graph with 5 vertices with following edges: (1, 3); (2, 4); (1, 0) """ # Allocate a new 'AdjacencyListGraph' object # ---------------------------------------------------- # newAdjacencyListGraph = AdjacencyListGraph() # Adding 5 vertices... # ---------------------------------------------------- # for _ in range(5): newAdjacencyListGraph.addNewNode(None) # Adding edge... # ---------------------------------------------------- # newAdjacencyListGraph.insertNewEdge(1, 3) newAdjacencyListGraph.insertNewEdge(2, 4) newAdjacencyListGraph.insertNewEdge(1, 0) self.assertIn(3, newAdjacencyListGraph._nodeSet[1]._adjacencyList) self.assertIn(0, newAdjacencyListGraph._nodeSet[1]._adjacencyList) self.assertIn(4, newAdjacencyListGraph._nodeSet[2]._adjacencyList) # Print for debug. newAdjacencyListGraph.printAdjacencyList()
def test_if_adding_the_inverse_of_an_existing_edge_to_adjacency_list_graph_results_in_raising_error( ): g = AdjacencyListGraph() g.add_nodes("a", 1) g.add_edges(("a", 1)) with pytest.raises(ValueError): g.add_edges((1, "a"))
def test_if_adding_edge_without_weight_results_in_default_edge_weight_equals_to_one( ): g = AdjacencyListGraph() g.add_nodes(1, 2) g.add_edges((1, 2)) v = AdjacencyListDiGraph() v.add_nodes("x", 4) v.add_edges(("x", 4)) assert g.weight(1, 2) == 1 and v.weight("x", 4) == 1
def test_if_outgoing_neighbors_in_adjacency_list_graph_results_in_all_neighbors( ): g = AdjacencyListGraph() g.add_nodes(1, 2, "a", "b") g.add_edges((1, 2), ("a", 2), (2, "b")) output = [] for node in g.outgoing_neighbors(2): output.append(node) assert len(output) == 3 and set(output) == {1, "a", "b"}
def test_if_ingoing_neighbors_in_adjacency_list_graph_results_in_all_neighbors( ): g = AdjacencyListGraph() g.add_nodes(1, 2, "a", "b") g.add_edges((1, 2), ("a", 2), (2, "b")) output = [] for node in g.ingoing_neighbors(2): output.append(node) assert output == [1, "a", "b"]
def test_if_neighbors_in_adjacency_list_graph_results_in_the_return_of_appropriate_vertices( ): g = AdjacencyListGraph() g.add_nodes(1, 2, 3) g.add_edges((2, 1, .3), (1, 3)) output = [] for vertex in g.neighbors(1): output.append(vertex) assert len(output) == 2 and set(output) == {2, 3}
def test_successors(self): """ This test is used to test getSuccessor() methods using a graph with 5 vertices with following edges: (3, 2); (3, 4); (0, 1); """ def test(myGraph): # Adding 5 vertices... # ---------------------------------------------------- # for _ in range(5): myGraph.addNewNode(None) # Adding some edges... # ---------------------------------------------------- # myGraph.insertNewEdge(3, 2) myGraph.insertNewEdge(3, 4) myGraph.insertNewEdge(0, 1) # Getting all successors of node 3... # ---------------------------------------------------- # myList = myGraph.getSuccessors(3) self.assertIn(2, myList) self.assertIn(4, myList) # Getting all successors of node 0... # ---------------------------------------------------- # myList = myGraph.getSuccessors(0) self.assertIn(1, myList) # Getting all successors of node 1... # ---------------------------------------------------- # myList = myGraph.getSuccessors(1) self.assertEqual(len(myList), 0) # Allocate a new 'AdjacencyMatrixGraph' and a 'AdjacencyListGraph' objects... # ---------------------------------------------------- # newAdjacencyMatrixGraph = AdjacencyMatrixGraph() newAdjacencyListGraph = AdjacencyListGraph() # Run test (Polymorphism is powerful :) )... # ---------------------------------------------------- # test(newAdjacencyMatrixGraph) test(newAdjacencyListGraph)
def test_if_designation_edge_weight_results_in_recording_that_value_in_adjacency_list_structure( ): g = AdjacencyListGraph() g.add_nodes(1, 2) g.add_edges((1, 2, .3)) v = AdjacencyListDiGraph() v.add_nodes("x", 4) v.add_edges(("x", 4, -.2)) assert g._adjacency_list == { 1: { 2: .3 }, 2: { 1: .3 } } and v._adjacency_list == { "x": { 4: -.2 }, 4: {} }
from AdjacencyMatrixDiGraph import AdjacencyMatrixDiGraph from AdjacencyMatrixGraph import AdjacencyMatrixGraph from AdjacencyListDiGraph import AdjacencyListDiGraph from AdjacencyListGraph import AdjacencyListGraph if __name__ == '__main__': # my_graph = AdjacencyMatrixDiGraph() # my_graph.add_node("x", 55, 1) # my_graph.add_edge(("x", 1), (1, "x")) # for edge in my_graph.edges(): # print(edge) my_graph = AdjacencyListGraph() my_graph.add_nodes(3) my_graph.add_nodes(2) # print(my_graph.number_of_nodes) # for v in my_graph.nodes(): # print(v) print(my_graph.number_of_nodes)
def test_if_creating_an_empty_adjacency_list_graph_object_results_in_adjacency_list_attribute_as_empty_dict( ): g = AdjacencyListGraph() assert g._adjacency_list == {}
def test_if_adding_new_edge_to_adjacency_list_graph_results_in_increasing_number_of_edges( ): g = AdjacencyListGraph() g.add_nodes(1, 2, 3) g.add_edges((3, 1), (3, 2)) assert g.number_of_edges == 2
def test_if_adding_new_node_to_adjacency_list_graph_results_in_increasing_number_of_nodes( ): g = AdjacencyListGraph() g.add_nodes(5, "a") assert g.number_of_nodes == 2
def test_if_creating_an_empty_adjacency_list_graph_object_results_in_number_of_edges_equals_zero( ): g = AdjacencyListGraph() assert g.number_of_edges == 0
def test_visibilityDegreeCalculation_2(self): """ This test is used to test visibility degree calculation using a graph with 4 vertices with following edges: (0, 2); (0, 1); (1, 3); (2, 3); Values: 0: 10 1: 10 2: 10 3: 10 In this case we have two walk with same lenght! """ # Allocate a new 'AdjacencyListGraph' objects... # ---------------------------------------------------- # myGraph = AdjacencyListGraph() # Adding vertices... # ---------------------------------------------------- # myGraph.addNewNode(10) myGraph.addNewNode(10) myGraph.addNewNode(10) myGraph.addNewNode(10) # Adding edge... # ---------------------------------------------------- # myGraph.insertNewEdge(0, 1) myGraph.insertNewEdge(0, 2) myGraph.insertNewEdge(2, 3) myGraph.insertNewEdge(1, 3) # Calculation visibility degree... # ---------------------------------------------------- # self.assertEqual(myGraph.calcNodeVisibilityDegree(0), 3) self.assertEqual(myGraph.calcNodeVisibilityDegree(1), 1) self.assertEqual(myGraph.calcNodeVisibilityDegree(2), 1) self.assertEqual(myGraph.calcNodeVisibilityDegree(3), 0) self.assertEqual(myGraph.getMaxVisibilityDegreeNodeID(), 0)
def test_visibilityDegreeCalculation(self): """ This test is used to test visibility degree calculation using a graph with 6 vertices with following edges: (2, 3); (3, 5); (5, 1); (3, 4); (4, 5); (4, 0); (2, 1) Values: 0: 2 1: 0 2: 8 3: 7 4: 1 5: 6 """ def init(myGraph): # Adding vertices... # ---------------------------------------------------- # myGraph.addNewNode(2) myGraph.addNewNode(0) myGraph.addNewNode(8) myGraph.addNewNode(7) myGraph.addNewNode(1) myGraph.addNewNode(6) # Adding edge... # ---------------------------------------------------- # myGraph.insertNewEdge(2, 1) myGraph.insertNewEdge(2, 3) myGraph.insertNewEdge(3, 5) myGraph.insertNewEdge(3, 4) myGraph.insertNewEdge(4, 5) myGraph.insertNewEdge(4, 0) myGraph.insertNewEdge(5, 1) # Checking values... # ---------------------------------------------------- # self.assertEqual(myGraph._nodeSet[0]._value, 2) self.assertEqual(myGraph._nodeSet[1]._value, 0) self.assertEqual(myGraph._nodeSet[2]._value, 8) self.assertEqual(myGraph._nodeSet[3]._value, 7) self.assertEqual(myGraph._nodeSet[4]._value, 1) self.assertEqual(myGraph._nodeSet[5]._value, 6) def test(graph): # Calculation visibility degree... # ---------------------------------------------------- # self.assertEqual(graph.calcNodeVisibilityDegree(3), 3) self.assertEqual(graph.calcNodeVisibilityDegree(2), 5) self.assertEqual(graph.calcNodeVisibilityDegree(4), 2) self.assertEqual(graph.calcNodeVisibilityDegree(0), 0) self.assertEqual(graph.calcNodeVisibilityDegree(1), 0) self.assertEqual(graph.calcNodeVisibilityDegree(5), 1) self.assertEqual(graph.getMaxVisibilityDegreeNodeID(), 2) def test_timing_1(graph): # Calculation node ID with max visibility degree... # ---------------------------------------------------- # graph.getMaxVisibilityDegreeNodeID() def test_timing_2(graph): # Calculation visibility degree node... # ---------------------------------------------------- # graph.calcNodeVisibilityDegree(3) # Allocate a new 'AdjacencyMatrixGraph' and a 'AdjacencyListGraph' objects... # ---------------------------------------------------- # newAdjacencyMatrixGraph = AdjacencyMatrixGraph() newAdjacencyListGraph = AdjacencyListGraph() # Initialitation... # ---------------------------------------------------- # init(newAdjacencyMatrixGraph) init(newAdjacencyListGraph) # Run test (Polymorphism is powerful :) )... # ---------------------------------------------------- # test(newAdjacencyMatrixGraph) test(newAdjacencyListGraph) print("\nTime to get max Visibility degree node") print("AdjacencyMatrixGraph: %.20f" % min( timeit.Timer( functools.partial(test_timing_1, newAdjacencyMatrixGraph)).repeat(repeat=1, number=1))) print("AdjacencyListGraph: %.20f" % min( timeit.Timer( functools.partial(test_timing_1, newAdjacencyListGraph)).repeat(repeat=1, number=1))) print("Time to get visibility degree of a specified node") print("AdjacencyMatrixGraph: %.20f" % min( timeit.Timer( functools.partial(test_timing_2, newAdjacencyMatrixGraph)).repeat(repeat=1, number=1))) print("AdjacencyListGraph: %.20f" % min( timeit.Timer( functools.partial(test_timing_2, newAdjacencyListGraph)).repeat(repeat=1, number=1)))