Which of the following algorithms is used to detect a cycle in a graph?
a) Depth-First Search (DFS)
b) Breadth-First Search (BFS)
c) Dijkstra’s Algorithm
d) Kruskal’s Algorithm
Answer:
a) Depth-First Search (DFS)
Explanation:
Depth-First Search (DFS) is commonly used to detect cycles in a graph. By tracking visited nodes and using a recursive or iterative approach, DFS can identify back edges, which indicate a cycle in the graph.
In an undirected graph, a cycle is detected when DFS encounters a vertex that has already been visited and is not the parent of the current vertex. In a directed graph, a cycle is found when DFS encounters a vertex that is still in the recursion stack.
Cycle detection is important in algorithms for topological sorting, deadlock detection, and graph traversal.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers