def undirected_connected_components(gr):
 if gr.DIRECTED:
  raise Exception('This method works only with a undirected graph')
 explored = set([])
 con_components = []
 for node in gr.nodes():
  if (node not in explored):
   reachable_nodes = BFS(gr, node)
   con_components.append(reachable_nodes)
   explored |= reachable_nodes
 return con_components

def undirected_connected_components ( gr ) : if gr . DIRECTED : raise Exception ( 'This method works only with a undirected graph' ) explored = set ( [ ] ) con_components = [ ] for node in gr . nodes ( ) : if ( node not in explored ) : reachable_nodes = BFS ( gr , node ) con_components . append ( reachable_nodes ) explored |= reachable_nodes return con_components
returns a list of connected components in an undirected graph .