This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 3: Search and graphs
BFS with a heap: always expand the cheapest place you can reach next.
Builds on Graph BFS & DFS, Heaps & Top K.
The question to ask
Do the edges have different costs, and do I want the cheapest route from a source?
Once edges have different costs (times, distances, probabilities, effort), plain BFS stops being correct, because fewer edges no longer means cheaper. Dijkstra fixes that by expanding the cheapest frontier node next, from a min-heap. It needs non-negative weights. Bellman-Ford handles a cap on the number of edges (Cheapest Flights Within K Stops) and negative weights. Floyd-Warshall gives all pairs when n is a few hundred. Prim's, a near-identical heap loop, builds a minimum spanning tree.
Time O((V + E) log V) for Dijkstra with a heap · Space O(V + E)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Relax every edge k + 1 times, reading from the previous round's copy so one round can't chain several flights together. Dijkstra struggles here because the cheapest route may use too many stops.
function findCheapestPrice(n: number, flights: number[][], src: number, dst: number, k: number): number {
let prices = new Array<number>(n).fill(Number.POSITIVE_INFINITY);
prices[src] = 0;
for (let round = 0; round <= k; round++) {
const next = [...prices];
for (const [from, to, price] of flights) {
if (prices[from] + price < next[to]) next[to] = prices[from] + price;
}
prices = next;
}
return prices[dst] === Number.POSITIVE_INFINITY ? -1 : prices[dst];
}Min Cost to Connect All Points: on a dense graph, skip the heap and scan an array of best-known connection costs, O(n²). Each round adds the cheapest unconnected point.
function minCostConnectPoints(points: number[][]): number {
const n = points.length;
const best = new Array<number>(n).fill(Number.POSITIVE_INFINITY);
const inTree = new Array<boolean>(n).fill(false);
best[0] = 0;
let total = 0;
for (let added = 0; added < n; added++) {
let pick = -1;
for (let i = 0; i < n; i++) {
if (!inTree[i] && (pick === -1 || best[i] < best[pick])) pick = i;
}
inTree[pick] = true;
total += best[pick];
for (let i = 0; i < n; i++) {
const cost = Math.abs(points[i][0] - points[pick][0]) + Math.abs(points[i][1] - points[pick][1]);
if (!inTree[i] && cost < best[i]) best[i] = cost;
}
}
return total;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given weighted directed travel times between n nodes, return how long it takes a signal sent from node k to reach every node, or -1.
"Signal" over "travel time" edges: Dijkstra from k, answer is the largest distance.
Not attempted yet
Each undirected edge has a success probability. Return the maximum probability of a path from start to end.
Best "probability" is a product to maximise: Dijkstra with a max-heap, multiplying instead of adding.
Not attempted yet
A route's effort is the largest height difference between consecutive cells. Return the minimum effort to travel from top-left to bottom-right of the grid.
"Effort" is the max step along the path: Dijkstra where a path's cost is max(cost so far, this step). Binary search plus BFS also works.
Not attempted yet
Given flights with prices, return the cheapest price from src to dst with at most k stops, or -1.
"Cheapest" with "at most k stops": Bellman-Ford for k + 1 rounds, copying the array each round.
Not attempted yet
The cost to connect two points is their Manhattan distance. Return the minimum cost to connect all points.
"Minimum cost to connect" everything is a spanning tree: Prim's (dense, O(n²)) or Kruskal's with union-find.
Not attempted yet
Return the city with the fewest other cities reachable within a distance threshold, preferring the larger number on ties.
All-pairs under a "distance threshold" with n ≤ 100: Floyd-Warshall's triple loop is the shortest code.
Not attempted yet
Each grid cell has an elevation. At time t you can swim through cells with elevation at most t. Return the least time to reach the bottom-right.
A path costs its highest "elevation": Dijkstra on max-so-far, popping the lowest cell next.
Not attempted yet