def get_connected_component(self, component=None): if component==None: component = Component() component.add_vertex(self) for neighbour in self.neighbours: edge = set([self, neighbour]) if edge not in component.edges: component.add_edge(edge) if neighbour not in component.vertices: component = neighbour.get_connected_component(component) return component