Ejemplo n.º 1
0
class TestMSTPrim(unittest.TestCase):
    def tearDown(self):
        self.mst_prim = None
        self.graph = None

    def setUp(self):
        self.graph = Graph()

        self.option = 1
        if self.option == 1:
            edges = [(0, 1, 2), (0, 4, 4), (1, 2, 3), (2, 4, 1), (2, 3, 5),
                     (3, 0, 8), (4, 3, 7)]
        elif self.option == 2:
            edges = []

        for edge in edges:
            self.graph.add_edge(edge[0], edge[1], weight=edge[2])

        self.mst_prim = MSTPrim(self.graph)

    def test_mst_prim(self):
        if self.option == 1:
            self.mst_prim.mst_prim(0)
            self.assertEqual(self.mst_prim.pred[0], -1)
            self.assertEqual(self.mst_prim.pred[1], 0)
            self.assertEqual(self.mst_prim.pred[2], 1)
            self.assertEqual(self.mst_prim.pred[3], 2)
            self.assertEqual(self.mst_prim.pred[4], 2)

        elif self.option == 2:
            pass
Ejemplo n.º 2
0
class TestBellmanFord(unittest.TestCase):
    
    def tearDown(self):
        self.graph = None

        self.bellman_ford = None

    def setUp(self):
        self.graph = Graph()
        self.option = 2

        if self.option == 1:
            edges = [(0, 4, 2),
                     (1, 3, -2),
                     (2, 1, -3),
                     (3, 2, 6),
                     (4, 3, 4), (4, 1, 5)]
        elif self.option == 2:
            edges = [(0, 1, 2),
                     (1, 3, 4), (1, 4, 5),
                     (2, 4, -3),
                     (3, 2, 6), # test negative cycle (3, 2, 4)
                     (4, 3, -2), ]

        for edge in edges:
            self.graph.add_edge(edge[0], edge[1], weight=edge[2])

        self.bellman_ford = BellmanFord(self.graph)

    def test_bellman_ford(self):
        self.bellman_ford.bellman_ford(0)
        if self.option == 1:
            self.assertEqual(self.bellman_ford.dist[0], 0)
            self.assertEqual(self.bellman_ford.dist[1], 7)
            self.assertEqual(self.bellman_ford.dist[2], 11)
            self.assertEqual(self.bellman_ford.dist[3], 5)
            self.assertEqual(self.bellman_ford.dist[4], 2)
            self.assertEqual(self.bellman_ford.full_path(0), [0])
            self.assertEqual(self.bellman_ford.full_path(1), [0, 4 ,1])
            self.assertEqual(self.bellman_ford.full_path(2), [0, 4, 1, 3, 2])
            self.assertEqual(self.bellman_ford.full_path(3), [0, 4, 1, 3])
            self.assertEqual(self.bellman_ford.full_path(4), [0, 4])
        if self.option == 2:
            self.assertEqual(self.bellman_ford.dist[0], 0)
            self.assertEqual(self.bellman_ford.dist[1], 2)
            self.assertEqual(self.bellman_ford.dist[2], 11)
            self.assertEqual(self.bellman_ford.dist[3], 5)
            self.assertEqual(self.bellman_ford.dist[4], 7)
            self.assertEqual(self.bellman_ford.full_path(0), [0])
            self.assertEqual(self.bellman_ford.full_path(1), [0, 1])
            self.assertEqual(self.bellman_ford.full_path(2), [0, 1, 4, 3, 2])
            self.assertEqual(self.bellman_ford.full_path(3), [0, 1, 4, 3])
            self.assertEqual(self.bellman_ford.full_path(4), [0, 1, 4])
Ejemplo n.º 3
0
def complete_multi_graph():

    count_num = int(input())
    count = 0
    while count < count_num:
        g = Graph()
        node_num, adjs_num = map(int, input().split(' '))
        for i in range(adjs_num):
            adj = list(map(int, input().split(' ')))
            g.add_edge(adj[0], adj[1])

    return is_graph(g)
Ejemplo n.º 4
0
    def test_graph(self):
        graph = Graph()
        for key in range(0, 6):
            graph.add_node(key)
        graph.add_edge(0, 1, weight=5)
        graph.add_edge(0, 5, weight=2)
        graph.add_edge(1, 2, weight=3)
        graph.add_edge(2, 3, weight=4)
        graph.add_edge(3, 4, weight=5)
        graph.add_edge(3, 5, weight=6)
        graph.add_edge(4, 0, weight=7)
        graph.add_edge(5, 4, weight=8)
        graph.add_edge(5, 2, weight=9)

        self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)
class TestFloydWarshall(unittest.TestCase):
    def tearDown(self):
        self.floyd_warshall = None
        self.graph = None

    def setUp(self):
        self.graph = Graph()

        self.option = 1
        if self.option == 1:
            edges = [(0, 1, 2), (0, 4, 4), (1, 2, 3), (2, 4, 1), (2, 3, 5),
                     (3, 0, 8), (4, 3, 7)]
        elif self.option == 2:
            edges = []

        for edge in edges:
            self.graph.add_edge(edge[0], edge[1], weight=edge[2])

        self.floyd_warshall = FloydWarshall(self.graph)

    def test_dijkstra_dense(self):
        if self.option == 1:
            self.floyd_warshall.floyd_warshall()
            print(self.floyd_warshall.dist)
            print(self.floyd_warshall.pred)
            self.assertEqual(self.floyd_warshall.dist[0][1], 2)
            self.assertEqual(self.floyd_warshall.dist[0][2], 5)
            self.assertEqual(self.floyd_warshall.dist[0][3], 10)
            self.assertEqual(self.floyd_warshall.dist[0][4], 4)
            self.assertEqual(self.floyd_warshall.dist[4][1], 17)
            self.assertEqual(self.floyd_warshall.construct_shortest_path(0, 1),
                             [0, 1])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(0, 2),
                             [0, 1, 2])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(0, 3),
                             [0, 1, 2, 3])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(0, 4),
                             [0, 4])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(1, 2),
                             [1, 2])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(1, 3),
                             [1, 2, 3])
            self.assertEqual(self.floyd_warshall.construct_shortest_path(4, 1),
                             [4, 3, 0, 1])

        elif self.option == 2:
            pass
Ejemplo n.º 6
0
class TestDijkstra(unittest.TestCase):
    def tearDown(self):
        self.dijkstra = None
        self.graph = None

    def setUp(self):
        self.graph = Graph()

        self.option = 2
        if self.option == 1:
            edges = [(0, 4, 4), (0, 1, 2), (4, 3, 7), (1, 2, 3), (2, 4, 1),
                     (2, 3, 5), (3, 0, 8)]
        elif self.option == 2:
            edges = [(1, 2, 7), (1, 3, 9), (1, 6, 14), (2, 3, 10), (2, 4, 15),
                     (3, 6, 2), (3, 4, 11), (4, 5, 6), (6, 5, 9)]

        for edge in edges:
            self.graph.add_edge(edge[0], edge[1], weight=edge[2])

        self.dijkstra = Dijkstra(self.graph)

    def test_dijkstra(self):
        if self.option == 1:
            self.dijkstra.dijkstra(0)
            self.assertEqual(self.dijkstra.dist[3], 10)
            self.assertEqual(self.dijkstra.dist[4], 4)
            self.assertEqual(self.dijkstra.dist[0], 0)
            self.assertEqual(self.dijkstra.dist[1], 2)
            self.assertEqual(self.dijkstra.dist[2], 5)
            self.assertEqual(self.dijkstra.full_path(2), [0, 1, 2])
        elif self.option == 2:
            self.dijkstra.dijkstra(1)
            self.assertEqual(self.dijkstra.dist[1], 0)
            self.assertEqual(self.dijkstra.dist[2], 7)
            self.assertEqual(self.dijkstra.dist[3], 9)
            self.assertEqual(self.dijkstra.dist[4], 20)
            self.assertEqual(self.dijkstra.dist[5], 20)
            self.assertEqual(self.dijkstra.dist[6], 11)
            self.assertEqual(self.dijkstra.full_path(1), [1])
            self.assertEqual(self.dijkstra.full_path(2), [1, 2])
            self.assertEqual(self.dijkstra.full_path(3), [1, 3])
            self.assertEqual(self.dijkstra.full_path(4), [1, 3, 4])
            self.assertEqual(self.dijkstra.full_path(5), [1, 3, 6, 5])
            self.assertEqual(self.dijkstra.full_path(6), [1, 3, 6])