The Stack Permutation Problem Given a sequence of integers 1 … N, a permutation of the sequence is a rearrangement of the integers. For example, permutations of the sequence <1, 2, 3> are: <2, 3, 1>, <2, 1, 3>, <3, 1, 2>, <3, 2, 1>, <1, 3, 2>, and <1, 2, 3> (the original sequence). A permutation, P, is called a stack permutation of the sequence, I = <1, 2, … , N>, if it can be derived by using a stack, S, and three rules. Consider both sequences I and P implemented as lists, where P and stack S are initially empty. Then the three rules are the following. Rule 1: If I is not empty, remove the element at the head of I and place it at the end of P. Rule 2: If I is not empty, remove the element at the head of I and push it onto S. Rule 3: If S is not empty, pop the element at the top and place it at the end of P. Using the three rules above, it should be clear that given the sequence I = <1, 2, 3, 4>, the sequence <3, 4, 2, 1> is a stack permutation of I but that the sequence <3, 4, 1, 2> is not a stack permutation of I. Now given a sequence I = <1, 2, …, N> and a permutation, P, of I, how can it be determined if P is a stack permutation of I? This problem can be solved using a State Space Search algorithm. In this approach, a state consists of three lists and a stack. Note that the reference to a list is a reference to a behavior, not a reference to an implementation. One list, I, represents the input. One list, R, represents the rules, and the third list, P, represents the output. The stack is called S. The initial state, sinitial, is the following: R, P, and S and empty, and I is the sequence <1, 2, …, N>. The goal state, sgoal, consists of the following: P contains the permutation that is being tested, I, and S are empty, and R is a list of integers between 1 and 3 that represents the sequence of rules that were applied to transform I into P. Using the definitions above, the pseudocode for the state space search is the following. Let s0, sN, s1, s2, and sg, be states. Set s0 to the initial state, sN to the goal state, and s1, s2, and sg to the empty state. Let Q be a queue of states initialized to empty. Let continue be a boolean variable initialized to true. Q.Enqueue( s0 ). While( Q is not empty and continue equals true ) do Set s1 = Q.Dequeue(). If ( Rule 1 applies to s1 ) Create a new state s2 and with a 1 appended If( s2 = sN ) then Set sg to s2 Set continue to Else Q.Enqueue( s2 ) then that is a copy of s1 transformed by Rule 1 to the list R of s2. false If ( Rule 2 applies to s1 ) then Create a new state s2 that is a copy of s1 transformed by Rule 2 and with a 2 appended to the list R of s2. If( s2 = sN ) then Set sg to s2 Set continue to false Else Q.Enqueue( s2 ) If ( Rule 3 applies to s1 ) then Create a new state s2 that is a copy of s1 transformed by Rule 3 and with a 3 appended to the list R of s2. If( s2 = sN ) then Set sg to s2 Set continue to false Else Q.Enqueue( s2 ) End While //The loop was exited because either the goal state was found or the //queue was empty If( sg = empty ) then //No goal state was found Print( “Not a stack permutation” ) Else Print( “Stack Permutation. The list of rules applied are:” ) Print( List R from sg )