def test_copy(self): """ Test the graph copy function to ensure a complete copy of the graph is made while preserving vertices and edges. """ vertices = [Vertex() for i in range(6)] edges = [ Edge(vertices[0], vertices[1]), Edge(vertices[1], vertices[2]), Edge(vertices[2], vertices[3]), Edge(vertices[3], vertices[4]), Edge(vertices[4], vertices[5]), ] graph = Graph() for vertex in vertices: graph.addVertex(vertex) for edge in edges: graph.addEdge(edge) graph2 = graph.copy() for vertex in graph.vertices: self.assertTrue(graph2.hasVertex(vertex)) for v1 in graph.vertices: for v2 in v1.edges: self.assertTrue(graph2.hasEdge(v1, v2)) self.assertTrue(graph2.hasEdge(v2, v1)) self.assertTrue(graph2.isIsomorphic(graph)) self.assertTrue(graph.isIsomorphic(graph2))
def test_copy(self): """ Test the graph copy function to ensure a complete copy of the graph is made while preserving vertices and edges. """ vertices = [Vertex() for i in range(6)] edges = [ Edge(vertices[0], vertices[1]), Edge(vertices[1], vertices[2]), Edge(vertices[2], vertices[3]), Edge(vertices[3], vertices[4]), Edge(vertices[4], vertices[5]), ] graph = Graph() for vertex in vertices: graph.addVertex(vertex) for edge in edges: graph.addEdge(edge) graph2 = graph.copy() for vertex in graph.vertices: self.assertTrue(graph2.hasVertex(vertex)) for v1 in graph.vertices: for v2 in v1.edges: self.assertTrue(graph2.hasEdge(v1, v2)) self.assertTrue(graph2.hasEdge(v2, v1)) self.assertTrue(graph2.isIsomorphic(graph)) self.assertTrue(graph.isIsomorphic(graph2))
def copy(self, deep=False): """ Create a copy of the current graph. If `deep` is ``True``, a deep copy is made: copies of the vertices and edges are used in the new graph. If `deep` is ``False`` or not specified, a shallow copy is made: the original vertices and edges are used in the new graph. """ g = Graph.copy(self, deep) other = Fragment(vertices=g.vertices) return other