def from_prufer_sequence(sequence):
 n = (len(sequence) + 2)
 degree = Counter(chain(sequence, range(n)))
 T = nx.empty_graph(n)
 not_orphaned = set()
 index = u = min((k for k in range(n) if (degree[k] == 1)))
 for v in sequence:
  T.add_edge(u, v)
  not_orphaned.add(u)
  degree[v] -= 1
  if ((v < index) and (degree[v] == 1)):
   u = v
  else:
   index = u = min((k for k in range((index + 1), n) if (degree[k] == 1)))
 orphans = (set(T) - not_orphaned)
 (u, v) = orphans
 T.add_edge(u, v)
 return T

def from_prufer_sequence ( sequence ) : n = ( len ( sequence ) + 2 ) degree = Counter ( chain ( sequence , range ( n ) ) ) T = nx . empty_graph ( n ) not_orphaned = set ( ) index = u = min ( ( k for k in range ( n ) if ( degree [ k ] == 1 ) ) ) for v in sequence : T . add_edge ( u , v ) not_orphaned . add ( u ) degree [ v ] -= 1 if ( ( v < index ) and ( degree [ v ] == 1 ) ) : u = v else : index = u = min ( ( k for k in range ( ( index + 1 ) , n ) if ( degree [ k ] == 1 ) ) ) orphans = ( set ( T ) - not_orphaned ) ( u , v ) = orphans T . add_edge ( u , v ) return T
returns the tree corresponding to the given prüfer sequence .