Graph Algorithms MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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:

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. Which data structure does breadth-first search (BFS) use to decide which vertex to visit next?

    1. A LIFO stack
    2. A min-priority queue
    3. A FIFO queue
    4. A hash table only
    Show answer

    Answer: C. A FIFO queue

    BFS explores all vertices at distance 1, then distance 2, and so on. A first-in, first-out queue guarantees that vertices discovered earlier (closer to the source) are visited first.

  2. Depth-first search (DFS) is usually written recursively. Which data structure does an iterative version use instead of recursion?

    1. A FIFO queue
    2. A min-heap
    3. A circular buffer
    4. An explicit stack
    Show answer

    Answer: D. An explicit stack

    Recursion uses the call stack to remember where to backtrack. An iterative DFS replaces it with an explicit LIFO stack, so the most recently discovered vertex is explored first.

  3. For which kind of graph does BFS from a source s find the shortest path (fewest edges) to every reachable vertex?

    1. Graphs with arbitrary positive weights
    2. Graphs with negative edge weights
    3. Only trees
    4. Unweighted graphs (or all edges of equal weight)
    Show answer

    Answer: D. Unweighted graphs (or all edges of equal weight)

    BFS reaches vertices in order of the number of edges from s, so the first time it discovers a vertex it has found a path with the fewest edges. With unequal weights, fewest edges is not always cheapest, so you need Dijkstra.

  4. Dijkstra's algorithm is not guaranteed to give correct shortest paths when the graph has which feature?

    1. Negative edge weights
    2. Cycles
    3. Edges of weight zero
    4. More than one shortest path to a vertex
    Show answer

    Answer: A. Negative edge weights

    Dijkstra finalises the closest unfinished vertex, assuming no later path can be cheaper. A negative edge can make a later path cheaper, which breaks that assumption. Cycles, zero weights and ties are all fine.

  5. A directed graph has a topological ordering if and only if it is:

    1. Strongly connected
    2. Weighted
    3. Acyclic (a DAG)
    4. A tree with one root
    Show answer

    Answer: C. Acyclic (a DAG)

    A topological order puts every edge u to v with u before v. A cycle would need some vertex to come before itself, so an order exists exactly when the graph has no directed cycle. A DAG need not be connected or tree-shaped.

  6. A minimum spanning tree (MST) of a connected undirected graph with V vertices has how many edges?

    1. V
    2. V - 1
    3. V + 1
    4. 2V - 2
    Show answer

    Answer: B. V - 1

    Any spanning tree connects all V vertices with no cycle, which takes exactly V - 1 edges. One more edge would create a cycle; one fewer would leave the graph disconnected.

  7. What is the time complexity of BFS on a graph stored as an adjacency list, with V vertices and E edges?

    1. O(V^2)
    2. O(E log V)
    3. O(V + E)
    4. O(V * E)
    Show answer

    Answer: C. O(V + E)

    Each vertex is enqueued and dequeued once (O(V)), and each adjacency list is scanned once, which totals O(E) (2E entries for undirected graphs). With an adjacency matrix it becomes O(V^2).

  8. What is the running time of Dijkstra's algorithm with a binary-heap priority queue on an adjacency list?

    1. O(V^2)
    2. O(V + E)
    3. O((V + E) log V)
    4. O(V * E)
    Show answer

    Answer: C. O((V + E) log V)

    There are V extract-min operations and up to E decrease-key (or push) operations, each O(log V) on a binary heap: O((V + E) log V). The simple array version is O(V^2), which is better for dense graphs; Fibonacci heaps give O(E + V log V).

  9. Which data structure lets Kruskal's algorithm check quickly whether adding an edge would create a cycle?

    1. Disjoint-set (union-find)
    2. Stack
    3. Binary search tree
    4. Adjacency matrix
    Show answer

    Answer: A. Disjoint-set (union-find)

    Kruskal keeps a forest. An edge (u, v) makes a cycle exactly when u and v are already in the same component, which union-find answers with find(u) == find(v) in near-constant amortised time.

  10. How does Prim's algorithm differ from Kruskal's when building an MST?

    1. Prim works on directed graphs; Kruskal only on undirected
    2. Prim allows cycles and removes them at the end
    3. Prim grows one tree from a start vertex; Kruskal adds the cheapest safe edge anywhere, merging a forest
    4. Kruskal needs a start vertex; Prim does not
    Show answer

    Answer: C. Prim grows one tree from a start vertex; Kruskal adds the cheapest safe edge anywhere, merging a forest

    Prim always adds the cheapest edge leaving the current tree, so there is one connected tree at every step. Kruskal sorts all edges and accepts each one that joins two different components, so it may hold several trees until the end. Both are greedy and both find an MST.

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.