Exemple #1
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))
Exemple #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)
Exemple #3
0
 def test_add_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)
     self.assertEqual(graph.e_size(), 9)
     graph.add_edge(9, 0, 5.5)
     self.assertEqual(graph.e_size(), 10)
     graph.add_edge(1, 3, 2.7)
     graph.add_edge(9, 4, 3)
     self.assertEqual(graph.e_size(), 12)
     graph.add_edge(9, 4, 3)
     self.assertEqual(graph.e_size(), 12)
     graph.add_edge(2, 2, 100)
     self.assertEqual(graph.e_size(), 12)
     graph.add_edge(6, 7, -3)
     self.assertEqual(graph.e_size(), 12)
     graph.add_edge(15, 8, 100)
     self.assertEqual(graph.e_size(), 12)
     graph.add_edge(0, 1, 10)
     self.assertEqual(graph.e_size(), 12)
Exemple #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()