AlgoViz
Algorithms are easier to understand when you can watch them think. AlgoViz animates three classic problems step by step: four search methods racing through the same maze, two ways of wrapping a fence around scattered points, and a Sudoku solver that guesses and backtracks. It runs on your own computer, and the source code is free on GitHub.
All toolsRacing through a maze
The program builds a 41 × 31 maze, knocks out a tenth of the inside walls so there's more than one way through, and lets four search methods solve it at the same time, one in each corner of the window. Each starts at the green square (top-left) and has to reach the red one (bottom-left). Blue squares are places it has looked; once it reaches the exit, the route it found lights up in yellow. The label above each maze counts the squares looked at so far.
Breadth-first search (BFS)
Spreads out evenly in every direction like water filling the maze: first everything one step away, then everything two steps away, and so on. It's guaranteed to find the shortest route.
Depth-first search (DFS)
Picks a corridor and follows it as far as it goes, backing up only at a dead end. It takes the first route it stumbles on, which is rarely the shortest: here it's more than twice as long as needed.
Dijkstra's algorithm
Always continues from the closest unexplored spot. On a maze where every step costs the same it behaves just like BFS. It earns its place on maps where some steps cost more, like roads with traffic.
A* search
Dijkstra plus a sense of direction: it prefers spots that are close to the start and look close to the exit (counting blocks as the crow walks). Same shortest route, usually far less looking around.
On the maze in the animation, which has 625 open squares. BFS, Dijkstra and A* all find the shortest route (68 steps); DFS's route is 152 steps.
Why A* wins: BFS and Dijkstra spread out evenly in every direction, so they look at everything near the start before getting anywhere. A* leans toward the exit, so it skips most of the maze and still never misses a shorter route. This isn't a lucky maze: over 100 random mazes built the same way, A* looked at the fewest squares in 88 of them (and never more than BFS or Dijkstra), and DFS returned a longer-than-necessary route in 98.
The setup matters, though. In a maze with no loops there's only one route, so DFS can't take a wrong one, and if the exit happens to lie in the direction DFS tries first (it tries right, then down), it can even look at fewer squares than A*. A*'s advantage shows when there are choices to make.
Under the hood the four are close cousins: they differ only in which spot they explore next. BFS takes the one it found earliest, DFS the one it found most recently, Dijkstra the one closest to the start, and A* the one with the best “distance so far plus distance still to go” estimate.
Wrapping a fence around points
Scatter 30 points on a page and stretch a rubber band around all of them: the shape it snaps to is the convex hull. It shows up whenever you need the outline of a set of things, from collision detection in games to mapping the range of an animal from GPS sightings. Both methods below find the same fence (I checked on 200 random point clouds); they just get there differently.
Jarvis march (left)
Also called gift wrapping. Start at the leftmost point, then at every corner check every point to find the one that turns furthest outward, and step there. Simple, but each corner costs a full pass over all the points.
Graham scan (right)
Sort the points once by angle around the lowest point, then sweep around them in order. Whenever the fence would bend inward, pull out the last post and try again. After one sweep the fence is done.
Median number of animation steps over 200 random clouds of 30 points. A typical fence has 9 corners.
Both are built on one simple test: given three points, does the path through them turn left or right? Jarvis uses it to ask “is this point further outside than my current best?”; Graham uses it to ask “does the fence bend the wrong way here?”. The gap grows with more points: Jarvis costs roughly points × corners, Graham roughly points × log(points) for the sort.
Solving a Sudoku by trial and error
The program makes a random complete Sudoku, blanks out some squares, and then solves it the way a very patient person would: go to the first empty square, write the smallest number that doesn't break a rule, and move on. If a later square has no legal number left, rub out the last guess and try the next one. This is called backtracking.
Median over 30 random puzzles at each level. Each step writes or erases one number. The worst medium puzzle took 671.
With 25 blanks the grid is so constrained that most squares have only one legal number, so the solver barely backtracks (a perfect run would be exactly 25 steps). With 40 blanks early guesses are often wrong, and you can watch it unwind several squares before finding its way.
Every algorithm in AlgoViz works the same way: instead of jumping straight to the answer, it records a snapshot after each small step, and the drawing code plays those snapshots back as frames. That is what lets you watch it think.
Run it yourself
You need Python 3.9 or newer (on Windows, install it from python.org, and use py where it says python3). Each visualizer opens its own window.
-
Get the code
git clone https://github.com/SanketRt/AlgoViz.git cd AlgoViz
No git? Use Download ZIP and unzip it.
-
Install what it needs
python3 -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -r requirements.txt -
Pick a visualizer
python3 visualize.py # maze race python3 visualize_ch.py # convex hull: ←/→ to step, R for new points python3 visualize_sudoku.py easy # or: medium
Each visualizer also saves its run as a GIF in the same folder, ready to share. To try a different maze, change
SEED,LOOPSor the start and exit at the top ofvisualize.py.
Source code
About 650 lines of Python in total, using pygame-ce for drawing and imageio for GIFs. Free to use and change under the MIT licence.
| File | What it does |
|---|---|
| maze/generator.py | Carves a random maze, then knocks out some walls to add loops. |
| algorithms/ | BFS, DFS, Dijkstra and A*, each returning the route and every square it looked at. |
| algorithms/geometry/ | Jarvis march and Graham scan, yielding one snapshot per step. |
| visualize.py | Draws the four-way maze race and saves a GIF. |
| visualize_ch.py | Draws both convex hull methods side by side and saves a GIF. |
| visualize_sudoku.py | Makes a puzzle, solves it by backtracking, and saves a GIF. |