Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

bfs-dfs

BFS and DFS

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches BFS and DFS: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Networks, dependencies, and reachability are graph problems—BFS/DFS are universal explorers.

You will apply BFS and DFS in contexts like: Social graphs, dependency resolution, routing, and crawl pipelines.

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

When you can explain the previous lesson's ideas in your own words.

BFS explores neighbors layer by layer with a queue—shortest path in unweighted graphs. DFS goes deep first with stack/recursion—cycle detection, topological sort, connected components.

BFS skeleton

  1. Enqueue start, mark visited
  2. Dequeue u, process, enqueue unvisited neighbors
  3. Track distance[] for shortest path layers

DFS skeleton

  1. Mark visited, process node
  2. Recurse on unvisited neighbors
  3. Backtrack implicitly when returning

Backtracking preview

Backtracking is DFS with undo: choose, recurse, unchoose—used for subsets, permutations, and constraint puzzles (N-queens). Prune branches that violate constraints early.

BFS on small graph

#include 
#include 
#include 

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

Important interview questions and answers

  1. Q: BFS vs DFS shortest path?
    A: BFS on unweighted graph finds shortest path; DFS does not guarantee shortest.
  2. Q: Visited array why?
    A: Prevents infinite loops on cycles.

Self-check

  1. What data structure does BFS use?
  2. Time complexity BFS on V vertices E edges?

Tip: Always mark visited when enqueue/push—cycles cause infinite loops otherwise.

Interview prep

BFS shortest path?

Unweighted graphs—first visit is shortest layer distance.

Visited array?

Prevents cycles and redundant work.

DFS use?

Cycle detection, topo sort, components, backtracking base.

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

  • BFS shortest unweighted?
  • DFS cycle detect?

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