def testLevels(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)
        gr.add_edge(31, 23)

        self.assertEqual(set([1, 2, 3, 23]), gr.get_levels()[0])
        self.assertEqual(set([11, 12]), gr.get_levels()[1])
        self.assertEqual(set([22, 21]), gr.get_levels()[2])
        self.assertEqual(set([31]), gr.get_levels()[3])

        # cycles no
        self.assertEqual([], gr.get_cycles())

        self.assertEqual(set([1, 2, 11, 12, 21, 22, 23]), gr.compute_closure(31))
        self.assertEqual(set([]), gr.compute_closure(23))
        self.assertEqual(set([1, 11]), gr.compute_closure(22))
        self.assertEqual(set([2, 12]), gr.compute_closure(21))
        self.assertEqual(set([1]), gr.compute_closure(11))
예제 #2
0
    def testLevels(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)
        gr.add_edge(31, 23)

        self.assertEqual(set([1, 2, 3, 23]), gr.get_levels()[0])
        self.assertEqual(set([11, 12]), gr.get_levels()[1])
        self.assertEqual(set([22, 21]), gr.get_levels()[2])
        self.assertEqual(set([31]), gr.get_levels()[3])

        # cycles no
        self.assertEqual([], gr.get_cycles())

        self.assertEqual(set([1, 2, 11, 12, 21, 22, 23]),
                         gr.compute_closure(31))
        self.assertEqual(set([]), gr.compute_closure(23))
        self.assertEqual(set([1, 11]), gr.compute_closure(22))
        self.assertEqual(set([2, 12]), gr.compute_closure(21))
        self.assertEqual(set([1]), gr.compute_closure(11))
예제 #3
0
    def testCycles(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        self.assertEqual([], gr.get_cycles())
        gr.add_edge(1, 2)
        gr.add_edge(2, 3)
        gr.add_edge(3, 1)

        self.assertEqual([1, 2, 3], gr.get_cycles())
    def testCycles(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        self.assertEqual([], gr.get_cycles())
        gr.add_edge(1, 2)
        gr.add_edge(2, 3)
        gr.add_edge(3, 1)

        self.assertEqual([1, 2, 3], gr.get_cycles())
예제 #5
0
    def test_serialize(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)
        gr.add_edge(31, 23)

        s = gr.serialize()
        gr2 = DirectedGraphDeserializer(int).deserialize(s)
        self.assertEquals(gr, gr2)
    def test_closures(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)
        gr.add_edge(31, 23)

        self.assertEqual({21, 12, 2, 22, 11, 1, 23}, set(gr.compute_closure(31)))
        # Inverse closure
        self.assertEqual({31}, gr.inverse_reachable_subgraph(31).nodes)
        self.assertEqual({11, 22, 31, 1}, gr.inverse_reachable_subgraph(1).nodes)
예제 #7
0
    def test_get_paths(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 11, 12, 21, 22, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(22, 12)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)

        paths = gr.get_paths(2)
        self.assertEqual(len(paths), 2)
        self.assertIn([31, 21, 12, 2], paths)
        self.assertIn([31, 22, 12, 2], paths)

        self.assertEqual([[31]], gr.get_paths(31))
예제 #8
0
    def test_closures(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 3, 11, 12, 21, 22, 23, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)
        gr.add_edge(31, 23)

        self.assertEqual({21, 12, 2, 22, 11, 1, 23},
                         set(gr.compute_closure(31)))
        # Inverse closure
        self.assertEqual({31}, gr.inverse_reachable_subgraph(31).nodes)
        self.assertEqual({11, 22, 31, 1},
                         gr.inverse_reachable_subgraph(1).nodes)
    def test_get_paths(self):
        gr = DirectedGraph()
        gr.add_nodes([1, 2, 11, 12, 21, 22, 31])

        gr.add_edge(11, 1)
        gr.add_edge(12, 2)
        gr.add_edge(21, 12)
        gr.add_edge(22, 11)
        gr.add_edge(22, 12)
        gr.add_edge(31, 21)
        gr.add_edge(31, 22)

        paths = gr.get_paths(2)
        self.assertEqual(len(paths), 2)
        self.assertIn([31, 21, 12, 2], paths)
        self.assertIn([31, 22, 12, 2], paths)

        self.assertEqual([[31]], gr.get_paths(31))