Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

Edges A-E, D-E, B-E, A-C, A-B are added one by one to an initially empty graph on vertices {A, B, C, D, E}, using Union-Find (with path compression) to detect cycles. Which edge is the first to create a cycle?

  1. A. A-C
  2. B. B-E
  3. C. A-B
  4. D. D-E

Correct answer: C. A-B

Track components with Union-Find: every edge up to A-B joins two different components; A-B joins two nodes already in the same component, closing a cycle.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic