VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 08: Recursion and backtracking

Lesson 8.1 of 10 in this module Unrolled Pro

Recursion Trees and Backtracking

Recursion solves a problem by solving smaller copies of it, and its cost is the size of the recursion tree. From naive Fibonacci (an exponential tree full of repeated work), to memoising it down to a line (O(n)), to replacing recursion with an explicit stack when the depth exceeds Python's limit. Then backtracking: choose, explore, un-choose, and pruning branches that cannot lead to a valid answer, which explores 6,763 nodes instead of 65,536 candidates. Then how to estimate the cost of any recursive search.

about 10 min 3 Python examples 2 quick checks

The question this module answers: how do you explore every possibility without writing a loop for each one? Subsets, orderings, placements, configurations: the number of nested choices isn't known in advance, so you can't write that many for loops. Recursion writes one level of choice and lets the call stack repeat it. Backtracking adds two habits: undo each choice when you return, and stop exploring a branch as soon as it can't succeed.

ML context.

  • Search over configurations: hyperparameter grids with constraints (lesson 8.10), feature subsets under a cost budget (lesson 8.3), valid architectures from a set of blocks.
  • Tree-shaped computation: parsing nested configs and JSON, walking a model's module tree (model.named_modules() is a recursive walk), decision trees.
  • Constraint solving: scheduling jobs onto GPUs, generating valid structured outputs.

1. Recursion and the recursion tree

A recursive function needs a base case (answer directly) and a recursive case (reduce to smaller inputs). Its cost is easiest to see by drawing the recursion tree: one node per call, children for the calls it makes.

Total work = number of nodes × work per node.

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Recursion Trees and Backtracking"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee