Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B95902098 周于荃.

Similar presentations


Presentation on theme: "Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B95902098 周于荃."— Presentation transcript:

1 Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B95902098 周于荃

2 ABSTRACT & BASIC DEFINITION

3 Abstract The final destination of each item is known. Just like hand-sort.

4 Abstract We want to show that in the worst case, the algorithm will terminate after 2 n-1 -1 steps.

5 Basic definition π is a permutation with n items. Each item is π (1)~ π (n). Any number i with π (i) ≠ i may be “placed” in its proper position. The numbers in positions between i an π (i) shifted up or down as necessary.

6 Basic definition

7

8 We call this operation “Homing”.

9 FAST HOMING

10 Fast homing A placement of either the least or the greatest number not currently in its home will be called extremal. The number i will never subsequently be dislodged from its home.

11 Fast homing - Theorem 4.1 Any algorithm that always places the smallest or largest available number will terminate in at most n- 1 steps. It’s trivial. Just like hand-sort file.

12 Fast homing - Theorem 4.1 The precise number of steps required is the smallest j such that the files which belong in positions j+1, j+2, …,n are already in the correct order.

13 Fast homing - Theorem 4.2 The expected number of steps required by random homing from π with n items is at most (n(n+1) - 2)/4. Proof : Mathematic. (#) We say a permutation is in stage k when k of the extremal number are home. Ex. 1,2,3,7,4,6,5,8,9 => stage 5.

14 Fast homing - Theorem 4.3 Let k be the length of the longest increasing subsequence in π. Then no sequence of fewer than n - k placements can sort π. Proof : Otherwise…

15 Fast homing - Corollary 4.1 The reverse permutation n,…,1 requires n-1 steps. Easy consequence by theorem 4.3.

16 Fast homing - Theorem 4.4 The reverse permutation is the only case requiring n-1 steps. By induction on n (#).

17 SLOW HOMING

18 Slow Homing Let’s home it with lots of idiot steps… 2,3,4,5,…,n-1,n,1 This permutation will take 2 n-1 -1 if we always place the left-most not-at-home numbers. Familiar with “tower of hanoi” pattern.

19 Slow homing Larson conjectured that 2 n-1 -1 is the maximum. Indeed, although many other, more complex, permutations can also support 2 n-1 -1 steps, none permit more.

20 Slow homing – Theorem 5.1 Homing always terminates in at most 2 n-1 -1steps. To prove this, we need lots of lemma and backward analysis.

21 Slow homing – Evicting Reverse of homing. Choose a number which is at home and displace it. Our goal is to prove that begin with the identity permutation {1,2,…,n}, at most 2 n-1 -1 displacements are possible. By induction on n, trivial when n = 1. Supposed that it’s true when k< n.

22 Slow homing – Lemma 5.1 After2 n-2 displacements, both 1 and n have been displaced and will never be displaced again. Otherwise… About never being displaced again, it’s trivial when we think it as a reverse of homing.

23 Slow homing – code and weight We associate with each permutation π a code α ( π ), and with each code α, a weight w = w( α ). The code is a sequence α = (a 2, a 3,…, a n-1 ) of length | α | = n-2 from the alphabet.{+,-,0}.

24 Slow homing – code and weight a i = “+” if π -1 (i) > i. a i = “ -” if π -1 (i) < i. a i = “0” if π -1 (i) = i.

25 Slow homing – code and weight The weight w( α ) is defined for codes of all lengths by recursion. If a i = 0 for each i, we put w( α ) = 0. For each i ◦ a i = “-”, let d i = i - 2. ◦ a i = “+”, let d i = n-1-i. Thus, d i represents the number of symbols to the left of a - or to the right of a +.

26 Slow homing – code and weight

27 Let i be the index maximizing d i. Let α [i] the code of length | α | -1 obtained by deleting the i th entry of α. w( α ) = w( α [i]) + 2 di.

28 Slow homing – code and weight

29 Slow homing – Lemma 5.2 The minimum of w( α ) over codes α of length k is 0, for the all-0 code, and the maximum is 2 k -1, for codes of the form + p - q.

30 Slow homing – Lemma 5.3 Let α = β + p γ - q δ where | β | = | δ |, β contains no “+”, δ contains no “-”, and γ neither begins with + nor ends with -. Then w( α ) = w( βγδ ) +2 p+| γ |+q+| β | - 2 | γ |+| β |. Proof: mathematic.

31 Slow homing – Corollary 5.1 The definition of the weight of a code does not depend on how ties are broken when d i = d j. Proof: by lemma 5.3.

32 Slow homing – Lemma 5.4 For any codes γ and δ, where γ has no +, w( γδ 0) ≦ w( γδ ) + 2 | δ | - 1. Proof : mathematic.

33 Slow homing – Lemma 5.5 Let α be any code, and β = (b 1, …, b n-1 ) the result of changing some a i = 0 to b i = + or b i = -. Then w( β ) > w( α ).

34 Slow homing – Proof of Lemma 5.5 The derivations of w( β ) and w( α ) are the same until b i is stripped. Let β ’ and α ’ be the corresponding codes at that point (before b i is stripped). We can write β ’ as γδ b i ε. Where γ contains no +, ε no -, and | ε | ≦ | γ |.

35 Slow homing – Proof of Lemma 5.5 w( β ’) = w( γδε ) + 2 | γ |+| δ | = w( γδ 0 | ε | ) + w( ε ) + 2 | γ |+| δ | ≧ w( γδ 0 | ε |+1 ) – (2 | δ |+| ε | - 1) + w( ε ) + 2 | γ |+| δ | (by Lemma 5.4) > w( γδ 0 | ε |+1 ) + w( ε ) = w( γδ 0 ε ) = w( α ’):

36 Slow homing – Lemma 5.6 Let π be any permutation of {1, …, n} in which π (1) ≠ 1 and π (n) ≠ n, and let π ‘ be the result of applying some displacement to π. then w( α ( π ’)) > w( α ( π )).

37 Slow homing – Proof of Lemma 5.6 A displacement on position i will cause the code α change from 0 to “+”or”-”. Assume α changes into “-”. This displacement will also cause some “-”changing into 0, or some 0 into “+”. We don’t care about “0=>+” because Lemma 5.5 told us it will only increase weight.

38 Slow homing – Proof of Lemma 5.6 Those number whose code change into 0 with initial code “-” is less than i. In extreme case, we can assume that all the”-” less than i change into 0. Let j be position of the right-most “-” less than i. Let 2 k be the contribution to of the “-” in the position i in th computation of w( α ’).

39 Slow homing – Proof of Lemma 5.6 If there are any + between a j and a i that are stripped after the “-” in position i in α ’, then their contribution to w( α ’) is less than their contribution to w( α ). Let “t” be the number of such “+”. The total contribution of those “+” to w( α ’) is at most 2 k-1 + 2 k-2 +…+2 k-t = 2 k-t (2 t – 1). The difference between their contribution is 2 k-t (2 t – 1), too.

40 Slow homing – Proof of Lemma 5.6 The total contribution to w( α ) of the ”-” less than a i in α is at most 2 k-t -1, since each adds a different power of 2 less than 2 k-t. In conclude that w( α ’) ≧ w( α ) + 2 k – (2 k-t -1) - 2 k-t (2 t – 1) > w( α ).

41 Slow homing – Conclusion Theorem 5.1 is an easy consequence of Lemmas 5.1 5.2 and 5.6. Finally we find out that Homing always terminates in at most 2 n-1 -1steps.

42 COUNTING BAD CASES

43 Counting bad cases With the analysis above, we can try to find out what permutation are they whose worst case take 2 n-1 -1steps.

44 Counting bad cases Let the worst case permutation set be M n. Let height h( π ) means the steps taken from identity to π. Let τ n denote the permutation as followed => n, 2, 3, …, n-1, 1

45 Counting bad cases – Lemma 6.1 h( τ n ) = 2 n-2 Proof : Place n first and that will leave a permutation as 2, 3, …, n-1, 1. In the above analysis, we know that h(2,3,…,n-1,1) = 2 n-2 -1.

46 Counting bad cases – Lemma 6.2 For any permutation with code α = + i 0 k - j, there is a sequence of 2 k-1 displacements that ends in a permutation with code + i 0 k-1 - j+1. Moreover, all the displacements in the sequence are unique, except for possibly the last one.

47 Counting bad cases – Lemma 6.2 Induction on k.

48 Counting bad cases Since w(+ i 0 k-1 - j+1 ) = w(+ i 0 k - j ) + 2 k-1, and by Lemma 6.2 we know that from + i 0 k - j to + i 0 k- 1 - j+1 takes 2 k-1 displacement. So the weight should increase by one at each step. This condition can only hold when the sequence of displacement is the one described above.

49 Counting bad cases - Firing We call the sequence of displacement as above “firing” i+k+1 “to left”. If the last displacement is done into position i-1, we call that “shortest firing”. In symmetric, there are firing to right.

50 Counting bad cases – Lemma 6.3 A permutation belongs to M n if and only if it can be obtained from τ n by successively applying n-2 left and right firings.

51 Counting bad cases – Lemma 6.3 (<=) α ( τ n ) = 0 n-2 The first firing transforms this code into 0 n-3 - using 2 n-3 displacements. The second firing using 2 n-4, and so on. After n-2 firing, the total number of displacement come to 2 n-2 -1,ending with permutation σ whose code is + k - n-2-k.

52 Counting bad cases – Lemma 6.3 By Lemma 6.1,h( σ ) ≧ 2 n-2 +2 n-2 -1= 2 n-1 -1 By Theorem 5.1, this is an equality. σ belongs to M n.

53 Counting bad cases – Lemma 6.3 (=>) By Lemma 6.1,any permutation of height 2 n-1 -1 has to be obtained from τ n in 2 n-2 -1 displacements. By Lemma 5.6, each one increasing weight is with one step.

54 Counting bad cases – Lemma 6.3 If the first displacement on τ n introduces a - to the code, then the first 2 n-3 displacements must constitute a left firing. Otherwise there will be a jump increasing on the weight. Then 2 n-4 displacements constitute the second left firing, and so on.

55 Counting bad cases - Corollary 6.1 For n ≧ 2, | M n | ≦ (n-1)! It follows from Lemma 6.3, with the fact that there are n+1 choice for the last step of firing.

56 Counting bad case – Proposition6.1 | M n | ≧ 2 n-2 We show that if we start from τ n and perform only short firings, then no permutation is obtained in more than one way.

57 Counting bad case – Proposition6.1

58 MY PRESENT IS OVER.


Download ppt "Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B95902098 周于荃."

Similar presentations


Ads by Google