def generate_involutions(n):
 idx = list(range(n))
 for p in permutations(idx):
  for i in idx:
   if (p[p[i]] != i):
    break
  else:
   (yield p)

def generate_involutions ( n ) : idx = list ( range ( n ) ) for p in permutations ( idx ) : for i in idx : if ( p [ p [ i ] ] != i ) : break else : ( yield p )
generates involutions .