CPS 100, Fall 2011 8.1 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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CPS 100, Spring Spirits of Linear Structures l Past, Present, Future, … l Linked-lists and arrays and ArrayLists  ADT: insert, delete, iterate/traverse,
Stacks.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
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,
Stacks, Queues & Deques CSC212.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
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
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
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
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?
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
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.
Compsci 06/101, Fall What's ahead in Compsci 6/101? l Software architecture and design, why Jotto has modules and how communication works  We.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
CompSci 100e Program Design and Analysis II March 15, 2011 Prof. Rodger CompSci 100e, Spring20111.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Compsci 6/101, Spring WBFSB l Plan for this week  Get test back, review, look for problems/weaknesses  How to address these?  Assignments.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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,
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
G64ADS Advanced Data Structures
Stack: What problems does it solve?
Marcus Biel, Software Craftsman
Stacks and Queues.
Data Structures revisited
Plan for Week Linear structures: Stack, Queue
CSE 373: Data Structures and Algorithms
Stacks and Queues.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Building Java Programs
Building Java Programs
Stacks and Queues.
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stacks and Queues 1.
slides created by Marty Stepp
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks and Queues.
Presentation transcript:

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 a partner on DNA?  When do you turn in DNA assignment?

CPS 100, Fall Time and Memory: Recombinant DNA l Splice three GTGATAATTC strands into DNA  Use strings: length of result is N + 3*10  Generalize to N + B*S (# breaks x size-of-splice) l We can use linked lists instead  Use same GTGATAATTC if strands are immutable  Generalize to N+ S + B, is this an improvement?

CPS 100, Fall Spirits of Linear Structures l Past, Present, Future, … l Linked-lists and arrays and ArrayLists  ADT: insert, delete, iterate/traverse, grow, …  Advantages and trade-offs include … l Goal: 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. One is very good, the other not-so-good.  What's the hybrid approach?

CPS 100, Fall Stacks Queues and Deques, Oh My! l Linear structures used in problem domains and algorithms: Stack, Queue, Dequeue.  Similar abstractions with different semantics  What?

CPS 100, Fall Why don't we just use arrays? l Stacks used in implementing recursion, postscript language, Java language, graph algorithms  Stacks implemented using array/ArrayList l Queues used in simulation, graph algorithms, scheduling  Queues implemented using array/LinkedList l Deque (dequeue) double ended queue, wiki…  Implemented using LinkedList or other

CPS 100, Fall Wordladder Story l Ladder from ‘white’ to ‘house’  White, while, whale, shale, … l I can do that… optimally  My brother was an English major  My ladder is 16, his is 15, how? l There's a ladder that's 14 words!  The key is ‘sough’ l Guarantee optimality!  QUEUE

CPS 100, Fall Simple stack example Stack is part of java.util.Collections hierarchy  As an ADT it's a LIFO data structure In Java you can randomly access it: OO abomination  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 100, Fall 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 100, Fall Interlude: 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 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 Some exceptions are caught or rethrown  FileNotFoundException and IOException l RuntimeException extends Exception  catch not required

CPS 100, Fall Danny Hillis The third culture consists of those scientists and other thinkers in the empirical world who, through their work and expository writing, are taking the place of the traditional intellectual in rendering visible the deeper meanings of our lives, redefining who and what we are. This comes back to the view of the Web as the first form of life to grow on the Internet. It's the slime mold of the Internet. I don't want to disparage it, but it's primitive. When computers pass around Web pages, they don't know what they're talking about. They're just passing around bits, and they can be meaningless bits as far as they're concerned. They're just acting like a big telephone system. (1996, edge.org)edge.org

CPS 100, Fall 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 100, Fall Stack and Queue implementations l Different implementations of queue (and stack) not interesting from an algorithmic standpoint  Complexity is the same, performance may change (why?)  Use ArrayList, growable array, Vector, linked list, … 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/nodes 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 100, Fall 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 100, Fall Implementation is very simple l Extends Vector, so simply wraps Vector/ArrayList methods in better names  Where could this. be used? What does 'extends' mean: is-a! public class Stack extends ArrayList { public E push(E o){ add(o); return o; } public E pop(Object o){ return remove(size()-1); }

CPS 100, Fall Uses or “has-a” rather than “is-a” Suppose there's a private ArrayList myStorage  Doesn't extend Vector, simply uses Vector/ArrayList  Better, faster, stronger, but harder to synchronize. 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 100, Fall 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 100, Fall 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 airport?  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  Get from "white" to "house" one-letter at a time? white, while, whale, shale, shake, …?

CPS 100, Fall 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 100, Fall Shortest Path reprised l How does 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 If we want the ladder, not just whether it exists  What’s path from white to house? We know there is one.  Ideas? Options?

CPS 100, Fall Shortest path proof l All words one away from start on queue before loop  Obvious from code l All one-away words dequeued before two-away  See previous assertion, property of queues  Two-away before 3-away, … l 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? l Any w seen/dequeued that's n-away is:  Seen before every n+k-away word for k >=1!