Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q11 — Graph Traversals (BFS, DFS)

Explain with AI
Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex tt at a distance four from the root. If tt is the nthn^{\text{th}} vertex in this BFS traversal, then the maximum possible value of nn is __________
View worked solution

Answer: 31

To find the maximum possible value of nn (the position of vertex tt in the BFS traversal), we need to maximize the number of vertices visited before tt.

In a BFS traversal of a tree starting from the root, vertices are visited level by level. Let the root be at Level 0.

  • A vertex tt at a distance of 44 from the root lies at Level 4.
  • All vertices at Levels 0, 1, 2, and 3 must be visited before any vertex at Level 4 is visited.

To maximize the index nn of tt, we must maximize:

  1. The number of vertices in Levels 0 to 3.
  2. The number of vertices in Level 4 that are visited before tt.

Since the graph is a binary tree, the maximum number of vertices at any level ii is 2i2^i:

  • Level 0: 20=12^0 = 1 vertex
  • Level 1: 21=22^1 = 2 vertices
  • Level 2: 22=42^2 = 4 vertices
  • Level 3: 23=82^3 = 8 vertices

The maximum number of vertices in Levels 0 through 3 is: 1+2+4+8=151 + 2 + 4 + 8 = 15

At Level 4, there can be a maximum of 24=162^4 = 16 vertices. To make tt the last vertex visited at Level 4, we assume Level 4 is fully populated and tt is the rightmost (last) vertex visited at this level.

Thus, the position nn of tt in the BFS traversal is: n=(Vertices in Levels 0 to 3)+(Vertices in Level 4 up to and including t)n = \text{(Vertices in Levels 0 to 3)} + \text{(Vertices in Level 4 up to and including } t\text{)} n=15+16=31n = 15 + 16 = 31

Therefore, the maximum possible value of nn is 31.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Graph Traversals (BFS, DFS) questions