fast rewiring algorithm