Previous Lecture lect15 Next Lecture

lect15,

Graph Search: Depth first traversal

Lecture Plan

1. DFS Visualizer

Step through the DFS algorithm on a small graph — trace the call stack, see how backtracking works, and understand why a single call to exploreDFS(v) is not enough for a disconnected graph. The third mode shows the outer loop in DepthFirstSearch(G) that handles disconnected components.

Interactive DFS Visualizer


2. LeetCode Problem: Keys and Rooms

Apply DFS to a new graph problem. Given the DFS pattern from the visualizer, identify what the graph is, what the visited set tracks, and how to detect whether all rooms are reachable.

Keys and Rooms

Input: vector<vector> rooms = [[1],[2, 3],[1],[]] Output: ?

Discuss with your peer (5 mins):

  1. What are the nodes and what are the edges in this problem?
  2. What does the input (rooms) represent in graph terms?

3. DFS Applied to PA03: Backpropagation

The neural network is a directed graph where edges only go forward. To compute the nudge (delta) for each weight, you need values from nodes in the next layer — but there is no backward edge to follow. The recursive DFS naturally solves this: dive to the output first, compute the error there, then pass results back up the call stack. A contributions map (memoization) ensures each node is computed only once.

Interactive Backpropagation Visualizer (PA03)

What this visualizer does not show — questions for the curious student:

These videos answer those questions, visually and without heavy math:

To see a real neural network train live in your browser — adjusting weights, watching the decision boundary shift, experimenting with architecture and learning rate: