Stacks and Deques Michael Albert 1 Mike Atkinson 1 Steve Linton 2 1 - - PowerPoint PPT Presentation

stacks and deques
SMART_READER_LITE
LIVE PREVIEW

Stacks and Deques Michael Albert 1 Mike Atkinson 1 Steve Linton 2 1 - - PowerPoint PPT Presentation

History Upper bounds Lower bounds Conclusions and questions Stacks and Deques Michael Albert 1 Mike Atkinson 1 Steve Linton 2 1 Department of Computer Science, University of Otago 2 School of Computer Science, University of St Andrews PP2008,


slide-1
SLIDE 1

History Upper bounds Lower bounds Conclusions and questions

Stacks and Deques

Michael Albert1 Mike Atkinson1 Steve Linton2

1Department of Computer Science, University of Otago 2School of Computer Science, University of St Andrews

PP2008, Otago, June 2008

Albert, Atkinson, Linton Stacks and Deques

slide-2
SLIDE 2

History Upper bounds Lower bounds Conclusions and questions

Outline of talk

1

Early pattern class history

2

Upper bounds

3

Lower bounds

4

Conclusions and questions

Albert, Atkinson, Linton Stacks and Deques

slide-3
SLIDE 3

History Upper bounds Lower bounds Conclusions and questions

1

Early pattern class history

2

Upper bounds

3

Lower bounds

4

Conclusions and questions

Albert, Atkinson, Linton Stacks and Deques

slide-4
SLIDE 4

History Upper bounds Lower bounds Conclusions and questions

Three early landmarks

D.E. Knuth: Fundamental Algorithms, The Art of Computer Programming

  • Vol. 1 (First Edition), especially §2.2.1

Addison-Wesley, Reading, Mass. (1968). R.E. Tarjan: Sorting using networks of queues and stacks, Journal of the ACM 19 (1972), 341–346. V.R. Pratt: Computing permutations with double-ended queues, parallel stacks and parallel queues,

  • Proc. ACM Symp. Theory of Computing 5 (1973), 268–277.

Albert, Atkinson, Linton Stacks and Deques

slide-5
SLIDE 5

History Upper bounds Lower bounds Conclusions and questions

Data Structures

Figure: What permutations can a data structure generate (or sort)?

Albert, Atkinson, Linton Stacks and Deques

slide-6
SLIDE 6

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-7
SLIDE 7

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-8
SLIDE 8

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-9
SLIDE 9

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-10
SLIDE 10

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-11
SLIDE 11

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-12
SLIDE 12

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-13
SLIDE 13

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-14
SLIDE 14

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-15
SLIDE 15

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-16
SLIDE 16

History Upper bounds Lower bounds Conclusions and questions

Generating a permutation 1 2 3 4 5

Albert, Atkinson, Linton Stacks and Deques

slide-17
SLIDE 17

History Upper bounds Lower bounds Conclusions and questions

Knuth

Enumeration of stack permutations and the 312-avoidance criterion

Albert, Atkinson, Linton Stacks and Deques

slide-18
SLIDE 18

History Upper bounds Lower bounds Conclusions and questions

Knuth

Enumeration of stack permutations and the 312-avoidance criterion Enumeration of restricted-input deque permutations, showed they avoid {4213, 4231}

Albert, Atkinson, Linton Stacks and Deques

slide-19
SLIDE 19

History Upper bounds Lower bounds Conclusions and questions

Knuth

Enumeration of stack permutations and the 312-avoidance criterion Enumeration of restricted-input deque permutations, showed they avoid {4213, 4231} Considered stacks in series

Albert, Atkinson, Linton Stacks and Deques

slide-20
SLIDE 20

History Upper bounds Lower bounds Conclusions and questions

Knuth

Enumeration of stack permutations and the 312-avoidance criterion Enumeration of restricted-input deque permutations, showed they avoid {4213, 4231} Considered stacks in series Exercise 2.2.1.13: “ [M48] How many permutations of n elements are obtainable with the use of a general deque?”

Albert, Atkinson, Linton Stacks and Deques

slide-21
SLIDE 21

History Upper bounds Lower bounds Conclusions and questions

Tarjan

Focused on minimal unsortable permutations for various networks

  • f data structures – nowadays called the basis problem.

Lemma 6: “There is an infinite set of permutations, none of which contains another as a pattern, and such that each permutation is unsortable using two parallel stacks”

Albert, Atkinson, Linton Stacks and Deques

slide-22
SLIDE 22

History Upper bounds Lower bounds Conclusions and questions

Tarjan

Focused on minimal unsortable permutations for various networks

  • f data structures – nowadays called the basis problem.

Lemma 6: “There is an infinite set of permutations, none of which contains another as a pattern, and such that each permutation is unsortable using two parallel stacks” Lemma 10: “Let Y be a series of 2 stacks. Then the shortest unsortable sequence in Y is of length 7.”

Albert, Atkinson, Linton Stacks and Deques

slide-23
SLIDE 23

History Upper bounds Lower bounds Conclusions and questions

Tarjan

Focused on minimal unsortable permutations for various networks

  • f data structures – nowadays called the basis problem.

Lemma 6: “There is an infinite set of permutations, none of which contains another as a pattern, and such that each permutation is unsortable using two parallel stacks” Lemma 10: “Let Y be a series of 2 stacks. Then the shortest unsortable sequence in Y is of length 7.” (End of paper): “The author has constructed a sequence of length 41 which is unsortable using three stacks in series; beyond this . . . getting hard”.

Albert, Atkinson, Linton Stacks and Deques

slide-24
SLIDE 24

History Upper bounds Lower bounds Conclusions and questions

Pratt

Formalised the subpermutation relation: “. . . the subtask relation on permutations is even more interesting than the networks we were characterizing. This relation seems to be the only partial order on permutations that arises in a simple and natural way, yet it has received no attention to date.”

Albert, Atkinson, Linton Stacks and Deques

slide-25
SLIDE 25

History Upper bounds Lower bounds Conclusions and questions

Pratt

Formalised the subpermutation relation: “. . . the subtask relation on permutations is even more interesting than the networks we were characterizing. This relation seems to be the only partial order on permutations that arises in a simple and natural way, yet it has received no attention to date.” Proved {4213, 4231} is the basis of the restricted-input deque permutations

Albert, Atkinson, Linton Stacks and Deques

slide-26
SLIDE 26

History Upper bounds Lower bounds Conclusions and questions

Pratt

Formalised the subpermutation relation: “. . . the subtask relation on permutations is even more interesting than the networks we were characterizing. This relation seems to be the only partial order on permutations that arises in a simple and natural way, yet it has received no attention to date.” Proved {4213, 4231} is the basis of the restricted-input deque permutations Found the (infinite) bases of the class of general deque permutations and the basis of the two parallel stacks class

Albert, Atkinson, Linton Stacks and Deques

slide-27
SLIDE 27

History Upper bounds Lower bounds Conclusions and questions

Pratt

Formalised the subpermutation relation: “. . . the subtask relation on permutations is even more interesting than the networks we were characterizing. This relation seems to be the only partial order on permutations that arises in a simple and natural way, yet it has received no attention to date.” Proved {4213, 4231} is the basis of the restricted-input deque permutations Found the (infinite) bases of the class of general deque permutations and the basis of the two parallel stacks class Used formal languages in enumeration results

Albert, Atkinson, Linton Stacks and Deques

slide-28
SLIDE 28

History Upper bounds Lower bounds Conclusions and questions

Data Structures

Figure: Data Structures with unknown enumerations

Albert, Atkinson, Linton Stacks and Deques

slide-29
SLIDE 29

History Upper bounds Lower bounds Conclusions and questions

Growth rates

The growth rate of a sequence (cn) is g = lim sup

n→∞

n

√cn (so cn behaves roughly like gn) Question What is the growth rate for deques, two stacks in parallel, two stacks in series? It is known that, in all three cases, the growth rate is between 4 and 16. Also the lim sup is a true limit.

Albert, Atkinson, Linton Stacks and Deques

slide-30
SLIDE 30

History Upper bounds Lower bounds Conclusions and questions

1

Early pattern class history

2

Upper bounds

3

Lower bounds

4

Conclusions and questions

Albert, Atkinson, Linton Stacks and Deques

slide-31
SLIDE 31

History Upper bounds Lower bounds Conclusions and questions

Upper bounds on growth rates – Stacks in series

I = Insert T = Transfer D = Delete

Figure: Two stacks in series

IIITITDTDDTD produces 4231 from input 1234

Albert, Atkinson, Linton Stacks and Deques

slide-32
SLIDE 32

History Upper bounds Lower bounds Conclusions and questions

Upper bounds on growth rates – Stacks in parallel

I1 = Insert I2 = Insert D1 = Delete D2 = Delete

Figure: Two stacks in parallel

I1I1I2D1I2I1D2I1D2D2D1D1 produces 24351 from input 12345

Albert, Atkinson, Linton Stacks and Deques

slide-33
SLIDE 33

History Upper bounds Lower bounds Conclusions and questions

Upper bounds on growth rates – Deques

I1 = Insert I2 = Insert D1 = Delete D2 = Delete

Figure: Deque

I1I2I1D2I2I2I1D2D1D2D2D2D1 produces 256413

Albert, Atkinson, Linton Stacks and Deques

slide-34
SLIDE 34

History Upper bounds Lower bounds Conclusions and questions

Permutations as words

Represent permutations by words over a 3 or 4 letter alphabet and count words. This is an overcount since

1

Not every word represents a permutation, and

2

Many words represent the same permutation

The first of these doesn’t seem to matter for growth rates. E.g. For two stacks in series there are 27n words of length 3n

  • n {I, D, T} but only

12.(3n)! (n + 2)!(n + 1)!n!

  • f them represent permutations. This has growth rate 27.

The second is much more serious.

Albert, Atkinson, Linton Stacks and Deques

slide-35
SLIDE 35

History Upper bounds Lower bounds Conclusions and questions

Rewriting rules

Definition If L, R are words then L → R if any permutation which can be generated by a word ULV is also generated by URV .

σ λx μ ρ yσ λ μ ρ x y

Figure: TDIT → ITTD

Albert, Atkinson, Linton Stacks and Deques

slide-36
SLIDE 36

History Upper bounds Lower bounds Conclusions and questions

Systematic search for rewriting rules of length t

Suppose we know rewriting rules for lengths less than t Construct FSA recognising all words not containing any LHS

  • f existing rules (a regular language)

Run through all words of length t that it accepts, compute their effect on a “generic” state of the data structure, and sort them by their effects Create new rules from any duplicate effects Check the rules are valid in “non-generic” settings.

Albert, Atkinson, Linton Stacks and Deques

slide-37
SLIDE 37

History Upper bounds Lower bounds Conclusions and questions

Word count

Having found all rewriting rules of length up to t construct FSA accepting all words not containing any LHS of a rule Use state equations to find generating functions and thereby growth rate for the number of words This will be an upper bound on the growth rate for the number of permutations

Albert, Atkinson, Linton Stacks and Deques

slide-38
SLIDE 38

History Upper bounds Lower bounds Conclusions and questions

Results – Deque

Length Number of Rules Growth Bound 8 51 8.4925 9 85 8.459 10 175 8.428 11 321 8.410 12 756 8.392 13 1480 8.380 14 3806 8.368 15 7734 8.361 16 21029 8.352

Albert, Atkinson, Linton Stacks and Deques

slide-39
SLIDE 39

History Upper bounds Lower bounds Conclusions and questions

Results – Parallel Stacks

Length Number of Rules Growth Bound 8 33 8.4606 9 43 8.4474 10 109 8.4087 11 143 8.4031 13 615 8.376 14 2366 8.3597 15 3131 8.3578 16 13263 8.3461

Albert, Atkinson, Linton Stacks and Deques

slide-40
SLIDE 40

History Upper bounds Lower bounds Conclusions and questions

Results – Two Stacks in Series

Length Number of Rules Growth Bound 8 23 14.201 9 35 14.048 10 71 13.826 11 106 13.747 13 215 13.623 14 368 13.477 15 1270 13.433 16 2825 13.374

Albert, Atkinson, Linton Stacks and Deques

slide-41
SLIDE 41

History Upper bounds Lower bounds Conclusions and questions

1

Early pattern class history

2

Upper bounds

3

Lower bounds

4

Conclusions and questions

Albert, Atkinson, Linton Stacks and Deques

slide-42
SLIDE 42

History Upper bounds Lower bounds Conclusions and questions

Lower bounds – via bounded capacities

Consider k-bounded versions of the three structures where the system is constrained to contain at most k elements at a time.

Albert, Atkinson, Linton Stacks and Deques

slide-43
SLIDE 43

History Upper bounds Lower bounds Conclusions and questions

Lower bounds – via bounded capacities

Consider k-bounded versions of the three structures where the system is constrained to contain at most k elements at a time. This corresponds to imposing extra forbidden patterns of the form (k + 1)α where α is any permutation of {1 · · · k}.

Albert, Atkinson, Linton Stacks and Deques

slide-44
SLIDE 44

History Upper bounds Lower bounds Conclusions and questions

Lower bounds – via bounded capacities

Consider k-bounded versions of the three structures where the system is constrained to contain at most k elements at a time. This corresponds to imposing extra forbidden patterns of the form (k + 1)α where α is any permutation of {1 · · · k}. The system can now be thought of as FSA with states that correspond to the disposition of elements residing in the stacks/deque.

Albert, Atkinson, Linton Stacks and Deques

slide-45
SLIDE 45

History Upper bounds Lower bounds Conclusions and questions

Lower bounds – via bounded capacities

Consider k-bounded versions of the three structures where the system is constrained to contain at most k elements at a time. This corresponds to imposing extra forbidden patterns of the form (k + 1)α where α is any permutation of {1 · · · k}. The system can now be thought of as FSA with states that correspond to the disposition of elements residing in the stacks/deque. It outputs rank-encoded permutations: e.g. 4163752 is encoded as 4142321 – and the ranks will be at most k

Albert, Atkinson, Linton Stacks and Deques

slide-46
SLIDE 46

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 7, 5 11 8 2 3 6 10 9

Figure: Bounded deque: snapshot

Albert, Atkinson, Linton Stacks and Deques

slide-47
SLIDE 47

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5

Figure: Bounded deque: encoded snapshot

Albert, Atkinson, Linton Stacks and Deques

slide-48
SLIDE 48

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3, 7 4 1 2 3 5 6 7

Figure: Bounded deque: encoded snapshot after D1

Albert, Atkinson, Linton Stacks and Deques

slide-49
SLIDE 49

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5

Figure: Bounded deque: encoded snapshot

Albert, Atkinson, Linton Stacks and Deques

slide-50
SLIDE 50

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3, 5 4 1 2 3 5 6 5

Figure: Bounded deque: encoded snapshot after D2

Albert, Atkinson, Linton Stacks and Deques

slide-51
SLIDE 51

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5

Figure: Bounded deque: encoded snapshot

Albert, Atkinson, Linton Stacks and Deques

slide-52
SLIDE 52

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5 8

Figure: Bounded deque: encoded snapshot after I1

Albert, Atkinson, Linton Stacks and Deques

slide-53
SLIDE 53

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5

Figure: Bounded deque: encoded snapshot

Albert, Atkinson, Linton Stacks and Deques

slide-54
SLIDE 54

History Upper bounds Lower bounds Conclusions and questions

Bounded deque

12, 13, ... 4, 1, 5, 3 7 4 1 2 3 6 5 8

Figure: Bounded deque: encoded snapshot after I2

Albert, Atkinson, Linton Stacks and Deques

slide-55
SLIDE 55

History Upper bounds Lower bounds Conclusions and questions

Affairs of state

Compute the non-deterministic FSA for a k-bounded system Determinise it and then minimise it Read off generating functions from the state transition rules Compute the growth rate of the k-bounded system which will be a lower bound for the growth rate of the unrestricted system

Albert, Atkinson, Linton Stacks and Deques

slide-56
SLIDE 56

History Upper bounds Lower bounds Conclusions and questions

Controlling the state explosion

The determinisation phase results in a state explosion which the subsequent minimisation phase somewhat controls For deques and parallel stacks some structure was observed in the final automaton which allowed us to construct it directly Deterministic states represented by sequences of words in letters λ, ρ. Eg for two parallel stacks λρ, λλ, λρλ represents configurations where the two smallest symbols are in different stacks, the next two smallest symbols are in the same stack, and of the last three symbols the middle one is not in the stack containing the first and third

Albert, Atkinson, Linton Stacks and Deques

slide-57
SLIDE 57

History Upper bounds Lower bounds Conclusions and questions

Example state transitions

λρ, λλ, λρλ λρ, λλ, λρ λρ, λλ, λλ λρ, λλ, λρ, λ, λ λλλλλ 7 6 9 1

Albert, Atkinson, Linton Stacks and Deques

slide-58
SLIDE 58

History Upper bounds Lower bounds Conclusions and questions

Further state reduction

States fall into equivalence classes - just count numbers of λ’s and ρ’s in each word - e.g. λλ, λρ, λρλ ∼ λλ, λρ, λλρ Pass to the quotient automaton Prove it gives the same growth rate This allows the computations for deques and for two parallel stacks to be pushed further

Albert, Atkinson, Linton Stacks and Deques

slide-59
SLIDE 59

History Upper bounds Lower bounds Conclusions and questions

Results

k Growth bound Serial stacks 8 7.5535 Parallel stacks 18 7.535 Deques 21 7.890

Albert, Atkinson, Linton Stacks and Deques

slide-60
SLIDE 60

History Upper bounds Lower bounds Conclusions and questions

Bottom line for growth rate γ

1

Early pattern class history

2

Upper bounds

3

Lower bounds

4

Conclusions and questions Two stacks in series: 8 ≤ γ ≤ 13.374 Two stacks in parallel: 7.535 ≤ γ ≤ 8.3461 Deque: 7.890 ≤ γ ≤ 8.352

Albert, Atkinson, Linton Stacks and Deques

slide-61
SLIDE 61

History Upper bounds Lower bounds Conclusions and questions

Open questions

What are the true growth rates? Do deques and two parallel stacks have the same growth rate? Why is two stacks in series more difficult? For deques and two parallel stacks we have efficient recognition algorithms; is the recognition problem for two stacks in series NP-complete? Can we get the exact enumerations for two parallel stacks? For deques?

Albert, Atkinson, Linton Stacks and Deques