P4 - Mazes: Debugging


Use this page when a test fails and you don’t know why. It covers:


Debugging Lecture

On Wednesday, November 4 we had a lecture on debugging techniques (see the course calendar). If you missed it, or want to go over it again before you start this project, the recording is below.

If the video doesn’t load, open it on Panopto.

Lecture materials (the same links as on the calendar):


A Debugging Workflow

When a test fails, it’s tempting to change code until the test passes. That usually takes longer than finding the cause first. Try these steps in order:

  1. Read the whole failure message. Note the expected and actual values, the exception type, and the first line of the stack trace that is in your code.
  2. Pick the smallest failing test. A test on a 4-vertex graph is much easier to follow than a stress test. Run just that one test (click the green arrow next to it in IntelliJ).
  3. Work out the right answer by hand, using the tables below. If you can’t say what the correct state should be after each step, you can’t tell where your code goes wrong.
  4. Run the test in the debugger. Put a breakpoint at the top of your main loop, right-click the test, and choose Debug. Each time the loop starts again, compare the variables in the debugger to the matching row of your table.
  5. Find the first row that doesn’t match. The bug happens between the last row that matched and the first one that didn’t. Step through that iteration one line at a time.
  6. Make one change, then run the test again. If the test still fails, undo the change instead of adding a second fix on top of it.
IntelliJ debugger tips

Tracing a Test Graph by Hand

To find a bug, you need to know what your code should be doing at each step. Tracing by hand means running the algorithm yourself on paper. You write down its state (the sets, the distances, the priority queue) in a table, one row per step. Then you compare that table to what your code actually does.

How to use this section:

  1. Find the graph for the test you’re failing. There are diagrams of the test graphs on the Kruskal and Dijkstra pages.
  2. Go to that algorithm below. Each one shows what to keep track of and a worked example on a small graph that we made up, which you can step through one move at a time. The example is not one of the test graphs. It’s there to show you how the state changes, and how to fill in the table.
  3. Fill in the same kind of table for your failing test’s graph.
  4. Run the test in the debugger with a breakpoint at the top of your main loop. Each time the program pauses, its state should match the next row of your table. The first row that doesn’t match is where your bug is.

Disjoint Sets

What to keep track of: write out the pointers array after every makeSet and union call in the test. Use the same format your code uses, including whatever your roots store according to lecture’s convention. The Gradescope tests check this array directly, so your hand trace should match it exactly.

What to check:

Kruskal’s Algorithm

What to keep track of: list the edges from lightest to heaviest. For each edge, write down the sets before you look at it, whether its two ends are already in the same set, and whether you add it.

Worked example (a made-up graph, not a test graph): an undirected graph with vertices A, B, C, D and edges A–B (1), C–D (2), A–C (3), B–D (4) and B–C (5). Use Next to step through it, or Play to watch it run.

Show all steps as a table

This is the kind of table to fill in for your own failing test.

Edge Weight Sets before this edge Same set? Result
A–B1{A} {B} {C} {D}NoAdd, union
C–D2{A,B} {C} {D}NoAdd, union
A–C3{A,B} {C,D}NoAdd, union
B–D4{A,B,C,D}YesSkip (would make a cycle)
B–C5{A,B,C,D}YesSkip (would make a cycle)

The MST is A–B, C–D and A–C, with total weight 6.

What to check once you’ve traced your failing test’s graph:

Dijkstra’s Algorithm

What to keep track of: the priority queue, the best distance found so far for each vertex (distTo), and the edge used to reach each vertex (edgeTo). Fill in one row each time you remove a vertex from the priority queue.

Worked example (a made-up graph, not a test graph): a directed graph with edges A→B (1), A→C (4), B→C (2), B→D (6) and C→D (1), starting at A and ending at D. Use Next to step through it, or Play to watch it run.

Show all steps as a table

This is the kind of table to fill in for your own failing test. Each row is one removal from the PQ, which is one iteration of your main loop.

Removed Edges relaxed distTo after edgeTo after PQ after
—(start)A=0—A:0
A (0)A→B, A→CA=0, B=1, C=4B: A→B
C: A→C
B:1, C:4
B (1)B→C (3 < 4, update), B→DA=0, B=1, C=3, D=7B: A→B
C: B→C
D: B→D
C:3, D:7
C (3)C→D (4 < 7, update)A=0, B=1, C=3, D=4B: A→B
C: B→C
D: C→D
D:4
D (4)— (reached the end)

Following edgeTo back from D gives D ← C ← B ← A, so the shortest path is A→B→C→D with total weight 4.

What to check once you’ve traced your failing test’s graph:

Note

Your code may give the same answer as your table on small tests but still fail stress tests. If so, the bug often only appears with ties (equal weights), unreachable vertices, or very long paths. Make a small graph with that property, trace it, and run it as your own test.


Before You Come to Support Hours

Support Hours go further when you arrive with this ready. You don’t need every item done before asking for help, but each one you do saves time in the queue.

If you’re asking on Ed instead, include the same information, and make your post private if it contains your code.

Support Hours times and locations are on the Support Hours page.

← Previous: Dijkstra's Algorithm