예제 #1
0
    def test_connect(self):
        digraph = Graph({
                "a":{},
                "b":{},
                "c":{},
                "d":{}
            }, directed=True)

        self.assertEqual(digraph.get_degree("a"), 0)
        self.assertEqual(digraph.get_degree("b"), 0)
        self.assertEqual(digraph.get_degree("c"), 0)
        self.assertEqual(digraph.get_degree("d"), 0)

        digraph.connect("a", "b")
        digraph.connect("c", "d")
        self.assertEqual(digraph.get_indegree("a"), 0)
        self.assertEqual(digraph.get_indegree("b"), 1)
        self.assertEqual(digraph.get_indegree("c"), 0)
        self.assertEqual(digraph.get_indegree("d"), 1)   

        self.assertEqual(digraph.get_outdegree("a"), 1)
        self.assertEqual(digraph.get_outdegree("b"), 0)
        self.assertEqual(digraph.get_outdegree("c"), 1)
        self.assertEqual(digraph.get_outdegree("d"), 0)

        self.assertEqual(digraph.get_successors("a"), {"b"})
        self.assertEqual(digraph.get_successors("b"), set())
        self.assertEqual(digraph.get_successors("c"), {"d"})
        self.assertEqual(digraph.get_successors("d"), set())

        self.assertEqual(digraph.get_predecessors("a"), set())
        self.assertEqual(digraph.get_predecessors("b"), {"a"})
        self.assertEqual(digraph.get_predecessors("c"), set())
        self.assertEqual(digraph.get_predecessors("d"), {"c"})

        digraph.connect("b", "c")
        self.assertEqual(digraph.get_indegree("a"), 0)
        self.assertEqual(digraph.get_indegree("b"), 1)
        self.assertEqual(digraph.get_indegree("c"), 1)
        self.assertEqual(digraph.get_indegree("d"), 1)   

        self.assertEqual(digraph.get_outdegree("a"), 1)
        self.assertEqual(digraph.get_outdegree("b"), 1)
        self.assertEqual(digraph.get_outdegree("c"), 1)
        self.assertEqual(digraph.get_outdegree("d"), 0)

        self.assertEqual(digraph.get_successors("a"), {"b"})
        self.assertEqual(digraph.get_successors("b"), {"c"})
        self.assertEqual(digraph.get_successors("c"), {"d"})
        self.assertEqual(digraph.get_successors("d"), set())

        self.assertEqual(digraph.get_predecessors("a"), set())
        self.assertEqual(digraph.get_predecessors("b"), {"a"})
        self.assertEqual(digraph.get_predecessors("c"), {"b"})
        self.assertEqual(digraph.get_predecessors("d"), {"c"})
예제 #2
0
    def test_disconnect(self):
        digraph = Graph({
                "a":{"b":None, "c":None},
                "b":{"d":None, "e":None},
                "c":{"f":None, "g":None},
                "d":{},
                "e":{},
                "f":{},
                "g":{}
            }, directed=True)

        digraph.disconnect("a", "c")
        digraph.disconnect("b", "d")
        digraph.disconnect("c", "g")

        self.assertEqual(digraph.get_indegree("a"), 0)
        self.assertEqual(digraph.get_indegree("b"), 1)
        self.assertEqual(digraph.get_indegree("c"), 0)
        self.assertEqual(digraph.get_indegree("d"), 0)
        self.assertEqual(digraph.get_indegree("e"), 1)
        self.assertEqual(digraph.get_indegree("f"), 1)
        self.assertEqual(digraph.get_indegree("g"), 0)

        self.assertEqual(digraph.get_outdegree("a"), 1)
        self.assertEqual(digraph.get_outdegree("b"), 1)
        self.assertEqual(digraph.get_outdegree("c"), 1)
        self.assertEqual(digraph.get_outdegree("d"), 0)
        self.assertEqual(digraph.get_outdegree("e"), 0)
        self.assertEqual(digraph.get_outdegree("f"), 0)
        self.assertEqual(digraph.get_outdegree("g"), 0)

        self.assertEqual(digraph.get_successors("a"), {"b"})
        self.assertEqual(digraph.get_successors("b"), {"e"})
        self.assertEqual(digraph.get_successors("c"), {"f"})
        self.assertEqual(digraph.get_successors("d"), set())
        self.assertEqual(digraph.get_successors("e"), set())
        self.assertEqual(digraph.get_successors("f"), set())
        self.assertEqual(digraph.get_successors("g"), set())

        self.assertEqual(digraph.get_predecessors("a"), set())
        self.assertEqual(digraph.get_predecessors("b"), {"a"})
        self.assertEqual(digraph.get_predecessors("c"), set())
        self.assertEqual(digraph.get_predecessors("d"), set())
        self.assertEqual(digraph.get_predecessors("e"), {"b"})
        self.assertEqual(digraph.get_predecessors("f"), {"c"})
        self.assertEqual(digraph.get_predecessors("g"), set())