Open In App

Algorithms | Graph Traversals | Question 2

Traversal of a graph is different from tree because

(A)



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

(B)



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

(C)

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

(D)

All of the above


Answer: (A)
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.

 

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :