Top MCQs on Graph Traversals with Answers

Graph traversal is a fundamental operation in graph theory and computer science that involves visiting all the vertices (nodes) of a graph in a systematic way. There are two main methods for graph traversal: depth-first traversal and breadth-first traversal.

Graph Traversal

Graph Traversal

Question 1

Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a given graph ?

Tick

Depth First Search

Cross

Breadth First Search

Cross

Prim\'s Minimum Spanning Tree Algorithm

Cross

Kruskal\' Minimum Spanning Tree Algorithm



Question 1-Explanation: 

To find cycle in a graph we can use the Depth First Traversal (DFS) technique. It is based on the idea that there is a cycle in a graph only if there is a back edge [i.e., a node points to one of its ancestors] present in the graph.

To detect a back edge, we need to keep track of the nodes visited till now and the nodes that are in the current recursion stack [i.e., the current path that we are visiting]. If during recursion, we reach a node that is already in the recursion stack, there is a cycle present in the graph.

Hence Option (A) is the correct answer.

Question 2

Traversal of a graph is different from tree because

Tick

There can be a loop in graph so we must maintain a visited flag for every vertex

Cross

DFS of a graph uses stack, but inorder traversal of a tree is recursive

Cross

BFS of a graph uses queue, but a time efficient BFS of a tree is recursive.

Cross

All of the above



Question 2-Explanation: 

Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain cycles (a node may be visited twice). To avoid processing a node more than once, use a boolean visited array. A graph can have more than one DFS traversal.

Hence Option (A) is the correct answer.


 

Question 3

What are the appropriate data structures for following algorithms?

1) Breadth-First Search                           
2) Depth First Search                            
3) Prim's Minimum Spanning Tree                 
4) Kruskal' Minimum Spanning Tree                

Cross
  •  Stack
  •  Queue
  • Priority Queue
  • Union Find
Tick
  • Queue
  • Stack
  • Priority Queue
  • Union Find
Cross
  • Stack
  • Queue
  • Union Find
  • Priority Queue 
Cross
  • Priority Queue
  • Queue
  • Stack
  • Union Find


Question 3-Explanation: 
Question 4

The Breadth First Search algorithm has been implemented using the queue data structure. One possible order of visiting the nodes of the following graph is 
 

 

Cross

MNOPQR

Cross

NQMPOR

Tick

QMNPRO

Cross

QMNPOR



Question 4-Explanation: 

Option (A) is MNOPQR. It cannot be a BFS as the traversal starts with M, but O is visited before N and Q. In BFS all adjacent must be visited before adjacent of adjacent. Option (B) is NQMPOR. It also cannot be BFS, because here, P is visited before O. (C) and (D) match up to QMNP. We see that M was added to the queue before N and P (because M comes before NP in QMNP). Because R is M\'s neighbor, it gets added to the queue before the neighbor of N and P (which is O). Thus, R is visited before O.

Hence (C) is the correct answer.

Question 5

Let G be an undirected graph. Consider a depth-first traversal of G, and let T be the resulting depth-first search tree. Let u be a vertex in G and let v be the first new (unvisited) vertex visited after visiting u in the traversal. Which of the following statements is always true? (GATE CS 2000)

Cross

{u,v} must be an edge in G, and u is a descendant of v in T

Cross

{u,v} must be an edge in G, and v is a descendant of u in T

Tick

If {u,v} is not an edge in G then u is a leaf in T

Cross

If {u,v} is not an edge in G then u and v must have the same parent in T



Question 5-Explanation: 
In DFS, if \'v\' is visited
after \'u\', then one of the following is true.
1) (u, v) is an edge.
     u
   /   \\
  v     w
 /     / \\
x     y   z

2) \'u\' is a leaf node.
     w
   /   \\
  x     v
 /     / \\
u     y   z 

In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited children (u is leaf), then control goes back to parent and parent then visits next unvisited children.

Hence (C) is the correct answer.

Question 6
Consider the following graph,

Among the following sequences:
(I) a b e g h f 
(II) a b f e h g
(III) a b f h g e 
(IV) a f g h b e  
Which are depth first traversals of the above graph?
Cross
I, II and IV only
Cross
I and IV only
Cross
II, III and IV only
Tick
I, III and IV only


Question 6-Explanation: 
We can check all DFSs for following properties.
In DFS, if a vertex \'v\' is visited
after \'u\', then one of the following is true.
1) (u, v) is an edge.
     u
   /   \\
  v     w
 /     / \\
x     y   z

2) \'u\' is a leaf node.
     w
   /   \\
  x     v
 /     / \\
u     y   z
In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited children (u is leaf), then control goes back to parent and parent then visits next unvisited children.
Question 7

Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Which of the following standard graph algorithms is used by Make.

Cross

Strongly Connected Components

Tick

Topological Sorting

Cross

Breadth First Search

Cross

Dijkstra\'s Shortest Path



Question 7-Explanation: 

Make can decide the order of building software using topological sorting. Topological sorting produces the order considering all dependencies provide by makefile. See following for details. Topological Sorting

Hence Option(B) is the correct answer.

Question 8

Given two vertices in a graph s and t, which of the two traversals (BFS and DFS) can be used to find if there is path from s to t?

Cross

Only BFS

Cross

Only DFS

Tick

Both BFS and DFS

Cross

Neither BFS nor DFS



Question 8-Explanation: 

We can use both traversals to find if there is a path from s to t.

Hence Option(C) is the correct answer.

Question 9

Which of the following condition is sufficient to detect cycle in a directed graph?

Cross

There is an edge from currently being visited node to an already visited node.

Tick

There is an edge from currently being visited node to an ancestor of currently visited node in DFS forest.

Cross

Every node is seen twice in DFS.

Cross

None of the above



Question 9-Explanation: 

To find cycle in a directed graph we can use the Depth First Traversal (DFS) technique. It is based on the idea that there is a cycle in a graph only if there is a back edge [i.e., a node points to one of its ancestors] present in the graph.

To detect a back edge, we need to keep track of the nodes visited till now and the nodes that are in the current recursion stack [i.e., the current path that we are visiting]. If during recursion, we reach a node that is already in the recursion stack, there is a cycle present in the graph.

If the graph is disconnected then get the DFS forest and check for a cycle in individual trees by checking back edges.

Hence Option(B) is the correct answer.

Question 10

Is following statement true/false If a DFS of a directed graph contains a back edge, any other DFS of the same graph will also contain at least one back edge.

Tick

True

Cross

False



Question 10-Explanation: 

A back edge means a cycle in graph. So if there is a cycle, all DFS traversals would contain at least one back edge.

There are 30 questions to complete.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads