Graphs Questions with Answers

109 practice questions on Graphs (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 109 free in the AptiNest app.

Sample questions

Question 1Easy

Which property guarantees that a graph's MST is unique?

  1. The graph is directed
  2. The graph is bipartite
  3. The graph has no cycles of length 3
  4. All edge weights are distinct

Answer: D. All edge weights are distinct

Explanation

With distinct weights, every cut has a unique lightest edge, which forces the tree.

Step-by-step solution

  1. If all edge weights differ, there is no tie between choices.
  2. Every cut has a unique lightest edge.
  3. So the MST is unique.

Why the other options are wrong

  • A. MSTs are defined for undirected graphs.
  • B. Bipartiteness is unrelated.
  • C. Triangle-free graphs can still have equal weights giving several MSTs.

Question 2Easy

What is the time complexity of Floyd-Warshall?

  1. O(V^3)
  2. O(E log V)
  3. O(V^2)
  4. O(VE)

Answer: A. O(V^3)

Explanation

Three nested loops over all vertices for k, i and j.

Step-by-step solution

  1. Three nested loops: intermediate k, source i, target j.
  2. Each runs V times.
  3. O(V^3).

Why the other options are wrong

  • B. That is a heap-based bound.
  • C. That is just the matrix size.
  • D. That is Bellman-Ford.

Tip: All-pairs, small graph: Floyd-Warshall O(V^3).

Question 3Easy

A directed graph on vertices 0-3 has this adjacency matrix (row = from, column = to). What are the in-degree and out-degree of vertex 2?

0 1 1 0
0 0 1 1
1 0 0 1
0 0 0 0
  1. in-degree 3, out-degree 2
  2. in-degree 2, out-degree 2
  3. in-degree 1, out-degree 2
  4. in-degree 2, out-degree 1

Answer: B. in-degree 2, out-degree 2

Explanation

Column 2 has 1s in rows 0 and 1 (in-degree 2);
row 2 has 1s in columns 0 and 3 (out-degree 2).

Step-by-step solution

  1. Out-degree = number of 1s in row 2: 1 0 0 1 -> 2.
  2. In-degree = number of 1s in column 2: rows 0 and 1 -> 2.
  3. Vertex 2: in 2, out 2.

Why the other options are wrong

  • A. Column 2 has only two 1s.
  • C. Rows 0 and 1 both point to 2.
  • D. Row 2 has two 1s (to 0 and 3).

Tip: Row = out, column = in.

Question 4Easy

A tree with n vertices has how many edges?

  1. n(n - 1)/2
  2. n - 1
  3. n
  4. n + 1

Answer: B. n - 1

Explanation

A tree is connected and acyclic; each vertex except the root has exactly one parent edge.

Step-by-step solution

  1. A tree is connected and acyclic.
  2. Starting from one vertex, each edge adds exactly one new vertex.
  3. n - 1 edges.

Why the other options are wrong

  • A. That is a complete graph.
  • C. n edges always create a cycle.
  • D. Even more edges.

Question 5Medium

What is the order in which recursive DFS visits the vertices, starting from 3, in this undirected graph (neighbours are visited in increasing/alphabetical order)?

Edges: 1-4, 1-6, 4-2, 6-2, 6-3, 2-5, 3-5, 3-8, 5-7, 8-7
  1. 3 5 2 4 1 6 7 8
  2. 1 2 3 4 5 6 7 8
  3. 3 8 7 5 2 6 1 4
  4. 3 5 6 8 2 7 1 4

Answer: A. 3 5 2 4 1 6 7 8

Explanation

Recursive DFS goes as deep as possible along the smallest unvisited neighbour before backtracking: 3 5 2 4 1 6 7 8.

Step-by-step solution

  1. 3 -> 5 (smallest of 5, 6, 8); 5 -> 2; 2 -> 4; 4 -> 1; 1 -> 6.
  2. Backtrack to 5 -> 7; 7 -> 8.
  3. Order 3 5 2 4 1 6 7 8.

Why the other options are wrong

  • B. Sorted order, not DFS.
  • C. It starts with 3 but then goes to 8, though 5 is the smallest neighbour of 3.
  • D. That is the BFS order, not DFS.

Question 6Medium

Word ladder: change one letter at a time, each intermediate word must be in {hot, dot, dog, lot, log, cog}. How many words are in the shortest sequence from "hit" to "cog", counting both ends?

  1. 6
  2. 5
  3. 7
  4. 4

Answer: B. 5

Explanation

BFS on words: hit -> hot -> dot -> dog -> cog, which has 5 words.

Step-by-step solution

  1. Each word is a vertex; edges join words differing in one letter.
  2. BFS from hit: hot (1), dot and lot (2), dog and log (3), cog (4).
  3. Path hit -> hot -> dot -> dog -> cog has 5 words.

Why the other options are wrong

  • A. There is a shorter sequence of 5 words.
  • C. Counts more steps than BFS needs.
  • D. That counts the 4 changes, not the words.

Question 7Medium

BFS from vertex 0 on a complete binary tree with 31 vertices (vertex 0 is the root) visits how many vertices at distance exactly 3?

  1. 16
  2. 8
  3. 7
  4. 4

Answer: B. 8

Explanation

Level k of a complete binary tree has 2^k vertices, so distance 3 has 8.

Step-by-step solution

  1. Level k of a complete binary tree has 2^k vertices.
  2. 31 = 1 + 2 + 4 + 8 + 16, five full levels.
  3. Distance 3 = level 3: 2^3 = 8.

Why the other options are wrong

  • A. 16 is level 4 (the leaves).
  • C. 7 counts all vertices up to level 2.
  • D. 4 is level 2.

Question 8Medium

What is the total weight of the MST found by Prim's algorithm (starting anywhere) on this graph?

Edges: A-B (2), A-C (3), B-C (1), B-D (4), C-D (5), C-E (6), D-E (7), D-F (3), E-F (2)
  1. 13
  2. 14
  3. 11
  4. 12

Answer: D. 12

Explanation

Prim from A adds A-B (2), B-C (1), B-D (4), D-F (3), F-E (2), total 12. Any start vertex gives the same total.

Step-by-step solution

  1. Start at A: add A-B (2).
  2. Add B-C (1); cheapest edge out now is B-D (4).
  3. From D add D-F (3), then F-E (2).
  4. Total = 2 + 1 + 4 + 3 + 2 = 12.

Why the other options are wrong

  • A. Picks A-C (3) or another heavier edge instead of one of the MST edges.
  • B. 14 is not minimal; a spanning tree of weight 12 exists.
  • C. An MST on 6 vertices needs 5 edges; 11 is not achievable.

Question 9Hard

Kruskal's algorithm processes edges by increasing weight (ties broken alphabetically). Which edge is the FIRST to be rejected because it would create a cycle?

Edges: A-B (7), A-D (5), B-C (8), B-D (9), B-E (7), C-E (5), D-E (15), D-F (6), E-F (8), E-G (9), F-G (11)
  1. B-C (8)
  2. A-B (7)
  3. B-E (7)
  4. B-D (9)

Answer: A. B-C (8)

Explanation

Edges accepted before the first rejection: A-D (5), C-E (5), D-F (6), A-B (7), B-E (7). Then B-C joins two vertices already connected.

Step-by-step solution

  1. Accept A-D 5, C-E 5, D-F 6, A-B 7, B-E 7.
  2. Next is B-C 8: B and C are already connected (B-E-C).
  3. So B-C is the first rejected.

Why the other options are wrong

  • B. A-B is accepted.
  • C. B-E is accepted.
  • D. B-D is rejected later.

Question 10Hard

In the same DFS, how is the edge 5 -> 2 classified?

Edges: 1->2, 1->3, 2->4, 3->4, 4->5, 5->2, 3->6, 6->5
  1. Forward edge
  2. Back edge
  3. Cross edge
  4. Tree edge

Answer: B. Back edge

Explanation

When 5 explores 2, vertex 2 is an ancestor still on the recursion stack, so 5 -> 2 is a back edge and the graph has a cycle 2 -> 4 -> 5 -> 2.

Step-by-step solution

  1. DFS path 1 -> 2 -> 4 -> 5.
  2. When 5 looks at 2, vertex 2 is still on the stack (discovered, not finished).
  3. An edge to an active ancestor is a back edge; it closes the cycle 2 -> 4 -> 5 -> 2.

Why the other options are wrong

  • A. A forward edge goes to an already finished descendant.
  • C. A cross edge goes to a finished vertex that is not an ancestor.
  • D. 2 was already discovered, so this edge cannot be a tree edge.

Tip: Edge to a grey (active) vertex = back edge = cycle.

Practise all 109 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics