parameters: (G)
docstring: Check a given graph is a lobster graph or not
  
      Removing leaf nodes twice:
  
      lobster -> caterpillar -> path
docstring_summary: Check a given graph is a lobster graph or not
docstring_tokens: Check a given graph is a lobster graph or not
function: def is_lobster_graph(G):
    """
      Check a given graph is a lobster graph or not
  
      Removing leaf nodes twice:
  
      lobster -> caterpillar -> path
  
    """
    ### Check if G is a tree
    if nx.is_tree(G):
      # import pdb; pdb.set_trace()
      ### Check if G is a path after removing leaves twice
      leaves = [n for n, d in G.degree() if d == 1]
      G.remove_nodes_from(leaves)
  
      leaves = [n for n, d in G.degree() if d == 1]
      G.remove_nodes_from(leaves)
  
      num_nodes = len(G.nodes())
      num_degree_one = [d for n, d in G.degree() if d == 1]
      num_degree_two = [d for n, d in G.degree() if d == 2]
  
      if sum(num_degree_one) == 2 and sum(num_degree_two) == 2 * (num_nodes - 2):
        return True
      elif sum(num_degree_one) == 0 and sum(num_degree_two) == 0:
        return True
      else:
        return False
    else:
      return False
function_tokens: def is_lobster_graph ( G ) : ### Check if G is a tree if nx . is_tree ( G ) : # import pdb; pdb.set_trace() ### Check if G is a path after removing leaves twice leaves = [ n for n , d in G . degree ( ) if d == 1 ] G . remove_nodes_from ( leaves ) leaves = [ n for n , d in G . degree ( ) if d == 1 ] G . remove_nodes_from ( leaves ) num_nodes = len ( G . nodes ( ) ) num_degree_one = [ d for n , d in G . degree ( ) if d == 1 ] num_degree_two = [ d for n , d in G . degree ( ) if d == 2 ] if sum ( num_degree_one ) == 2 and sum ( num_degree_two ) == 2 * ( num_nodes - 2 ) : return True elif sum ( num_degree_one ) == 0 and sum ( num_degree_two ) == 0 : return True else : return False else : return False