$ cargo run --release -- [-r <num rows>] [-c <num cols>] [-p (path compression)] [-s (progress bar)]or
$ cargo build --release
$ ./target/release/maze-gen [-r <num rows>] [-c <num cols>] [-p (path compression)] [-s (progress bar)]For large mazes, make sure to use the --release and -p flags.
$ cargo run --release -- -r 7 -c 20
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | | |
+---+ +---+ + +---+ +---+ + + + + + + + + + +---+ +
| | | | | | | | |
+ +---+ +---+ +---+---+---+---+---+ +---+ + + +---+---+ + +---+
| | | | | | | | | | | | | |
+ + + + +---+ + + + +---+---+---+---+---+ +---+---+ +---+ +
| | | | | | | | | |
+---+---+ +---+ + + + + + +---+ +---+---+---+---+ + +---+ +
| | | | | | | |
+ + +---+---+---+---+ +---+---+---+---+ +---+ +---+ +---+ + + +
| | | | | | | |
+ +---+---+---+---+ + + +---+---+ + + + + +---+ + + +---+
| | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
Learning about the disjoint set ADT in my algorithms class, I was interested to see it's practical applications, so I built a maze generator in Rust.
The disjoint set ADT is used to solve the dynamic equivalence problem, which is the problem of determining if two elements are in the same equivalence class given implicit data. Applications include determining if two nodes in a circuit are wired, if two people are friends on a social network, or in our case, if two cells in a maze are connected.
A relation
- Reflexivity:
$aRa \quad \forall \quad a \in S$ - Symmetry:
$aRb \implies bRa \quad \forall \quad a, b \in S$ - Transitivity:
$aRb \land bRc \implies aRc \quad \forall \quad a, b, c \in S$
We use the
The equivalence class of an element
- The
$=$ operator is an equivalence relation over$\mathbb{Z}$ . Thus,$a \sim b \iff a = b$ . - The
$\leq$ operator is not an equivalence relation over$\mathbb{Z}$ (symmetry is violated). - Electrical connectivity is an equivalence relation over a set of nodes in a circuit. Thus,
$a \sim b \iff a$ and$b$ are connected by some path of wires.
It's easy to verify if
In this example, we can can partition the set of nodes in the circuit as a set of disjoint equivalence classes (unconnected subcircuits). To know if
To solve the above example, we begin with
-
$\text{find}(a)$ = returns the name of the set containing element$a$ -
$\text{union}(A, B)$ = merges the sets named$A$ and$B$ into a single set
The problem of dynamic equivalence can be stated as wanting to know if find(a)==find(b).
Note that these operations don't actually do any type of relative comparison of the set values, so we will proceed with a simple numbering of our data as
We can implement the disjoint set ADT using a set of trees represented as a 1D array
To union, simply make the root of one tree the child of the root of the other tree - this is
to improve the running time of union, we can use union-by-size, where we store store the size of each tree in the root node as a negative value (e.g.
Performing a find operations currently takes