Esempio n. 1
0
    def test_003(self):
        g = ActionDigraph(17, 31)
        for i in range(17):
            with self.assertRaises(RuntimeError):
                g.number_of_scc()
            for j in range(31):
                g.add_edge(i, (7 * i + 23 * j) % 17, j)

        self.assertEqual(g.number_of_edges(), 31 * 17)
        self.assertEqual(g.number_of_nodes(), 17)
        with self.assertRaises(RuntimeError):
            g.add_edge(0, 0, 32)
        for i in range(17):
            for j in range(31):
                self.assertEqual(g.neighbor(i, j), (7 * i + 23 * j) % 17)

        g.add_to_out_degree(10)
        self.assertEqual(g.out_degree(), 41)
        self.assertEqual(g.number_of_nodes(), 17)
        self.assertFalse(g.validate())

        for i in range(17):
            for j in range(10):
                g.add_edge(i, (7 * i + 23 * j) % 17, 31 + j)

        self.assertEqual(g.number_of_edges(), 41 * 17)
        self.assertEqual(g.number_of_nodes(), 17)
Esempio n. 2
0
    def test_036(self):
        algorithm = ActionDigraph.algorithm

        ad = ActionDigraph()
        ad.add_nodes(10)
        ad.add_to_out_degree(20)
        ad.add_edge(0, 7, 5)
        ad.add_edge(0, 5, 7)
        ad.add_edge(1, 9, 14)
        ad.add_edge(1, 5, 17)
        ad.add_edge(3, 8, 5)
        ad.add_edge(5, 8, 1)
        ad.add_edge(6, 8, 14)
        ad.add_edge(7, 8, 10)
        ad.add_edge(8, 9, 12)
        ad.add_edge(8, 9, 13)

        self.assertTrue(is_acyclic(ad))
        self.assertFalse(ad.validate())

        self.assertEqual(ad.number_of_paths_algorithm(0, 0, 16),
                         algorithm.acyclic)
        self.assertEqual(ad.number_of_paths(0, 0, 30), 9)
        self.assertEqual(ad.number_of_paths(1, 0, 10, algorithm.matrix), 6)
        self.assertEqual(ad.number_of_paths(1, 9, 0, 10, algorithm.matrix), 3)
Esempio n. 3
0
    def test_037(self):  # pylint: disable=too-many-statements
        algorithm = ActionDigraph.algorithm
        n = 10
        ad = ActionDigraph()
        ad.add_nodes(10)
        ad.add_to_out_degree(20)
        ad.add_edge(0, 9, 0)
        ad.add_edge(0, 1, 1)
        ad.add_edge(0, 6, 2)
        ad.add_edge(0, 3, 3)
        ad.add_edge(0, 7, 4)
        ad.add_edge(0, 2, 5)
        ad.add_edge(0, 2, 6)
        ad.add_edge(0, 8, 7)
        ad.add_edge(0, 1, 8)
        ad.add_edge(0, 4, 9)
        ad.add_edge(0, 3, 10)
        ad.add_edge(0, 1, 11)
        ad.add_edge(0, 7, 12)
        ad.add_edge(0, 9, 13)
        ad.add_edge(0, 4, 14)
        ad.add_edge(0, 7, 15)
        ad.add_edge(0, 8, 16)
        ad.add_edge(0, 9, 17)
        ad.add_edge(0, 6, 18)
        ad.add_edge(0, 9, 19)
        ad.add_edge(1, 8, 0)
        ad.add_edge(1, 2, 1)
        ad.add_edge(1, 5, 2)
        ad.add_edge(1, 7, 3)
        ad.add_edge(1, 9, 4)
        ad.add_edge(1, 0, 5)
        ad.add_edge(1, 2, 6)
        ad.add_edge(1, 4, 7)
        ad.add_edge(1, 0, 8)
        ad.add_edge(1, 3, 9)
        ad.add_edge(1, 2, 10)
        ad.add_edge(1, 7, 11)
        ad.add_edge(1, 2, 12)
        ad.add_edge(1, 7, 13)
        ad.add_edge(1, 6, 14)
        ad.add_edge(1, 6, 15)
        ad.add_edge(1, 5, 16)
        ad.add_edge(1, 4, 17)
        ad.add_edge(1, 6, 18)
        ad.add_edge(1, 3, 19)
        ad.add_edge(2, 2, 0)
        ad.add_edge(2, 9, 1)
        ad.add_edge(2, 0, 2)
        ad.add_edge(2, 6, 3)
        ad.add_edge(2, 7, 4)
        ad.add_edge(2, 9, 5)
        ad.add_edge(2, 5, 6)
        ad.add_edge(2, 4, 7)
        ad.add_edge(2, 9, 8)
        ad.add_edge(2, 7, 9)
        ad.add_edge(2, 9, 10)
        ad.add_edge(2, 9, 11)
        ad.add_edge(2, 0, 12)
        ad.add_edge(2, 7, 13)
        ad.add_edge(2, 9, 14)
        ad.add_edge(2, 6, 15)
        ad.add_edge(2, 3, 16)
        ad.add_edge(2, 3, 17)
        ad.add_edge(2, 4, 18)
        ad.add_edge(2, 1, 19)
        ad.add_edge(3, 1, 0)
        ad.add_edge(3, 9, 1)
        ad.add_edge(3, 6, 2)
        ad.add_edge(3, 2, 3)
        ad.add_edge(3, 9, 4)
        ad.add_edge(3, 8, 5)
        ad.add_edge(3, 1, 6)
        ad.add_edge(3, 6, 7)
        ad.add_edge(3, 1, 8)
        ad.add_edge(3, 0, 9)
        ad.add_edge(3, 5, 10)
        ad.add_edge(3, 0, 11)
        ad.add_edge(3, 2, 12)
        ad.add_edge(3, 7, 13)
        ad.add_edge(3, 4, 14)
        ad.add_edge(3, 0, 15)
        ad.add_edge(3, 4, 16)
        ad.add_edge(3, 8, 17)
        ad.add_edge(3, 3, 18)
        ad.add_edge(3, 1, 19)
        ad.add_edge(4, 0, 0)
        ad.add_edge(4, 4, 1)
        ad.add_edge(4, 8, 2)
        ad.add_edge(4, 5, 3)
        ad.add_edge(4, 5, 4)
        ad.add_edge(4, 1, 5)
        ad.add_edge(4, 3, 6)
        ad.add_edge(4, 8, 7)
        ad.add_edge(4, 4, 8)
        ad.add_edge(4, 4, 9)
        ad.add_edge(4, 4, 10)
        ad.add_edge(4, 7, 11)
        ad.add_edge(4, 8, 12)
        ad.add_edge(4, 6, 13)
        ad.add_edge(4, 3, 14)
        ad.add_edge(4, 7, 15)
        ad.add_edge(4, 6, 16)
        ad.add_edge(4, 7, 17)
        ad.add_edge(4, 0, 18)
        ad.add_edge(4, 2, 19)
        ad.add_edge(5, 3, 0)
        ad.add_edge(5, 0, 1)
        ad.add_edge(5, 4, 2)
        ad.add_edge(5, 7, 3)
        ad.add_edge(5, 2, 4)
        ad.add_edge(5, 5, 5)
        ad.add_edge(5, 7, 6)
        ad.add_edge(5, 7, 7)
        ad.add_edge(5, 7, 8)
        ad.add_edge(5, 7, 9)
        ad.add_edge(5, 0, 10)
        ad.add_edge(5, 8, 11)
        ad.add_edge(5, 6, 12)
        ad.add_edge(5, 8, 13)
        ad.add_edge(5, 8, 14)
        ad.add_edge(5, 1, 15)
        ad.add_edge(5, 5, 16)
        ad.add_edge(5, 5, 17)
        ad.add_edge(5, 3, 18)
        ad.add_edge(5, 7, 19)
        ad.add_edge(6, 8, 0)
        ad.add_edge(6, 7, 1)
        ad.add_edge(6, 6, 2)
        ad.add_edge(6, 5, 3)
        ad.add_edge(6, 6, 4)
        ad.add_edge(6, 1, 5)
        ad.add_edge(6, 7, 6)
        ad.add_edge(6, 2, 7)
        ad.add_edge(6, 7, 8)
        ad.add_edge(6, 3, 9)
        ad.add_edge(6, 3, 10)
        ad.add_edge(6, 8, 11)
        ad.add_edge(6, 3, 12)
        ad.add_edge(6, 9, 13)
        ad.add_edge(6, 4, 14)
        ad.add_edge(6, 1, 15)
        ad.add_edge(6, 4, 16)
        ad.add_edge(6, 3, 17)
        ad.add_edge(6, 9, 18)
        ad.add_edge(6, 8, 19)
        ad.add_edge(7, 9, 0)
        ad.add_edge(7, 4, 1)
        ad.add_edge(7, 3, 2)
        ad.add_edge(7, 8, 3)
        ad.add_edge(7, 0, 4)
        ad.add_edge(7, 5, 5)
        ad.add_edge(7, 6, 6)
        ad.add_edge(7, 8, 7)
        ad.add_edge(7, 9, 8)
        ad.add_edge(7, 1, 9)
        ad.add_edge(7, 7, 10)
        ad.add_edge(7, 0, 11)
        ad.add_edge(7, 6, 12)
        ad.add_edge(7, 2, 13)
        ad.add_edge(7, 3, 14)
        ad.add_edge(7, 8, 15)
        ad.add_edge(7, 6, 16)
        ad.add_edge(7, 3, 17)
        ad.add_edge(7, 2, 18)
        ad.add_edge(7, 7, 19)
        ad.add_edge(8, 0, 0)
        ad.add_edge(8, 6, 1)
        ad.add_edge(8, 3, 2)
        ad.add_edge(8, 5, 3)
        ad.add_edge(8, 7, 4)
        ad.add_edge(8, 9, 5)
        ad.add_edge(8, 9, 6)
        ad.add_edge(8, 8, 7)
        ad.add_edge(8, 1, 8)
        ad.add_edge(8, 5, 9)
        ad.add_edge(8, 7, 10)
        ad.add_edge(8, 9, 11)
        ad.add_edge(8, 6, 12)
        ad.add_edge(8, 0, 13)
        ad.add_edge(8, 0, 14)
        ad.add_edge(8, 3, 15)
        ad.add_edge(8, 6, 16)
        ad.add_edge(8, 0, 17)
        ad.add_edge(8, 8, 18)
        ad.add_edge(8, 9, 19)
        ad.add_edge(9, 3, 0)
        ad.add_edge(9, 7, 1)
        ad.add_edge(9, 9, 2)
        ad.add_edge(9, 1, 3)
        ad.add_edge(9, 4, 4)
        ad.add_edge(9, 9, 5)
        ad.add_edge(9, 4, 6)
        ad.add_edge(9, 0, 7)
        ad.add_edge(9, 5, 8)
        ad.add_edge(9, 8, 9)
        ad.add_edge(9, 3, 10)
        ad.add_edge(9, 2, 11)
        ad.add_edge(9, 0, 12)
        ad.add_edge(9, 2, 13)
        ad.add_edge(9, 3, 14)
        ad.add_edge(9, 4, 15)
        ad.add_edge(9, 0, 16)
        ad.add_edge(9, 5, 17)
        ad.add_edge(9, 3, 18)
        ad.add_edge(9, 5, 19)
        self.assertFalse(is_acyclic(ad))
        self.assertTrue(ad.validate())

        self.assertEqual(ad.number_of_paths_algorithm(0), algorithm.acyclic)
        self.assertEqual(ad.number_of_paths(0), POSITIVE_INFINITY)
        with self.assertRaises(RuntimeError):
            ad.number_of_paths(0, 0, 10, algorithm.acyclic)
        with self.assertRaises(RuntimeError):
            ad.number_of_paths(1, 9, 0, 10, algorithm.acyclic)

        ad = binary_tree(n)
        self.assertEqual(ad.number_of_paths_algorithm(0), algorithm.acyclic)
        self.assertEqual(ad.number_of_paths(0), 1023)

        add_cycle(ad, n)
        ad.add_edge(0, n + 1, 0)
        self.assertFalse(is_acyclic(ad))
        self.assertFalse(ad.validate())
        self.assertEqual(ad.number_of_paths(1), 511)
        self.assertEqual(
            ad.number_of_paths_algorithm(1, 0, POSITIVE_INFINITY),
            algorithm.acyclic,
        )
        self.assertEqual(ad.number_of_paths(1, 0, POSITIVE_INFINITY), 511)
        self.assertEqual(len(topological_sort(ad)), 0)
        for m in ad.nodes_iterator():
            if len(topological_sort(ad, m)) == 0:
                self.assertEqual(m, 1023)
                break