Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Chapter 3 Stacks.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks and QueuesCSC311: Data Structures1 Chapter 5 Stacks and Queues Objectives –Stacks and implementations –Queues and implementations –Double-ended.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Stacks & Queues EECS: Stacks & Queues Stacks April 23, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Stacks 1/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks (and Queues).
Stacks 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Recap: Solution of Last Lecture Activity
Stacks.
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Arrays, Stacks, and Queues
Stacks © 2013 Goodrich, Tamassia, Goldwasser Stacks.
Stacks.
Stacks.
Chapter 5 Stacks and Queues 11/28/2018 Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Copyright © Aiman Hanna All rights reserved
Recall What is a Data Structure Very Fundamental Data Structures
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Stacks.
Topic 15 Implementing and Using Stacks
Introduction to Stacks
Computing Spans Given an an array X, the span S[i] of X[i] is
Introduction to Stacks
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Stacks © 2014 Goodrich, Tamassia, Goldwasser Stacks

The Stack ADT The Stack ADT stores arbitrary objects Stacks 12/7/2018 The Stack ADT The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme (LIFO) Think of a spring-loaded plate dispenser © 2014 Goodrich, Tamassia, Goldwasser Stacks

Main stack operations push(S, item) item pop(S) inserts an element Stacks 12/7/2018 Main stack operations push(S, item) inserts an element item pop(S) removes and returns the last inserted element © 2014 Goodrich, Tamassia, Goldwasser Stacks

Auxiliary stack operations Stacks 12/7/2018 Auxiliary stack operations item top(S) returns the last inserted element without removing it integer size(S) returns the number of elements stored boolean isEmpty(S) indicates whether no elements are stored © 2014 Goodrich, Tamassia, Goldwasser Stacks

Example Omitting parameter S in the table © 2014 Goodrich, Tamassia, Goldwasser Stacks

Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures © 2014 Goodrich, Tamassia, Goldwasser Stacks

Function Stack keeps track of the chain of active function with a stack When a function is called, a frame is pushed onto the stack containing Local variables and return value Program counter, keeping track of the statement being executed When a function ends, its frame is popped from the stack and control is passed to the function on top of the stack Allows for recursion main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 © 2014 Goodrich, Tamassia, Goldwasser Stacks

Array-based Stack Algorithm size(S) return S->t + 1 Algorithm pop(S) if isEmpty(S) then return null else S->t  S->t  1 return S->data[S->t + 1] A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element … S->data 1 2 t © 2014 Goodrich, Tamassia, Goldwasser Stacks

Array-based Stack (cont.) The array storing the stack elements may become full A push operation will print error Limitation of the array-based implementation Not intrinsic to the Stack ADT Algorithm push(S, item) if t = S->length  1 then print stack is full error else S->t  S->t + 1 S->data[S->t]  item S->data 1 2 t … © 2014 Goodrich, Tamassia, Goldwasser Stacks

Can we use a linked list instead of an array? Top of the stack? What are the tradeoffs between linked lists and arrays? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case time complexity N items on the stack Operation Array Linked List Push Pop © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case time complexity N items on the stack Operation Array Linked List Push O(1) Pop © 2014 Goodrich, Tamassia, Goldwasser Stacks

Parentheses Matching Each “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” correct: ( )(( )){([( )])} correct: ((( )(( )))) {([( )])} incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: ( © 2014 Goodrich, Tamassia, Goldwasser Stacks

How to match parentheses with a stack? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Parentheses that match ([]{}) Stack (top of the stack is on the right) ( ([ ({ empty © 2014 Goodrich, Tamassia, Goldwasser Stacks

Parentheses that don’t match ([) Stack (top of the stack is on the right) ( ([ Sees a ), but the top is [ © 2014 Goodrich, Tamassia, Goldwasser Stacks

HTML Tag Matching The Little Boat For fully-correct HTML, each <name> should pair with a matching </name> <body> <center> <h1> The Little Boat </h1> </center> <p> The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage. </p> <ol> <li> Will the salesman die? </li> <li> What color is the boat? </li> <li> And what about Naomi? </li> </ol> </body> The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage. 1. Will the salesman die? 2. What color is the boat? 3. And what about Naomi? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Arithmetic Expressions Infix expression (notation) Regular notation (x + y (x – y) / 2 Postfix expression (notation) x y + x y – 2 / © 2014 Goodrich, Tamassia, Goldwasser Stacks

Evaluating Postfix Expressions if a variable push the value of the variable if an operator pop two items, perform the operation, and push the result x y + 2 / (x + y) / 2 in infix notation © 2014 Goodrich, Tamassia, Goldwasser Stacks

Evaluating Postfix Expressions x y + 2 / [(x+y)/2 in infix notation] Assume x has 10 and y has 20 Stack (top is far right): 10 10, 20 30 30, 2 15 © 2014 Goodrich, Tamassia, Goldwasser Stacks

Evaluating Infix Expressions Stacks 12/7/2018 Slide by Matt Stallmann included with permission. Evaluating Infix Expressions 14 – 3 * 2 + 7 = (14 – (3 * 2) ) + 7 Operator precedence * has precedence over +/– Associativity operators of the same precedence group evaluated from left to right Example: (x – y) + z rather than x – (y + z) Idea: push each operator on the stack, but first pop and perform higher and equal precedence operations. © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm on an Example Expression Stacks 12/7/2018 Slide by Matt Stallmann included with permission. Algorithm on an Example Expression Operator ≤ has lower precedence than +/– 14 ≤ 4 – 3 * 2 + 7 – ≤ 14 4 * 3 – ≤ 14 4 + 2 * 3 – ≤ 14 4 + ≤ 14 -2 2 * 3 – ≤ 14 4 + 6 – ≤ 14 4 $ 7 + ≤ 14 -2 $ F $ ≤ 14 5 © 2014 Goodrich, Tamassia, Goldwasser Stacks

Highest since … 2017: “Brent Crude Spikes To Highest Since July 2015” 2008: “[…] turnout in the western states […], the highest since 1960” 2006: “Americans' Optimism About Stock Market Highest Since 2000” © 2014 Goodrich, Tamassia, Goldwasser Stacks

Computing Spans (not in book) Given an an array X, the span S[i] of X[i] is the maximum number of consecutive elements X[j] immediately preceding X[i] such that X[j]  X[i] Spans have applications to financial analysis E.g., stock at 52-week high X 6 3 4 5 2 1 S © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 1 For each index i Array size is N Scan backward to find a higher value Array size is N Worst-case time complexity? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Quadratic Algorithm Algorithm spans1(X, n) Input array X of n integers Output array S of spans of X # S  new array of n integers n for i  0 to n  1 do n s  1 n while s  i  X[i - s]  X[i] 1 + 2 + …+ (n  1) s  s + 1 1 + 2 + …+ (n  1) S[i]  s n return S 1 Algorithm spans1 runs in O(n2) time © 2014 Goodrich, Tamassia, Goldwasser Stacks

Computing Spans with a Stack We keep in a stack the indices of the last element that is taller when “looking back” We scan the array from left to right Let i be the current index We pop indices from the stack until we find index j such that X[i]  X[j] We set S[i]  i - j We push i onto the stack © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Keep on the stack the last building that is taller than the current one Root top that can’t be seen Ie. While a building is shorter on the stack Pop the building © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] 0,6 [pop 5,2; push 6] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] 0,6 [pop 5,2; push 6] 0,6,7 [push 7] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 What is the time complexity (stack operations)? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Linear Time Algorithm Each index of the array Is pushed into the stack exactly one Is popped from the stack at most once The statements in the while-loop are executed at most n times Algorithm spans2 runs in O(n) time Algorithm spans2(X, n) # S  new array of n integers n A  new empty stack 1 for i  0 to n  1 do n while (isEmpty(A)  X[top(A)]  X[i] ) do n pop(A) n if isEmpty(A) then n S[i]  i + 1 n else S[i]  i - top(A) n push(A,i) n return S 1 © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity O(N2) O(N) Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks