A rigorous proof of an algorithm for finding the minimum number of interchanges is strictly proof, not taken for granted

Source: Internet
Author: User

Problem Description:

There is an array of 1~n, but this sequence has been scrambled to order, if we just "exchange any two elements", then to implement the elements from the 1~n order, "the minimum number of exchanges?" ”

Solution process:

First of all, we can write a simple case to try, such as arranging: 4 3 1 2, the number of exchanges = 3; we can test in multiple groups, the edge of testing, the real implementation needs to meet: 4 should be 2, 2 should be 3, 3 should be 1, 1 should be to 4, just a loop.

So, consider:

Lemma: Is there a minimum number of exchanges equal to 1 of elements in order to satisfy such a circular arrangement?

Mathematical induction can be considered.

First k=1,2,3 When we can know is established;

Assuming that when n <=k, the minimum number of exchanges for this single loop arrangement is n-1;

Consider the situation of n=k+1: At this time, we arbitrarily exchange the k+1 of the two elements, we will find that the single cycle split into two single loops (such as above, 4 and 1 after the exchange, the sequence becomes 1 3 4 2 then left <1> and <3 4 2> two sub-sequences, <1> exchange The number of times =0 <3 4 2> interchange = 2, then the total Exchange is 3 times, if we exchange 1 and 2 (this exchange does not have any element in the bit), the remainder is the sequence 4 3 2 1, is two single-cycle sequence <4 1> and <3 2>, the total Exchange or 1+ (1 + 1) = 3 = 4-1), then there are two different approaches:

Method 1: Perform recursive calls to two single-loop sequences, the minimum number of interchanges is 1 + (number of sequence 1-1) + (number of sequence 2-1) = total number of elements-1

Method 2: We merge the separate two single-cycle sequence, that is, the elements in sequence 1 and sequence 2 in the element Exchange, after the exchange of a new composition sequence, the exchange of the original sequence is separated and merged, in vain to Exchange 2 times, the remainder of the sequence is k+1 a single cycle, this exchange is obviously not the optimal mode of exchange

In this view, the exchange of Method 1 is the way to the least exchange.

Description when N=k + 1 o'clock, we can be better in accordance with the Mode 1 exchange, at this time can guarantee the number of exchanges =k=n-1 times

At this point we prove that: single cycle sequence, the minimum number of exchanges is n-1 times.

scenarios where multiple single-loop sequences are considered

Because of the different two single-cycle sequence merge exchange called a single-cycle sequence, the above method 2 indicates that the invalid exchange is actually

Therefore, the exchange of multiple single-cycle sequences is the sum of the number of times each single-cycle sequence is exchanged.

For example 4 3 2 1 7 6 5

is two single-cycle sequence <4 3 2 1> <7 6 5> The sum of their respective exchanges = 3 + 2 = 5 = 7-The number of single-cycle sequences

It can be seen that, for an n-length sequence, the optimal number of exchanges is the number of =n-n decomposed into a single cycle.


Understand this process, the code is naturally a cloud ...






A rigorous proof of an algorithm for finding the minimum number of interchanges is strictly proof, not taken for granted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.