code: def sets(G, top_nodes=None):
      """Returns bipartite node sets of graph G.
  
      Raises an exception if the graph is not bipartite or if the input
      graph is disconnected and thus more than one valid solution exists.
      See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
      for further details on how bipartite graphs are handled in NetworkX.
  
      Parameters
      ----------
      G : NetworkX graph
  
      top_nodes : container, optional
        Container with all nodes in one bipartite node set. If not supplied
        it will be computed. But if more than one solution exists an exception
        will be raised.
  
      Returns
      -------
      X : set
        Nodes from one side of the bipartite graph.
      Y : set
        Nodes from the other side.
  
      Raises
      ------
      AmbiguousSolution
        Raised if the input bipartite graph is disconnected and no container
        with all nodes in one bipartite set is provided. When determining
        the nodes in each bipartite set more than one valid solution is
        possible if the input graph is disconnected.
      NetworkXError
        Raised if the input graph is not bipartite.
  
      Examples
      --------
      >>> from networkx.algorithms import bipartite
      >>> G = nx.path_graph(4)
      >>> X, Y = bipartite.sets(G)
      >>> list(X)
      [0, 2]
      >>> list(Y)
      [1, 3]
  
      See Also
      --------
      color
  
      """
      if G.is_directed():
          is_connected = nx.is_weakly_connected
      else:
          is_connected = nx.is_connected
      if top_nodes is not None:
          X = set(top_nodes)
          Y = set(G) - X
      else:
          if not is_connected(G):
              msg = "Disconnected graph: Ambiguous solution for bipartite sets."
              raise nx.AmbiguousSolution(msg)
          c = color(G)
          X = {n for n, is_top in c.items() if is_top}
          Y = {n for n, is_top in c.items() if not is_top}
      return (X, Y)
  
file_path: networkx/networkx/algorithms/bipartite/basic.py
imports: import networkx
  from networkx.algorithms.components import connected_components
  from networkx.exception import AmbiguousSolution
label_desc: Use image node_id 4 for calling a global function with example usage: sets(G, top_nodes) and returns: X,  Y