Пример #1
0
 def test_center_two2(self):
     T = Graph(2)
     T.add_edge(Edge(0, 1))
     algorithm = TreeCenter(T)
     algorithm.run()
     self.assertEqual(algorithm.tree_center, [0, 1])
     self.assertEqual(algorithm.tree_radius, 1)
Пример #2
0
class TestTransitiveClosure(unittest.TestCase):
    def setUp(self):
        self.N = 4  # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()
        self.expected_T = {
            0: {
                0: True,
                1: True,
                2: True,
                3: True
            },
            1: {
                0: False,
                1: True,
                2: True,
                3: True
            },
            2: {
                0: False,
                1: False,
                2: True,
                3: True
            },
            3: {
                0: False,
                1: False,
                2: False,
                3: True
            }
        }

    def test_closure(self):
        algorithm = TransitiveClosure(self.G)
        algorithm.run()
        self.assertEqual(algorithm.T, self.expected_T)

    def test_closure_simple(self):
        algorithm = TransitiveClosureSimple(self.G)
        algorithm.run()
        self.assertEqual(algorithm.T, self.expected_T)

    def test_closure_bfs(self):
        algorithm = TransitiveClosureBFS(self.G)
        algorithm.run()
        self.assertEqual(algorithm.T, self.expected_T)

    def test_closure_dfs(self):
        algorithm = TransitiveClosureDFS(self.G)
        algorithm.run()
        self.assertEqual(algorithm.T, self.expected_T)

    def tearDown(self):
        pass
Пример #3
0
class TestStronglyConnectedComponents(unittest.TestCase):

    def setUp(self):
        self.N = 8           # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1), Edge(1, 4), Edge(4, 0), Edge(4, 5), Edge(1, 5), 
            Edge(1, 2), Edge(5, 6), Edge(6, 5), Edge(2, 6), Edge(2, 3), 
            Edge(3, 2), Edge(6, 7), Edge(3, 7)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()
        self.expected_scc = {0: 0, 1: 0, 2: 1, 3: 1, 4: 0, 5: 2, 6: 2, 7: 3}
        self.expected_n_scc = 4

    def test_scc(self):
        algorithm = StronglyConnectedComponents(self.G)
        algorithm.run()
        self.assertEqual(algorithm.n_scc, self.expected_n_scc)
        self.assertEqual(algorithm.scc, self.expected_scc)
        self.assertRaises(
            ValueError, StronglyConnectedComponents, Graph(1, False))

    def tearDown(self): pass
Пример #4
0
 def test_center_two2(self):
     T = Graph(2)
     T.add_edge(Edge(0, 1))
     algorithm = TreeCenter(T)
     algorithm.run()
     self.assertEqual(algorithm.tree_center, [0, 1])
     self.assertEqual(algorithm.tree_radius, 1)
Пример #5
0
class TestMatchingWithWeights(unittest.TestCase):

    def setUp(self):
        # Graf pelny z kolejnymi wagami.
        self.N = 4
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [Edge(0, 1, 5), Edge(0, 2, 7), Edge(0, 3, 2),
            Edge(1, 2, 3), Edge(1, 3, 6), Edge(2, 3, 4)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_min_weight_matching(self):
        algorithm = MinimumWeightMatchingWithEdges(self.G)
        algorithm.run()
        expected_cardinality = 2
        expected_weight = 5
        expected_mate = {0: Edge(0, 3, 2), 1: Edge(1, 2, 3),
            2: Edge(2, 1, 3), 3: Edge(3, 0, 2)}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        # Krawedzie sa po dwa razy w slowniku.
        weight = sum(algorithm.mate[node].weight
            for node in algorithm.mate if algorithm.mate[node]) / 2
        self.assertEqual(weight, expected_weight)
        self.assertEqual(algorithm.mate, expected_mate)
        # Is it matching?
        for source in algorithm.mate:
            if algorithm.mate[source] is not None:
                edge = algorithm.mate[source]
                self.assertEqual(algorithm.mate[edge.target], ~edge)

    def tearDown(self): pass
Пример #6
0
class TestMatching3(unittest.TestCase):
    def setUp(self):
        self.N = 6
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1),
            Edge(1, 2),
            Edge(2, 3),
            Edge(3, 4),
            Edge(4, 5)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_matching(self):
        algorithm = BorieMatching(self.G)
        algorithm.run()
        expected1 = set([Edge(0, 1), Edge(2, 3), Edge(4, 5)])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.mate_set, expected1)
        # Testing matching.
        S = set()
        for edge in algorithm.mate_set:
            S.add(edge.source)
            S.add(edge.target)
        self.assertEqual(len(S), 2 * len(algorithm.mate_set))

    def tearDown(self):
        pass
Пример #7
0
 def test_exceptions(self):
     self.assertRaises(ValueError, MatchingFordFulkersonSet,
                       Graph(2, directed=True))
     self.assertRaises(ValueError, MatchingFordFulkersonList,
                       Graph(2, directed=True))
     self.assertRaises(ValueError, MatchingFordFulkersonColor,
                       Graph(2, directed=True))
Пример #8
0
 def setUp(self):
     # The modified graph (unique weights) from Cormen.
     self.N = 9  # number of nodes
     self.G = Graph(self.N)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1, 4),
         Edge(0, 7, 8),
         Edge(1, 7, 11),
         Edge(1, 2, 12),
         Edge(7, 8, 7),
         Edge(8, 2, 2),
         Edge(8, 6, 6),
         Edge(7, 6, 1),
         Edge(2, 3, 13),
         Edge(2, 5, 5),
         Edge(6, 5, 3),
         Edge(3, 5, 14),
         Edge(3, 4, 9),
         Edge(5, 4, 10)
     ]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #9
0
class TestFordFulkerson(unittest.TestCase):
    def setUp(self):
        self.N = 4  # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [Edge(0, 1, 10), Edge(0, 2, 10), Edge(1, 2, 1), Edge(1, 3, 10), Edge(2, 3, 10)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        # self.G.show()

    def test_fordfulkerson(self):
        algorithm = FordFulkerson(self.G)
        algorithm.run(0, 3)
        expected_max_flow = 20
        expected_flow = {
            0: {0: 0, 2: 10, 1: 10, 3: 0},
            1: {0: -10, 2: 0, 1: 0, 3: 10},
            2: {0: -10, 2: 0, 1: 0, 3: 10},
            3: {0: 0, 2: -10, 1: -10, 3: 0},
        }
        self.assertEqual(algorithm.max_flow, expected_max_flow)
        self.assertEqual(algorithm.flow, expected_flow)

    def test_fordfulkerson_sparse(self):
        algorithm = FordFulkersonSparse(self.G)
        algorithm.run(0, 3)
        expected_max_flow = 20
        expected_flow = {0: {2: 10, 1: 10}, 1: {0: -10, 3: 10}, 2: {0: -10, 3: 10}, 3: {2: -10, 1: -10}}
        self.assertEqual(algorithm.max_flow, expected_max_flow)
        self.assertEqual(algorithm.flow, expected_flow)
Пример #10
0
class TestJohnsonWiki(unittest.TestCase):

    def setUp(self):
        self.N = 4           # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 3), Edge(0, 3, 6), Edge(1, 3, 4), Edge(1, 2, 5), 
            Edge(3, 2, 2), Edge(2, 0, -7), Edge(2, 1, -3)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_johnson(self):
        algorithm = Johnson(self.G)
        algorithm.run()
        expected_distance = {
            0: {1: 3, 0: 0, 2: 8, 3: 6}, 
            1: {1: 0, 0: -2, 2: 5, 3: 4}, 
            2: {1: -4, 0: -7, 2: 0, 3: -1}, 
            3: {1: -2, 0: -5, 2: 2, 3: 0}}
        self.assertEqual(algorithm.distance, expected_distance)

    def test_johnson_faster(self):
        algorithm = JohnsonFaster(self.G)
        algorithm.run()
        expected_distance = {
            0: {1: 3, 0: 0, 2: 8, 3: 6}, 
            1: {1: 0, 0: -2, 2: 5, 3: 4}, 
            2: {1: -4, 0: -7, 2: 0, 3: -1}, 
            3: {1: -2, 0: -5, 2: 2, 3: 0}}
        self.assertEqual(algorithm.distance, expected_distance)
Пример #11
0
class TestBellmanFordCormen(unittest.TestCase):

    def setUp(self):
        # The graph from Cormen p.666, negative weights.
        self.N = 5           # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 6), Edge(0, 3, 7), Edge(1, 3, 8), Edge(1, 2, 5), 
            Edge(1, 4, -4), Edge(2, 1, -2), Edge(3, 2, -3), Edge(3, 4, 9),
            Edge(4, 0, 2), Edge(4, 2, 7)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_shortest_path_cormen(self):
        source = 0
        target = 4
        algorithm = BellmanFord(self.G)
        algorithm.run(source)
        distance_expected = {3: 7, 2: 4, 0: 0, 4: -2, 1: 2}
        self.assertEqual(algorithm.distance, distance_expected)
        parent_expected = {3: 0, 2: 3, 0: None, 4: 1, 1: 2}
        self.assertEqual(algorithm.parent, parent_expected)
        path_expected = [0, 3, 2, 1, 4]
        self.assertEqual(algorithm.path(target), path_expected)

    def tearDown(self): pass
Пример #12
0
class TestMatching3(unittest.TestCase):

    def setUp(self):
        self.N = 6
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3),
            Edge(3, 4), Edge(4, 5)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_matching(self):
        algorithm = BorieMatching(self.G)
        algorithm.run()
        expected1 = set([Edge(0, 1), Edge(2, 3), Edge(4, 5)])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.mate_set, expected1)
        # Testing matching.
        S = set()
        for edge in algorithm.mate_set:
            S.add(edge.source)
            S.add(edge.target)
        self.assertEqual(len(S), 2 * len(algorithm.mate_set))

    def tearDown(self): pass
Пример #13
0
class TestBoruvkaDisconnectedGraph(unittest.TestCase):

    def setUp(self):
        # The modified graph (unique weights) from Cormen.
        self.N = 9           # number of nodes
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 4), Edge(0, 7, 8), Edge(1, 7, 11), Edge(1, 2, 12), 
            Edge(7, 8, 7), Edge(8, 2, 2), Edge(8, 6, 6), Edge(7, 6, 1),
            Edge(3, 5, 14), Edge(3, 4, 9), Edge(5, 4, 10)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #print self.G

    def test_boruvka(self):
        self.assertEqual(self.G.v(), self.N)
        algorithm = BoruvkaMST(self.G)
        algorithm.run()
        self.assertEqual(algorithm.mst.v(), self.N)
        self.assertEqual(algorithm.mst.e(), self.N-2) # 2 components
        mst_weight_expected = 40
        mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
        self.assertEqual(mst_weight, mst_weight_expected)
        mst_edges_expected = [
            Edge(0, 1, 4), Edge(0, 7, 8), Edge(8, 2, 2), Edge(7, 6, 1), 
            Edge(6, 8, 6), Edge(3, 4, 9), Edge(5, 4, 10)]
        for edge in mst_edges_expected:
            self.assertTrue(algorithm.mst.has_edge(edge))

    def tearDown(self): pass
Пример #14
0
 def test_exceptions(self):
     self.assertRaises(ValueError, UnorderedSequentialIndependentSet1,
                       Graph(5, directed=True))
     self.assertRaises(ValueError, UnorderedSequentialIndependentSet2,
                       Graph(5, directed=True))
     self.assertRaises(ValueError, UnorderedSequentialIndependentSet3,
                       Graph(5, directed=True))
Пример #15
0
class TestEdmondsKarpWiki(unittest.TestCase):

    def setUp(self):
        self.N = 7           # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 3), Edge(0, 3, 3), Edge(1, 2, 4), Edge(2, 0, 3), 
            Edge(2, 3, 1), Edge(2, 4, 2), Edge(3, 4, 2), Edge(3, 5, 6), 
            Edge(4, 1, 1), Edge(4, 6, 1), Edge(5, 6, 9)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_wiki_sparse(self):
        algorithm = EdmondsKarpSparse(self.G)
        algorithm.run(0, 6)
        expected_max_flow = 5
        expected_flow = {
            0: {1: 2, 3: 3}, 
            1: {0: -2, 2: 2}, 
            2: {1: -2, 4: 1, 3: 1}, 
            3: {0: -3, 2: -1, 4: 0, 5: 4}, 
            4: {2: -1, 3: 0, 6: 1}, 
            5: {3: -4, 6: 4}, 
            6: {4: -1, 5: -4}}
        self.assertEqual(algorithm.max_flow, expected_max_flow)
        self.assertEqual(algorithm.flow, expected_flow)

    def tearDown(self): pass
Пример #16
0
 def setUp(self):
     self.N = 5
     self.G = Graph(self.N)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1),
         Edge(0, 2),
         Edge(1, 2),
         Edge(1, 3),
         Edge(1, 4)
     ]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
     #self.G.show()
     node1 = Node(0, 1, "edge")
     node2 = Node(0, 2, "edge")
     node3 = Node(2, 1, "edge")
     node4 = Node(1, 3, "edge")
     node5 = Node(1, 4, "edge")
     node6 = Node(0, 1, "series", node2, node3)
     node7 = Node(0, 1, "parallel", node1, node6)
     node8 = Node(0, 1, "jackknife", node7, node4)
     node9 = Node(0, 1, "jackknife", node8, node5)
     self.root = node9
Пример #17
0
 def setUp(self):
     self.N = 8  # number of nodes
     self.G = Graph(self.N, directed=True)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1),
         Edge(1, 4),
         Edge(4, 0),
         Edge(4, 5),
         Edge(1, 5),
         Edge(1, 2),
         Edge(5, 6),
         Edge(6, 5),
         Edge(2, 6),
         Edge(2, 3),
         Edge(3, 2),
         Edge(6, 7),
         Edge(3, 7)
     ]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
     #self.G.show()
     self.expected_scc = {0: 0, 1: 0, 2: 1, 3: 1, 4: 0, 5: 2, 6: 2, 7: 3}
     self.expected_n_scc = 4
Пример #18
0
class TestBoruvkaWiki(unittest.TestCase):

    def setUp(self):
        # The graph (unique weights) from
        # http://en.wikipedia.org/wiki/Boruvka's_algorithm
        self.N = 7           # number of nodes
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 7), Edge(1, 2, 11), Edge(0, 3, 4), Edge(3, 1, 9), 
            Edge(4, 1, 10), Edge(2, 4, 5), Edge(3, 4, 15), Edge(3, 5, 6), 
            Edge(5, 4, 12), Edge(5, 6, 13), Edge(4, 6, 8)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #print self.G

    def test_boruvka(self):
        self.assertEqual(self.G.v(), self.N)
        algorithm = BoruvkaMST(self.G)
        algorithm.run()
        self.assertEqual(algorithm.mst.v(), self.N)
        self.assertEqual(algorithm.mst.e(), self.N-1)
        mst_weight_expected = 40
        mst_weight = sum(edge.weight for edge in algorithm.mst.iteredges())
        self.assertEqual(mst_weight, mst_weight_expected)
        mst_edges_expected = [
            Edge(0, 1, 7), Edge(0, 3, 4), Edge(2, 4, 5), Edge(1, 4, 10), 
            Edge(4, 6, 8), Edge(3, 5, 6)]
        for edge in mst_edges_expected:
            self.assertTrue(algorithm.mst.has_edge(edge))

    def tearDown(self): pass
Пример #19
0
class TestBellmanFord(unittest.TestCase):

    def setUp(self):
        self.N = 4           # number of nodes
        self.G = Graph(self.N, directed=True) # directed graph
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 1), Edge(0, 2, 5), Edge(1, 2, 1), Edge(1, 3, 3), 
            Edge(2, 3, 1)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_shortest_path(self):
        source = 0
        target = 3
        algorithm = BellmanFord(self.G)
        algorithm.run(source)
        distance_expected = {0: 0, 2: 2, 1: 1, 3: 3}
        self.assertEqual(algorithm.distance, distance_expected)
        parent_expected = {0: None, 2: 1, 1: 0, 3: 2}
        self.assertEqual(algorithm.parent, parent_expected)
        path_expected = [0, 1, 2, 3]
        self.assertEqual(algorithm.path(target), path_expected)

    def tearDown(self): pass
Пример #20
0
class TestHamiltonianCycleDirected(unittest.TestCase):
    def setUp(self):
        # 3-prism graph, Halin graph
        self.N = 6  # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1),
            Edge(2, 0),
            Edge(0, 5),
            Edge(2, 1),
            Edge(1, 4),
            Edge(3, 2),
            Edge(3, 4),
            Edge(5, 3),
            Edge(4, 5)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_hamilton(self):
        algorithm = HamiltonianCycleDFS(self.G)
        algorithm.run(0)
        expected_cycle = [0, 1, 4, 5, 3, 2, 0]
        self.assertEqual(algorithm.hamiltonian_cycle, expected_cycle)

    def test_hamilton_with_edges(self):
        algorithm = HamiltonianCycleDFSWithEdges(self.G)
        algorithm.run(0)
        expected_cycle = [
            Edge(0, 1),
            Edge(1, 4),
            Edge(4, 5),
            Edge(5, 3),
            Edge(3, 2),
            Edge(2, 0)
        ]
        self.assertEqual(algorithm.hamiltonian_cycle, expected_cycle)

    def test_hamilton_with_cycle_graph(self):
        algorithm = HamiltonianCycleDFSWithGraph(self.G)
        algorithm.run(0)
        # 5 solutions
        expected_cycle = [
            Edge(0, 1),
            Edge(1, 4),
            Edge(4, 5),
            Edge(5, 3),
            Edge(3, 2),
            Edge(2, 0)
        ]
        #print "directed", list(algorithm.hamiltonian_cycle.iteredges())
        for edge in expected_cycle:
            self.assertTrue(algorithm.hamiltonian_cycle.has_edge(edge))

    def tearDown(self):
        pass
Пример #21
0
 def test_tree_three_nodes(self):
     T = Graph(3)
     for node in (0, 1, 2):
         T.add_node(node)
     for edge in (Edge(0, 1), Edge(1, 2)):
         T.add_edge(edge)
     algorithm = TreePlot(T)
     algorithm.run()
     self.assertEqual(len(algorithm.point_dict), 3)
Пример #22
0
class TestDominatingSet2(unittest.TestCase):
    def setUp(self):
        self.N = 7
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1),
            Edge(1, 3),
            Edge(1, 2),
            Edge(1, 4),
            Edge(2, 5),
            Edge(2, 6)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_borie_dset(self):
        algorithm = BorieDominatingSet(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def test_tree_dset1(self):
        algorithm = TreeDominatingSet1(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def test_tree_dset2(self):
        algorithm = TreeDominatingSet2(self.G)
        algorithm.run()
        expected1 = set([1, 2])
        self.assertEqual(algorithm.cardinality, len(expected1))
        self.assertEqual(algorithm.dominating_set, expected1)
        # Testing dset.
        neighbors = set(algorithm.dominating_set)
        for node in algorithm.dominating_set:
            neighbors.update(self.G.iteradjacent(node))
        self.assertEqual(len(neighbors), self.G.v())

    def tearDown(self):
        pass
Пример #23
0
 def test_exceptions(self):
     self.G.add_edge(Edge(0, 4))
     algorithm = BipartiteGraphBFS(self.G)
     self.assertRaises(ValueError, algorithm.run)
     algorithm = BipartiteGraphDFS(self.G)
     self.assertRaises(ValueError, algorithm.run)
     self.assertRaises(ValueError, BipartiteGraphBFS, Graph(2,
                                                            directed=True))
     self.assertRaises(ValueError, BipartiteGraphDFS, Graph(2,
                                                            directed=True))
Пример #24
0
 def setUp(self):
     self.N = 7
     self.G = Graph(self.N)
     self.nodes = range(self.N)
     self.edges = [Edge(0, 1), Edge(1, 3), Edge(1, 2), 
         Edge(1, 4), Edge(2, 5), Edge(2, 6)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #25
0
 def test_halin8c(self):
     N = 8
     G = Graph(N, False)
     edges = [
         Edge(0, 1),
         Edge(0, 5),
         Edge(0, 7),
         Edge(1, 2),
         Edge(1, 7),
         Edge(2, 3),
         Edge(2, 6),
         Edge(3, 4),
         Edge(3, 6),
         Edge(4, 5),
         Edge(4, 6),
         Edge(5, 6),
         Edge(6, 7)
     ]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinNodeColoring(G, outer=set([0, 1, 2, 3, 4, 5]))
     algorithm.run()
     #print "halin8c outer", algorithm.outer
     parent = {0: 7, 1: 7, 2: 6, 3: 6, 4: 6, 5: 6, 6: None, 7: 6}
     self.assertEqual(algorithm.parent, parent)
     for node in G.iternodes():
         self.assertNotEqual(algorithm.color[node], None)
     for edge in G.iteredges():
         self.assertNotEqual(algorithm.color[edge.source],
                             algorithm.color[edge.target])
     all_colors = set(algorithm.color[node] for node in G.iternodes())
     self.assertEqual(len(all_colors), 3)
Пример #26
0
 def test_3prism(self):
     N = 6
     G = Graph(N, False)
     edges = [
         Edge(0, 1),
         Edge(1, 2),
         Edge(2, 3),
         Edge(3, 4),
         Edge(4, 5),
         Edge(0, 5),
         Edge(1, 4),
         Edge(2, 0),
         Edge(3, 5)
     ]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     #print "3prism"
     algorithm = HalinNodeColoring(G, outer=set([0, 2, 3, 5]))
     #algorithm = HalinNodeColoring(G, outer=set([0, 1, 4, 5]))
     #algorithm = HalinNodeColoring(G, outer=set([1, 2, 3, 4]))
     algorithm.run()
     #print "3prism outer", algorithm.outer
     parent = {0: 1, 1: None, 2: 1, 3: 4, 4: 1, 5: 4}
     self.assertEqual(algorithm.parent, parent)
     for node in G.iternodes():
         self.assertNotEqual(algorithm.color[node], None)
     for edge in G.iteredges():
         self.assertNotEqual(algorithm.color[edge.source],
                             algorithm.color[edge.target])
     all_colors = set(algorithm.color[node] for node in G.iternodes())
     self.assertEqual(len(all_colors), 3)
Пример #27
0
    def test_dijkstra_for_path_not_found(self):
        print("Testing Matrix 2nd time")
        self.N = 8  # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 65),
            Edge(1, 8, 41),
            Edge(1, 2, 35),
            Edge(2, 3, 56),
            Edge(3, 4, 4),
            Edge(3, 6, 20),
            Edge(5, 2, 30),
            Edge(6, 5, 18),
            Edge(6, 7, 15),
            Edge(8, 3, 28)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        # self.G.show()

        algorithm = Dijkstra(self.G)
        source = 0
        algorithm.run(source)

        target = 7
        path_expected = [0, 1, 8, 3, 6, 7]
        distance_expected = 169
        self.assertEqual(path_expected, algorithm.path(target))
        self.assertEqual(distance_expected, algorithm.distance[target])

        algorithm2 = DijkstraMatrix(self.G)
        algorithm2.run(source)
        self.assertEqual(path_expected, algorithm.path(target))
        self.assertEqual(distance_expected, algorithm.distance[target])

        source = 2
        target = 8
        algorithm.run(source)
        try:
            algorithm.path(target)
        except:
            pass
        else:
            self.fail("Path exception was not raised!")

        algorithm2.run(source)
        try:
            algorithm2.path(target)
        except:
            pass
        else:
            self.fail("Path exception was not raised!")
Пример #28
0
 def test_wheel5(self):
     N = 5
     G = Graph(N, False)
     edges = [
         Edge(0, 1),
         Edge(0, 2),
         Edge(0, 3),
         Edge(0, 4),
         Edge(1, 2),
         Edge(2, 3),
         Edge(3, 4),
         Edge(4, 1)
     ]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinNodeColoring(G, outer=set([1, 2, 3, 4]))
     algorithm.run()
     #print "wheel5 outer", algorithm.outer
     #print "wheel5 cycle", algorithm.cycle
     parent = {0: None, 1: 0, 2: 0, 3: 0, 4: 0}
     self.assertEqual(algorithm.parent, parent)
     for node in G.iternodes():
         self.assertNotEqual(algorithm.color[node], None)
     for edge in G.iteredges():
         self.assertNotEqual(algorithm.color[edge.source],
                             algorithm.color[edge.target])
     all_colors = set(algorithm.color[node] for node in G.iternodes())
     self.assertEqual(len(all_colors), 3)
Пример #29
0
 def setUp(self):
     self.N = 4           # number of nodes
     self.G = Graph(self.N, directed=True)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1, 3), Edge(0, 2, 6), Edge(1, 2, 4), Edge(1, 3, 5), 
         Edge(2, 3, 2), Edge(3, 0, -5), Edge(3, 1, -3)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #30
0
 def setUp(self):
     self.N = 6           # number of nodes
     self.G = Graph(self.N, directed=True)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1), Edge(3, 0), Edge(1, 4), Edge(4, 3), Edge(2, 4), 
         Edge(4, 5), Edge(5, 2)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #31
0
 def setUp(self):
     self.N = 4           # number of nodes
     self.G = Graph(self.N, directed=True) # directed graph
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1, 1), Edge(0, 2, 5), Edge(1, 2, 1), Edge(1, 3, 3), 
         Edge(2, 3, 1)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #32
0
 def setUp(self):
     self.N = 5           # number of nodes
     self.G = Graph(self.N, directed=False)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 2, 6), Edge(0, 3, 3), Edge(1, 0, 3), Edge(2, 3, 2), 
         Edge(3, 1, 1), Edge(4, 1, 4), Edge(4, 3, 2)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #33
0
 def setUp(self):
     self.N = 7           # number of nodes
     self.G = Graph(self.N, directed=False)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1), Edge(0, 4), Edge(1, 4), Edge(2, 3), Edge(2, 5), 
         Edge(3, 5), Edge(4, 5), Edge(4, 6), Edge(5, 6)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #34
0
class TestTSP(unittest.TestCase):
    def setUp(self):
        self.N = 4
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 20),
            Edge(0, 3, 35),
            Edge(0, 2, 42),
            Edge(1, 2, 30),
            Edge(1, 3, 34),
            Edge(2, 3, 12)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        self.best_weight = 97
        #self.G.show()

    def test_nearest_neighbor_with_edges(self):
        algorithm = NearestNeighborTSPWithEdges(self.G)
        algorithm.run(0)
        expected_hamiltonian_cycle = [
            Edge(0, 1, 20),
            Edge(1, 2, 30),
            Edge(2, 3, 12),
            Edge(3, 0, 35)
        ]
        self.assertEqual(algorithm.hamiltonian_cycle,
                         expected_hamiltonian_cycle)
        weight = sum(edge.weight for edge in algorithm.hamiltonian_cycle)
        self.assertEqual(weight, self.best_weight)

    def test_nearest_neighbor_with_cycle_graph(self):
        algorithm = NearestNeighborTSPWithGraph(self.G)
        algorithm.run(0)
        weight = sum(edge.weight
                     for edge in algorithm.hamiltonian_cycle.iteredges())
        self.assertEqual(weight, self.best_weight)
        #algorithm.hamiltonian_cycle.show()
        self.assertEqual(algorithm.hamiltonian_cycle.e(),
                         algorithm.hamiltonian_cycle.v())
        for node in algorithm.hamiltonian_cycle.iternodes():
            self.assertEqual(algorithm.hamiltonian_cycle.degree(node), 2)

    def test_exceptions(self):
        self.assertRaises(ValueError, NearestNeighborTSPWithEdges,
                          Graph(5, True))
        self.assertRaises(ValueError, NearestNeighborTSPWithGraph,
                          Graph(5, True))

    def tearDown(self):
        pass
Пример #35
0
 def setUp(self):
     #  Knuth s.273 t.1
     self.N = 10           # number of nodes
     self.G = Graph(self.N, directed=True)
     self.nodes = range(self.N)
     self.edges = [
         Edge(9, 2), Edge(3, 7), Edge(7, 5), Edge(5, 8), Edge(8, 6), 
         Edge(4, 6), Edge(1, 3), Edge(7, 4), Edge(9, 5), Edge(2, 8)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #36
0
 def setUp(self):
     # Wilson, ex. 25.1, bipartite graph
     self.N = 7
     self.G = Graph(self.N, directed=False)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 4), Edge(0, 5), Edge(0, 6), Edge(1, 3), Edge(1, 5), 
         Edge(2, 3), Edge(2, 4)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #37
0
class TestHopcroftKarp(unittest.TestCase):
    def setUp(self):
        # Wilson, ex. 25.1, bipartite graph
        self.N = 7
        self.G = Graph(self.N, directed=False)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 4),
            Edge(0, 5),
            Edge(0, 6),
            Edge(1, 3),
            Edge(1, 5),
            Edge(2, 3),
            Edge(2, 4)
        ]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_matching_hopcroftkarp_set(self):
        algorithm = HopcroftKarpSet(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 3
        expected_mate1 = {0: 5, 5: 0, 1: 3, 3: 1, 2: 4, 4: 2, 6: None}
        expected_mate2 = {0: 4, 4: 0, 1: 5, 5: 1, 2: 3, 3: 2, 6: None}
        expected_mate3 = {0: 6, 6: 0, 1: 3, 3: 1, 2: 4, 4: 2, 5: None}
        expected_mate4 = {0: 6, 6: 0, 1: 5, 5: 1, 2: 3, 3: 2, 4: None}
        expected_mate5 = {0: 6, 6: 0, 1: 5, 5: 1, 2: 4, 4: 2, 3: None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate1)

    def test_matching_hopcroftkarp_list(self):
        algorithm = HopcroftKarpList(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 3
        expected_mate1 = {0: 5, 5: 0, 1: 3, 3: 1, 2: 4, 4: 2, 6: None}
        expected_mate2 = {0: 4, 4: 0, 1: 5, 5: 1, 2: 3, 3: 2, 6: None}
        expected_mate3 = {0: 6, 6: 0, 1: 3, 3: 1, 2: 4, 4: 2, 5: None}
        expected_mate4 = {0: 6, 6: 0, 1: 5, 5: 1, 2: 3, 3: 2, 4: None}
        expected_mate5 = {0: 6, 6: 0, 1: 5, 5: 1, 2: 4, 4: 2, 3: None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate1)

    def test_exceptions(self):
        self.assertRaises(ValueError, HopcroftKarpSet, Graph(2, directed=True))
        self.assertRaises(ValueError, HopcroftKarpList, Graph(2,
                                                              directed=True))

    def tearDown(self):
        pass
Пример #38
0
 def test_4prism(self):
     N = 8
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 0), 
         Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(7, 4), 
         Edge(0, 4), Edge(1, 5), Edge(2, 6), Edge(3, 7)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     #G.show()
     algorithm = HalinGraph(G)
     self.assertRaises(ValueError, algorithm.run)
Пример #39
0
class TestMatching(unittest.TestCase):

    def setUp(self):
        # Wilson, ex. 25.1, bipartite graph
        # 0 : 4 5 6
        # 1 : 3 5
        # 2 : 3 4
        # ...
        self.N = 7
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 4), Edge(0, 5), Edge(0, 6), 
            Edge(1, 3), Edge(1, 5), Edge(2, 3), Edge(2, 4)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_maximal_matching(self):
        algorithm = MaximalMatching(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 2   # max 3
        expected_mate = {0: 4, 4: 0, 1: 3, 3: 1, 2: None, 5: None, 6: None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate)
        # Is it matching?
        for source in algorithm.mate:
            if algorithm.mate[source] is not None:
                target = algorithm.mate[source]
                self.assertEqual(algorithm.mate[target], source)

    def test_maximal_matching_with_edges(self):
        algorithm = MaximalMatchingWithEdges(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 2   # max 3
        expected_mate = {0: Edge(0, 4), 1: Edge(1, 3), 2: None,
            3: Edge(3, 1), 4: Edge(4, 0), 5: None, 6: None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate)
        # Is it matching?
        for source in algorithm.mate:
            if algorithm.mate[source] is not None:
                edge = algorithm.mate[source]
                self.assertEqual(algorithm.mate[edge.target], ~edge)

    def tearDown(self): pass
Пример #40
0
 def test_wheel5(self):
     N = 5
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), 
         Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 1)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraph(G)
     algorithm.run()
     #print "wheel5 outer", algorithm.outer
     self.assertEqual(algorithm.outer, set([1, 2, 3, 4]))
     self.assertTrue(algorithm.is_outer_k4())
Пример #41
0
 def test_halin16(self):
     N = 16
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 2), Edge(0, 15), Edge(1, 2), 
         Edge(1, 6), Edge(2, 3), Edge(3, 4), Edge(3, 5), 
         Edge(4, 5), Edge(4, 10), Edge(5, 6), Edge(6, 7), 
         Edge(7, 8), Edge(7, 15), Edge(8, 9), Edge(8, 13), 
         Edge(9, 10), Edge(9, 11), Edge(10, 11), Edge(11, 12), 
         Edge(12, 13), Edge(12, 14), Edge(13, 14), Edge(14, 15)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     #print "halin16"
     algorithm = HalinNodeColoring(G, outer=set([0, 2, 3, 4, 10, 11, 12, 14, 15]))
     algorithm.run()
     #print "halin16 outer", algorithm.outer
     parent = {0: 1, 1: None, 2: 1, 3: 5, 4: 5, 5: 6, 6: 1, 7: 6, 
         8: 7, 9: 8, 10: 9, 11: 9, 12: 13, 13: 8, 14: 13, 15: 7}
     self.assertEqual(algorithm.parent, parent)
     for node in G.iternodes():
         self.assertNotEqual(algorithm.color[node], None)
     for edge in G.iteredges():
         self.assertNotEqual(algorithm.color[edge.source],
                             algorithm.color[edge.target])
     all_colors = set(algorithm.color[node] for node in G.iternodes())
     self.assertEqual(len(all_colors), 3)
Пример #42
0
 def test_tree_three_nodes_radius_angle(self):
     T = Graph(3)
     for node in (0, 1, 2):
         T.add_node(node)
     for edge in (Edge(0, 1), Edge(1, 2)):
         T.add_edge(edge)
     algorithm = TreePlotRadiusAngle(T)
     algorithm.run()
     self.assertEqual(len(algorithm.point_dict), 3)
     #print algorithm.point_dict
     self.assertEqual(algorithm.point_dict,
         {0: (1, Fraction(3, 2)),
         1: (0, Fraction(3, 1)), 
         2: (1, Fraction(9, 2))})
Пример #43
0
class TestTSP(unittest.TestCase):

    def setUp(self):
        self.N = 4
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 20), Edge(0, 3, 35), Edge(0, 2, 42), 
            Edge(1, 2, 30), Edge(1, 3, 34), Edge(2, 3, 12)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        self.best_weight = 97
        #self.G.show()

    def test_brute_force_with_edges(self):
        algorithm = BruteForceTSPWithEdges(self.G)
        algorithm.run(2)
        # Cycles for different starting nodes.
        expected_hamiltonian_cycle0 = [
            Edge(0, 1, 20), Edge(1, 2, 30), Edge(2, 3, 12), Edge(3, 0, 35)]
        expected_hamiltonian_cycle1 = [
            Edge(1, 0, 20), Edge(0, 3, 35), Edge(3, 2, 12), Edge(2, 1, 30)]
        expected_hamiltonian_cycle2 = [
            Edge(2, 1, 30), Edge(1, 0, 20), Edge(0, 3, 35), Edge(3, 2, 12)]
        expected_hamiltonian_cycle3 = [
            Edge(3, 0, 35), Edge(0, 1, 20), Edge(1, 2, 30), Edge(2, 3, 12)]
        expected_hamiltonian_cycle = expected_hamiltonian_cycle2
        self.assertEqual(algorithm.hamiltonian_cycle, expected_hamiltonian_cycle)
        weight = sum(edge.weight for edge in algorithm.hamiltonian_cycle)
        self.assertEqual(weight, self.best_weight)

    def test_brute_force_with_cycle_graph(self):
        algorithm = BruteForceTSPWithGraph(self.G)
        algorithm.run(2)
        # Hamiltonian cycle as a graph.
        weight = sum(edge.weight for edge in algorithm.hamiltonian_cycle.iteredges())
        self.assertEqual(weight, self.best_weight)
        #algorithm.hamiltonian_cycle.show()
        self.assertEqual(algorithm.hamiltonian_cycle.e(),
                         algorithm.hamiltonian_cycle.v())
        for node in algorithm.hamiltonian_cycle.iternodes():
            self.assertEqual(algorithm.hamiltonian_cycle.degree(node), 2)

    def test_exceptions(self):
        self.assertRaises(ValueError, BruteForceTSPWithEdges, Graph(5, True))
        self.assertRaises(ValueError, BruteForceTSPWithGraph, Graph(5, True))

    def tearDown(self): pass
Пример #44
0
 def test_frucht12(self):
     N = 10
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 4), Edge(0, 11), Edge(1, 2), 
         Edge(1, 10), Edge(2, 3), Edge(2, 7), Edge(3, 4), 
         Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), 
         Edge(6, 8), Edge(7, 8), Edge(8, 9), Edge(9, 10), 
         Edge(9, 11), Edge(10, 11)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     #print "frucht12"
     algorithm = HalinNodeColoring(G, outer=set([0, 4, 5, 6, 8, 9, 11]))
     algorithm.run()
     #print "frucht12 outer", algorithm.outer
     parent = {0: 1, 1: None, 2: 1, 3: 2, 4: 3, 5: 3, 6: 7, 7: 2, 8: 7, 9: 10, 10: 1, 11: 10}
     self.assertEqual(algorithm.parent, parent)
     for node in G.iternodes():
         self.assertNotEqual(algorithm.color[node], None)
     for edge in G.iteredges():
         self.assertNotEqual(algorithm.color[edge.source],
                             algorithm.color[edge.target])
     all_colors = set(algorithm.color[node] for node in G.iternodes())
     self.assertEqual(len(all_colors), 3)
Пример #45
0
class TestMaximumFlow3(unittest.TestCase):

    def setUp(self):
        self.N = 6           # number of nodes
        self.G = Graph(self.N, directed=True)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1, 10), Edge(0, 2, 10), Edge(1, 2, 2), Edge(1, 3, 4), 
            Edge(1, 4, 8), Edge(2, 4, 9), Edge(4, 3, 6), Edge(4, 5, 10), 
            Edge(3, 5, 10)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_edmonds_karp(self):
        algorithm = EdmondsKarp(self.G)
        algorithm.run(0, 5)
        expected_max_flow = 19
        expected_flow = {
            0: {0: 0, 1: 10, 2: 9, 3: 0, 4: 0, 5: 0},
            1: {0: -10, 1: 0, 2: 0, 3: 4, 4: 6, 5: 0},
            2: {0: -9, 1: 0, 2: 0, 3: 0, 4: 9, 5: 0},
            3: {0: 0, 1: -4, 2: 0, 3: 0, 4: -5, 5: 9},
            4: {0: 0, 1: -6, 2: -9, 3: 5, 4: 0, 5: 10},
            5: {0: 0, 1: 0, 2: 0, 3: -9, 4: -10, 5: 0}}
        self.assertEqual(algorithm.max_flow, expected_max_flow)
        self.assertEqual(algorithm.flow, expected_flow)

    def test_edmonds_karp_sparse(self):
        algorithm = EdmondsKarpSparse(self.G)
        algorithm.run(0, 5)
        expected_max_flow = 19
        expected_flow = {
            0: {1: 10, 2: 9},
            1: {0: -10, 2: 0, 3: 4, 4: 6},
            2: {0: -9, 1: 0, 4: 9},
            3: {1: -4, 4: -5, 5: 9},
            4: {1: -6, 2: -9, 3: 5, 5: 10},
            5: {3: -9, 4: -10}}
        self.assertEqual(algorithm.max_flow, expected_max_flow)
        self.assertEqual(algorithm.flow, expected_flow)

    def test_exceptions(self):
        self.assertRaises(ValueError, EdmondsKarp, Graph())
        self.assertRaises(ValueError, EdmondsKarpSparse, Graph())
        self.assertRaises(ValueError, lambda: EdmondsKarp(self.G).run(0,0))
        self.assertRaises(ValueError, lambda: EdmondsKarpSparse(self.G).run(0,0))
Пример #46
0
 def test_halin7(self):
     N = 7
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 2), Edge(0, 6), Edge(1, 2), 
         Edge(1, 4), Edge(2, 3), Edge(3, 4), Edge(3, 5), 
         Edge(4, 5), Edge(4, 6), Edge(5, 6)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraph(G)
     algorithm.run()
     #print "halin7 outer", algorithm.outer
     self.assertEqual(algorithm.outer, set([0, 2, 3, 5, 6]))
     self.assertTrue(algorithm.is_outer_k4())
Пример #47
0
class TestCutNode(unittest.TestCase):

    def setUp(self):
        # The graph from Cormen p.607 changed.
        self.N = 8           # number of nodes
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 4, 2), Edge(0, 1, 3), Edge(1, 4, 12), Edge(1, 5, 4), 
            Edge(5, 2, 5), Edge(5, 6, 6), Edge(2, 6, 7), Edge(2, 3, 8), 
            Edge(2, 7, 9), Edge(3, 7, 11)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #print self.G
        #self.G.show()

    def test_trivial_cut_node(self):
        algorithm = TrivialCutNode(self.G)
        algorithm.run(0)
        cut_nodes_expected = [1, 2, 5]
        self.assertEqual(algorithm.cut_nodes, cut_nodes_expected)

    def test_tarjan_cut_node(self):
        algorithm = TarjanCutNode(self.G)
        algorithm.run(0)
        cut_nodes_expected = [2, 5, 1]
        self.assertEqual(algorithm.cut_nodes, cut_nodes_expected)
        dd_expected = {0: 1, 1: 2, 2: 5, 3: 6, 4: 3, 5: 4, 6: 8, 7: 7}
        self.assertEqual(algorithm._dd, dd_expected)
        parent_expected = {0: None, 1: 0, 2: 5, 3: 2, 4: 1, 5: 1, 6: 2, 7: 3}
        self.assertEqual(algorithm.parent, parent_expected)

    def test_is_biconnected(self):
        self.assertFalse(is_biconnected(self.G))
        self.G.add_edge(Edge(4, 5))
        self.assertFalse(is_biconnected(self.G))
        self.G.add_edge(Edge(1, 2))
        self.assertFalse(is_biconnected(self.G))
        self.G.add_edge(Edge(6, 7))
        self.assertTrue(is_biconnected(self.G))

    def test_exceptions(self):
        self.assertRaises(ValueError, TrivialCutNode, Graph(1, True))
        self.assertRaises(ValueError, TarjanCutNode, Graph(1, True))

    def tearDown(self): pass
Пример #48
0
 def test_3prism(self):
     N = 6
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), 
         Edge(4, 5), Edge(0, 5), Edge(1, 4), Edge(2, 0), Edge(3, 5)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraph(G)
     algorithm.run()
     #print "3prism outer", algorithm.outer
     #self.assertEqual(algorithm.outer, set([0, 2, 3, 5]))
     self.assertEqual(algorithm.outer, set([0, 1, 4, 5]))
     #self.assertEqual(algorithm.outer, set([1, 2, 3, 4]))
     self.assertTrue(algorithm.is_outer_k4())
Пример #49
0
 def setUp(self):
     # The modified graph (unique weights) from Cormen.
     self.N = 9  # number of nodes
     self.G = Graph(self.N)
     self.nodes = range(self.N)
     self.edges = [
         Edge(0, 1, 4),
         Edge(0, 7, 8),
         Edge(1, 7, 11),
         Edge(1, 2, 12),
         Edge(7, 8, 7),
         Edge(8, 2, 2),
         Edge(8, 6, 6),
         Edge(7, 6, 1),
         Edge(2, 3, 13),
         Edge(2, 5, 5),
         Edge(6, 5, 3),
         Edge(3, 5, 14),
         Edge(3, 4, 9),
         Edge(5, 4, 10),
     ]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
Пример #50
0
 def setUp(self):
     # 0      s=0, t=1
     # | \
     # 1---2
     # | \
     # 3   4
     self.N = 5
     self.G = Graph(self.N)
     self.nodes = range(self.N)
     self.edges = [Edge(0, 1), Edge(0, 2), Edge(1, 2),
                   Edge(1, 3), Edge(1, 4)]
     for node in self.nodes:
         self.G.add_node(node)
     for edge in self.edges:
         self.G.add_edge(edge)
     # self.G.show()
     node1 = Node(0, 1, "edge")
     node2 = Node(0, 2, "edge")
     node3 = Node(2, 1, "edge")
     node4 = Node(1, 3, "edge")
     node5 = Node(1, 4, "edge")
     node6 = Node(0, 1, "series", node2, node3)
     node7 = Node(0, 1, "parallel", node1, node6)
     node8 = Node(0, 1, "jackknife", node7, node4)
     node9 = Node(0, 1, "jackknife", node8, node5)
     self.root = node9
Пример #51
0
 def test_halin10l(self):
     N = 10
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 4), Edge(0, 9), Edge(1, 2), 
         Edge(1, 9), Edge(2, 3), Edge(2, 7), Edge(3, 4), 
         Edge(3, 5), Edge(4, 5), Edge(5, 6), Edge(6, 7), 
         Edge(6, 8), Edge(7, 8), Edge(8, 9)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraph(G)
     algorithm.run()
     #print "halin10l outer", algorithm.outer
     self.assertEqual(algorithm.outer, set([0, 4, 5, 6, 8, 9]))
     self.assertTrue(algorithm.is_outer_k4())
Пример #52
0
class TestHopcroftKarp(unittest.TestCase):

    def setUp(self):
        # Wilson, ex. 25.1, bipartite graph
        self.N = 7
        self.G = Graph(self.N, directed=False)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 4), Edge(0, 5), Edge(0, 6), Edge(1, 3), Edge(1, 5), 
            Edge(2, 3), Edge(2, 4)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)

    def test_matching_hopcroftkarp_set(self):
        algorithm = HopcroftKarpSet(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 3
        expected_mate1 = {0:5, 5:0, 1:3, 3:1, 2:4, 4:2, 6:None}
        expected_mate2 = {0:4, 4:0, 1:5, 5:1, 2:3, 3:2, 6:None}
        expected_mate3 = {0:6, 6:0, 1:3, 3:1, 2:4, 4:2, 5:None}
        expected_mate4 = {0:6, 6:0, 1:5, 5:1, 2:3, 3:2, 4:None}
        expected_mate5 = {0:6, 6:0, 1:5, 5:1, 2:4, 4:2, 3:None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate1)

    def test_matching_hopcroftkarp_list(self):
        algorithm = HopcroftKarpList(self.G)
        algorithm.run()
        # 5 solutions
        expected_cardinality = 3
        expected_mate1 = {0:5, 5:0, 1:3, 3:1, 2:4, 4:2, 6:None}
        expected_mate2 = {0:4, 4:0, 1:5, 5:1, 2:3, 3:2, 6:None}
        expected_mate3 = {0:6, 6:0, 1:3, 3:1, 2:4, 4:2, 5:None}
        expected_mate4 = {0:6, 6:0, 1:5, 5:1, 2:3, 3:2, 4:None}
        expected_mate5 = {0:6, 6:0, 1:5, 5:1, 2:4, 4:2, 3:None}
        self.assertEqual(algorithm.cardinality, expected_cardinality)
        self.assertEqual(algorithm.mate, expected_mate1)

    def test_exceptions(self):
        self.assertRaises(ValueError, HopcroftKarpSet, Graph(2, directed=True))
        self.assertRaises(ValueError, HopcroftKarpList, Graph(2, directed=True))

    def tearDown(self): pass
Пример #53
0
 def test_wheel5(self):
     N = 5
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), 
         Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 1)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraphPEO(G, outer=set([1, 2, 3, 4]))
     algorithm.run()
     #print "wheel5 outer", algorithm.outer
     #print "wheel5 cycle", algorithm.cycle
     parent = {0: None, 1: 0, 2: 0, 3: 0, 4: 0}
     self.assertEqual(algorithm.parent, parent)
     order = [1, 2, 3, 4, 0]
     self.assertEqual(algorithm.order, order)
Пример #54
0
 def test_halin8a(self):
     N = 8
     G = Graph(N, False)
     edges = [Edge(0, 1), Edge(0, 2), Edge(0, 7), Edge(1, 2), 
         Edge(1, 5), Edge(2, 3), Edge(3, 4), Edge(3, 7), 
         Edge(4, 5), Edge(4, 6), Edge(5, 6), Edge(6, 7)]
     for node in range(N):
         G.add_node(node)
     for edge in edges:
         G.add_edge(edge)
     algorithm = HalinGraph(G)
     algorithm.run()
     #print "halin8a outer", algorithm.outer
     # Sa dwie mozliwosci narysowania tego grafu.
     self.assertEqual(algorithm.outer, set([0, 1, 5, 6, 7]))
     #self.assertEqual(algorithm.outer, set([1, 2, 3, 4, 5]))
     self.assertTrue(algorithm.is_outer_k4())
Пример #55
0
class TestNodeColoring(unittest.TestCase):

    def setUp(self):
        self.N = 6
        self.G = Graph(self.N)
        self.nodes = range(self.N)
        self.edges = [
            Edge(0, 1), Edge(0, 3), Edge(1, 3), Edge(1, 4), Edge(1, 2), 
            Edge(2, 4), Edge(2, 5), Edge(3, 4), Edge(4, 5)]
        for node in self.nodes:
            self.G.add_node(node)
        for edge in self.edges:
            self.G.add_edge(edge)
        #self.G.show()

    def test_us_node_coloring(self):
        algorithm = UnorderedSequentialNodeColoring(self.G)
        algorithm.run()
        for node in self.G.iternodes():
            self.assertNotEqual(algorithm.color[node], None)
        for edge in self.G.iteredges():
            self.assertNotEqual(algorithm.color[edge.source],
                                algorithm.color[edge.target])
        #print algorithm.color
        all_colors = set(algorithm.color[node] for node in self.G.iternodes())
        self.assertEqual(len(all_colors), 4)

    def test_exceptions(self):
        self.assertRaises(ValueError, UnorderedSequentialNodeColoring,
            Graph(5, directed=True))

    def tearDown(self): pass