VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 11: Graphs

Lesson 11.11 of 16 in this module Unrolled Pro

Cheapest Flights Within K Stops

The cheapest route from src to dst using at most k stops (k + 1 flights). From exploring every route with depth-first search up to the hop limit (exponential), to Bellman-Ford limited to k + 1 rounds with a copy of the previous round's prices (O(k · E)), and Dijkstra over (city, flights used) states. Then why plain Dijkstra fails with a hop limit, returning the route, a budget instead of a hop limit, and routing a request through at most k model calls.

about 10 min 3 Python examples 2 quick checks

Problem. There are n cities and flights (from, to, price). Return the cheapest price from src to dst with at most k stops (so at most k + 1 flights), or −1 if no such route exists.

text
flights = [(0, 1, 100), (1, 2, 100), (2, 3, 100), (0, 3, 500)],  src = 0, dst = 3
k = 1   →   500     (0 → 3 directly; 0 → 1 → 2 → 3 needs 2 stops)
k = 2   →   300

Lesson 11.10's Dijkstra finds the cheapest route, but ignores the number of edges. With a hop limit, the cheapest route may be too long, and the best allowed route may be one that Dijkstra discarded because it was more expensive at an intermediate city. The fix is to make the hop count part of the problem.

ML context. Constrained routing shows up when requests pass through a chain of models or services with a cap on calls: an agent allowed at most k tool calls, a cascade that may escalate at most k times to bigger models, a data-format conversion graph where each hop loses some precision so only short chains are acceptable. "Cheapest with at most k edges" is the core query in each (Change D).


1. Step 1: Clarify

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

Unlock "Cheapest Flights Within K Stops"

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