The algorithm of Swendsen & Wang (1987) forms clusters of neighbouring pixels, then updates all of the labels within a cluster to the same value. When simulating from the prior, such as a Potts model without an external field, this algorithm is very efficient.

1 2 |

`beta` |
The inverse temperature parameter of the Potts model. |

`k` |
The number of unique labels. |

`neighbors` |
A matrix of all neighbors in the lattice, one row per pixel. |

`blocks` |
A list of pixel indices, dividing the lattice into independent blocks. |

`slices` |
Deprecated. |

`niter` |
The number of iterations of the algorithm to perform. |

A list containing the following elements:

`alloc`

An n by k matrix containing the number of times that pixel i was allocated to label j.

`z`

An

`(n+1)`

by k matrix containing the final sample from the Potts model after niter iterations of Swendsen-Wang.`sum`

An

`niter`

by 1 matrix containing the sum of like neighbors, i.e. the sufficient statistic of the Potts model, at each iteration.

Swendsen, R. H. and Wang, J.-S. 1987 "Nonuniversal critical dynamics in Monte Carlo simulations" *Physical Review Letters* **58**(2), 86–88, DOI: 10.1103/PhysRevLett.58.86

1 2 3 4 5 6 |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.