Skip to content
Learn Netverks

Lesson

Step 32/36 89% through track

shortest-path-preview

Shortest path preview

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Shortest path preview: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Teams apply Shortest path preview in every serious DSA project—skipping it leaves blind spots in analysis and reviews.

You will apply Shortest path preview in contexts like: Interview loops, performance tuning, and foundational CS courses.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

Toward the end—consolidate before AI literacy and capstone interview prep.

Shortest path algorithms: BFS for unweighted graphs; Dijkstra for non-negative weights with priority queue; Bellman-Ford handles negative edges (preview).

Algorithm pick

SettingAlgorithm
UnweightedBFS O(V+E)
Non-negative weightsDijkstra O((V+E) log V)
Negative edgesBellman-Ford O(VE)

BFS distances

#include 
#include 
#include 

int main() {
    std::vector> adj = {{1}, {0, 2}, {1}};
    std::vector dist(3, -1);
    std::queue q;
    dist[0] = 0; q.push(0);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int v : adj[u])
            if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); }
    }
    std::cout << "dist to 2=" << dist[2] << "\n";
    return 0;
}

Dijkstra idea

Greedy: always settle closest unvisited vertex; relax edges with priority queue—requires non-negative weights.

Topological sort preview

On a DAG, topological sort orders nodes so every edge u→v has u before v—Kahn's algorithm enqueues in-degree zero nodes (same BFS queue skills). Cycles make topo sort impossible.

Important interview questions and answers

  1. Q: Why Dijkstra fails with negative edge?
    A: Settled distance can be improved later—breaks greedy invariant.
  2. Q: BFS distance array?
    A: dist[v] = dist[u]+1 when first visit—shortest in unweighted graph.

Self-check

  1. Which algorithm for unweighted shortest path?
  2. Why priority queue in Dijkstra?

Pitfall: Dijkstra does not work with negative edge weights—say Bellman-Ford instead.

Interview prep

Unweighted?

BFS O(V+E).

Non-negative weights?

Dijkstra with priority queue.

Negative edges?

Bellman-Ford—not Dijkstra.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Dijkstra when?
  • Topo sort use?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump