[M4] Percolation (6 pts) [00P8]
- October 14, 2026
In this implementation challenge you will explore an interesting application of a union-find structure.
Introduction [00P9]
- October 14, 2026
Percolating on 25x25 grid...
. . . X . . . . . . .
. . . . X . . . . . .
. . . . . X X . . . . . . .
. . X X . . . . .
. . . . . . X . . . . . .
. . . . X . . . . . . . . . .
. . . . . . X . . . . . .
. . . . . X . . . . .
. . . . . . X X X . . . X X X . .
. . . . . . . . . . X . X X . X . . .
. . . . . . . X X . . X . . X . .
. . . . . . . . . X X X X . X . .
. . . . . . . . . X
. . . . . . . . . . . . X X .
. . . . . . . . . . . . . X
. . . . . . . X
. . . . . . . . X X .
. . . . . . . . . . X X X X .
. . . . X . . .
. . . . . . . . . . X . .
. . . . . . X . . . .
. . . . . . . . . X . . . . .
. . . . . . . . . X X . .
. . . . . . . . . X . .
. . . . . . . X .
Percolation achieved with 352 cells opened (56.32%)
Shortest path length = 48Imagine an n \times n square grid, where each cell is connected to its four neighbors to the north, east, south, and west. Each cell can be either “open” or “blocked”. Paths through the grid can only travel through open cells. We say the grid percolates if there is a path from some open cell in the top row all the way to some open cell in the bottom row.
Now, carry out the following process: the cells all start out blocked; at each iteration, randomly pick a blocked cell and open it. Keep iterating until the grid percolates. It is a curious fact that on average, the grid will percolate after 59.2% of the cells are opened—no matter what size the grid is. (The larger the grid, the more precise the percentage will be.) Moreover, this percentage is a sort of “phase transition”: if you take a large grid and randomly open fewer than 59.2% of its cells, then it probably will not percolate; open any more than 59.2%, and it probably will.
Requirements [00PA]
- October 14, 2026
Write a program (using a language of your choice) which will explore this percolation model, producing output like the above. That is, your program should carry out the above random cell-opening procedure until the grid percolates, and then display the resulting grid along with the shortest percolating path.
Note that in the given sample output shown above, open cells are displayed as a space, blocked cells as a dot, and open cells through which the shortest percolating path passes as an X. Alternate columns are left blank just to make the grid look relatively square. Your output does not have to look exactly like this.
To receive credit for this problem:
- Your program runs correctly and can produce output like the above.
- Your program runs within a few seconds for grids up to 500 \times 500 in size.
- Your implementation of a generic union-find data structure is in a separate file from the rest of your code (or a separate class within the same file).
Hints [00PB]
- October 14, 2026
- You’ll want to implement your own union-find data structure, as discussed in class. Make a generic union-find data structure which supports the following operations:
new(n: int): create a new union-find structure with n nodes, numbered 0 \dots n-1, with each node conceptually starting out in its own singleton set.find(u: int) -> int: return the “representative” or root node of the set containing node u.union(u: int, v: int): union the sets containing nodes u and v, if they are not already in the same set.connected(u: int, v: int) -> bool: test whether two nodes are already in the same set or not. This can be implemented simply in terms offind.
- If you want to assign a unique node number to each cell in an n \times n grid, it is easiest to number the first row 0 \dots n-1, then the second row n \dots 2n-1, and so on. In general, coordinates (r,c) will be node nr + c. Conversely, node k will correspond to the row and column coordinates (k \mathbin {\mathbf {div}} n, k \mathbin {\mathbf {mod}} n), i.e. the quotient and remainder when dividing k by n.
- We have discussed in class how to find a shortest path between two specific nodes. Since we want to find a path from any node in the top row to any node in the bottom row, try adding a new “virtual” start node connecting to every node in the top row, and a virtual end node connecting to every node in the bottom row. For example, these virtual nodes could be given node numbers n^2 and n^2 + 1 in a union-find structure of size n^2 + 2. This reduces the problem back to finding a path between two specific nodes (the virtual start and end nodes).