コード例 #1
0
def find_best(g: Graph):
    """Find the best candidate of kingpin"""
    best = g.get_node(0)
    for v in g.nodes[1:]:
        if not g.contains_edge(v, best) or g.contains_edge(best, v):
            best = v
    return best
コード例 #2
0
def test_best(best, g: Graph):
    """
    Test if the best candidate is kingpin
    :return: best if best is kingpin, None if best is not
    """
    for v in g.nodes:
        if v is not best:
            if not g.contains_edge(v, best) or g.contains_edge(best, v):
                return None
    return best
コード例 #3
0
class TestFindKingpin(unittest.TestCase):
    nodes = [0, 1, 2, 3]
    g = Graph(nodes)
    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(1, 2)
    g.add_edge(3, 1)
    g.add_edge(3, 2)

    def test_g(self):
        print(self.g)

    def test_get_best(self):
        best = find_best(self.g)
        self.assertEqual(best, 2)

    def test_kingpin(self):
        best = find_best(self.g)
        kingpin = test_best(best, self.g)
        self.assertEqual(kingpin, 2)

    def test_no_kingpin(self):
        nodes = [0, 1, 2, 3]
        g = Graph(nodes)
        g.add_edge(0, 1)
        g.add_edge(0, 2)
        g.add_edge(3, 1)
        g.add_edge(3, 2)

        best = find_best(g)
        self.assertEqual(best, 2)

        kingpin = test_best(best, g)
        self.assertIsNone(kingpin)
コード例 #4
0
    def test_no_kingpin(self):
        nodes = [0, 1, 2, 3]
        g = Graph(nodes)
        g.add_edge(0, 1)
        g.add_edge(0, 2)
        g.add_edge(3, 1)
        g.add_edge(3, 2)

        best = find_best(g)
        self.assertEqual(best, 2)

        kingpin = test_best(best, g)
        self.assertIsNone(kingpin)
コード例 #5
0
 def test_init(self):
     g = Graph(2)
     self.assertEqual(g.size, 2)
     self.assertEqual(g.matrix, [[0, 0], [0, 0]])
コード例 #6
0
 def test_str(self):
     g = Graph(2)
     print(g)
コード例 #7
0
 def test_len(self):
     g = Graph(2)
     self.assertEqual(len(g), 2)
コード例 #8
0
 def test_contains_edge(self):
     g = Graph(2)
     g.add_edge(0, 1)
     self.assertTrue(g.contains_edge(0, 1))
     self.assertFalse(g.contains_edge(0, 0))
コード例 #9
0
 def test_remove_edge(self):
     g = Graph(2)
     g.add_edge(0, 1)
     g.remove_edge(0, 1)
     self.assertEqual(g.matrix, [[0, 0], [0, 0]])