Esempio n. 1
0
    def add_edge(self, v1, v2, weight=0):
        """Adds an edge to this graph only if that edge does not already
        exist. In our interpretation of the program, this means an undirected
        edge. Furthermore, only edges containing vertices that actually
        exist in the graph are acceptable.

        Note that since this is an undirected graph, adding an edge with
        either vertice as v1 or v2 will be the same edge. Adding an edge is
        also idempotent, and will also connect the vertices.

        Args:
            v1(str): The name of one of the vertices in the edge
            v2(str): The name of the other vertice in the edge.
            weight(int): the weight of the graph, which is non-negative
        Raises:
            ValueError: if the edge in question contains vertices that do
                not exist in the graph
            ValueError: if the weight is negative"""
        if v1 not in self.__vertices or v2 not in self.__vertices:
            raise ValueError("Vertice contained in edge not in graph")
        vertice_one = self.__vertices[v1]
        vertice_two = self.__vertices[v2]
        edge = Edge(vertice_one, vertice_two, weight)
        edge.connect_vertices()
        self.__edges[self.__edge_key.format(v1, v2)] = edge
Esempio n. 2
0
    def test_edge_disconnect_idempotency(self):
        vertice1 = Vertice("one")
        vertice2 = Vertice("two")

        edge = Edge(vertice1, vertice2)
        edge.connect_vertices()

        self.assertTrue(edge.connected())
        edge.disconnect_vertices()
        self.assertFalse(edge.connected())
        edge.disconnect_vertices()
        self.assertFalse(edge.connected())
Esempio n. 3
0
    def test_single_direction_connection_to_both_connected(self):
        vertice1 = Vertice("one")
        vertice2 = Vertice("two")

        vertice1.add_neighbor(vertice2)
        edge = Edge(vertice1, vertice2)
        self.assertTrue(edge.connected())

        self.assertTrue(vertice1.is_neighbor(vertice2))
        self.assertFalse(vertice2.is_neighbor(vertice1))
        edge.connect_vertices()
        self.assertTrue(vertice1.is_neighbor(vertice2))
        self.assertTrue(vertice2.is_neighbor(vertice1))
Esempio n. 4
0
    def test_single_direction_connection_both_connected_other_way(self):
        """This should be equivalent as the previous test"""
        vertice1 = Vertice("one")
        vertice2 = Vertice("two")

        vertice2.add_neighbor(vertice1)
        edge = Edge(vertice1, vertice2)
        self.assertTrue(edge.connected())

        self.assertFalse(vertice1.is_neighbor(vertice2))
        self.assertTrue(vertice2.is_neighbor(vertice1))
        edge.connect_vertices()
        self.assertTrue(vertice1.is_neighbor(vertice2))
        self.assertTrue(vertice2.is_neighbor(vertice1))
Esempio n. 5
0
    def test_edge_linking_idempotency(self):
        """Multiple connections should be simply idempotent."""

        vertice1 = Vertice("one")
        vertice2 = Vertice("two")

        edge = Edge(vertice1, vertice2)

        self.assertFalse(edge.connected())
        edge.connect_vertices()
        self.assertTrue(edge.connected())
        edge.connect_vertices()
        self.assertTrue(edge.connected())
        self.assertTrue(vertice1.is_neighbor(vertice2))
        self.assertTrue(vertice2.is_neighbor(vertice1))
Esempio n. 6
0
    def test_simple_connect(self):
        """Ensure that the connection function works with simple connections"""

        vertice1 = Vertice("one")
        vertice2 = Vertice("two")
        edge = Edge(vertice1, vertice2)

        self.assertFalse(vertice1.is_neighbor(vertice2))
        self.assertFalse(vertice2.is_neighbor(vertice1))
        self.assertFalse(edge.connected())

        edge.connect_vertices()
        self.assertTrue(edge.connected())
        self.assertTrue(vertice1.is_neighbor(vertice2))
        self.assertTrue(vertice2.is_neighbor(vertice1))