applications of stacks and
play

Applications of Stacks and you know in advance what all the data is. - PDF document

Lists, Queues, Stacks, and CSE 143 Searching Lists are fine for searching especially once they have been sorted. Applications that search lists have a hidden assumption: Applications of Stacks and you know in advance what all the


  1. Lists, Queues, Stacks, and CSE 143 Searching • Lists are fine for searching • especially once they have been sorted. • Applications that search lists have a hidden assumption: Applications of Stacks and you know in advance what all the data is. Queues • Sometimes you don't! • Sometimes you discover things in the process of searching. • Other times the list is too long to compile before searching it. • Other times the list has no obvious order. • Other times the cost of getting all the needed information is too high. 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-1 @ 1998-2003 University of Washington 18b-2 Queues and Searching Travel Search Refinements • Queues and stacks are often appropriate structures for When adding flights to the queue... does it matter what organizing a partial list as a process is on-going. order they are added in?? • Example: finding the cheapest non-stop fare from Sea-Tac • Answer: if you are looking for the absolute best, it doesn't to Cleveland, Dec. 24. • Eventually you have to look at all possibilities • Ahead of time, you don't have a list of all flights to search through. • If your goal is less strict, you might stop earlier • Possible process: • "Find an affordable flight from Sea-Tac to Cleveland, preferably Think of the possible airlines and put them in a queue. non-stop, Dec. 24, or Dec. 23 if necessary " Take first item off the queue. • The order in which you examine the possibilities affects the answer. if "airline", find all flights from Sea-Tac to Cleveland 12/23 or 12/24 and add each to queue. • Interpret items on the queue not as individual flights, but if "flight", examine price, time, day, etc. and as flight characteristics (that might expand to one, many, decide if it's good enough to stop Keep going until queue is empty, or until you decide to or no flights) stop. 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-3 @ 1998-2003 University of Washington 18b-4 CSE 143 O

  2. Searching: Queue vs. Stack Another Search Application • Searching for a path to escape a maze • Instead of a Queue, a Stack could be used. • Algorithm: try all possible sequences of moves in the maze • This primarily affects the order in which the possibilities are expanded and examined. until either • "Find a flight which costs under $200", starting with a list of • you find a sequence that works, or... airlines to consider. • no more to try • Several airlines might have a flight that qualifies. Which • An all-possibilities search is called and “exhaustive will be chosen... search” • using a Queue: • A stack helps keep track of the possibilities • using a Stack: • Traces a path of moves • The usual picture that is drawn (a tree) gives rise to the • Popping the stack moves you backwards expressions "depth first" and "breadth first". • Can get a similar effect without a stack, by using recursion 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-5 @ 1998-2003 University of Washington 18b-6 Another Application: Palindromes Computers and Simulation • Computer programs are often used to “simulate” • "Madam, I'm Adam." some aspect of the real world • "Enid and Edna dine." • Movement of people and things • "A man, a plan, a canal – Panama!" • Economic trends • Weather forecasting • Capitalization, spacing, and punctuation are usually • Physical, chemical, industrial processes • Why? ignored. • Suppose characters are arriving on a Stream Reader. • Cheaper, safer, more humane Suggest an algorithm to see if the string forms a • But have to worry about accuracy and faithfulness to palindrome. real world • Hint: this lecture is about stacks and queues... 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-7 @ 1998-2003 University of Washington 18b-8 CSE 143 O

  3. Queues and Simulations Example: Simulation of a Bank • Queues are often useful in simulations • People arrive and get in line for a teller • Arrival patterns may depend on time of day, day of week, • Common considerations etc. • When a teller is free, person at the head of the line gets • Time between arrival served • Service time • Sounds like a queue is the right data model • Number of servers • A bank might have different kinds of “tellers” • Often want to investigate/predict (commercial tellers, loan officers, etc) • different queues for each one • Time spend waiting in queue • Simulation can be used to answer questions like • Effect of more/fewer servers • What is the average or longest wait in line • What would be the effect of hiring another teller • Effect of different arrival rates 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-9 @ 1998-2003 University of Washington 18b-10 Simulations in Science Time-Based Simulations • Classical physics: describe the physical world with • Time-based simulation (differential) equations • Look and see what happens at every “tick” of the clock • Might "throw dice" to determine what happens • Problem: too many interactions, equations too numerous and complex to solve exactly • Random number or probability distribution • Alternative: build a model to simulate the operation • Size of time step? • Zillions of applications in physics, weather, astronomy, • A day, a millesecond, etc. depending on application chemistry, biology, ecology, economics, etc. etc. • Ideal model would allow safe virtual experiments and dependable conclusions 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-11 @ 1998-2003 University of Washington 18b-12 CSE 143 O

  4. Event-Based Simulations Another Application: Evaluating Expressions • Expressions like “3 * (4 + 5)” have to be evaluated • Event-based simulation • Schedule future events and process each event as its time arrives by calculators and compilers • Bank simulation events • We’ll look first at another form of expression, • “Customer arrives” could be one event (external) called “postfix” or “reverse Polish notation” • “Customer starts getting service” (internal) • Turns out a stack algorithm works like magic to do • “Customer finishes transaction” postfix evaluation • “Teller goes to lunch”... • And... another stack algorithm can be used to • Event list holds the events waiting to happen convert from infix to postfix! • Each one is processed in chronological order • External events might come from a file, user input, etc. • Internal events are generated by other events 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-13 @ 1998-2003 University of Washington 18b-14 Postfix vs. Infix More on Postfix • 3 4 5 * - means same as (3 (4 5 *) -) • Review: Expressions have operators (+, -, *, /, etc) and • infix: 3 - (4 * 5) operands (numbers, variables) • Parentheses aren’t needed! • In everyday use, we write the binary operators in • When you see an operator: between the operands • “ 4 + 5 ” means “add 4 and 5” both operands must already be available. Stop and apply the operator, then go on • called in fix notation • Precedence is implicit • No reason why we couldn’t write the two operands first, • Do the operators in the order found, period! then the operator • Practice converting and evaluating: • “ 4 5 + ” would mean “add 4 and 5” • 1 2 + 7 * 2 % • called post fix notation • (3 + (5 / 3) * 6) - 4 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-15 @ 1998-2003 University of Washington 18b-16 CSE 143 O

  5. Why Postfix? Postfix Evaluation via a Stack • Does not require parentheses! • Read in the next “token” (operator or data) • If data, push it on the data stack • Some calculators make you type in that way • If (binary) operator (call it “op”): • Easy to process by a program Pop off the most recent data (B) and next most recent (A) • The processing algorithm uses a stack for Perform the operation R = A op B Push R on the stack operands (data) • Continue with the next token • simple and efficient • When finished, the answer is the stack top. • Simple, but works like magic! • Note: "tokens" are not necessarily single characters • In the expression 2002 56 + there are three tokens • White space is generally ignored 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-17 @ 1998-2003 University of Washington 18b-18 Refinements and Errors Example: 3 4 5 - * • If data stack is ever empty when data is needed for an Draw the stack at each step! • Read 3. Push it (because it’s data) operation: • Then the original expression was bad • Read 4. Push it. • Too many operators up to that point • Read 5. Push it. • If the data stack is not empty after the last token has been • Read -. Pop 5, pop 4, perform 4 - 5. Push -1 processed and the stack popped: • Read *. Pop -1, pop 3, perform 3 * -1. Push -3. • Then the original expression was bad • No more tokens. Final answer: pop the -3. • Too few operators or too many operands • note that stack is now empty 3/14/2003 3/14/2003 @ 1998-2003 University of Washington 18b-19 @ 1998-2003 University of Washington 18b-20 CSE 143 O

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend