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?
- A. Breadth-first search always finds the longest path.
- B. Swaps have different costs depending on the element.
- C. The first visited state is always the target.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer…
- Closed real intervals [(1, 4), (3, 6), (8, 9), (9, 12), (14, 15)] are merged whenever they overlap (sharing an endpoint counts as…
- Over all pairs of DIFFERENT positions of [2, 5, 3, 8, 4], take the product of each pair and sort all these products. What is the 6-th…
- Starting from [9, 6, 15, 4], repeat 3 times: take the current LARGEST element, replace it by its floor half, and keep the others. What is…
- How many substrings of "cbaebabacd" of length 3 are anagrams of "abc" (contain exactly the same letters with the same counts, in any order)?
- In "abababaabab", what is the maximum number of NON-OVERLAPPING occurrences of "aba" that can be chosen (left to right, each occurrence…