Programming Fundamentals · Algorithms Behind AI

BFS and DFS: How an Agent Finds Files in a Codebase

Watch two search personalities in a maze animation: sweep layer by layer vs go all the way down one path; Coding Agent grep and web crawlers are variants of both

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “BFS and DFS: How an Agent Finds Files in a Codebase”?

Watch two search personalities in a maze animation: sweep layer by layer vs go all the way down one path; Coding Agent grep and web crawlers are variants of both

DECISION RULE

Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.

TRY NEXT

Write one question you could answer with evidence after trying this idea.

WATCH FOR

A conclusion that sounds complete but leaves the key assumption untested.

Maze personality test · one map, two ways

🏁 is the start, 🎯 the goal, dark gray the walls. Each button is a personality—watch three things: the shape of the paint (BFS ripples in rings; DFS is a snake); the two counters below; and the final green path—whose is shorter? Who visited more cells?

Run BFS first—watch the ripple spread
BFS · ripple layer sweep
cells visited
final path length
DFS · single-head snake dive
cells visited
final path length
Two numbers teach the whole theory. BFS visits 77 cells before hitting the goal—it floods every cell at distance 1, 2, 3… from the start, so that whole sheet must live in memory; but because it advances by distance, the path that first hits the goal is always shortest (23 cells). DFS reaches it after only 44 visits, keeping just the current path in memory; yet its path is 37 cells—more than half longer than shortest—and unlucky runs dive into dead ends and back out (faded cells are backtracking).
What does this have to do with AI?

Swap maze cells for folders and web pages—and both personalities show up next to you:

Neither wins everywhere—only fit to the scene. Need “shortest / nearest / most relevant”? BFS, pay the memory. Need “any usable answer fast, memory tight”? DFS, accept detours. Next lesson’s greedy and sampling, then Beam Search, are also picking spots on this “search-strategy spectrum.”

The algorithmic cost curve in “Maze personality test · one map, two ways”

“🏁 is the start, 🎯 the goal, dark gray the walls.” is not asking you to memorize steps. It trains you to spot repeated work: as the input grows, how many comparisons, moves, or recursive calls does the program perform?

Find repeated work before declaring something fast

Break “Swap maze cells for folders and web pages—and both personalities show up next to you” into three questions: how input size changes, what each round does, and whether the next round can shrink its search space. Big-O describes growth, not an exact time on every machine; constants, memory, and data distribution still matter.

  • BFS layer sweep : flood by distance ring by ring—what you find is always shortest, at the cost of a big sheet in memory
  • DFS go all the way down one path : saves memory, often hits a solution sooner, but paths aren’t guaranteed short—and it backtracks
  • Visits vs path length : 77/23 vs 44/37—two number pairs are the full ledger of both personalities

Theoretical optimum is not always practical optimum

When AI writes an algorithm, trace a small input by hand and benchmark progressively larger inputs. That turns ““People you may know” = BFS two layers from you: layer 1 is friends;” from a slogan into a performance claim you can check.

From “Maze personality test · one map, two ways” to “What does this have to do with AI”

“Maze personality test · one map, two ways” grounds the problem in “🏁 is the start, 🎯 the goal, dark gray the walls. Each button is a personality— watch three things : the shape of the paint (BFS ripples in rings; DFS is a snake); the two counters below; and the final green p…”. “What does this have to do with AI” then moves it toward “Swap maze cells for folders and web pages—and both personalities show up next to you”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

For a real task, find the repeated work first, ask how input size changes, and use a small benchmark to verify the theoretical judgment. Complexity should not become a label detached from the situation.

  • “Maze personality test · one map, two ways”: 🏁 is the start, 🎯 the goal, dark gray the walls. Each button is a personality— watch three things : the shape of the paint (BFS ripples in rings; DFS is a snake); the two counters below; and the final green p…
  • “What does this have to do with AI”: Swap maze cells for folders and web pages—and both personalities show up next to you
  • “The closing point”: Crawlers and mutual-friend recs are graph search—the maze is just the most intuitive graph

The final “The closing point” brings the discussion to “Crawlers and mutual-friend recs are graph search—the maze is just the most intuitive graph”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

What this lesson wants to share

  • BFS layer sweep: flood by distance ring by ring—what you find is always shortest, at the cost of a big sheet in memory
  • DFS go all the way down one path: saves memory, often hits a solution sooner, but paths aren’t guaranteed short—and it backtracks
  • Visits vs path length: 77/23 vs 44/37—two number pairs are the full ledger of both personalities
  • Real systems mix them: Agents ls one layer (BFS) then dive into a suspicious folder (DFS)
  • Crawlers and mutual-friend recs are graph search—the maze is just the most intuitive graph
Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing BFS and DFS: How an Agent Finds Files in a Codebase Algorithms Behind AI
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful