Data Structures & AlgorithmsVery hard

When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the minimum?

  1. A. Breadth-first search always finds the longest path.
  2. B. Swaps have different costs depending on the element.
  3. C. The first visited state is always the target.
  4. D. Every swap has the same cost of one, so breadth-first search visits states in nondecreasing number of swaps.

Correct answer: D. Every swap has the same cost of one, so breadth-first search visits states in nondecreasing number of swaps.

Unit-cost edges make breadth-first order equal to distance order, so the first time the target state is dequeued the count is minimal. This is why BFS suits small swap-counting puzzles.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic