P4 - Mazes: Debugging
Use this page when a test fails and you don’t know why. It covers:
- the debugging lecture
- a step-by-step way to find a bug
- how to trace a test graph by hand
- a checklist to go through before you come to Support Hours or post on Ed
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):
- Debugging Script: a written version of the lecture, so you can follow along
- Demo code, so you can try the steps yourself in IntelliJ:
PayloadFn.java,Main.java,LinkedList.jar
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:
- 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.
- 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).
- 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.
- 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.
- 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.
- 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
- Step Over runs the current line. Step Into goes inside a method call on that line, which is useful for checking your disjoint sets from inside Kruskal’s.
- Conditional breakpoints: right-click a breakpoint
and enter a condition (for example,
vertex.equals(end)). The program then pauses only when the condition is true, so you don’t have to click through every iteration. - Evaluate Expression (the calculator icon) lets you
run code like
distTo.get(v)orfindSet(x)while the program is paused. - You can expand maps and arrays in the Variables panel to
see every entry. This is the quickest way to check your
pointersarray or your distance map.
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:
- Find the graph for the test you’re failing. There are diagrams of the test graphs on the Kruskal and Dijkstra pages.
- 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.
- Fill in the same kind of table for your failing test’s graph.
- 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:
- After each
union: did the smaller set join the larger one? What happens when the two sets are the same size? - After each
findSet: does every item on the path you followed now point straight to the root? - What does
unionreturn when both items are already in the same set?
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–B | 1 | {A} {B} {C} {D} | No | Add, union |
| C–D | 2 | {A,B} {C} {D} | No | Add, union |
| A–C | 3 | {A,B} {C,D} | No | Add, union |
| B–D | 4 | {A,B,C,D} | Yes | Skip (would make a cycle) |
| B–C | 5 | {A,B,C,D} | Yes | Skip (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:
- When the loop ends, how many edges did you add? How many edges does a spanning tree on V vertices have? Try this with the disconnected test graph.
- What should happen with a self-loop edge, where both ends are the same vertex?
- Does your code add every vertex to the disjoint sets, including vertices that have no edges?
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→C | A=0, B=1, C=4 | B: A→B C: A→C | B:1, C:4 |
| B (1) | B→C (3 < 4, update), B→D | A=0, B=1, C=3, D=7 | B: 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=4 | B: 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:
- Look at the step where D is first reached, with distance 7. If your code stops there, what path does it return? When do you actually know a vertex’s shortest distance?
- When a vertex’s distance goes down (like C going from 4 to 3), does its priority in the PQ go down too? What does your PQ do if you add a vertex that is already in it?
- Does your extracted path list the edges in order from start to end?
Following
edgeTogives them in reverse. - What should happen when
startequalsend, or whenendcan’t be reached? Check the Javadoc on the shortest-path finder interface.
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.
- I can name the test that fails, and I can make it fail on my own computer (not only on Gradescope).
- I have read the whole failure message and know the expected and actual values or the exception.
- I have pulled the latest skeleton and refreshed Gradle, and the code compiles.
- I have traced the test’s graph by hand and know what the correct answer is.
- I have run the test in the debugger and found the first point where my code’s state is different from my trace.
- I can explain in one or two sentences what I think is wrong and what I have already tried.
- For a test that only fails on Gradescope, I have the test name and its output.
- My latest code is committed and pushed, so a TA can look at it.
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.