© 2012 Prof. Dr. Franz J. Brandenburg Stacks, Queues,Deques and their Representations as Drawings Franz J. Brandenburg University of Passau joint work with Christopher Auer and Andreas Gleißner 34/1 Basics © 2012 Prof. Dr. Franz J. Brandenburg Data Structures are used to store data by operations which get (read), insert and remove items. What makes the difference between different data structures? Stack vs queue? Insertion: anytime, no difference Removal: When can an item be accessed. Together: An axiomatic description of the behavior. Example Stack: insert (push) and remove (pop) and get (top) main axiom: s = s.insert(x); s.remove(); The stack is unchanged after insert, remove. x = s.get(); s.remove(), s.insert(x) may cause an error if s is empty. Array: random access main axiom: NONE, its unpredicable, its chaos 34/2 Stack and Queue © 2012 Prof. Dr. Franz J. Brandenburg stack (LIFO) a linked list with insert and remove to the left (or top) insert/push 4 3 ..... 2 1 remove/pop 4 3 2 1 queue (FIFO) a linked list with a head (right) for insert and a tail (left) for get and remove Axioms: NONE, it is too complex head tail 4 3 2 ..... 1 34/3 Deques © 2012 Prof. Dr. Franz J. Brandenburg deque (double-ended queue) implemented by the class Arraydeque in Java 6 seen as a doubly linked list with a head (right) and a tail (left) and insert and remove at either ends. head tail 5 3 ..... 1 tail U 5 3 1 6 4 2 2 4 6 head commuting pipes 34/4 History © 2012 Prof. Dr. Franz J. Brandenburg • Who invented stacks and queues? • use of stacks in programming languages for the formal description of ALGOL (1960) by F.L. Bauer (TU Munich) … the axioms for a stack • use as data structures by D.E. Knuth (Stanford) in The Art of Computer Programming (1968) … stack, queue, deque, input/output restricted deque deque = double ended queue This name is misleading. The queue mode is secondary! But ... (see later) 34/5 Deques © 2012 Prof. Dr. Franz J. Brandenburg a deque has a stack mode at either end has a queue mode from left to right (or right to left) a deque can simulate 2 stacks a deque can simulate 1 queue What happens in queue mode? Press all items out (6,4,2,1,3,5) How strong is a deque? a deque can be simulated by 2 stacks + 1 queue Can a deque simulate 2 stacks + 1 queue head tail 0 5 3 1 ..... 2 4 6 34/6 Stack © 2012 Prof. Dr. Franz J. Brandenburg What is typical? What is characteristic? • • • • • • • algorithm depth first search structure LIFO last in, first out pattern nested parenthesis ((( )) ()), [ ( ) ] programming paradigm recursion storage behavior destroy information you cannot save the item you remove area of influence local the closing parenthesis bounds the inner substructure graphs nested rainbows ( ( ( ( ) ) ) ( ( ) ) ) 34/7 Queue © 2012 Prof. Dr. Franz J. Brandenburg What is typical? What is characteristic? • • • • • • • algorithm structure pattern breadth first search FIFO first in, first out twist ( ( ( ) ) ( ) ), [ ( ] ) equality set p(x) = p‘(x) with projections p, p‘ programming paradigm streams, buffers storage behavior copy information reinsert the removed item area of influence global graphs twists with very many crossings 34/8 Linear Layouts of Graphs © 2012 Prof. Dr. Franz J. Brandenburg Definition a linear layout of a graph G = (V,E) place the vertices on a line or number the vertices 1,2,....,n for every pair of edges (u, v) and (x, y) nesting if u < x < y < v twist or cross if u < x < v < y e.g. (1,4) (2,3) e.g. (1,3), (2,4) A linear layout is a stack layout if no two edges twist or cross a queue layout if no two edges nest This is the complementary view: define by exclusion. 34/9 Book Embeddings © 2012 Prof. Dr. Franz J. Brandenburg Definition (Bernhard, Kainen, 1979) A book-embedding of a (undirected) graph G = (V,E) is – a linear ordering of the vertices 1,2,...n on the spine of the book – a planar drawing of the edges (u,v) on the pages as nested arches (LIFO) 1 2 3 4 5 6 7 8 9 10 11 12 Add some more edges, (2,8) and (2,11), (2,12) but (5,9) were illegal 34/10 Book Embeddings © 2012 Prof. Dr. Franz J. Brandenburg Theorem (Bernhard, Kainen, 1979) A graph G has a book embedding (with 1 page) if and only if G is a subgraph of an outerplanar graph if and only if G is a stack graph visit the vertices from left to right and push/pop the edges At 1: push (1,12), push (1,2) At 2: pop (1,2), push (2,12), (2,11), (2,8), (2,7) top 1 2 3 4 5 6 7 8 9 10 11 12 (2,7) (2,8) (2,11) (2,12) (1,12) 34/11 Graph and Data Structures © 2012 Prof. Dr. Franz J. Brandenburg Definiton Let DS be a structure with insert and remove operations e.g., stack, queue, deque, 2-stacks, 1-stack + 1-queue, .... with insert/remove at the head/tail (1st, 2nd stack....) A graph G = (V,E) is a DS graph (e.g. stack/queue/deque/2-stack/...) if there is a linear order of the vertices, say 1,2,....,n such that the edges at vertex v can be correctly processed by DS (u,v) is removed from DS if u < v (v,w) is inserted into DS if v < w. The local order at a vertex is chosen appropriately. For the correctness: an edge (u,v) must be directly accessible if it is removed at v. The order of the edges must meet the data structure. 34/12 Example: K4 © 2012 Prof. Dr. Franz J. Brandenburg Process on the stack in the order 1,2,3,4 1: push (1,4), (1,3), (1,2) 2: pop (1,2) top push (2,4), (2,3) (2,4) 3: pop (2,3), pop(1,3) (1,3) (1,4) Process on a queue in the order 1,2,3,4 1: insert (1,2), (1,3), 1,4) 2: remove (1,2) insert (2,3), (2,4) 3: remove (1,3), (2,3) 4 3 1 tail (insert) 2 head (remove) (1,4) (1,3) (1,2) (2,4) (2,3) (1,4) (1,3) (2,4) (2,3) (1,4) 34/13 K4 and Deque © 2012 Prof. Dr. Franz J. Brandenburg Process the vertices 1,2,3,4 4 1: insert-left (1,2), insert-right (1,4), (1,3) 2: remove-left (1,2) insert-left (2,4), (2,3) 3: remove-right (1,3), remove-left (2,3) insert-left (3,4) 4: remove-right (1,4), (2,4), (3,4), 3 1 (2,4) (2,3) (1,2) 2 (1,3) (1,4) 34/14 Stack Graphs © 2012 Prof. Dr. Franz J. Brandenburg Theorem (Bernhard, Kainen (1979)) 1-stack = outerplanar (subgraph of) Test: in O(n) 2-stack = subgraph of a planar graph with a Hamilton cycle Test: NP-complete Theorem (Yannakakis (1989)) Every planar graph is a 4-stack graph Claim: some planar graphs need 4 stacks. stack-number = min # stacks Test: NP-hard. 34/15 Queue Graphs © 2012 Prof. Dr. Franz J. Brandenburg Theorem (Heath, Rosenberg (1992)) 1-queue = arch leveled planar Test: NP-hard Definition A graph G = (V,E) is arch leveled planar if there is a leveling of the vertices - proper level edges - arches from the leftmost vertex to the „open“ right vertices on the same level and planar 34/16 Simulations © 2012 Prof. Dr. Franz J. Brandenburg Theorem (Heath, Leighton, Rosenberg (1993)) Every 1-queue graph is a 2-stack graph namely: a planar sub-Hamiltonian graph Every 1-stack graph is a 2-queue graph namely: outerplanar 1-queue < 2-stack and 1-stack < 2-queue Hence stack-thickness < 2 queue-thickness queue-thickness < 2 stack-thickness But this does not scale to stack-number and queue-number since you must choose one linear odering of the vertices OPEN stack-number vs queue-number 34/17 Linear Cylindric Drawing © 2012 Prof. Dr. Franz J. Brandenburg Definition (Auer, Brandenburg, et al GD2010) A linear cylindric drawing is a planar drawing on a rolling cylinder with the vertices in a linear ordering on the front line with edges on the front or winding over the backside 34/18 2D-drawing © 2012 Prof. Dr. Franz J. Brandenburg cut along the front line split or copy the vertices on the front line and unfold 34/19 Linear Cylindric Drawings © 2012 Prof. Dr. Franz J. Brandenburg • • • • • • take two horizontal levels or the upper and lower borders of a square take identical copies of the order of the vertices draw „stack“ edges / rainbows below the upper level draw „stack“ edge / rainbows above the lower level draw „queue“ edges between the levels PLANAR 34/20 Deque Graphs © 2012 Prof. Dr. Franz J. Brandenburg THEOREM 1 (Auer, Bachmaier, Brandenburg, Brunner, Gleißner, GD2010) A graph G is a deque graph if and only if G has a linear cylindric drawing Proof: Take the order of the vertices, 1,2,...,n Classify the edges: lower stack iff insert and remove at the head upper stack iff insert and remove at the tail queue from bottom left to top right iff insert at head and remove at tail queue from top left to bottom right iff insert at tail and remove from head 34/21 Queue Graphs © 2012 Prof. Dr. Franz J. Brandenburg Corollary A graph G is a queue graph if and only if G is arch leveled planar (Heath, Rosenberg 1992) if and only if G is linear cylindric with only queue arcs with only straight lines from NW to SE between the two sides 34/22 2-Stack vs Deque © 2012 Prof. Dr. Franz J. Brandenburg Theorem G is a 2-stack graph (Bernhard, Kainen (1979)) iff G is a subgraph of a planar graph with a Hamilton cycle G is a deque graph iff G is a subgraph of a planar graph with a Hamilton path ... and there are planar graphs which have no Hamilton path which have a Hamilton path and not a Hamilton cycle Di Giacomo et al Comput Geom 30 (2005), 34/23 Deque & Hamilton Path © 2012 Prof. Dr. Franz J. Brandenburg Proof: Take the linear order. Add edges for a Hamilton path. Draw the stack edges Draw the queue edges as arches over the north pole (right) 34/24 Computability © 2012 Prof. Dr. Franz J. Brandenburg Computability is defined via Turing machines with a finite state control a read-only input tape and (one or more) read-write working tapes. pushdown automaton = Turing machine with 1 stack ... the working tape is/operates like a stack and nondeterminism. ... these are the context-free languages with an O(n3) membership test queue machine = Turing machine with 1 queue (Post (1936), Brandenburg (1982)) ... the working tapes are queues with two heads (head, tail) 34/25 Universality © 2012 Prof. Dr. Franz J. Brandenburg Universal Machines • Turing machines (with one or more Turing tapes) • machines with 2-pushdown stacks with O(1) slowdown per operation with O(n) to simulate a queue • machines with 1 queue with Q(n2) slowdown The Power of a Queue the Post Correspondence Problem Given: a finite set of pairs of strings (xi, yi) Problem: Is there a correspondence? xi1 xi2 ...xiq = yi1 yi2 ...yiq Solution: Use a queue to buffer the difference between xi1 xi2 ... xij and yi1 yi2 ...yij 34/26 PCP © 2012 Prof. Dr. Franz J. Brandenburg Example 1: 1. 2. Solution: 1 10 011 101 00 11 1 3 2 3 = 1 011 10 011 = 101 11 00 11 Example 2: 1: 011 01 2: 0 011 Solution (with 66 entries) 01 101 10 011 24344 21243 43443 44214 42134 11344 42121 11343 41214 42141 13411 31131 21411 3 34/27 Typical Languages © 2012 Prof. Dr. Franz J. Brandenburg Formal Language Theory abstract families of languages, AFL (here trios) generators of language families initiators: investigators: N. Chomsky, G. Schützenberger S.A. Greibach, S. Ginsburg, J. Berstel .. in the 60-80th Which languages characterizes the context-free languages? the Dyck set of well nested parenthesis ( ) and brackets [ ] generated by the rules S SS, S ( S ), S [ S ], S e [[()()] ()()] generator: use homomorphisms g and h-1 over alphabets/strings S* and intersection with regular sets R such that CFL = g(h-1(Dyck) R) known as the Chomsky-Schützenberger Theorem 34/28 Queue Languages © 2012 Prof. Dr. Franz J. Brandenburg equality sets of two homomorphisms g, h Eg(g,h) = {w S* | g(w) = h(w)} Example: let S = (, ), [, ] let g preserve ( and ] and g erases ) ] let h preserve and rename ( ) and ] [ Then Eg(g,h) = all FIFO nested strings of parenthesis and brackets ( [ [ ) ] ( ] [ ) ] Theorem (Book, Brandenburg SICOMP80) A language L is a queue language iff L is recognized by a nondeterministic real-time queue machine iff L = g(h-1(FIFO) R), where FIFO are all FIFO nestings. 34/29 2-stacks © 2012 Prof. Dr. Franz J. Brandenburg Theorem (Ginsburg, Greibach 1968) L is a 2-stack language iff L is recognized in time n by a nondeterministic TM with two stacks iff L = g(h-1(S2) R) where S2 is the shuffle / interleavings of all strings from two disjoint Dyck sets i.e. take well nested parenthesis over (), [ ] and over < >, { } and then consider all strings which are pairwise well-nested e.g. < { [ ( > ] } ) 34/30 Deque Languages © 2012 Prof. Dr. Franz J. Brandenburg Consider the equality set FIFO = Eq(g,h) which characterizes a queue, say over the alphabet a, b and a, b Define a (context-free) substitution by (i) a Dyck a Dyck and b Dyck b Dyck (ii) a Dyck a Dyck and b Dyck b Dyck where underscoring means a new alphabet Thus attach an arbitrary well nested string to the left and right. Theorem A language L is a deque language iff L is recognized in time T(n)=n by a nondeterminstic TM with a deque iff L = g(h-1( X R), The language X is the above context-free substitution on FIFO such that the nesting is complete between any pair of a,b or underscored 34/31 Interpretation © 2012 Prof. Dr. Franz J. Brandenburg Queues – at Turingmachines are universal since they can copy information since they exactly describe the Post Correspondence Problem Deques – in formal language theory behave like queues (FIFO) with context-free substitition .... if this were true, the name deque = double ended queue should be queue with free stack mode 34/32 Deque ? © 2012 Prof. Dr. Franz J. Brandenburg The behavior of a deque: a stack at the tail a stack at the head queue mode: an item x moves from the head/tail to the tail/head tail 3 1 ..... U 2 4 head commuting pipes 34/33 Deque ? © 2012 Prof. Dr. Franz J. Brandenburg The behavior of a deque: a stack at the tail a stack at the head queue mode: an item x moves from the head/tail to the tail/head tail 3 1 ..... U 2 4 head remove red to the right if blue has been removed before The right stack must be empty. If empty the right stack steels the red item 34/34 Deque ? Stack Steeling Double © 2012 Prof. Dr. Franz J. Brandenburg The behavior of a deque: a stack at the tail a stack at the head queue mode: if one stack is empty, it steels the deepest item 34/35 Generalization © 2012 Prof. Dr. Franz J. Brandenburg A tripod: take three stacks which meet at their bottom and if a stack is empty, it takes the next item from the bottom of the next nonempty stack in circular order 34/36 Queue Graphs © 2012 Prof. Dr. Franz J. Brandenburg Corollary The dual G* of a queue graph has an Eulerian path. Proof: Sweep the drawing from left to right. Theorem Tests are NP-hard. Is G a deque (queue, 2-stack) graph? Theorem G is a bipartite queue graph if and only if G is proper arch leveled planar (no arches) 34/37 Arches © 2012 Prof. Dr. Franz J. Brandenburg When do we get the arches? ... at the ends of the full queue phases ...where can we add edges? ... (3,7) or (4,6) 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 7 8 9 4 5 6 1 2 3 8 9 4 5 6 1 2 3 7 7 8 9 4 5 6 1 2 3 34/38 Stacks © 2012 Prof. Dr. Franz J. Brandenburg 4-stacks (Yannakakis (1989)) Every planar graph is a 4-stack graph. and there are planar graphs which need 4 stacks 3-stacks (Dujmovic, Wood (2005)) Every graph has a 3-stack embedding with O(g(n)) subdivisions g(n) is minimum of stack-number and queue-number 2-stacks (Di Giacomo et al. (2005), Kaufmann,Wiese (1999) Dujmovic, Wood (2005)) Every planar graph has a 2-stack embedding with 1 subdivision 34/39 1 Stack + 1 Queue © 2012 Prof. Dr. Franz J. Brandenburg Drawing style Example K5 and K3,3 are 1-stack + 1-queue graphs K6 and K4,4 are not Conjecture of Heath and Rosenberg (1992) Every planar graph is a 1 stack + 1 queue graph. I say NO! but my first counterexamples were refuted by A. Gleißner using a SAT solver 34/40 Perspectives and Open Problems © 2012 Prof. Dr. Franz J. Brandenburg • a new drawing style for queues Dujmovic, Wood (2005) • • • • • planar graphs and 3-stacks planar graphs and 1-stack+ 1-queue (Heath, Rosenberg conjecture) stack and queue number: are they related deque number and its relation to stack (queue) number a sophisticated data structure to capture all planar graphs 34/41