RosettaCodeData/Task/Percolation-Mean-cluster-density/Mathematica/percolation-mean-cluster-density.math
2023-07-01 13:44:08 -04:00

10 lines
493 B
Text

(*Calculate C_n / n^2 for n=1000, 2000, ..., 10 000*)
In[1]:= Table[N[Max@MorphologicalComponents[
RandomVariate[BernoulliDistribution[.5], {n, n}],
CornerNeighbors -> False]/n^2], {n, 10^3, 10^4, 10^3}]
(*Find the average*)
In[2]:= % // MeanAround
(*Show a 15x15 matrix with each cluster given an incrementally higher number, Colorize instead of MatrixForm creates an image*)
In[3]:= MorphologicalComponents[RandomChoice[{0, 1}, {15, 15}], CornerNeighbors -> False] // MatrixForm