Ejemplo n.º 1
0
 def test_add_node(self):
     graph = DiGraph()
     for i in range(10):
         graph.add_node(i)
     self.assertEqual(graph.v_size(), 10)
     graph.add_node(21)
     self.assertEqual(graph.v_size(), 11)
     graph.add_node(-10)
     self.assertEqual(graph.v_size(), 12)
     graph.add_node(7)
     self.assertEqual(graph.v_size(), 12)
Ejemplo n.º 2
0
 def test_remove_node(self):
     graph = DiGraph()
     for i in range(10):
         graph.add_node(i)
     for i in range(9):
         graph.add_edge(i, i + 1, i * 2 + 1)
     self.assertTrue(graph.remove_node(0))
     self.assertEqual(graph.e_size(), 8)
     self.assertEqual(graph.v_size(), 9)
     self.assertFalse(0 in graph.Nodes.keys())
     self.assertTrue(8 in graph.Nodes.keys())
     self.assertFalse(graph.remove_node(0))
     self.assertFalse(graph.remove_node(20))
     self.assertEqual(graph.v_size(), 9)
Ejemplo n.º 3
0
 def test_remove_edge(self):
     graph = DiGraph()
     for i in range(10):
         graph.add_node(i)
     for i in range(9):
         graph.add_edge(i, i + 1, i * 2 + 1)
     graph.remove_edge(2, 3)
     self.assertEqual(graph.v_size(), 10)
     self.assertEqual(graph.e_size(), 8)
     self.assertFalse(graph.all_out_edges_of_node(2).__contains__(3))
     self.assertFalse(graph.all_in_edges_of_node(3).__contains__(2))
     self.assertFalse(graph.remove_edge(6, 0))
     self.assertFalse(graph.remove_edge(5, 100))
Ejemplo n.º 4
0
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(2, 3, 1.1)
    g.add_edge(1, 3, 1.9)
    g.remove_edge(1, 3)
    g.add_edge(1, 3, 10)
    print(g.v_size())  # prints the __repr__ (func output)
    print(g.e_size())  # 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(0, 3))
    g_algo.plot_graph()