Skip to content
We areTOP 1%
DSA · Pro · Pattern 2 of 9

Shortest Paths & Topological Sort

15 problems.
Your progress0%
1st Visit
0/15
2nd Visit
0/15
Revise
0/15

About this pattern

Weighted graphs need more than plain BFS. Dijkstra's algorithm finds single-source shortest paths when every weight is non-negative, using a min-heap in O((V + E) log V). Bellman-Ford handles negative weights or a cap on the number of edges in O(V * E), and Floyd-Warshall gives all-pairs distances in O(V^3) for small graphs. Topological sort orders a directed acyclic graph so that every edge points forward; it detects cycles and lets a DP run over dependencies such as prerequisites or task durations. Signals: weighted edges, a minimum total cost or time, precedence constraints, or an order of letters or tasks to reconstruct.

// Dijkstra from src; adj[u] holds {v, w} with w >= 0
vector<long long> dijkstra(int src, const vector<vector<pair<int, int>>>& adj) {
    vector<long long> dist(adj.size(), LLONG_MAX);
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
    dist[src] = 0;
    pq.push({0, src});
    while (!pq.empty()) {
        auto [d, u] = pq.top(); pq.pop();
        if (d > dist[u]) continue;                 // stale heap entry
        for (auto [v, w] : adj[u])
            if (d + w < dist[v]) {
                dist[v] = d + w;
                pq.push({dist[v], v});
            }
    }
    return dist;
}

Problems

Shortest Paths & Topological Sort questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Course Schedule IILeetCodeMedium Solve Course Schedule II on LeetCode (opens in a new tab)
2Find Eventual Safe StatesLeetCodeMedium Solve Find Eventual Safe States on LeetCode (opens in a new tab)
3Network Delay TimeLeetCodeMedium Solve Network Delay Time on LeetCode (opens in a new tab)
4Path With Minimum EffortLeetCodeMedium Solve Path With Minimum Effort on LeetCode (opens in a new tab)
5Cheapest Flights Within K StopsLeetCodeMedium Solve Cheapest Flights Within K Stops on LeetCode (opens in a new tab)
6Find the City With the Smallest Number of Neighbors at a Threshold DistanceLeetCodeMedium Solve Find the City With the Smallest Number of Neighbors at a Threshold Distance on LeetCode (opens in a new tab)
7Number of Ways to Arrive at DestinationLeetCodeMedium Solve Number of Ways to Arrive at Destination on LeetCode (opens in a new tab)
8Fox And NamesCodeforcesMedium 1600Solve Fox And Names on Codeforces (opens in a new tab)
9The Two RoutesCodeforcesMedium 1600Solve The Two Routes on Codeforces (opens in a new tab)
10Greg and GraphCodeforcesMedium 1700Solve Greg and Graph on Codeforces (opens in a new tab)
11SubstringCodeforcesMedium 1700Solve Substring on Codeforces (opens in a new tab)
12LabyrinthCodeforcesHard 1800Solve Labyrinth on Codeforces (opens in a new tab)
13BicyclesCodeforcesHard 1800Solve Bicycles on Codeforces (opens in a new tab)
14Parallel Courses IIILeetCodeHard Solve Parallel Courses III on LeetCode (opens in a new tab)
15Dijkstra?CodeforcesHard 1900Solve Dijkstra? on Codeforces (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account