Plan for Week Linear structures: Stack, Queue

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CPS 100, Spring Spirits of Linear Structures l Past, Present, Future, … l Linked-lists and arrays and ArrayLists  ADT: insert, delete, iterate/traverse,
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CompSci 100 Prog Design and Analysis II October 14, 2010 Prof. Rodger CompSci 100, Fall
Stacks and Queues Introduction to Computing Science and Programming I.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
CPS Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete, traverse,
CPS Sequential search revisited l What does the code below do? How would it be called initially?  Another overloaded function search with 2 parameters?
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CompSci 100E 9.1 Stack: What problems does it solve?  Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called.
CPS 100, Fall Catching Up, Moving ahead l Last week and this week  How did you study for the exam? Next time?  How can you/should you work with.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CompSci 100e Program Design and Analysis II March 15, 2011 Prof. Rodger CompSci 100e, Spring20111.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CPS Recurrences l Summing Numbers int sum(int n) { if (0 == n) return 0; else return n + sum(n-1); } l What is complexity? justification? l T(n)
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
CPS 100, Fall Linear Structures Revisited l Linked-lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete,
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Prefix notation in action
Building Java Programs
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stack: What problems does it solve?
Marcus Biel, Software Craftsman
Stacks and Queues.
Revised based on textbook author’s notes.
Compsci 201 Linked Lists from A-Z
Compsci 201 Trees and Tradeoffs
Week 3 - Friday CS221.
Objectives In this lesson, you will learn to: Define stacks
CSE 373: Data Structures and Algorithms
Week 15 – Monday CS221.
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
Stacks and Queues.
Building Java Programs
structures and their relationships." - Linus Torvalds
Building Java Programs Chapter 14
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Stacks and Queues.
slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
slides created by Marty Stepp
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Plan for Week Linear structures: Stack, Queue Applications: LIFO, FIFO, other Canonical examples and view to more Java specifics of how to use these Friday: More examples of recursion and alternatives Blob counting and applications of neighbor-search Guide to using practice-it for APT-like problems

What is a linear structure? Structure elements have predecessors, successors Arrays, Linked-lists, ArrayLists ADT: insert, delete, iterate/traverse, grow, … Advantages and trade-offs include … Some linear structures are used to solve problems and are typically studied in courses like 201 We won't look in detail at implementations We will mention the implementations Viewed via APIs and problems solved Discuss that linear structures are not necessarily contiguous, like linked lists, but each element typically has one before and one after. Not necessarily indexable, but can be

Stacks Queues and Deques, Oh My! Linear structures used in problem domains and algorithms: Stack, Queue, Dequeue. Similar abstractions with different semantics Mention stacks and queues, continued on next slides. These are in the real-world versions of the terms

https://www.google.com/search?q=stack&source=lnms&tbm=isch

https://www.google.com/search?q=queue&source=lnms&tbm=isch

Why don't we just use arrays? Stacks used in implementing recursion, postscript language, Java language, graph algorithms Stacks implemented using array/ArrayList Queues used in simulation, graph algorithms, scheduling Queues implemented using array/LinkedList Deque (dequeue) double ended queue, Gradescope Implemented using LinkedList or other Key ideas here are that operations on these structures should be fast: O(1). So add to end of array or arraylist, not at front. Add at front or back of linked-list e.g., for queue.

Stacks: Java and Otherwise https://git.cs.duke.edu/201fall16/linearstructures/tree/master/src See StackDemo.java Note that Stack is in java.util Stack is generic, e.g., Stack<String> https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html See API documentation for details on methods In code example: push, pop, create, size Also noted isEmpty and peek Note that isEmpty is shorthand for .size() == 0. Note that peek looks at top element, but leaves it on the stack

Simple stack example Stack is part of java.util.Collections hierarchy Typically avoid .peek(), but can be useful Stack<String> s = new Stack<>(); s.push("panda"); s.push("grizzly"); s.push("brown"); System.out.println("size = "+s.size()); System.out.println(s.peek()); String str = s.pop(); System.out.println(s.pop()); Go over what code prints: size = 3, "brown", stores grizzly in str, but doesn't print. Prints "panda" since "grizzly" was popped, then prints "panda" again. What would happen if .pop() called again? Error

Two applications of stacks Manage runtime of method invocations, e.g., in implementing compiler. Note that Java Virtual machine is a stack-based machine Postfix notation for arithmetic expressions Can be used in evaluating expressions What is 3 + 5 * 7 ? This is NOT 56 Without precedence, need parentheses, e.g., we'd write (3 + 5) * 7 Postfix is sometimes called Reverse Polish Notation or RPN https://en.wikipedia.org/wiki/Reverse_Polish_notation

Postfix, prefix, and infix notation Postfix notation used in some HP calculators No parentheses needed, precedence rules work 3 5 + 4 2 * 7 + 3 - 9 7 + * Read expression For number/operand: push onto stack For operator: pop, pop, operate, push To evaluate prefix: (3 + 5) * 7 we'd use 3 5 + 7 * What is 3 5 7 * + Discuss first expression is 8, why. Discuss second by explicitly detailing stack: 4 2 (stack has two elements). Then 8 on stack, then (8,7), then 15 then 15 3, then 12 The last expression is an error: pops an empty stack since only 16 on stack when * encountered

Postfix.java To evaluate a postfix expression: If number, push. If operator: pop, pop, op, push https://git.cs.duke.edu/201fall16/linearstructures/blob/master/src/Postfix.java Things to look at in program: What exceptions are caught? When do these occur? Number format and Stack exceptions How does StringTokenizer work: Like .split(), but delivers tokens or chunks one at a time. Delimiters or separators are tokens too Run program a few times. First try expressions like 3 5 * 7 + or 3 5 7 * +, then try some with errors, e.g., 3 + or 3 5 7 + and talk about why the errors are identified Can mention that StringTokenizer can return delimiters or ignore them. The TRUE in constructor means return the delimiters. Walk through the hasNextToken and nextToken code to remind students this is like the hasNextLine and nextLine used in the readLoop – this general iterator pattern shows up often in Java

Modifying Postfix.java How can we add exponentiation to the program 2 3 ^ would be 8 and 3 2 ^ would be 9 Must add new delimiter What happens before we add ^ when ^ entered? What happens after we add ^ when ^ entered? Must add new line in method operator How do we return appropriate value? Test? Run through adding ^, first just typing it, then adding as a delimiter and typing it, then adding if (op.equals("^") return Math.pow(lhs,rhs) Talk about testing with 2 3 ^ and 3 2 ^ Ask students how to commit this to git, but DO NOT commit it

http://bit.ly/201fall16-stacks WOTO Look at source code for Postfix.java to understand both RPN and parsing using tokens in Java

Barbara Liskov Turing Award Winner in 2008 for For contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing. The advice I give people in general is that you should figure out what you like to do, and what you can do well—and the two are not all that dissimilar, because you don’t typically like doing something if you don’t do it well. … So you should instead watch—be aware of what you’re doing, and what the opportunities are, and step into what seems right, and see where it takes you.

Queue: another linear ADT FIFO: first in, first out, used in many applications Scheduling jobs/processes on a computer Computer simulations, lines at stores or banks: one line or multiple lines? Common operations: add (back), remove (front), peek ?? java.util.Queue is an interface java.util.LinkedList implements the interface add(), remove(), offer(), poll() We'll use .add() and.remove() – see API for offer() and poll() but we won't use these. Also have .peek()

Stacks: Java and Otherwise https://git.cs.duke.edu/201fall16/linearstructures/tree/master/src See QueueDemo.java Note that Queue is in java.util and is an Interface! Typically use LinkedList to create concrete object https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html See API documentation for details on methods and on many implementing classes Why use LinkedList? O(1) add back, remove front Note that isEmpty is shorthand for .size() == 0. Note that peek looks at top element, but leaves it on the stack

Simple Queue example Queueis part of java.util.Collections hierarchy Typically avoid .peek(), but can be useful Queue<String> q = new LinkedList<>(); q.add("panda"); q.add("grizzly"); q.add("brown"); System.out.println("size = "+q.size()); System.out.println(q.peek()); String str = q.remove(); System.out.println(q.remove()); Go over what code prints: size = 3, "panda", stores grizzly in str, but doesn't print. Prints "brown" since "grizzly" was removed, then prints "brown" again. What would happen if .remove() called again? Error CAN DO THIS at javarepl.com, i.e., type Queue<String> q = new LinkedList<>() and then play

Queue applications Simulation, discrete-event simulation How many toll-booths do we need? How many express lanes or self-checkout at grocery store? Runway access at airport? Queues facilitate simulation with mathematical distributions governing events, e.g., Poisson distribution for arrival times Shortest path, e.g., in word-ladder or Flood-Fill algorithms we'll see on Friday Get from "white" to "house" one-letter at a time? white, while, whale, shale, shake, …?

Wordladder Story Ladder from ‘white’ to ‘house’ White, while, whale, shale, … I can do that… optimally My brother was an English major My ladder is 16, his is 15, how? There's a ladder that's 14 words! The key is ‘sough’ Guarantee optimality! QUEUE I heard the puzzle on the way into Duke in the mid 90's. I called my brother and said I'd solve the problem. He said he would too. He used his brain, I wrote a program. Called him an hour alter and said "got it". He said "got it". I said my code was provably optimal and had a 16-word ladder. He said he had 15. WHAT? I added "sough" to dictionary and got a 14-word ladder 

Queue for shortest path – see gitlab public boolean findLadder(String[] words, String first, String last){ Queue<String> qu = new LinkedList<>(); Set<String> set = new HashSet<>(); qu.add(first); while (qu.size() > 0){ String current = qu.remove(); if (oneAway(current,last)) return true; for(String s : words){ if (! set.contains(s) && oneAway(from,s)){ qu.add(s); set.(s); } } return false;

Shortest Path reprised How does Queue ensure we find shortest path? Where are words one away from first? Where are words two away from first? Why do we need to avoid revisiting a word, when? Why do we use a set for this? Why a HashSet? Alternatives? If we want the ladder, not just whether it exists What's path from white to house? We know there is one. All words one-away from first are on Q and are added when first is dequeued/removed first time through loop. Each of these 1-away-from-start words comes off the queue and words that are one-away from these, or 2-away from start, are put on the Q. BUT it's a queue so all 2-away words go on after the 1-away. In general, we remove N-away words before (N+1)-away words because of FIFO structure

Shortest path proof All words one away from start on queue first iteration What is first value of current when loop entered? All one-away words dequeued before two-away See previous assertion, property of queues Two-away before 3-away, … Each 2-away word is one away from a 1-away word So all enqueued after one-away, before three-away How do we find three-away word? Any w seen/dequeued that's n-away is: Seen before every n+k-away word for k >=1! Don't dwell on this, but walk through it as a semi-formal argument, akin to a hand-wavy proof

Keeping track of ladder Find w, a one-away word from current Enqueue w if not seen Call map.put(w,current) Remember keys are unique! Put word on queue once! map.put("lot", "hot") map.put("dot", "hot") map.put("hat", "hot") Remind students that we need to keep track of how words are connected to reconstruct the ladder. Keys in a map are unique, but we only put a word on the queue once! Why? Using a set. We can make this one time the time we make a word the key in a map. The value is what caused the word to be put onto the queue, see examples

Reconstructing Word Ladder Run WordLaddersFull https://git.cs.duke.edu/201fall16/linearstructures/blob/master/src/WordLaddersFull.java Notice map and call to map.put(word,current) What about when returning the ladder, why is the returned ladder in reverse order? What do we know about code when statement adding (key,value) to map runs? Run program with "white" "house" and with "voter" "crazy" and with "voter" "smart"

http://bit.ly/201fall16-queue WOTO What's your favorite word ladder? Could you write code to find the longest word ladder?