Pancake Flipping with Two Spatulas

# Pancake Flipping with Two Spatulas

Masud Hasan Department of Computer Science and Engineering
Atif Rahman Department of Computer Science and Engineering
M. Sohel Rahman Department of Computer Science and Engineering
Algorithm Design Group, Department of Computer Science
King’s College London, Strand, London WC2R 2LS, England
Mahfuza Sharmin Department of Computer Science and Engineering
Rukhsana Yeasmin Department of Computer Science and Engineering
###### Abstract

In this paper we study several variations of the pancake flipping problem, which is also well known as the problem of sorting by prefix reversals. We consider the variations in the sorting process by adding with prefix reversals other similar operations such as prefix transpositions and prefix transreversals. These type of sorting problems have applications in interconnection networks and computational biology. We first study the problem of sorting unsigned permutations by prefix reversals and prefix transpositions and present a 3-approximation algorithm for this problem. Then we give a 2-approximation algorithm for sorting by prefix reversals and prefix transreversals. We also provide a 3-approximation algorithm for sorting by prefix reversals and prefix transpositions where the operations are always applied at the unsorted suffix of the permutation. We further analyze the problem in more practical way and show quantitatively how approximation ratios of our algorithms improve with the increase of number of prefix reversals applied by optimal algorithms. Finally, we present experimental results to support our analysis.

Keywords: Approximation algorithms, pancake flipping, sorting by prefix reversals and prefix transpositions, adaptive approximation ratio, interconnection network, computational biology.

## 1 Introduction

Given a permutation , a reversal reverses a substring of , a transposition cuts a substring of and pastes it in a different location, and a transreversal is a transposition of a substring with a reversal done before it is pasted. In a prefix reversal/transposition/transreversals the corresponding substring is always a prefix of .

The pancake flipping problem [1, 2, 3, 4, 5] deals with finding the minimum number of prefix reversals (i.e., flips) required to sort a given permutation. This problem was first introduced in 1975 by [1] which describes the motivation of a chef to rearrange a stack of pancakes from the smallest pancake on the top to the largest one on the bottom by grabbing several pancakes from the top with his spatula and flipping them over, repeating them as many times as necessary.

Aside from being an interesting combinatorial problem, this problem and its variations have applications in interconnection networks and computational biology. The number of flips required to sort the stack of pancakes is the diameter of the -dimensional pancake network [4, 5]. The diameter of a network is the maximum distance between any pair of nodes in the network and corresponds to the worst communication delay for broadcasting messages in the network [4, 5]. A well studied variation of pancake flipping problem is the burnt pancake flipping problem [2, 4, 5] where each element in the permutation has a sign, and the sign of an element changes with reversals. Pancake and burnt pancake networks have better diameter and better vertex degree than the popular hypercubes [4]. There exists some other variations of pancake flipping, giving different efficient interconnection networks [2].

A broader class consisting of similar sorting problems, called the genome rearrangement problems, are extensively studied in computational molecular biology, where the orders of genes in two species are represented by permutations and the problem is to transform one into another by using minimum number of pre-specified rearrangement operations. In order to explain the existence of essentially the same set of genes but differences in their order in different species, several rearrangement operations have been suggested, including reversals [6, 7, 8], block interchange [9], transpositions [10, 11, 12], transreversals [13], fission and fusion [14], prefix transposition [15], etc.

The abovementioned sorting problems are mostly NP-complete or their complexity is unknown. Caprara [16] proved that sorting by reversals is NP-hrad, whereas Heydari and Sudborough [17] have claimed that sorting by prefix reversals is NP-complete too. The complexity of sorting by transposition and sorting by prefix transposition is still open. As a result, many approximation algorithms are known for each of these problems and their variations.

A number of authors have also considered the problem of sorting permutations by using more than one rearrangement operations (reversals, transpositions etc.) [18, 19, 20, 13, 21, 22, 23], mostly for signed permutations. Rahman et.al. [22] studied the problem of sorting permutations by transpositions and reversals, where they give an approximation algorithm with approximation ratio .

### 1.1 Our results

In this paper we study some variations of pancake flipping problem from the view point of sorting permutations. We consider the problem of sorting an (unsigned) permutation by prefix reversals and prefix transpositions. We give a 3-approximation algorithm for sorting by prefix reversals and prefix transpositions, and a 2-approximation algorithm for sorting by prefix reversals and prefix transreversals. Experimental result shows that our algorithms perform much better in practice.

Note that the problem of sorting by reversals and transpositions in [22] and the problem of sorting by prefix reversals and prefix transpositions considered in this paper are not the same and they do not imply each other.

We also introduce the concept of forward march. The idea of forward march comes naturally from a greedy approach where someone may try to sort the permutation from starting to end. While applying prefix reversals and prefix transpositions, a prefix of the given permutation may become sorted. Whenever this happens, we move forward and apply the next operation in the remaining unsorted suffix of the permutation. We give a 3-approximation algorithm for this problem which also performs better on average.

The above problems that we consider in this paper are variations of the original pancake flipping problem where the chef has two spatulas in his two free hands. He can either lift some pancakes from the top of the stack and flip them (a prefix reversal) or he can lift a top portion of the stack with one hand, lift another portion from the top with the other hand, and place the top portion under the second portion (a prefix transposition) possibly with a flip (a prefix transreversal). Also, time to time, when a top portion of the stack is sorted he can remove it from the stack (a forward march).

It is worth mentioning that the worst case ratios of our algorithms can only be realized when an optimal algorithm applies no prefix reversals at all. But it is very likely that an optimal algorithm will apply both operations. Keeping this observation in mind, we derive mathematically the equations for approximation ratio in terms of the number of prefix reversals applied by an optimal algorithm.

We organize rest of the paper as follows. In Section 2 we give the definitions and other preliminaries. In Section 34, and 5 we present the approximation algorithms. In Section 6 we derive equations for approximation ratio in terms of number of prefix reversals applied by an optimal algorithm. In Section 7 we present our experimental results. Finally, Section 8 concludes the paper.

## 2 Preliminaries

Let be a permutation of distinct elements where , and for each (the middle elements of are to be sorted). A prefix reversal for some applied to reverses the elements and thus transforms into permutation . A prefix transposition for some and some such that cuts the elements and pastes between and and thus transforms into permutation .

An identity permutation is a permutation such that for all . Given two permutations, the problem of sorting one permutation to another is equivalent to the problem of sorting a given permutation to the identity permutation. The prefix reversal and prefix transposition distance between and is the minimum number of operations such that , where each operation is either a prefix reversal or a prefix transposition . The problem of sorting by prefix reversals and prefix transpositions is, given a permutation , to find a shortest sequence of prefix reversals and prefix transpositions such that permutation transforms into the identity permutation , i.e. finding the distance .

A breakpoint for this problem is a position of a permutation such that , and . By definition, position 1 (beginning of the permutation) is always considered a breakpoint. Position (end of the permutation) is considered a breakpoint when . We denote by the number of breakpoints of permutation . Therefore, for any permutation and the only permutations with exactly one breakpoint are the identity permutations ( , for all ).

The breakpoint graph of is an undirected multi graph whose vertices are , for , and edges are of two types: grey and black. For each , the vertices and are joined by a black edge iff there is a breakpoint between them, i.e., iff . For and , there is a grey edge between and iff .

For convenience of illustration, in this paper the vertices of are drawn horizontally from left to right in the order of , the black edges are drawn by horizontal lines, and the grey edges are drawn by dotted arcs.

## 3 3-approximation algorithm for prefix reversals and prefix transpositions

### 3.1 The lower bound

For a permutation and an operation , denote as the number of breakpoints that are removed due to operation . Following are some important observations about breakpoints.

.

###### Lemma 2

.

From Lemma 1 and Lemma 2 an optimal algorithm for this problem can not remove more than two breakpoints by a single operation. So, a lower bound follows.

.

### 3.2 The algorithm

Our algorithm works on considering different orientations of grey and black edges. Note that if a permutation is not sorted there must be at least two grey edges in the breakpoint graph and each grey edge will be incident to two black edges. A grey edge with its two adjacent black edges must be of one of the four types as shown in Fig. 1.

###### Lemma 3

Let be a Type 1 grey edge. Then there exists at least one black edge for some .

###### Proof

If no such black edge exists, then subsequence is sorted. But in that case would not be a grey edge. ∎

We call such a black edge a trapped black edge.

In our algorithm we scan the permutation from left to right to find the first black edge incident to a grey edge. There are four possible scenarios for the four types of edges. We consider the scenarios in the order as presented below in Fig. 1 and apply a prefix transposition or a prefix reversal accordingly.

###### Lemma 4

Given a permutation and its associated breakpoint graph , if any of the following two conditions is satisfied, then a prefix reversal or a prefix transposition can be applied to such that it removes at least one breakpoint.

1. contains a grey edge of Type 1 or Type 2 with .

2. contains a grey edge of Type 3 with .

###### Proof

If and there is a grey edge of Type 1, then Scenario 1 is applicable: according to Lemma 3, there exists a trapped black edge for some and we apply a prefix transposition that creates adjacency between and without introducing any new breakpoint. If on the other hand the grey edge is of Type 2, then Scenario 2 is applicable: apply a prefix reversal that removes a breakpoint.

If and the first grey edge is , then is sorted. If is of Type 3, then a prefix transposition removes one breakpoint according to Scenario 3. ∎

###### Lemma 5

Given a permutation and its associated breakpoint graph , if none of the Scenario 1, 2 and 3 is applicable, then a prefix reversal can be applied that does not remove any breakpoint but is followed by two subsequent operations removing at least two breakpoints.

###### Proof

If scenario 1 or 2 is not applicable, then . Let , for some , be the largest subsequence that is already sorted. Then there is a breakpoint between and . If the grey edge adjacent to is not of Type 3, then it must be of Type 4. Let the other endpoint of the grey edge be . So, we can apply, according to Scenario 4, a prefix reversal that does not remove any breakpoint but causes the grey edge to become of Type 2. Then in the next step Scenario 2 will be applicable with a prefix reversal that will remove one breakpoint. After applying , will become a breakpoint with and . Hence, again, either Scenario 1 or Scenario 2 will be applicable, which will further remove a breakpoint. So, as a whole, we get two consecutive operations removing at least two breakpoints after applying a reversal that does not remove a breakpoint. ∎

Our algorithm (SortByRT3) is summarized in Algorithm 1. It clearly runs in polynomial time.

###### Theorem 3.2

SortByRT3 is a 3-approximation algorithm.

###### Proof

By Lemma 4, if any of the Scenario 1, 2 or 3 is applicable, then the algorithm can remove at least one breakpoint at each step. Otherwise according to Lemma 5 it removes at least two breakpoints in three steps. Hence, it sorts in at most operations. By Theorem 3.1, . So, we get an approximation ratio of . ∎

## 4 2-approximation algorithm

Now we improve the ratio considering a third rearrangement operation, called prefix transreversal. A prefix transreversal for some and some such that reverses the elements and then pastes it between and and thus transforms into permutation .

### 4.1 The lower bound

Another important observation about breakpoints regarding prefix transreversals is the following.

###### Lemma 6

.

From Lemma 1, Lemma 2 and Lemma 6 a lower bound for sorting by prefix reversals and prefix transreversals is the following.

.

### 4.2 The algorithm

The next lemma is the key to our 2-approximation.

###### Lemma 7

Let be a permutation with and let its associated breakpoint graph be . If contains a grey edge of Type 4, then a prefix transreversal can be applied that removes at least one breakpoint.

###### Proof

Let the Type 4 grey edge be with its two adjacent black edges and . We can apply a prefix transreversal creating an adjacency between and and thus removing a breakpoint. ∎

The above lemma along with Lemma 4 proves that in every situation at least one breakpoint is removed by each operation.

###### Lemma 8

For every permutation , we have .

.

###### Theorem 4.3

An algorithm (let us call it SortByRT2) that produces prefix reversals, prefix transpositions, and/or prefix transreversals according to Lemma 7 is an approximation algorithm with factor 2 for sorting by prefix reversals and prefix transreversals.

## 5 3-approximation algorithm with forward march

In this section we introduce a new concept that we call forward march. At the very beginning or after applying a prefix reversal or a prefix transposition, a prefix , for may be already sorted. In this case we update as the unsorted suffix of , i.e., as and the size of is reduced by , i.e., the value of is updated as . The next prefix reversal or prefix transposition is applied on updated . This concept of moving forward along with the sorting is called forward march.

For our algorithm with forward march we redefine breakpoint and breakpoint graph. In the redefined breakpoint graph of there is a black edge between and iff there is a breakpoint between them, i.e., iff . Clearly, is sorted iff it has no breakpoint. Note that at any time is the last element in the sorted part of the permutation and there always exists a black edge between and . We call this black edge the starting black edge.

### 5.1 The lower bound

Due to breakpoint redefinition some of our previous observations are modified.

.

###### Lemma 10

.

From Lemma 9 and Lemma 10 new lower bound is the following.

.

### 5.2 The algorithm

Our algorithm works on considering different orientations of grey and black edges. If is unsorted then it has at least two grey edges and at least one black edge in addition to the starting black edge .

We consider different orientations of the four edge types of Fig. 1 described in Section 3. We try five scenarios in the order shown in Fig. 2, apply a prefix transposition or a prefix reversal accordingly and perform a forward march if possible. In fact, Lemma 12 proves that Scenario 4 and 5 are sufficient to sort , and Scenario 1, 2 and 3 improve practical performance of our algorithm without affecting approximation ratio. Our algorithm (SortByRTwFM3) is summarized in Algorithm 2. It clearly runs in polynomial time.

The following lemma is immediate from the scenarios presented in Fig. 2.

###### Lemma 11

After each prefix reversal or prefix transposition the number of breakpoints is reduced by at least one.

The following lemma proves that our algorithm always terminates by sorting the permutation.

###### Lemma 12

Scenario 4 and Scenario 5 are sufficient to sort the permutation.

###### Proof

Since , we always have a grey edge of Type 1 or Type 2 whose left black edge is . If the grey edge is of Type 1, then by Lemma 3 we can find a trapped black edge and can apply Scenario 4. On the other hand, if the grey edge is of Type 2, then Scenario 5 is applicable. By Lemma 11 since every scenario reduces at least one breakpoint, Scenario 4 and Scenario 5 can successfully sort the permutation. ∎

###### Theorem 5.2

SortByRTwFM3 is a 3-approximation algorithm.

###### Proof

By Lemma 12 our algorithm successfully sorts a given permutation and by Lemma 11 it sorts in at most operations. By Lemma 5.1, . So, we get an approximation ratio of . ∎

The algorithms presented in this paper realize their worst case approximation ratios as a result of combination of the best case behavior of an optimal algorithm, where no prefix reversal is applied, and a worst case behavior of our algorithm, where no prefix transposition (or prefix transreversal) is applied. This is due to the inferiority of prefix reversals to prefix transpositions with respect to their ability to remove breakpoints. However, it is expected that an optimal algorithm would apply both operations. Motivated by the above observation, we derive adaptive approximation ratios for our algorithms in terms of the number of prefix reversals, , applied by an optimal algorithm. Although there is no change in the upper bound of the algorithm, the approximation ratio will improve, because of the increased lower bound.

###### Theorem 6.1

When prefix reversals are applied by an optimal algorithm, SortByRT3 sorts a permutation with approximation ratio .

###### Proof

For , if an optimal algorithm uses prefix reversals, it removes at most breakpoints (by Lemma 1). For the remaining breakpoints it must use at least operations (by Theorem 3.1).

As a result,

Therefore,

###### Theorem 6.2

When prefix reversals are applied by an optimal algorithm, SortByRT2 sorts a permutation with approximation ratio .

###### Proof

An optimal algorithm removes at most breakpoints by prefix reversals (by Lemma 1). For the remaining breakpoints it must use at least operations (by Theorem 4.1).

As a result,

Therefore,

###### Theorem 6.3

When prefix reversals are performed by an optimal algorithm, SortByRTwFM3 sorts a permutation with approximation ratio .

###### Proof

For an optimal algorithm applies prefix reversals to remove at most breakpoints (by Lemma 9). For the remaining breakpoints it must use at least operations (by Theorem 5.1).

Therefore,

So,

## 7 Experimental Results

We have implemented our algorithms and tested their average performance on above 60,000 permutations taken randomly of size up to 3000. In each case the cost of solution given by proposed algorithm is compared to corresponding lower bound instead of comparing with the cost of optimal solution. For both SortByRT3 (Theorem 3.2) and SortByRTwFM3 (Theorem 5.2) worst cases occur very few times in practice and shows ratio near 2 in average (Fig. 3 (a) and (e) respectively). For SortByRT2 (Theorem 4.3) the practical ratio is no better than the theoretical one due to its strategy of choosing scenarios most of the time which remove only breakpoint in one operation (Fig. 3(c)).

The ultimate effect of inducing inferior operations (Theorem 6.16.2 and 6.3) is the increase of lower bounds and thereby the decrease of approximation ratios. This is reflected in Fig. 3(b), (d) and (f), respectively, as the corresponding theoretical and practical curves decrease and become closer to the optimal with the increase of .

Observe that if we could compare our algorithms with corresponding optimal algorithms, then the experimental ratios would be better.

## 8 Conclusion

In this paper we have studied some variations of the pancake flipping problem from the view point of sorting unsigned permutations. We have given a 3-approximation algorithm for sorting by prefix reversals and prefix transpositions. Then we considered a third operation, called prefix transreversal, and provided a 3-approximation algorithm. We also introduced a new concept called forward march where we skip over the sorted prefix of the permutation and apply operations on some prefix of the unsorted suffix of the permutation and contributed a 3-approximation algorithm. We have further analyzed the problems in more practical way and presented better approximation ratios when a certain number of inferior operations (i.e., prefix reversals) are applied by an optimal algorithm. An experimental study shows that our algorithms performs much better in practice than suggested by their approximation ratios.

It will be interesting to redefine the problems to “force” certain number of inferior operations and analyze the approximation ratios of the algorithms. This idea can be applied for other combination of more than one operation. The complexity of the problems are also unknown. In future it would be interesting to see whether the problems are NP-hard.

## References

• [1] Dweighter, H. American Mathematical Monthly 82(1975), 1010
• [2] Bass, D.W., Sudborough, I.H.: Pancake problems with restricted prefix reversals and some corresponding cayley networks. Journal of Parallel and Distributed Computing archive 63(3) (March 2003) 327–336
• [3] Hannenhalli, S., Pevzner, P.: Transforming cabbage into turnip. In: Proc. of 27th Annual ACM Symposium on Theory of Computing (STOC’95). (1995) 178–189
• [4] Heydari, M.H., Sudborough, I.H.: On sorting by prefix reversals and the diameter of pancake networks. Parallel Architectures and Their Efficient Use 678 (1993) 218–227
• [5] Heydari, M.H., Sudborough, I.H.: On the diameter of the pancake network. Journal of Algorithms 25(1) (1997) 67–94
• [6] Kececioglu, J., Sankoff, D.: Exact and approximation algorithms for the inversion distance between two permutations. In: Proc. of 4th Annual Symposium on Combinatorial Pattern Matching, (CPM’93). Volume 684 of Lecture Notes in Computer Science., Springer (1993) 87–105 Extended version has appeared in Algorithmica, 13:180-210, 1995.
• [7] Bafna, V., Pevzner, P.: Genome rearrangements and sorting by reversals. In: Proc. of 34th Annual IEEE Symposium on Foundations of Computer Science (FOCS’93). (1993) 148–157 Also in SIAM Journal on Computing, 25:272-289, 1996.
• [8] Berman, P., Hannenhalli, S., Karpinski, M.: 1.375-approximation algorithm for sorting by reversals. In: Proc. of 10th European Symposium on Algorithms (ESA’02). Volume 2461 of Lecture Notes in Computer Science., Springer (2002) 200–210
• [9] Christie, D.A.: Sorting permutations by block-interchanges. Information Processing Letters 60(4) (1996) 165–169
• [10] Bafna, V., Pevzner, P.: Sorting permutations by transpositions. In: Proc. of the 6th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA’95). (1995) 614–623 Also in SIAM Journal on Discrete Mathematics, 11(2):224-240, 1998.
• [11] Elias, I., Hartman, T.: A 1.375-approximation algorithm for sorting by transpositions. In: Proc. of 5th International Workshop on Algorithms in Bioinformatics (WABI’05). Volume 3692 of Lecture Notes in Computer Science., Springer (October 2005) 204–214
• [12] Hartman, T.: A simpler 1.5-approximation algorithm for sorting by transpositions. In: Proc. of 14th Annual Symposium on Combinatorial Pattern Matching (CPM’03). Volume 2676 of Lecture Notes in Computer Science., Springer (2003) 156–169
• [13] Hartman, T., Sharan, R.: A 1.5-approximation algorithm for sorting by transpositions and transreversals. In: Proc. of 4th International Workshop on Algorithms in Bioinformatics (WABI’04). Volume 3240 of Lecture Notes in Computer Science., Springer (2004) 50–61
• [14] Lu, C.L., Huang, Y.L., Wang, T.C., Chiu, H.T.: Analysis of circular genome rearrangement by fusions, fissions and block-interchanges. BMC Bioinformatics (12 June 2006)
• [15] Dias, Z., Meidanis, J.: Sorting by prefix transpositions. In: 9th International Symposium on String Processing and Information Retrieval (SPIRE 2002). Volume 2476 of Lecture Notes in Computer Science., Springer (2002) 463–468
• [16] Caprara, A.: Sorting by reversals is difficult. In: Proc. of 1st ACM Conference on Research in Computational Molecular Biology (RECOMB’97). (1997) 75–83
• [17] Heydari, M.H., Sudborough, I.H.: Sorting by prefix reversals is np-complete To be submitted (as mentioned in [18]).
• [18] Walter, M., Dias, Z., Meidanis, J.: Reversal and transposition distance of linear chromosomes. In: South American Symposium on String Processing and Information Retrieval (SPIRE’98), IEEE Computer Society (1998) 96–102
• [19] Gu, Q., Peng, S., Sudborough, H.: A 2-approximation algorithm for genome rearrangements by reversals and transpositions. Theoritical Computer Science 210(2) (1999) 327–339
• [20] Lin, G., Xue, G.: Signed genome rearrangements by reversals and transpositions: Models and approximations. In: Proc. of 5th Annual International Conference on Computing and Combinatorics (COCOON’99). Volume 1627 of Lecture Notes in Computer Science., Springer (1999) 71–80
• [21] Eriksen, N.: (1+)-approximation of sorting by reversals and transpositions. Theoretical Computer Science 289(1) (2002) 517–529
• [22] Rahman, A., Shatabda, S., Hasan, M.: An appoximation algorithm for sorting by reversals and transpositions. Journal of Discrete Algorithms 6(3) (2008) 449–457
• [23] Lou, X., Zhu, D.: A 2.25-approximation algorithm for cut-and-paste sorting of unsigned circular permutations. In: COCOON. (2008) 331–341
You are adding the first comment!
How to quickly get a good reply:
• Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
• Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
• Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters