Algorithms | Graph Traversals | Question 1
Which of the following algorithms can be used to most efficiently determine the presence of a cycle in a given graph ?
(A)
Depth First Search
(B)
Breadth First Search
(C)
Prim\’s Minimum Spanning Tree Algorithm
(D)
Kruskal\’ Minimum Spanning Tree Algorithm
Answer: (A)
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.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...