Shortest Path
shortest-path
Data Structures
Weighted Graph Representation
Highways have lengths in kilometers, flights have ticket prices in dollars, and network links have latencies in milliseconds. Treating any of those graphs as unweighted throws away the only information that matters for the question you actually care about (the shortest, cheapest, or fastest route), which is exactly the gap a **Weighted Graph** representation closes by attaching a number to every edge. This lesson covers three representations: a weighted adjacency list backed by arrays of `{node, weight}` pairs, a weighted adjacency matrix where `matrix[u][v]` is the edge cost (and infinity for non-edges), and a weighted edge list that stores `(u, v, weight)` triples. You will analyze the space and time trade-offs, see why Dijkstra wants the adjacency list, why Floyd-Warshall wants the matrix, and why Kruskal wants the edge list, and you will represent both directed and undirected weighted graphs correctly in each format. In **Graphs: Representation Basics**, you stored connectivity (whether an edge exists) using the same three shapes; the weighted version simply replaces a boolean or membership check with a numeric value. **Hash Map (Dictionary) Basics** is what makes the adjacency list work: a vertex maps to its neighbor-and-weight list in expected `O(1)`, the same way the unweighted version did. With weights in the picture, the algorithms track unlocks shortest-path and minimum-spanning-tree algorithms that read the weight on every edge during their core inner loop.
Not Started
0%
Algorithms
Advanced Graph Algorithms (Network Flow)
Two seemingly different problems, the maximum amount of water you can push from a source to a sink through a network of capacitated pipes, and the minimum total capacity you must remove to disconnect the sink from the source, turn out to have the same answer for every graph. The max-flow min-cut theorem ties them together, and once you accept it a surprising number of optimization problems (image segmentation, project selection, bipartite matching) collapse into max-flow instances. **Advanced Graph Algorithms (Network Flow)** introduces the algorithms that compute that maximum flow. You will work through the Ford-Fulkerson method built on augmenting paths and the residual graph, the BFS-based Edmonds-Karp variant with its `O(V * E^2)` guarantee, and Dinic's algorithm which uses level graphs and blocking flows to reach `O(V^2 * E)`. The lesson then turns to maximum bipartite matching (Hungarian and Hopcroft-Karp), applications like airline scheduling and image segmentation, and a first look at minimum-cost flow. In **Graph Algorithms (Advanced)**, you handled MST, all-pairs shortest paths, and SCCs on edge-weighted graphs. Network flow keeps the weighted-edge model but flips the question from "shortest" to "highest throughput." Next, **String Algorithms** turns to pattern matching, the implicit automaton of a regular expression.
Not Started
0%
Practice Problems
Swim in Rising Water
Given an n x n grid of elevations, find the minimum time t at which you can swim from the top-left to the bottom-right corner.
Word Ladder
Given a begin word, end word, and a dictionary, find the length of the shortest transformation sequence where each step changes exactly one letter.
Cheapest Flights Within K Stops
Find the cheapest price to fly from a source to a destination with at most k intermediate stops, given a list of flights with prices.
Network Delay Time
Given a network of n nodes and weighted directed edges representing signal travel times, find the time it takes for a signal sent from node k to reach all nodes.
Snakes and Ladders
Given a Snakes and Ladders board, find the minimum number of dice rolls to reach the final square from square 1.
Question Banks
Dijkstra and Shortest Paths
Decide between Dijkstra, Bellman-Ford, and 0/1 BFS, and trace Dijkstra on a small weighted graph. Code stems are Python.
Community
Shortest Path in Binary Matrix
BFS through 0-cells in 8 directions to find the shortest path from top-left to bottom-right.
Path with Maximum Probability
Find the path between two nodes that maximizes the product of edge success probabilities, via modified Dijkstra.
Open the Lock
Find the minimum wheel turns to open a 4-digit combination lock while avoiding deadend states.
Dijkstra vs Bellman-Ford vs Floyd-Warshall: Pick One
Three shortest-path algorithms, three honest decision criteria. When negative weights matter, when all-pairs is worth O(V^3), and why Dijkstra is the default for a reason.
Minimum Genetic Mutation
Transform a gene string to a target one mutation at a time, where each intermediate must be in the bank.
