Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.

Similar presentations


Presentation on theme: "Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks."— Presentation transcript:

1 Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks

2 Fundamentals of Python: From First Programs Through Data Structures2 Objectives After completing this chapter, you will be able to: Describe the behavior of a stack from a user’s perspective Explain how a stack can be used to support a backtracking algorithm Describe the use of a stack in evaluating postfix expressions

3 Fundamentals of Python: From First Programs Through Data Structures3 Objectives (continued) Explain how the Python virtual machine uses a stack to support function and method calls Analyze the performance trade-offs between an array-based implementation of a stack and a linked implementation of a stack

4 Fundamentals of Python: From First Programs Through Data Structures4 Overview of Stacks Stack: LIFO structure in which access is completely restricted to just one end, called the top –Basic operations: push and pop

5 Fundamentals of Python: From First Programs Through Data Structures5 Using a Stack A stack type is not built into Python Can use a Python list to emulate a stack –Use append to push and pop to pop –Drawback: stack can be manipulated by all of the other list operations as well Extra operations violate the spirit of a stack as an ADT We define a more restricted interface or set of operations for any authentic stack implementation and show how these operations are used in a brief example

6 Fundamentals of Python: From First Programs Through Data Structures6 The Stack Interface

7 Fundamentals of Python: From First Programs Through Data Structures7 Instantiating a Stack We assume that any stack class that implements this interface will also have a constructor that allows its user to create a new stack instance Later, we’ll consider two different implementations: –ArrayStack and LinkedStack With different performance trade-offs For now, assume that someone has coded these so we can use them: s1 = ArrayStack() s2 = LinkedStack()

8 Fundamentals of Python: From First Programs Through Data Structures8 Example Application: Matching Parentheses Compilers need to determine if the bracketing symbols in expressions are balanced correctly

9 Fundamentals of Python: From First Programs Through Data Structures9 Example Application: Matching Parentheses (continued) Approach 1: Count left and right parentheses –Does not work Approach 2: –Scan expression; push left brackets onto a stack –On encountering a closing bracket, if stack is empty or if item on top of stack is not an opening bracket of the same type, we know the brackets do not balance –Pop an item off the top of the stack and, if it is the right type, continue scanning the expression –When we reach the end of the expression, stack should be empty; if not, brackets do not balance

10 Fundamentals of Python: From First Programs Through Data Structures10 Three Applications of Stacks We now discuss three other applications of stacks: –First, we present algorithms for evaluating arithmetic expressions –Second, we describe a general technique for using stacks to solve backtracking problems –Third, we examine the role of stacks in computer memory management

11 Fundamentals of Python: From First Programs Through Data Structures11 Evaluating Arithmetic Expressions In the infix form of an arithmetic expression, each operator is located between its operands In the postfix form of an arithmetic expression, an operator immediately follows its operands

12 Fundamentals of Python: From First Programs Through Data Structures12 Evaluating Postfix Expressions Steps: –Scan across the expression from left to right –On encountering an operator, apply it to the two preceding operands; replace all three by the result –Continue scanning until you reach expression’s end, at which point only the expression’s value remains To express this procedure as a computer algorithm, you use a stack of operands –The time complexity of the algorithm is O(n), where n is the number of tokens in the expression

13 Fundamentals of Python: From First Programs Through Data Structures13 Evaluating Postfix Expressions (continued) In the algorithm, token refers to an operand or an operator: Create a new stack While there are more tokens in the expression Get the next token If the token is an operand Push the operand onto the stack Else if the token is an operator Pop the top-two operands from the stack Apply the operator to the two operands just popped Push the resulting value onto the stack Return the value at the top of the stack

14 Fundamentals of Python: From First Programs Through Data Structures14 Evaluating Postfix Expressions (continued)

15 Fundamentals of Python: From First Programs Through Data Structures15 Converting Infix to Postfix Start with an empty postfix expression and an empty stack –Stack will hold operators and left parentheses Scan across infix expression from left to right On encountering an operand, append it to postfix expression On encountering a (, push it onto the stack

16 Fundamentals of Python: From First Programs Through Data Structures16 Converting Infix to Postfix (continued) On encountering an operator –Pop operators with equal or higher precedence –Append them to postfix expression –Push scanned operator onto stack On encountering a ), shift operators from stack to postfix expression until meeting matching (, which is discarded On encountering the end of the infix expression, transfer remaining operators from the stack to the postfix expression

17 Fundamentals of Python: From First Programs Through Data Structures17 Backtracking A backtracking algorithm begins in a predefined starting state and moves from state to state in search of a desired ending state –When there is a choice between alternative states, picks one, possibly at random, and continues –If it reaches a state that represents an undesirable outcome, it backs up to last point at which there was an unexplored alternative and tries it –It searches all states or reaches desired ending state Two implementation techniques: –Use stacks or use recursion

18 Fundamentals of Python: From First Programs Through Data Structures18 Backtracking (continued) Stack approach: Create an empty stack Push the starting state onto the stack While the stack is not empty Pop the stack and examine the state If the state represents an ending state Return SUCCESSFUL CONCLUSION Else if the state has not been visited previously Mark the state as visited Push onto the stack all unvisited adjacent states Return UNSUCCESSFUL CONCLUSION

19 Fundamentals of Python: From First Programs Through Data Structures19 Backtracking (continued) Backtracking algorithm to solve maze problem: Instantiate a stack Locate the character “P” in the grid Push its location onto the stack While the stack is not empty Pop a location, (row, column), off the stack If the grid contains “T” at this location, then A path has been found Return SUCCESS Else if this location does not contain a dot Place a dot in the grid at this location Examine the adjacent cells to this one and for each one that contains a space, push its location onto the stack Return FAILURE

20 Memory Management The computer’s run-time system must keep track of various details that are invisible to the programmer: –Associating variables with data objects stored in memory so they can be located when these variables are referenced –Remembering the address of the instruction in which a method or function is called, so control can return to the next instruction when that function or method finishes execution –Allocating memory for a function’s or a method’s arguments and temporary variables, which exist only during the execution of that function or method Fundamentals of Python: From First Programs Through Data Structures20

21 Fundamentals of Python: From First Programs Through Data Structures21 Memory Management (continued)

22 When a subroutine is called, the PVM: –Creates the subroutine’s activation record and pushes it onto the call stack –Saves the basePtr’s current value in the region labeled Prev basePtr and sets the basePtr to the new activation record’s base –Saves the locationCounter’s current value in region labeled Return Address and sets locationCounter to the first instruction of the called subroutine –Copies calling parameters into Parameters region –Starts executing the called subroutine at location indicated by locationCounter Fundamentals of Python: From First Programs Through Data Structures22

23 Memory Management (continued) When a subroutine has finished executing, the PVM does the following: –Reestablishes the settings needed by the calling subroutine by restoring the values of the locationCounter and the basePtr from values stored in the activation record –Pops the activation record from the call stack –Resumes execution of the calling subroutine at the location indicated by the locationCounter Fundamentals of Python: From First Programs Through Data Structures23

24 Fundamentals of Python: From First Programs Through Data Structures24 Implementations of Stacks Because of their simple behavior and linear structure, stacks are implemented easily using arrays or linked structures Our two implementations of stacks illustrate the typical trade-offs involved in using these two recurring approaches

25 Fundamentals of Python: From First Programs Through Data Structures25 Test Driver

26 Fundamentals of Python: From First Programs Through Data Structures26 Test Driver (continued) Output: –Note that the items in the stack print from bottom to top in the stack’s string representation, whereas when they are popped, they print from top to bottom

27 Fundamentals of Python: From First Programs Through Data Structures27 Array Implementation Built around an array called items and two integers called top and size –Initially, the array has a default capacity of 10 positions, top equals -1, and size equals 0

28 Fundamentals of Python: From First Programs Through Data Structures28 Linked Implementation Uses a singly linked sequence of nodes with a variable top pointing at the list’s head, and a variable size to track the items on the stack

29 Fundamentals of Python: From First Programs Through Data Structures29 Linked Implementation (continued) The implementation of str is complicated by the fact that the items must be visited from the end of the linked structure to its beginning –Solution: use recursion

30 Fundamentals of Python: From First Programs Through Data Structures30 Linked Implementation (continued)

31 Fundamentals of Python: From First Programs Through Data Structures31 Linked Implementation (continued)

32 Fundamentals of Python: From First Programs Through Data Structures32 Time and Space Analysis of the Two Implementations With the exception of __str__, all of the stack methods have a maximum running time of O(1) –__str__ runs in linear time; recursive function used in linked stack causes a linear growth of memory In the array implementation, at the moment of doubling, push ’s running time jumps to O(n) –The rest of the time it remains at O(1) –Similar remarks can be made about pop Space requirement: 2n + 2 for linked stack and capacity + 2 for array implementation

33 Fundamentals of Python: From First Programs Through Data Structures33 Case Study: Evaluating Postfix Expressions Request: –Write an interactive program for evaluating postfix expressions Analysis: –Program should detect and report all input errors, be they intentional or unintentional –The view class is named PFView –The model class is named PFEvaluatorModel Processes are worth encapsulating in separate classes: Scanner and PFEvaluator

34 Fundamentals of Python: From First Programs Through Data Structures34 Case Study: Evaluating Postfix Expressions (continued)

35 Fundamentals of Python: From First Programs Through Data Structures35 Case Study: Evaluating Postfix Expressions (continued)

36 Fundamentals of Python: From First Programs Through Data Structures36 Case Study: Evaluating Postfix Expressions (continued)

37 Fundamentals of Python: From First Programs Through Data Structures37 Case Study: Evaluating Postfix Expressions (continued)

38 Fundamentals of Python: From First Programs Through Data Structures38 Case Study: Evaluating Postfix Expressions (continued) Implementation:

39 Fundamentals of Python: From First Programs Through Data Structures39 Case Study: Evaluating Postfix Expressions (continued)

40 Fundamentals of Python: From First Programs Through Data Structures40 Summary A stack allows access to one end only, called the top, where items are pushed onto or popped from Stacks are used in applications that manage data items in LIFO manner, such as: –Matching bracket symbols in expressions –Evaluating postfix expressions –Backtracking algorithms –Managing memory for subroutine calls on a VM Arrays and singly linked structures support simple implementations of stacks


Download ppt "Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks."

Similar presentations


Ads by Google