CPS 100 6.1 Sequential search revisited l What does the code below do? How would it be called initially?  Another overloaded function search with 2 parameters?

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
CPS 100, Spring Spirits of Linear Structures l Past, Present, Future, … l Linked-lists and arrays and ArrayLists  ADT: insert, delete, iterate/traverse,
Topic 15 Implementing and Using Stacks
Stacks.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
TCSS 342, Winter 2005 Lecture Notes
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Topic 15 Implementing and Using Stacks
Stacks, Queues, and Deques
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CPS 100, Spring Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete,
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CPS Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete, traverse,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Compsci 6/101, Spring WBFSB l Plan for this week  Get test back, review, look for problems/weaknesses  How to address these?  Assignments.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
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.
Compsci 06/101, Spring This week in Compsci 6/101 l From sets to dictionaries  Incredibly powerful in solving problems  Power in Perl, Python,
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
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)
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
Stack: What problems does it solve?
Data Structures revisited
Plan for Week Linear structures: Stack, Queue
Objectives In this lesson, you will learn to: Define stacks
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Data Structures
Stacks and Queues.
Building Java Programs
Stacks and Queues.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stacks and Queues.
Presentation transcript:

CPS Sequential search revisited l What does the code below do? How would it be called initially?  Another overloaded function search with 2 parameters? boolean search(String[] a, int index, String target) { if (index >= a.length) return false; else if (a[index].equals(target)) return true; else return search(a,index+1,target); } l What is complexity (big-Oh) of this function?

CPS Recursion and Recurrences boolean occurs(String s, String x) { // post: returns true iff x is a substring of s if (s.equals(x)) return true; if (s.length() <= x.length()) return false; return occurs(s.substring(1, s.length()), x) || occurs(s.substring(0, s.length()-1), x); } l In worst case, both calls happen Say T( N ) is the worst case cost of occurs(s,x), N == s.length()  T( N ) = 1 if N < x.length()  T( N ) = 2T(N-1) + O(1) if N > x.length()  T(N) = O(1) if N <= x.length()  T( N ) = O(1) if s == x l What is T( N )? T( N ) = 2T(N-1) + O(1) = 2 (2T(N-2) + O(1)) + O(1) = 4T(N-2) + 3O(1) = 4[2T(N-3) + O(1)] + 3O(1) = 8T(N-3) + 7O(1) =... = 2 k T(N-k) + (2 k - 1)O(1) -- x.length() == 0 T(0) = O(1) N -k = 1 => k = N-1 N-k = 0 -> k => 2 N-1 T(1) +( 2 N-1 -1) * O(1) = 2 N-1 O(1) + 2 N-1) O(1) - 1= 2 N -1

CPS Binary search revisited bool bsearch(String[] a, int start, int end, String target) { // base case if (start == end) return a[start].equals(target); int mid = (start + end)/2; int comp = a[mid].compareTo(target); if (comp == 0) // found target return true; else if (comp < 0) // target on right return bsearch(a, mid +1, end); else // target on left return bsearch(a, start, mid - 1); } What is the big-Oh of bsearch ?

CPS Why we study recurrences/complexity? l Tools to analyze algorithms l Machine-independent measuring methods l Familiarity with good data structures/algorithms l What is CS person: programmer, scientist, engineer? scientists build to learn, engineers learn to build l Mathematics is a notation that helps in thinking, discussion, programming

CPS The Power of Recursion: Brute force l Consider the TypingJob APT problemn: What is minimum number of minutes needed to type n term papers given page counts and three typists typing one page/minute? (assign papers to typists to minimize minutes to completion)  Example: {3, 3, 3,5,9,10,10} as page counts l How can we solve this in general? Suppose we're told that there are no more than 10 papers on a given day.  How does this constraint help us?  What is complexity of using brute-force?

CPS Recasting the problem l Instead of writing this function, write another and call it min minutes to type papers in pages int bestTime(int[] pages) { } l What cases do we consider in function below? int best(int[] pages, int index, int t1, int t2, int t3) // returns min minutes to type papers in pages // starting with index-th paper and given // minutes assigned to typists, t1, t2, t3 { } return best(pages,0,0,0,0);

CPS Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete, traverse, …  Advantages and trade-offs include … l We want to move toward structures that support very efficient insertion and lookup, lists can't do better than O(n) for one of these: consider binary search and insert for arrays, or insert and lookup for linked lists l Interlude: two linear structures that facilitate certain algorithms: Stack and Queue  Restricted access linear structures

CPS Stack: What problems does it solve? l Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called recursively l Stacks are used to evaluate arithmetic expressions, to implement compilers, to implement interpreters  The Java Virtual Machine (JVM) is a stack-based machine  Postscript is a stack-based language  Stacks are used to evaluate arithmetic expressions in many languages l Small set of operations: LIFO or last in is first out access  Operations: push, pop, top, create, clear, size  More in postscript, e.g., swap, dup, rotate, …

CPS Simple stack example Stack is part of java.util.Collections hierarchy  It's an OO abomination, extends Vector (like ArrayList) Should be implemented using Vector Doesn't model "is-a" inheritance  what does pop do? What does push do? Stack 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.peek()); System.out.println(s.pop());

CPS Implementation is very simple l Extends Vector, so simply wraps Vector/ArrayList methods in better names  push==add, pop==remove (also peek and empty )  Note: code below for ArrayList, Vector is used Stack is generic, so Object replaced by generic reference (see next slide) public Object push(Object o){ add(o); return o; } public Object pop(){ return remove(size()-1); }

CPS Implementation is very simple l Extends Vector, so simply wraps Vector/ArrayList methods in better names  What does generic look like? public class Stack extends ArrayList { public E push(E o){ add(o); return o; } public E pop(Object o){ return remove(size()-1); }

CPS Uses rather than "is-a" Suppose there's a private ArrayList myStorage  Doesn't extend Vector, simply uses Vector/ArrayList  Disadvantages of this approach? Synchronization issues public class Stack { private ArrayList myStorage; public E push(E o){ myStorage.add(o); return o; } public E pop(o){ return myStorage.remove(size()-1); }

CPS Postfix, prefix, and infix notation l Postfix notation used in some HP calculators  No parentheses needed, precedence rules still respected * *  Read expression For number/operand: push For operator: pop, pop, operate, push l See Postfix.java for example code, key ideas:  Use StringTokenizer, handy tool for parsing  Note: Exceptions thrown, what are these? l What about prefix and infix notations, advantages?

CPS Exceptions l Exceptions are raised or thrown in exceptional cases  Bad indexes, null pointers, illegal arguments, …  File not found, URL malformed, … l Runtime exceptions aren't meant to be handled or caught  Bad index in array, don't try to handle this in code  Null pointer stops your program, don't code that way! l Other exceptions must be caught or rethrown  See FileNotFoundException and IOException in Scanner class implementation l RuntimeException extends Exception, catch not required

CPS Prefix notation in action l Scheme/LISP and other functional languages tend to use a prefix notation (define (square x) (* x x)) (define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1)))))

CPS Postfix notation in action l Practical example of use of stack abstraction l Put operator after operands in expression  Use stack to evaluate operand: push onto stack operator: pop operands push result l PostScript is a stack language mostly used for printing  drawing an X with two equivalent sets of code %! moveto rlineto moveto 100 –100 rlineto stroke showpage %! 100 – moveto rlineto stroke showpage

CPS Queue: another linear ADT l FIFO: first in, first out, used in many applications  Scheduling jobs/processes on a computer  Tenting policy?  Computer simulations l Common operations: add (back), remove (front), peek ??  java.util.Queue is an interface (jdk5) offer(E), remove(), peek(), size()  java.util.LinkedList implements the interface add(), addLast(), getFirst(), removeFirst() l Downside of using LinkedList as queue  Can access middle elements, remove last, etc. why?

CPS Stack and Queue implementations l Different implementations of queue (and stack) aren’t really interesting from an algorithmic standpoint  Complexity is the same, performance may change (why?)  Use ArrayList, growable array, Vector, linked list, … Any sequential structure l As we'll see java.util.LinkedList is good basis for all  In Java 5, LinkedList implements the Queue interface, low-level linked lists facilitate (circular list!) l ArrayList for queue is tricky, ring buffer implementation, add but wrap-around if possible before growing  Tricky to get right (exercise left to reader)

CPS Using linear data structures l We’ve studied arrays, stacks, queues, which to use?  It depends on the application  ArrayList is multipurpose, why not always use it? Make it clear to programmer what’s being done Other reasons? l Other linear ADTs exist  List: add-to-front, add-to-back, insert anywhere, iterate Alternative: create, head, tail, Lisp or Linked-list nodes are concrete implementation  Deque: add-to-front, add-to-back, random access Why is this “better” than an ArrayList? How to implement?

CPS Maria Klawe Chair of Computer Science at UBC, Dean of Engineering at Princeton, President of Harvey Mudd College, ACM Fellow,… Klawe's personal interests include painting, long distance running, hiking, kayaking, juggling and playing electric guitar. She describes herself as "crazy about mathematics" and enjoys playing video games. "I personally believe that the most important thing we have to do today is use technology to address societal problems, especially in developing regions"

CPS Queue applications l Simulation, discrete-event simulation  How many toll-booths do we need? How many express lanes or self-checkout at grocery store? Runway access at aiport?  Queues facilitate simulation with mathematical distributions governing events, e.g., Poisson distribution for arrival times l Shortest path, e.g., in flood-fill to find path to some neighbor or in word-ladder  How do we get from "white" to "house" one-letter at a time? white, while, whale, shale, shake, …?

CPS Blob Finding with Queues myGrid[row][col] = fillWith; // mark pixel size++; // count pixel myQueue.add(myPairGrid[row][col]); while (myQueue.size() != 0){ Pair p = myQueue.remove(); for(int k=0; k < rowDelta.length; k++){ row = p.row + rowDelta[k]; col = p.col + colDelta[k]; if (inRange(row,col) && myGrid[row][col] == lookFor){ myQueue.add(myPairGrid[row][col]); myGrid[row][col] = fillWith; size++; }

CPS Queue for shortest path (see APT) public boolean ladderExists(String[] words, String from, String to){ Queue q = new LinkedList (); Set used = new TreeSet (); for(String s : words){ if (oneAway(from,s)){ q.add(s); used.add(s); } while (q.size() != 0){ String current = q.remove(); if (oneAway(current,to)) return true; // add code here, what? } return false; }

CPS Shortest Path reprised l How does use of Queue ensure we find shortest path?  Where are words one away from start?  Where are words two away from start? l Why do we need to avoid revisiting a word, when?  Why do we use a set for this? Why a TreeSet?  Alternatives? l What if we want the ladder, not just whether it exists  What’s path from white to house? We know there is one.  Ideas? Options?