def setUp(self):
        self.g = DirectedGraph()
        self.b = False
        self.currentnode = None
        self.currentarc = None

        self.g2 = DirectedGraph()
        v1 = self.g2.add_node()
        v2 = self.g2.add_node()
        v3 = self.g2.add_node()
        v4 = self.g2.add_node()
        v5 = self.g2.add_node()
        v6 = self.g2.add_node()
        v7 = self.g2.add_node()
        v8 = self.g2.add_node()
        self.nodes = [v1, v2, v3, v4, v5, v6, v7, v8]

        e1 = self.g2.add_arc(v1, v5)
        e2 = self.g2.add_arc(v2, v6)
        e3 = self.g2.add_arc(v3, v7)
        e4 = self.g2.add_arc(v4, v8)
        e5 = self.g2.add_arc(v1, v2)
        e6 = self.g2.add_arc(v2, v3)
        e7 = self.g2.add_arc(v3, v4)
        e8 = self.g2.add_arc(v4, v1)
        self.arcs = [e1, e2, e3, e4, e5, e6, e7, e8]
Beispiel #2
0
    def test_get_incident_edge_raise_TypeError_with_not_node(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8 = self.edges

        g2 = DirectedGraph()
        v = g2.add_node()

        with self.assertRaises(TypeError):
            v1.get_incident_edge(1)

        with self.assertRaises(TypeError):
            v1.get_incident_edge('abc')

        with self.assertRaises(TypeError):
            v1.get_incident_edge((v1, v2))

        with self.assertRaises(TypeError):
            v1.get_incident_edge(e5)

        with self.assertRaises(TypeError):
            v1.get_incident_edge(None)

        with self.assertRaises(TypeError):
            v1.get_incident_edge(v)
 def test_add_arc_raise_NodeMembershipError_with_uncontained_nodes(self):
     v = self.g.add_node()
     g2 = DirectedGraph()
     w1 = g2.add_node()
     g3 = UndirectedGraph()
     w2 = g3.add_node()
     with self.assertRaises(NodeMembershipError):
         self.g.add_arc(v, w1)
     with self.assertRaises(NodeMembershipError):
         self.g.add_arc(v, w2)
     with self.assertRaises(NodeMembershipError):
         self.g.add_arc(w1, w2)
Beispiel #4
0
    def test_get_incident_arc_raise_NodeError_with_not_neighbor(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        g2 = DirectedGraph()
        v = g2.add_node()

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v3)

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v)
Beispiel #5
0
    def setUp(self):
        self.g = DirectedGraph()
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        v3 = self.g.add_node()
        v4 = self.g.add_node()
        v5 = self.g.add_node()
        v6 = self.g.add_node()
        v7 = self.g.add_node()
        v8 = self.g.add_node()
        self.nodes = [v1, v2, v3, v4, v5, v6, v7, v8]

        self.couples = [(v1, v5), (v2, v6), (v3, v7), (v4, v8), (v1, v2), (v2, v3), (v3, v4), (v4, v1), (v2, v1),
                        (v4, v3)]
        self.arcs = [self.g.add_arc(u, v) for u, v in self.couples]
    def test_remove_arc_raise_LinkMembershipError_with_not_contained_arc(self):
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        self.g.add_arc(v1, v2)

        g2 = DirectedGraph()
        v3 = g2.add_node()
        v4 = g2.add_node()
        e2 = g2.add_arc(v3, v4)

        g3 = UndirectedGraph()
        v5 = g3.add_node()
        v6 = g3.add_node()
        e3 = g3.add_edge(v5, v6)

        with self.assertRaises(LinkMembershipError):
            self.g.remove_arc(e2)

        with self.assertRaises(LinkMembershipError):
            self.g.remove_arc(e3)
    def test_neighbor_raise_LinkError_with_not_extremity(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        u = self.g.add_node()

        g2 = DirectedGraph()
        w = g2.add_node()

        e1 = self.edges[0]

        for v in self.nodes:
            if v == v1 or v == v5:
                continue

            with self.assertRaises(LinkError):
                e1.neighbor(v)

        with self.assertRaises(LinkError):
            e1.neighbor(u)

        with self.assertRaises(LinkError):
            e1.neighbor(w)
    def test_remove_node_raise_NodeMembershipError_with_not_contained_node(
            self):
        u = self.g.add_node()
        v = self.g.add_node()
        self.g.add_arc(u, v)

        g2 = DirectedGraph()
        v3 = g2.add_node()
        v4 = g2.add_node()
        g2.add_arc(v3, v4)

        g3 = UndirectedGraph()
        v5 = g3.add_node()
        v6 = g3.add_node()
        g3.add_edge(v5, v6)

        with self.assertRaises(NodeMembershipError):
            self.g.remove_node(v3)

        with self.assertRaises(NodeMembershipError):
            self.g.remove_node(v5)
    def test_graph_not_contain_added_arcs_of_other_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        g2 = DirectedGraph()
        for _ in range(n):
            g2.add_node()

        for u in g2:
            for v in g2:
                if u == v:
                    continue
                arc = g2.add_arc(u, v)
                self.assertNotIn(arc, self.g)
class TestDirectedGraph(unittest.TestCase):
    def setUp(self):
        self.g = DirectedGraph()
        self.b = False
        self.currentnode = None
        self.currentarc = None

        self.g2 = DirectedGraph()
        v1 = self.g2.add_node()
        v2 = self.g2.add_node()
        v3 = self.g2.add_node()
        v4 = self.g2.add_node()
        v5 = self.g2.add_node()
        v6 = self.g2.add_node()
        v7 = self.g2.add_node()
        v8 = self.g2.add_node()
        self.nodes = [v1, v2, v3, v4, v5, v6, v7, v8]

        e1 = self.g2.add_arc(v1, v5)
        e2 = self.g2.add_arc(v2, v6)
        e3 = self.g2.add_arc(v3, v7)
        e4 = self.g2.add_arc(v4, v8)
        e5 = self.g2.add_arc(v1, v2)
        e6 = self.g2.add_arc(v2, v3)
        e7 = self.g2.add_arc(v3, v4)
        e8 = self.g2.add_arc(v4, v1)
        self.arcs = [e1, e2, e3, e4, e5, e6, e7, e8]

    def test_directed_graph_is_directed(self):
        self.assertTrue(self.g.directed)

    # ADD NODE TESTS

    def test_add_node_create_directed_node(self):
        v1 = self.g.add_node()
        self.assertIsInstance(v1, DirectedNode)

    def test_add_node_increase_size_by_one(self):
        n = 100
        for i in range(n):
            self.g.add_node()
            self.assertEqual(len(self.g), i + 1)

    def test_add_node_add_the_node_to_nodes_in_that_order(self):
        nodes = [self.g.add_node() for _ in range(100)]
        nodes2 = list(self.g)

        self.assertEqual(nodes, nodes2)

        nodes3 = list(self.g.nodes)
        self.assertEqual(nodes, nodes3)

    def test_graph_contain_added_nodes(self):
        n = 100
        nodes = [self.g.add_node() for _ in range(n)]
        for node in nodes:
            self.assertIn(node, self.g)

    def test_add_node_do_not_increase_arcs(self):
        n = 100
        for _ in range(n):
            self.g.add_node()
            self.assertEqual(len(list(self.g.arcs)), 0)

    def test_add_node_submit_pubsub_msg(self):
        pub.subscribe(self.receive_msg_add_node, str(id(self.g)) + '.add_node')
        v1 = self.g.add_node()
        if not self.b:
            self.assertFalse(True)
        else:
            self.assertEqual(v1, self.currentnode)

    def receive_msg_add_node(self, node, draw):
        self.b = not self.b
        self.assertIsInstance(node, DirectedNode)
        self.assertTrue(draw)
        self.currentnode = node

    # ADD ARCS

    def test_add_arc_create_an_arc(self):
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        e = self.g.add_arc(v1, v2)
        self.assertIsInstance(e, Arc)

    def test_add_arc_increase_size_of_arcs_by_one(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        i = 0

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)
                self.assertEqual(len(list(self.g.arcs)), i + 1)
                self.assertEqual(self.g.nb_arcs, i + 1)
                self.assertEqual(self.g.nb_links, i + 1)
                i += 1

    def test_add_arc_do_not_increase_nodes(self):

        n = 100
        for _ in range(n):
            self.g.add_node()

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)
                self.assertEqual(len(self.g), n)

    def test_graph_contain_added_arcs(self):
        for _ in range(100):
            self.g.add_node()

        arcs = []

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                arc = self.g.add_arc(u, v)
                arcs.append(arc)
                self.assertIn(arc, self.g)

        for arc in arcs:
            self.assertIn(arc, self.g)

    def test_graph_not_contain_added_arcs_of_other_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        g2 = DirectedGraph()
        for _ in range(n):
            g2.add_node()

        for u in g2:
            for v in g2:
                if u == v:
                    continue
                arc = g2.add_arc(u, v)
                self.assertNotIn(arc, self.g)

    def test_add_arc_add_the_arc_to_arcs_in_that_order(self):
        arcs = []
        n = 100
        for _ in range(n):
            self.g.add_node()

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                arcs.append(self.g.add_arc(u, v))

        arcs2 = list(self.g.arcs)

        self.assertEqual(arcs, arcs2)

    def test_add_arc_submit_pubsub_msg(self):
        pub.subscribe(self.receive_msg_add_arc, str(id(self.g)) + '.add_arc')
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        e = self.g.add_arc(v1, v2)
        if not self.b:
            self.assertFalse(True)
        else:
            self.assertEqual(e, self.currentarc)

    def receive_msg_add_arc(self, arc, draw):
        self.b = not self.b
        self.assertIsInstance(arc, Arc)
        self.assertTrue(draw)
        self.currentarc = arc

    def test_add_arc_raise_GraphError_with_same_nodes(self):
        v = self.g.add_node()
        with self.assertRaises(GraphError):
            self.g.add_arc(v, v)

    def test_add_arc_raise_TypeError_with_not_nodes(self):
        v = self.g.add_node()
        with self.assertRaises(TypeError):
            self.g.add_arc(1, v)
        with self.assertRaises(TypeError):
            self.g.add_arc('abc', 2.0)
        with self.assertRaises(TypeError):
            self.g.add_arc(1, 1)
        with self.assertRaises(TypeError):
            self.g.add_arc(v, 'abc')

    def test_add_arc_raise_NodeMembershipError_with_uncontained_nodes(self):
        v = self.g.add_node()
        g2 = DirectedGraph()
        w1 = g2.add_node()
        g3 = UndirectedGraph()
        w2 = g3.add_node()
        with self.assertRaises(NodeMembershipError):
            self.g.add_arc(v, w1)
        with self.assertRaises(NodeMembershipError):
            self.g.add_arc(v, w2)
        with self.assertRaises(NodeMembershipError):
            self.g.add_arc(w1, w2)

    def test_add_arc_raise_LinkError_if_arc_exists(self):
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        self.g.add_arc(v1, v2)
        with self.assertRaises(LinkError):
            self.g.add_arc(v1, v2)
        try:
            self.g.add_arc(v2, v1)
        except LinkError:
            self.assertFalse(True)
        with self.assertRaises(LinkError):
            self.g.add_arc(v1, v2)

    # REMOVE ARC

    def test_remove_arc_decrease_size_of_arcs_by_one(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        arcs = []
        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                e = self.g.add_arc(u, v)
                arcs.append(e)

        for i, e in enumerate(arcs):
            self.g.remove_arc(e)
            self.assertEqual(len(list(self.g.arcs)), (n * (n - 1)) - 1 - i)
            self.assertEqual(self.g.nb_arcs, (n * (n - 1)) - 1 - i)
            self.assertEqual(self.g.nb_links, (n * (n - 1)) - 1 - i)

    def test_remove_arc_do_not_decrease_nodes(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        arcs = []
        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                e = self.g.add_arc(u, v)
                arcs.append(e)

        for i, e in enumerate(arcs):
            self.g.remove_arc(e)
            self.assertEqual(len(self.g), n)

    def test_graph_not_contain_removed_arcs(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        arcs = []
        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                a = self.g.add_arc(u, v)
                arcs.append(a)

        for i, a in enumerate(arcs):
            self.g.remove_arc(a)
            self.assertNotIn(a, self.g)

        # Try after all removal
        for a in arcs:
            self.assertNotIn(a, self.g)

    def test_remove_arc_u_v_does_not_remove_v_u(self):
        u = self.g.add_node()
        v = self.g.add_node()

        e = self.g.add_arc(u, v)
        f = self.g.add_arc(v, u)

        self.g.remove_arc(e)

        self.assertIn(f, self.g)

    def test_remove_arc_remove_the_arc_from_arcs_in_that_order(self):
        arcs = []
        n = 100
        for _ in range(n):
            self.g.add_node()

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                arcs.append(self.g.add_arc(u, v))

        import random
        random.seed(100)
        while len(arcs) > 0:
            arc = random.choice(arcs)
            arcs.remove(arc)
            self.g.remove_arc(arc)
            self.assertEqual(arcs, list(self.g.arcs))

        self.assertEqual(arcs, list(self.g.arcs))

    def test_remove_arc_submit_pubsub_msg(self):
        pub.subscribe(self.receive_msg_remove_arc,
                      str(id(self.g)) + '.remove_arc')
        u = self.g.add_node()
        v = self.g.add_node()
        e = self.g.add_arc(u, v)
        self.g.remove_arc(e)
        if not self.b:
            self.assertFalse(True)
        else:
            self.assertEqual(e, self.currentarc)

    def receive_msg_remove_arc(self, arc, draw):
        self.b = not self.b
        self.assertIsInstance(arc, Arc)
        self.assertTrue(draw)
        self.currentarc = arc

    def test_remove_arc_raise_TypeError_with_not_arc(self):
        u = self.g.add_node()
        v = self.g.add_node()
        self.g.add_arc(u, v)

        with self.assertRaises(TypeError):
            self.g.remove_arc(1)

        with self.assertRaises(TypeError):
            self.g.remove_arc('abc')

        with self.assertRaises(TypeError):
            self.g.remove_arc((u, v))

        with self.assertRaises(TypeError):
            self.g.remove_arc(u)

        with self.assertRaises(TypeError):
            self.g.remove_arc(None)

    def test_remove_arc_raise_LinkMembershipError_with_not_contained_arc(self):
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        self.g.add_arc(v1, v2)

        g2 = DirectedGraph()
        v3 = g2.add_node()
        v4 = g2.add_node()
        e2 = g2.add_arc(v3, v4)

        g3 = UndirectedGraph()
        v5 = g3.add_node()
        v6 = g3.add_node()
        e3 = g3.add_edge(v5, v6)

        with self.assertRaises(LinkMembershipError):
            self.g.remove_arc(e2)

        with self.assertRaises(LinkMembershipError):
            self.g.remove_arc(e3)

    # REMOVE NODE

    def test_remove_node_decrease_size_of_nodes_by_one_empty_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)
        for i, v in enumerate(nodes):
            self.g.remove_node(v)
            self.assertEqual(len(self.g), n - i - 1)
            self.assertEqual(len(list(self.g.nodes)), n - i - 1)

    def test_remove_node_decrease_size_of_nodes_by_one_not_empty_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        for i, v in enumerate(nodes):
            self.g.remove_node(v)
            self.assertEqual(len(self.g), n - i - 1)
            self.assertEqual(len(list(self.g.nodes)), n - i - 1)

    def test_graph_not_contain_removed_nodes_empty_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)
        for v in nodes:
            self.g.remove_node(v)
            self.assertNotIn(v, self.g)

        # After removal
        for v in nodes:
            self.assertNotIn(v, self.g)

    def test_graph_not_contain_removed_nodes_not_empty_graph(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        for v in nodes:
            self.g.remove_node(v)
            self.assertNotIn(v, self.g)

        # After removal
        for v in nodes:
            self.assertNotIn(v, self.g)

    def test_remove_node_remove_the_node_from_nodes_in_that_order_empty_graph(
            self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)
        import random
        random.seed(100)

        while len(nodes) > 0:
            v = random.choice(nodes)
            nodes.remove(v)
            self.g.remove_node(v)
            self.assertEqual(nodes, list(self.g.nodes))
            self.assertEqual(nodes, list(self.g))

        self.assertEqual(nodes, list(self.g.nodes))
        self.assertEqual(nodes, list(self.g))

    def test_remove_node_remove_the_node_from_nodes_in_that_order_not_empty_graph(
            self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        import random
        random.seed(100)

        while len(nodes) > 0:
            v = random.choice(nodes)
            nodes.remove(v)
            self.g.remove_node(v)
            self.assertEqual(nodes, list(self.g.nodes))
            self.assertEqual(nodes, list(self.g))

        self.assertEqual(nodes, list(self.g.nodes))
        self.assertEqual(nodes, list(self.g))

    def test_remove_node_decreases_nb_arcs(self):
        self.g2.remove_node(self.nodes[2])

        self.assertEqual(len(list(self.g2.arcs)), 5)
        self.assertEqual(self.g2.nb_arcs, 5)
        self.assertEqual(self.g2.nb_links, 5)

    def test_remove_node_do_remove_incident_arcs_2(self):
        n = 100
        for _ in range(n):
            self.g.add_node()

        nodes = list(self.g)

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
                self.g.add_arc(u, v)

        import random
        random.seed(100)

        for v in nodes:
            self.g.remove_node(v)

        self.assertEqual(self.g.nb_arcs, 0)
        self.assertEqual(self.g.nb_links, 0)
        self.assertEqual(list(self.g.arcs), [])

    def test_remove_node_do_remove_incident_arcs(self):
        self.g2.remove_node(self.nodes[2])
        indexes = [2, 5, 6]
        for i in range(8):
            if i in indexes:
                self.assertNotIn(self.arcs[i], self.g2)
            else:
                self.assertIn(self.arcs[i], self.g2)

    def test_remove_node_u_remove_edge_u_v_and_v_u(self):
        u = self.g.add_node()
        v = self.g.add_node()

        e = self.g.add_arc(u, v)
        f = self.g.add_arc(v, u)

        self.g.remove_node(u)
        self.assertNotIn(e, self.g)
        self.assertNotIn(f, self.g)

    def test_remove_node_submit_pubsub_msg(self):
        pub.subscribe(self.receive_msg_remove_node,
                      str(id(self.g)) + '.remove_node')
        u = self.g.add_node()
        v = self.g.add_node()
        self.g.add_arc(u, v)
        self.g.remove_node(u)

        self.assertTrue(self.b)
        self.assertEqual(u, self.currentnode)

    def receive_msg_remove_node(self, node, draw):
        self.b = not self.b
        self.assertIsInstance(node, DirectedNode)
        self.assertTrue(draw)
        self.currentnode = node

    def test_remove_node_submit_pubsub_remove_edge_msg(self):
        pub.subscribe(self.receive_msg_remove_arc_from_remove_node,
                      str(id(self.g2)) + '.remove_arc')

        self.g2.remove_node(self.nodes[4])
        self.assertTrue(self.b)
        self.assertEqual(self.arcs[0], self.currentarc)

    def receive_msg_remove_arc_from_remove_node(self, arc, draw):
        self.b = not self.b
        self.assertIsInstance(arc, Arc)
        self.assertFalse(draw)  # !!
        self.currentarc = arc

    def test_remove_node_raise_TypeError_with_not_node(self):
        u = self.g.add_node()
        v = self.g.add_node()
        a = self.g.add_arc(u, v)

        with self.assertRaises(TypeError):
            self.g.remove_node(1)

        with self.assertRaises(TypeError):
            self.g.remove_node('abc')

        with self.assertRaises(TypeError):
            self.g.remove_node((u, v))

        with self.assertRaises(TypeError):
            self.g.remove_node(None)

        with self.assertRaises(TypeError):
            self.g.remove_node(a)

    def test_remove_node_raise_NodeMembershipError_with_not_contained_node(
            self):
        u = self.g.add_node()
        v = self.g.add_node()
        self.g.add_arc(u, v)

        g2 = DirectedGraph()
        v3 = g2.add_node()
        v4 = g2.add_node()
        g2.add_arc(v3, v4)

        g3 = UndirectedGraph()
        v5 = g3.add_node()
        v6 = g3.add_node()
        g3.add_edge(v5, v6)

        with self.assertRaises(NodeMembershipError):
            self.g.remove_node(v3)

        with self.assertRaises(NodeMembershipError):
            self.g.remove_node(v5)
Beispiel #11
0
                    gd.set_label(a, str(flows[a]) + '/' + str(w))
                    gd.set_line_width(a, flows[a])
                if flows[a] == w and v not in d:
                    dp[v] = d[u] + w
                    gd.set_label(v, str(dp[v]))
                    gd.set_color(v, (255, 0, 255))

        d.update(dp)

    gd.pause()

    return d


if __name__ == '__main__':
    g = DirectedGraph()

    v1 = g.add_node()
    v2 = g.add_node()
    v3 = g.add_node()
    v4 = g.add_node()
    v5 = g.add_node()
    v6 = g.add_node()

    weights = {
        v1: {
            v2: 10,
            v5: 5
        },
        v2: {
            v3: 1
Beispiel #12
0
class TestArc(unittest.TestCase):

    def setUp(self):
        self.g = DirectedGraph()
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        v3 = self.g.add_node()
        v4 = self.g.add_node()
        v5 = self.g.add_node()
        v6 = self.g.add_node()
        v7 = self.g.add_node()
        v8 = self.g.add_node()
        self.nodes = [v1, v2, v3, v4, v5, v6, v7, v8]

        self.couples = [(v1, v5), (v2, v6), (v3, v7), (v4, v8), (v1, v2), (v2, v3), (v3, v4), (v4, v1), (v2, v1),
                        (v4, v3)]
        self.arcs = [self.g.add_arc(u, v) for u, v in self.couples]

    def test_arc_is_directed(self):

        for e in self.arcs:
            self.assertTrue(e.directed)

    def test_arc_extremities_are_nodes_defined_by_add_arc_in_the_same_order(self):

        for e, couple in zip(self.arcs, self.couples):
            c1 = list(e.extremities)
            c2 = list(couple)
            self.assertEqual(c1, c2)

    def test_arc_input_node_is_first_node_defined_by_add_arc(self):

        for e, couple in zip(self.arcs, self.couples):
            u, v = couple
            self.assertEqual(e.input_node, u)

    def test_arc_output_node_is_second_node_defined_by_add_arc(self):

        for e, couple in zip(self.arcs, self.couples):
            u, v = couple
            self.assertEqual(e.output_node, v)

    def test_neighbor_return_other_extremity_of_arc(self):

        for e, couple in zip(self.arcs, self.couples):
            u, v = couple
            self.assertEqual(u, e.neighbor(v))
            self.assertEqual(v, e.neighbor(u))

    def test_neighbor_raise_TypeError_with_not_node(self):
        u = self.g.add_node()
        v = self.g.add_node()
        e = self.g.add_arc(u, v)

        e1 = self.arcs[0]

        with self.assertRaises(TypeError):
            e1.neighbor(1)

        with self.assertRaises(TypeError):
            e1.neighbor('abc')

        with self.assertRaises(TypeError):
            e1.neighbor((u, v))

        with self.assertRaises(TypeError):
            e1.neighbor(None)

        with self.assertRaises(TypeError):
            e1.neighbor(e)

    def test_neighbor_raise_LinkError_with_not_extremity(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        u = self.g.add_node()

        g2 = UndirectedGraph()
        w = g2.add_node()

        e1 = self.arcs[0]

        for v in self.nodes:
            if v == v1 or v == v5:
                continue

            with self.assertRaises(LinkError):
                e1.neighbor(v)

        with self.assertRaises(LinkError):
            e1.neighbor(u)

        with self.assertRaises(LinkError):
            e1.neighbor(w)
                gd.set_color(v, (0, 255, 0))
                gd.set_line_width(v, 10)
                gd.pause()
                if u.is_input_neighbor_of(w) and w.is_input_neighbor_of(v):
                    g.add_arc(u, v)
                    gd.pause()
                gd.set_color(v, (0, 0, 0))
                gd.set_line_width(v, NODE_LINE_WIDTH)
            gd.set_color(u, (0, 0, 0))
            gd.set_line_width(u, NODE_LINE_WIDTH)
        gd.set_color(w, (0, 0, 0))
        gd.set_line_width(w, NODE_LINE_WIDTH)


if __name__ == '__main__':
    g = DirectedGraph()

    v1 = g.add_node()
    v2 = g.add_node()
    v3 = g.add_node()
    v4 = g.add_node()
    v5 = g.add_node()
    v6 = g.add_node()

    a1 = g.add_arc(v1, v2)
    a2 = g.add_arc(v1, v5)
    a3 = g.add_arc(v2, v3)
    a4 = g.add_arc(v4, v1)
    a5 = g.add_arc(v4, v3)
    a6 = g.add_arc(v5, v2)
    a7 = g.add_arc(v5, v3)
Beispiel #14
0
class TestDirectedNode(unittest.TestCase):
    def setUp(self):
        self.g = DirectedGraph()
        v1 = self.g.add_node()
        v2 = self.g.add_node()
        v3 = self.g.add_node()
        v4 = self.g.add_node()
        v5 = self.g.add_node()
        v6 = self.g.add_node()
        v7 = self.g.add_node()
        v8 = self.g.add_node()
        self.nodes = [v1, v2, v3, v4, v5, v6, v7, v8]

        self.couples = [(v1, v5), (v2, v6), (v3, v7), (v4, v8), (v1, v2),
                        (v2, v3), (v3, v4), (v4, v1), (v2, v1), (v4, v3)]
        self.arcs = [self.g.add_arc(u, v) for u, v in self.couples]

    def test_add_node_increase_index(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.assertEqual(v1.index + 1, v2.index)
        self.assertEqual(v2.index + 1, v3.index)
        self.assertEqual(v3.index + 1, v4.index)
        self.assertEqual(v4.index + 1, v5.index)
        self.assertEqual(v5.index + 1, v6.index)
        self.assertEqual(v6.index + 1, v7.index)
        self.assertEqual(v7.index + 1, v8.index)

    def test_add_arc_increase_nb_neighbors(self):
        sizes = [3, 3, 3, 3, 1, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

    def test_add_arc_increase_nb_input_neighbors(self):
        sizes = [2, 1, 2, 1, 1, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_input_neighbors, size)

    def test_add_arc_increase_nb_output_neighbors(self):
        sizes = [2, 3, 2, 3, 0, 0, 0, 0]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_output_neighbors, size)

    def test_add_node_do_not_increase_nb_neighbors(self):
        self.g.add_node()
        self.g.add_node()
        self.g.add_node()
        sizes = [3, 3, 3, 3, 1, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

    def test_add_node_do_not_increase_nb_input_neighbors(self):
        self.g.add_node()
        self.g.add_node()
        self.g.add_node()
        sizes = [2, 1, 2, 1, 1, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_input_neighbors, size)

    def test_add_node_do_not_increase_nb_output_neighbors(self):
        self.g.add_node()
        self.g.add_node()
        self.g.add_node()
        sizes = [2, 3, 2, 3, 0, 0, 0, 0]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_output_neighbors, size)

    def test_remove_node_decrease_nb_neighbors_of_neighbors(self):
        self.g.remove_node(self.nodes[0])
        sizes = [2, 3, 2, 0, 1, 1, 1]

        for v, size in zip(self.nodes[1:], sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

        self.g.remove_node(self.nodes[5])
        sizes = [1, 3, 2, 0, 1, 1]

        for v, size in zip(self.nodes[1:5] + self.nodes[6:], sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

    def test_remove_node_decrease_nb_input_neighbors_of_output_neighbors(self):
        self.g.remove_node(self.nodes[0])
        sizes = [0, 2, 1, 0, 1, 1, 1]

        for v, size in zip(self.nodes[1:], sizes):
            self.assertEqual(v.nb_input_neighbors, size)

        self.g.remove_node(self.nodes[5])
        sizes = [0, 2, 1, 0, 1, 1]

        for v, size in zip(self.nodes[1:5] + self.nodes[6:], sizes):
            self.assertEqual(v.nb_input_neighbors, size)

    def test_remove_node_decrease_nb_output_neighbors_of_input_neighbors(self):
        self.g.remove_node(self.nodes[0])
        sizes = [2, 2, 2, 0, 0, 0, 0]

        for v, size in zip(self.nodes[1:], sizes):
            self.assertEqual(v.nb_output_neighbors, size)

        self.g.remove_node(self.nodes[5])
        sizes = [1, 2, 2, 0, 0, 0]

        for v, size in zip(self.nodes[1:5] + self.nodes[6:], sizes):
            self.assertEqual(v.nb_output_neighbors, size)

    def test_remove_arc_decrease_nb_neighbors_of_extremities(self):
        self.g.remove_arc(self.arcs[0])
        sizes = [2, 3, 3, 3, 0, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

        for i, e in enumerate(self.arcs):
            if i != 0:
                self.g.remove_arc(e)

        for v in self.nodes:
            self.assertEqual(len(v), 0)
            self.assertEqual(v.nb_neighbors, 0)

    def test_remove_u_v_decrease_nb_neighbors_of_extremities_except_if_v_u_exists(
            self):
        self.g.remove_arc(self.arcs[4])
        sizes = [3, 3, 3, 3, 1, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(len(v), size)
            self.assertEqual(v.nb_neighbors, size)

    def test_remove_arc_decrease_nb_input_neighbors_of_output_node(self):
        self.g.remove_arc(self.arcs[0])
        sizes = [2, 1, 2, 1, 0, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_input_neighbors, size)

        self.g.remove_arc(self.arcs[4])
        sizes = [2, 0, 2, 1, 0, 1, 1, 1]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_input_neighbors, size)

        for i, e in enumerate(self.arcs):
            if i != 0 and i != 4:
                self.g.remove_arc(e)

        for v in self.nodes:
            self.assertEqual(v.nb_input_neighbors, 0)

    def test_remove_arc_decrease_nb_output_neighbors_of_input_node(self):
        self.g.remove_arc(self.arcs[0])
        sizes = [1, 3, 2, 3, 0, 0, 0, 0]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_output_neighbors, size)

        self.g.remove_arc(self.arcs[4])
        sizes = [0, 3, 2, 3, 0, 0, 0, 0]

        for v, size in zip(self.nodes, sizes):
            self.assertEqual(v.nb_output_neighbors, size)

        for i, e in enumerate(self.arcs):
            if i != 0 and i != 4:
                self.g.remove_arc(e)

        for v in self.nodes:
            self.assertEqual(v.nb_output_neighbors, 0)

    def test_add_arc_add_neighbors(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertTrue(v.is_neighbor_of(u))
                self.assertTrue(u.is_neighbor_of(v))
            elif (v, u) not in self.couples:
                self.assertFalse(v.is_neighbor_of(u))
                self.assertFalse(u.is_neighbor_of(v))

    def test_add_arc_add_input_neighbors(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertTrue(u.is_input_neighbor_of(v))
            elif (v, u) not in self.couples:
                self.assertFalse(u.is_input_neighbor_of(v))

    def test_add_arc_add_output_neighbors(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertTrue(v.is_output_neighbor_of(u))
            elif (v, u) not in self.couples:
                self.assertFalse(v.is_output_neighbor_of(u))

    def test_add_node_do_not_add_neighbors(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertFalse(u.is_neighbor_of(v2))
            self.assertFalse(v.is_neighbor_of(v2))
            self.assertFalse(w.is_neighbor_of(v2))
            self.assertFalse(v2.is_neighbor_of(u))
            self.assertFalse(v2.is_neighbor_of(v))
            self.assertFalse(v2.is_neighbor_of(w))

    def test_add_node_do_not_add_input_neighbors(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertFalse(u.is_input_neighbor_of(v2))
            self.assertFalse(v.is_input_neighbor_of(v2))
            self.assertFalse(w.is_input_neighbor_of(v2))
            self.assertFalse(v2.is_input_neighbor_of(u))
            self.assertFalse(v2.is_input_neighbor_of(v))
            self.assertFalse(v2.is_input_neighbor_of(w))

    def test_add_node_do_not_add_output_neighbors(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertFalse(u.is_output_neighbor_of(v2))
            self.assertFalse(v.is_output_neighbor_of(v2))
            self.assertFalse(w.is_output_neighbor_of(v2))
            self.assertFalse(v2.is_output_neighbor_of(u))
            self.assertFalse(v2.is_output_neighbor_of(v))
            self.assertFalse(v2.is_output_neighbor_of(w))

    def test_remove_node_remove_neighbor_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertFalse(v2.is_neighbor_of(v1))
        self.assertFalse(v4.is_neighbor_of(v1))
        self.assertFalse(v5.is_neighbor_of(v1))
        self.assertFalse(v1.is_neighbor_of(v2))
        self.assertFalse(v1.is_neighbor_of(v4))
        self.assertFalse(v1.is_neighbor_of(v5))

    def test_remove_node_remove_input_neighbor_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertFalse(v2.is_input_neighbor_of(v1))
        self.assertFalse(v4.is_input_neighbor_of(v1))
        self.assertFalse(v1.is_input_neighbor_of(v2))
        self.assertFalse(v1.is_input_neighbor_of(v5))

    def test_remove_node_remove_output_neighbor_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertFalse(v1.is_output_neighbor_of(v2))
        self.assertFalse(v1.is_output_neighbor_of(v4))
        self.assertFalse(v2.is_output_neighbor_of(v1))
        self.assertFalse(v5.is_output_neighbor_of(v1))

    def test_remove_arc_remove_neighbors_of_extremities(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertFalse(v1.is_neighbor_of(v5))
        self.assertFalse(v5.is_neighbor_of(v1))

        self.g.remove_arc(self.arcs[7])
        self.assertFalse(v1.is_neighbor_of(v4))
        self.assertFalse(v4.is_neighbor_of(v1))

    def test_remove_u_v_remove_neighbors_of_extremities_except_if_v_u_exists(
            self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[4])
        self.assertTrue(v1.is_neighbor_of(v2))
        self.assertTrue(v2.is_neighbor_of(v1))

    def test_remove_arc_remove_input_neighbors_of_output_node(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertFalse(v1.is_input_neighbor_of(v5))

        self.g.remove_arc(self.arcs[7])
        self.assertFalse(v4.is_input_neighbor_of(v1))

        self.g.remove_arc(self.arcs[4])
        self.assertFalse(v1.is_input_neighbor_of(v2))

    def test_remove_arc_remove_output_neighbors_of_input_node(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertFalse(v5.is_output_neighbor_of(v1))

        self.g.remove_arc(self.arcs[7])
        self.assertFalse(v1.is_output_neighbor_of(v4))

        self.g.remove_arc(self.arcs[4])
        self.assertFalse(v2.is_output_neighbor_of(v1))

    def test_add_arc_add_neighbors_2(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertIn(v, list(u.neighbors))
                self.assertIn(u, list(v.neighbors))
            elif (v, u) not in self.couples:
                self.assertNotIn(v, list(u.neighbors))
                self.assertNotIn(u, list(v.neighbors))

    def test_add_arc_add_input_neighbors_2(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertIn(u, list(v.input_neighbors))
            elif (v, u) not in self.couples:
                self.assertNotIn(u, list(v.input_neighbors))

    def test_add_arc_add_output_neighbors_2(self):

        for u in self.g:
            for v in self.g:
                if u == v:
                    continue
            if (u, v) in self.couples:
                self.assertIn(v, list(u.output_neighbors))
            elif (v, u) not in self.couples:
                self.assertNotIn(v, list(u.output_neighbors))

    def test_add_node_do_not_add_neighbors_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertNotIn(u, list(v2.neighbors))
            self.assertNotIn(v, list(v2.neighbors))
            self.assertNotIn(w, list(v2.neighbors))
            self.assertNotIn(v2, list(u.neighbors))
            self.assertNotIn(v2, list(v.neighbors))
            self.assertNotIn(v2, list(w.neighbors))

    def test_add_node_do_not_add_input_neighbors_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertNotIn(u, list(v2.input_neighbors))
            self.assertNotIn(v, list(v2.input_neighbors))
            self.assertNotIn(w, list(v2.input_neighbors))
            self.assertNotIn(v2, list(u.input_neighbors))
            self.assertNotIn(v2, list(v.input_neighbors))
            self.assertNotIn(v2, list(w.input_neighbors))

    def test_add_node_do_not_add_output_neighbors_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for v2 in self.nodes:
            self.assertNotIn(u, list(v2.output_neighbors))
            self.assertNotIn(v, list(v2.output_neighbors))
            self.assertNotIn(w, list(v2.output_neighbors))
            self.assertNotIn(v2, list(u.output_neighbors))
            self.assertNotIn(v2, list(v.output_neighbors))
            self.assertNotIn(v2, list(w.output_neighbors))

    def test_remove_node_remove_neighbor_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertNotIn(v2, list(v1.neighbors))
        self.assertNotIn(v4, list(v1.neighbors))
        self.assertNotIn(v5, list(v1.neighbors))
        self.assertNotIn(v1, list(v2.neighbors))
        self.assertNotIn(v1, list(v4.neighbors))
        self.assertNotIn(v1, list(v5.neighbors))

    def test_remove_node_remove_input_neighbor_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertNotIn(v2, list(v1.input_neighbors))
        self.assertNotIn(v4, list(v1.input_neighbors))
        self.assertNotIn(v1, list(v2.input_neighbors))
        self.assertNotIn(v1, list(v5.input_neighbors))

    def test_remove_node_remove_output_neighbor_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_node(v1)
        self.assertNotIn(v1, list(v2.output_neighbors))
        self.assertNotIn(v1, list(v4.output_neighbors))
        self.assertNotIn(v2, list(v1.output_neighbors))
        self.assertNotIn(v5, list(v1.output_neighbors))

    def test_remove_arc_remove_neighbors_of_extremities_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertNotIn(v1, list(v5.neighbors))
        self.assertNotIn(v5, list(v1.neighbors))

        self.g.remove_arc(self.arcs[7])
        self.assertNotIn(v1, list(v4.neighbors))
        self.assertNotIn(v4, list(v1.neighbors))

    def test_remove_u_v_remove_neighbors_of_extremities_except_if_v_u_exists_2(
            self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[4])
        self.assertIn(v1, list(v2.neighbors))
        self.assertIn(v2, list(v1.neighbors))

    def test_remove_arc_remove_input_neighbors_of_output_node_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertNotIn(v1, list(v5.input_neighbors))

        self.g.remove_arc(self.arcs[7])
        self.assertNotIn(v4, list(v1.input_neighbors))

        self.g.remove_arc(self.arcs[4])
        self.assertNotIn(v1, list(v2.input_neighbors))

    def test_remove_arc_remove_output_neighbors_of_input_node_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        self.g.remove_arc(self.arcs[0])
        self.assertNotIn(v5, list(v1.output_neighbors))

        self.g.remove_arc(self.arcs[7])
        self.assertNotIn(v1, list(v4.output_neighbors))

        self.g.remove_arc(self.arcs[4])

    def test_nb_neighbors_does_not_equal_nb_incident_arcs_iff_u_v_and_v_u_exists(
            self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        for v in [v5, v6, v7, v8]:
            self.assertEqual(v.nb_neighbors, len(list(v.incident_arcs)))
        for v in [v1, v2, v3, v4]:
            self.assertNotEqual(v.nb_neighbors, len(list(v.incident_arcs)))

    def test_nb_input_arc_plus_nb_output_arc_equal_nb_incident_arcs(self):
        for v in self.nodes:
            self.assertEqual(
                len(list(v.input_arcs)) + len(list(v.output_arcs)),
                len(list(v.incident_arcs)))

    def test_add_arc_add_incident_arc(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != u and w != v:
                    self.assertFalse(w.is_incident_to(a))
                else:
                    self.assertTrue(w.is_incident_to(a))

    def test_add_arc_add_input_arc_of_output_node(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != v:
                    self.assertFalse(w.is_input_arc(a))
                else:
                    self.assertTrue(w.is_input_arc(a))

    def test_add_arc_add_output_arc_of_input_node(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != u:
                    self.assertFalse(w.is_output_arc(a))
                else:
                    self.assertTrue(w.is_output_arc(a))

    def test_new_node_are_not_incident_to_previous_arcs(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for e in self.arcs:
            self.assertFalse(u.is_incident_to(e))
            self.assertFalse(v.is_incident_to(e))
            self.assertFalse(w.is_incident_to(e))

    def test_previous_arcs_are_not_input_arcs_of_new_node(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for a in self.arcs:
            self.assertFalse(u.is_input_arc(a))
            self.assertFalse(v.is_input_arc(a))
            self.assertFalse(w.is_input_arc(a))

    def test_previous_arcs_are_not_output_arcs_of_new_node(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for a in self.arcs:
            self.assertFalse(u.is_output_arc(a))
            self.assertFalse(v.is_output_arc(a))
            self.assertFalse(w.is_output_arc(a))

    def test_remove_node_remove_incident_arcs_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertFalse(v2.is_incident_to(e5))
        self.assertFalse(v2.is_incident_to(e9))
        self.assertFalse(v5.is_incident_to(e1))
        self.assertFalse(v4.is_incident_to(e8))

    def test_remove_node_remove_input_arcs_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertFalse(v2.is_input_arc(e5))
        self.assertFalse(v5.is_input_arc(e1))

    def test_remove_node_remove_output_arcs_of_neighbors(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertFalse(v2.is_input_arc(e9))
        self.assertFalse(v4.is_input_arc(e8))

    def test_remove_arc_remove_incident_arcs_extremities(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertFalse(v1.is_incident_to(e1))
        self.assertFalse(v5.is_incident_to(e1))

        self.g.remove_arc(e8)
        self.assertFalse(v1.is_incident_to(e8))
        self.assertFalse(v4.is_incident_to(e8))

        self.g.remove_arc(e5)
        self.assertFalse(v1.is_incident_to(e5))
        self.assertFalse(v2.is_incident_to(e5))

    def test_remove_arc_remove_input_arc_of_output_node(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertFalse(v5.is_input_arc(e1))

        self.g.remove_arc(e8)
        self.assertFalse(v4.is_input_arc(e8))

        self.g.remove_arc(e5)
        self.assertFalse(v2.is_input_arc(e5))

    def test_remove_arc_remove_output_arc_of_input_node(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertFalse(v1.is_output_arc(e1))

        self.g.remove_arc(e8)
        self.assertFalse(v1.is_output_arc(e8))

        self.g.remove_arc(e5)
        self.assertFalse(v1.is_output_arc(e5))

    def test_add_arc_add_incident_arc_2(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != u and w != v:
                    self.assertNotIn(a, list(w.incident_arcs))
                else:
                    self.assertIn(a, list(w.incident_arcs))

    def test_add_arc_add_input_arc_of_output_node_2(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != v:
                    self.assertNotIn(a, list(w.input_arcs))
                else:
                    self.assertIn(a, list(w.input_arcs))

    def test_add_arc_add_output_arc_of_input_node_2(self):

        for a, couple in zip(self.arcs, self.couples):
            u, v = couple
            for w in self.nodes:
                if w != u:
                    self.assertNotIn(a, list(w.output_arcs))
                else:
                    self.assertIn(a, list(w.output_arcs))

    def test_new_node_are_not_incident_to_previous_arcs_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for e in self.arcs:
            self.assertNotIn(e, list(u.incident_arcs))
            self.assertNotIn(e, list(v.incident_arcs))
            self.assertNotIn(e, list(w.incident_arcs))

    def test_previous_arcs_are_not_input_arcs_of_new_node_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for a in self.arcs:
            self.assertNotIn(a, list(u.input_arcs))
            self.assertNotIn(a, list(v.input_arcs))
            self.assertNotIn(a, list(w.input_arcs))

    def test_previous_arcs_are_not_output_arcs_of_new_node_2(self):
        u = self.g.add_node()
        v = self.g.add_node()
        w = self.g.add_node()

        for a in self.arcs:
            self.assertNotIn(a, list(u.output_arcs))
            self.assertNotIn(a, list(v.output_arcs))
            self.assertNotIn(a, list(w.output_arcs))

    def test_remove_node_remove_incident_arcs_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertNotIn(e5, list(v2.incident_arcs))
        self.assertNotIn(e9, list(v2.incident_arcs))
        self.assertNotIn(e1, list(v5.incident_arcs))
        self.assertNotIn(e8, list(v4.incident_arcs))

    def test_remove_node_remove_input_arcs_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertNotIn(e5, list(v2.input_arcs))
        self.assertNotIn(e1, list(v5.input_arcs))

    def test_remove_node_remove_output_arcs_of_neighbors_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_node(v1)
        self.assertNotIn(e9, list(v2.input_arcs))
        self.assertNotIn(e8, list(v4.input_arcs))

    def test_remove_arc_remove_incident_arcs_extremities_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertNotIn(e1, list(v1.incident_arcs))
        self.assertNotIn(e1, list(v5.incident_arcs))

        self.g.remove_arc(e8)
        self.assertNotIn(e8, list(v1.incident_arcs))
        self.assertNotIn(e8, list(v4.incident_arcs))

        self.g.remove_arc(e5)
        self.assertNotIn(e5, list(v1.incident_arcs))
        self.assertNotIn(e5, list(v2.incident_arcs))

    def test_remove_arc_remove_input_arc_of_output_node_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertNotIn(e1, list(v5.input_arcs))

        self.g.remove_arc(e8)
        self.assertNotIn(e8, list(v4.input_arcs))

        self.g.remove_arc(e5)
        self.assertNotIn(e5, list(v2.input_arcs))

    def test_remove_arc_remove_output_arc_of_input_node_2(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        self.g.remove_arc(e1)
        self.assertNotIn(e1, list(v1.output_arcs))

        self.g.remove_arc(e8)
        self.assertNotIn(e8, list(v1.output_arcs))

        self.g.remove_arc(e5)
        self.assertNotIn(e5, list(v1.output_arcs))

    def test_add_arc_add_incident_arc_3(self):

        c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 = self.couples
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs
        for e, couple in zip([e1, e2, e3, e4, e6, e8],
                             [c1, c2, c3, c4, c6, c8]):
            u, v = couple
            self.assertEqual(e, u.get_incident_arc(v))
            self.assertEqual(e, v.get_incident_arc(u))

    def test_get_incident_arc_return_input_arc_by_default(self):
        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        self.assertEqual(e5, v2.get_incident_arc(v1))
        self.assertEqual(e9, v1.get_incident_arc(v2))

    def test_add_arc_add_input_arc_3(self):
        for e, couple in zip(self.arcs, self.couples):
            u, v = couple
            self.assertEqual(e, v.get_input_arc(u))

    def test_add_arc_add_output_arc_3(self):
        for e, couple in zip(self.arcs, self.couples):
            u, v = couple
            self.assertEqual(e, u.get_output_arc(v))

    def test_get_incident_arc_raise_TypeError_with_not_node(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        g2 = UndirectedGraph()
        v = g2.add_node()

        with self.assertRaises(TypeError):
            v1.get_incident_arc(1)

        with self.assertRaises(TypeError):
            v1.get_incident_arc('abc')

        with self.assertRaises(TypeError):
            v1.get_incident_arc((v1, v2))

        with self.assertRaises(TypeError):
            v1.get_incident_arc(e5)

        with self.assertRaises(TypeError):
            v1.get_incident_arc(None)

        with self.assertRaises(TypeError):
            v1.get_incident_arc(v)

    def test_get_input_arc_raise_TypeError_with_not_node(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        g2 = UndirectedGraph()
        v = g2.add_node()

        with self.assertRaises(TypeError):
            v1.get_input_arc(1)

        with self.assertRaises(TypeError):
            v1.get_input_arc('abc')

        with self.assertRaises(TypeError):
            v1.get_input_arc((v1, v2))

        with self.assertRaises(TypeError):
            v1.get_input_arc(e5)

        with self.assertRaises(TypeError):
            v1.get_input_arc(None)

        with self.assertRaises(TypeError):
            v1.get_input_arc(v)

    def test_get_output_arc_raise_TypeError_with_not_node(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        g2 = UndirectedGraph()
        v = g2.add_node()

        with self.assertRaises(TypeError):
            v1.get_output_arc(1)

        with self.assertRaises(TypeError):
            v1.get_output_arc('abc')

        with self.assertRaises(TypeError):
            v1.get_output_arc((v1, v2))

        with self.assertRaises(TypeError):
            v1.get_output_arc(e5)

        with self.assertRaises(TypeError):
            v1.get_output_arc(None)

        with self.assertRaises(TypeError):
            v1.get_output_arc(v)

    def test_get_incident_arc_raise_NodeError_with_not_neighbor(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        g2 = DirectedGraph()
        v = g2.add_node()

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v3)

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v)

    def test_get_input_arc_raise_NodeError_with_not_neighbor(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        g2 = DirectedGraph()
        v = g2.add_node()

        with self.assertRaises(NodeError):
            v1.get_input_arc(v3)

        with self.assertRaises(NodeError):
            v1.get_input_arc(v5)

        with self.assertRaises(NodeError):
            v1.get_input_arc(v)

    def test_get_output_arc_raise_NodeError_with_not_neighbor(self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        g2 = DirectedGraph()
        v = g2.add_node()

        with self.assertRaises(NodeError):
            v1.get_output_arc(v3)

        with self.assertRaises(NodeError):
            v1.get_output_arc(v4)

        with self.assertRaises(NodeError):
            v1.get_output_arc(v)

    def test_get_incident_arc_raise_NodeError_with_not_neighbor_due_to_remove_arc(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        self.g.remove_arc(e1)

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v5)

        with self.assertRaises(NodeError):
            v5.get_incident_arc(v1)

    def test_get_incident_arc_do_notraise_NodeError_due_to_remove_u_v_if_v_u_exists(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        self.g.remove_arc(e5)

        try:
            v1.get_incident_arc(v2)
        except NodeError:
            self.assertFalse(True)

    def test_get_input_arc_raise_NodeError_with_not_neighbor_due_to_remove_arc(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        self.g.remove_arc(e1)

        with self.assertRaises(NodeError):
            v5.get_input_arc(v1)

    def test_get_output_arc_raise_NodeError_with_not_neighbor_due_to_remove_arc(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes
        e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 = self.arcs

        self.g.remove_arc(e1)

        with self.assertRaises(NodeError):
            v5.get_output_arc(v1)

    def test_get_incident_arc_raise_NodeError_with_not_neighbor_due_to_remove_node(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        self.g.remove_node(v5)

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v5)

        self.g.remove_node(v2)

        with self.assertRaises(NodeError):
            v1.get_incident_arc(v2)

    def test_get_input_arc_raise_NodeError_with_not_neighbor_due_to_remove_node(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        self.g.remove_node(v2)

        with self.assertRaises(NodeError):
            v1.get_input_arc(v2)

    def test_get_output_arc_raise_NodeError_with_not_neighbor_due_to_remove_node(
            self):

        v1, v2, v3, v4, v5, v6, v7, v8 = self.nodes

        self.g.remove_node(v5)

        with self.assertRaises(NodeError):
            v1.get_output_arc(v5)

        self.g.remove_node(v2)

        with self.assertRaises(NodeError):
            v1.get_output_arc(v2)