Graph Algorithms MCQs for NSCT
Graph algorithms explore and optimise over vertices joined by edges. The core set for the NSCT is breadth-first search (BFS), depth-first search (DFS), Dijkstra's shortest paths, minimum spanning trees (Prim and Kruskal) and topological sort. Graph Algorithms is topic 8 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% in DSA, per the HEC NSCT report and APP.
Most questions ask you to run an algorithm on a small graph. Traversal orders depend on the order neighbours are visited, so a fair question always states it. On this page neighbours are taken alphabetically.
BFS
BFS visits the source, then every vertex 1 edge away, then 2 edges away, and so on, using a FIFO queue. It runs in O(V + E) with adjacency lists and O(V²) with an adjacency matrix.
Take the undirected graph A-B, A-C, B-D, C-D, D-E. BFS from A gives A B C D E: B and C are at distance 1, D at 2 and E at 3. Exam angle: in an unweighted graph, BFS distances are shortest-path distances. Mark a vertex as visited when you enqueue it, or it can enter the queue twice.
DFS
DFS goes as deep as possible before backtracking, using recursion or an explicit stack. On the same graph, recursive DFS from A gives A B D C E: from D, C comes before E alphabetically, and once C has nothing new the search backtracks to D for E.
In a directed graph, DFS classifies edges as tree, back, forward or cross. A back edge (to an ancestor still on the recursion stack) means the graph has a cycle. Exam angle: an iterative DFS that pushes neighbours in alphabetical order pops the last one first, so its order differs from the recursive version unless you push them in reverse.
Dijkstra
Dijkstra's algorithm finds single-source shortest paths when all edge weights are non-negative. It repeatedly finalises the unfinished vertex with the smallest tentative distance, then relaxes its edges: if d[u] + w(u, v) < d[v], it sets d[v] = d[u] + w(u, v).
With edges A-B 2, A-C 5, B-C 1, B-D 4, C-D 1 and source A, vertices are finalised in the order A (0), B (2), C (3), D (4). C improves from 5 to 3 through B, and D from 6 to 4 through C. With a binary heap it runs in O((V + E) log V).
| Algorithm | Problem | Negative edges? | Time |
|---|---|---|---|
| BFS | Shortest paths, unweighted | n/a | O(V + E) |
| Dijkstra | Single source | No | O((V + E) log V) |
| Bellman-Ford | Single source | Yes, detects negative cycles | O(VE) |
| Floyd-Warshall | All pairs | Yes, no negative cycles | O(V³) |
Exam angle: a negative edge can make Dijkstra finalise a vertex too early and report a wrong distance; use Bellman-Ford instead.
MST
A minimum spanning tree (MST) connects all V vertices with V − 1 edges at the lowest total weight. Both standard algorithms are greedy:
- Kruskal sorts all edges and adds each one that joins two different components, using union-find to detect cycles. Time O(E log E).
- Prim grows one tree from a start vertex, always adding the cheapest edge that leaves the tree. Time O((V + E) log V) with a heap, or O(V²) with an array, which suits dense graphs.
On the graph above, Kruskal takes B-C (1), C-D (1) and A-B (2) for a total of 4. Exam angle: if all edge weights are distinct the MST is unique; with ties several MSTs can exist, all with the same total weight.
Topological sort
A topological order of a directed acyclic graph (DAG) lists every edge u → v with u before v. Kahn's algorithm repeatedly removes a vertex with in-degree 0; the DFS method outputs vertices in reverse order of finish time. Both are O(V + E).
For edges A → B, A → C, B → D, C → D, D → E there are two valid orders: A B C D E and A C B D E. Exam angle: if Kahn's algorithm outputs fewer than V vertices, the graph has a cycle and no topological order exists.
Common traps
- Running BFS or DFS without the stated neighbour order, then picking an option that is valid but not the one asked for.
- Using Dijkstra on a graph with a negative edge.
- Assuming the path between two vertices inside the MST is their shortest path. An MST minimises total weight, not pairwise distances.
- Forgetting that an MST has exactly V − 1 edges, or that isolated vertices count as separate components.
- Confusing Prim (one growing tree) with Kruskal (a forest that merges).
Worked examples
Understanding. Which vertex does Dijkstra's algorithm finalise at each step? Answer: the unfinished vertex with the smallest tentative distance. That greedy choice is safe only because no non-negative edge can later produce a shorter path to it.
Application. In the undirected graph A-B, A-C, B-D, C-D, C-E, what is the recursive DFS order from A, with neighbours taken alphabetically? Answer: A B D C E. A goes to B, B to D, and D to C (B is already visited). C's only new neighbour is E.
Analysis. Kruskal runs on edges A-B 1, B-C 2, A-C 3, C-D 4, B-D 5. Which edge is the first to be rejected, and what is the MST weight? Answer: A-C is rejected first; the weight is 7. A-B and B-C are accepted, so A-C would close the triangle A-B-C. C-D (4) is then accepted, giving 1 + 2 + 4 = 7, and B-D is rejected too.
Practise
The quiz below has 100 Graph Algorithms questions, which you can filter by tier and difficulty. For longer step-by-step traces, see graph traversal traces.
Practise: 100 questions
FAQs
When should I use BFS instead of DFS?
Use BFS when you need the fewest edges between two vertices, or anything level by level. Use DFS for cycle detection, topological sort, connected components and maze-style exhaustive search. Both run in O(V + E) on adjacency lists, so the choice depends on the question, not on speed.
Is Dijkstra a greedy algorithm or dynamic programming?
It is normally classed as greedy, because it commits to the closest unfinished vertex at each step and never revisits it. It also relies on optimal substructure (every part of a shortest path is itself a shortest path), which is why some authors note its link to dynamic programming.
Do Prim and Kruskal always give the same tree?
They always give the same total weight. If all edge weights are distinct, the MST is unique, so the trees are identical. With equal weights they may choose different edges, and each result is still a valid MST.
Next step
Continue with Algorithm Design Techniques MCQs, go back to the DSA hub, or try a timed NSCT mock test.