Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
6
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.
7
Stacks: Java and Otherwise
See StackDemo.java Note that Stack is in java.util Stack is generic, e.g., Stack<String> 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
8
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
9
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 * 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
10
Postfix, prefix, and infix notation
Postfix notation used in some HP calculators No parentheses needed, precedence rules work * * Read expression For number/operand: push onto stack For operator: pop, pop, operate, push To evaluate prefix: (3 + 5) * 7 we'd use * What is * + 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
11
Postfix.java To evaluate a postfix expression:
If number, push. If operator: pop, pop, op, push 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 * +, then try some with errors, e.g., 3 + or 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
12
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
13
http://bit.ly/201fall16-stacks WOTO
Look at source code for Postfix.java to understand both RPN and parsing using tokens in Java
14
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.
15
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()
16
Stacks: Java and Otherwise
See QueueDemo.java Note that Queue is in java.util and is an Interface! Typically use LinkedList to create concrete object 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
17
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
18
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, …?
19
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
20
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;
21
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
22
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
23
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
24
Reconstructing Word Ladder
Run WordLaddersFull 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"
25
http://bit.ly/201fall16-queue WOTO What's your favorite word ladder?
Could you write code to find the longest word ladder?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.