def postorder_unrooted(graph):
 allnodes = set(graph.nodes())
 visited = set()
 orders = []
 def dfs_walk(node):
  visited.add(node)
  for succ in graph.successors(node):
   if (not (succ in visited)):
    dfs_walk(succ)
  orders[(-1)].append(node)
 while (len(allnodes) > len(visited)):
  remaining = (allnodes - visited)
  root = remaining.pop()
  orders.append([])
  dfs_walk(root)
 return orders

def postorder_unrooted ( graph ) : allnodes = set ( graph . nodes ( ) ) visited = set ( ) orders = [ ] def dfs_walk ( node ) : visited . add ( node ) for succ in graph . successors ( node ) : if ( not ( succ in visited ) ) : dfs_walk ( succ ) orders [ ( - 1 ) ] . append ( node ) while ( len ( allnodes ) > len ( visited ) ) : remaining = ( allnodes - visited ) root = remaining . pop ( ) orders . append ( [ ] ) dfs_walk ( root ) return orders
unrooted post-order traversal of a graph .