def test_can_add_multiple_edges_between_two_vertices(self):
        graph = UndirectedGraph()
        graph.add_edge(0, 1, 1)
        graph.add_edge(0, 1, 2)

        adjacency_list = graph.get_adjacency_list()
        self.assertEqual(2, len(adjacency_list))

        self.assertEqual(2, len(adjacency_list[0]))
        self.assertListEqual([0, 0],
                             sorted(map(lambda e: e.start, adjacency_list[0])))
        self.assertListEqual([1, 1],
                             sorted(map(lambda e: e.end, adjacency_list[0])))
        self.assertListEqual([1, 2],
                             sorted(map(lambda e: e.weight,
                                        adjacency_list[0])))

        self.assertEqual(2, len(adjacency_list[1]))
        self.assertListEqual([1, 1],
                             sorted(map(lambda e: e.start, adjacency_list[1])))
        self.assertListEqual([0, 0],
                             sorted(map(lambda e: e.end, adjacency_list[1])))
        self.assertListEqual([1, 2],
                             sorted(map(lambda e: e.weight,
                                        adjacency_list[1])))

        edges = graph.get_edges()
        self.assertEqual(4, len(edges))
        self.assertListEqual([0, 0, 1, 1], sorted(map(lambda e: e.start,
                                                      edges)))
        self.assertListEqual([0, 0, 1, 1], sorted(map(lambda e: e.end, edges)))
        self.assertListEqual([1, 1, 2, 2],
                             sorted(map(lambda e: e.weight, edges)))
示例#2
0
 def test_returns_minimum_edge_when_has_two_edges_with_different_weight(
         self):
     graph = UndirectedGraph()
     graph.add_edge(0, 1, 1)
     graph.add_edge(0, 1, 2)
     mst = minimum_spanning_tree(graph.get_adjacency_list())
     self.__compare_vertices([0, 1], mst.get_adjacency_list())
     self.__compare_edges([Edge(0, 1, 1), Edge(1, 0, 1)], mst.get_edges())
示例#3
0
 def test_returns_two_edges_for_two_edges_starting_in_same_vertex(self):
     graph = UndirectedGraph()
     graph.add_edge(0, 1, 1)
     graph.add_edge(0, 2, 2)
     mst = minimum_spanning_tree(graph.get_adjacency_list())
     self.__compare_vertices([0, 1, 2], mst.get_adjacency_list())
     self.__compare_edges(
         [Edge(0, 1, 1),
          Edge(1, 0, 1),
          Edge(0, 2, 2),
          Edge(2, 0, 2)], mst.get_edges())
示例#4
0
 def test_returns_mst_for_graph_with_loops(self):
     graph = UndirectedGraph()
     graph.add_edge('A', 'B', 3)
     graph.add_edge('A', 'C', 4)
     graph.add_edge('D', 'B', 6)
     graph.add_edge('E', 'B', 2)
     graph.add_edge('B', 'C', 5)
     graph.add_edge('C', 'E', 7)
     mst = minimum_spanning_tree(graph.get_adjacency_list())
     self.__compare_vertices(['A', 'B', 'C', 'D', 'E'],
                             mst.get_adjacency_list())
     self.__compare_edges([
         Edge('A', 'B', 3),
         Edge('B', 'A', 3),
         Edge('B', 'E', 2),
         Edge('E', 'B', 2),
         Edge('A', 'C', 4),
         Edge('C', 'A', 4),
         Edge('B', 'D', 6),
         Edge('D', 'B', 6)
     ], mst.get_edges())
示例#5
0
 def test_does_not_change_tree(self):
     graph = UndirectedGraph()
     graph.add_edge(0, 1, 1)
     graph.add_edge(0, 6, 1)
     graph.add_edge(2, 3, 2)
     graph.add_edge(2, 4, 3)
     graph.add_edge(4, 5, 4)
     mst = minimum_spanning_tree(graph.get_adjacency_list())
     self.__compare_vertices([0, 1, 2, 3, 4, 5, 6],
                             mst.get_adjacency_list())
     self.__compare_edges([
         Edge(0, 1, 1),
         Edge(1, 0, 1),
         Edge(0, 6, 1),
         Edge(6, 0, 1),
         Edge(2, 3, 2),
         Edge(3, 2, 2),
         Edge(2, 4, 3),
         Edge(4, 2, 3),
         Edge(4, 5, 4),
         Edge(5, 4, 4)
     ], mst.get_edges())
    def test_adding_single_edge_produces_two_directed_edges(self):
        graph = UndirectedGraph()
        graph.add_edge(0, 1, 1)

        adjacency_list = graph.get_adjacency_list()
        self.assertEqual(2, len(adjacency_list))

        self.assertEqual(1, len(adjacency_list[0]))
        self.assertEqual(0, adjacency_list[0][0].start)
        self.assertEqual(1, adjacency_list[0][0].end)
        self.assertEqual(1, adjacency_list[0][0].weight)

        self.assertEqual(1, len(adjacency_list[1]))
        self.assertEqual(1, adjacency_list[1][0].start)
        self.assertEqual(0, adjacency_list[1][0].end)
        self.assertEqual(1, adjacency_list[1][0].weight)

        edges = graph.get_edges()
        self.assertEqual(2, len(edges))
        self.assertListEqual([0, 1], sorted(map(lambda e: e.start, edges)))
        self.assertListEqual([0, 1], sorted(map(lambda e: e.end, edges)))
        self.assertListEqual([1, 1], sorted(map(lambda e: e.weight, edges)))
示例#7
0
 def test_returns_single_edge_for_single_edge_in_graph(self):
     graph = UndirectedGraph()
     graph.add_edge(0, 1, 1)
     mst = minimum_spanning_tree(graph.get_adjacency_list())
     self.__compare_vertices([0, 1], mst.get_adjacency_list())
     self.__compare_edges([Edge(0, 1, 1), Edge(1, 0, 1)], mst.get_edges())
 def test_graph_is_empty_by_default(self):
     graph = UndirectedGraph()
     self.assertEqual(0, len(graph.get_adjacency_list()))
     self.assertEqual(0, len(graph.get_edges()))