CS : Data Structures

Similar documents
CS : Data Structures

CS : Data Structures Michael Schatz. Sept Lecture 10. Stacks and JUnit

CS : Data Structures

CS : Data Structures

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

IT 4043 Data Structures and Algorithms

CS : Data Structures Michael Schatz. Sept Lecture 7. More Complexity

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Unit #3: Recursion, Induction, and Loop Invariants

CS24 Week 4 Lecture 2

CS : Data Structures

Programming II (CS300)

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CS 211: Recursion. Chris Kauffman. Week 13-1

CS : Data Structures

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

CS : Data Structures

CS 211: Recursion. Chris Kauffman. Week 13-1

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CS : Data Structures

CS : Data Structures

C/C++ Programming Lecture 18 Name:

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Programming II (CS300)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 10: Recursion vs Iteration

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CS 216 Exam 1 Fall SOLUTION

CS350: Data Structures Stacks

Programming II (CS300)

Unit #2: Recursion, Induction, and Loop Invariants

Recursive Definitions

Stacks Fall 2018 Margaret Reid-Miller

ITI Introduction to Computing II

CMSC 132: Object-Oriented Programming II. Stack and Queue

CS126 Final Exam Review

ITI Introduction to Computing II

CSC 222: Computer Programming II. Spring 2005

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Recursion: The Beginning

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 1

Computer Algorithms CISC4080 CIS, Fordham Univ. Acknowledgement. Outline. Instructor: X. Zhang Lecture 1

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

CS : Data Structures

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

HEAPS & PRIORITY QUEUES

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Lecture 6 CS2110 Spring 2013 RECURSION

Lecture 7 CS2110 Fall 2014 RECURSION

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Elementary Sorting Algorithms

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

RECURSION (CONTINUED)

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

APCS-AB: Java. Recursion in Java December 12, week14 1

CS 206 Introduction to Computer Science II

Algorithmic Analysis. Go go Big O(h)!

Lecture 6: Recursion RECURSION

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Pseudo code of algorithms are to be read by.

Programming II (CS300)

Introduction to Analysis of Algorithms

CS : Data Structures

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

Searching & Sorting in Java Bubble Sort

Data Structures and Algorithms. Part 2

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

RECURSION. Many Slides from Ken Birman, Cornell University

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Data Structures and Algorithms Chapter 2

Summer Final Exam Review Session August 5, 2009

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

CSE 373: Introduction, ADTs, Design Decisions, Generics. Michael Lee Wednesday Jan 3, 2017

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 617 Object Oriented Systems Lecture 3 Object Abstractions, Encapsulation, Abstract Data Types 3:30-5:00pm Thu, Jan 10

Recursion Chapter 3.5

CS 112 Introduction to Programming

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

CS 310 Advanced Data Structures and Algorithms

LECTURE 17. Array Searching and Sorting

WYSE Academic Challenge State Finals Computer Science 2007 Solution Set

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

BOBJ: A Quickstart for Software Engineers

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Week 12: Running Time and Performance

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Algorithms and Data Structures

Transcription:

CS 600.226: Data Structures Michael Schatz Sept 23, 2016 Lecture 9: Stacks

Assignment 3: Due Sunday Sept 25 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently written!

Part 1: Recap

Growth of functions http://bigocheatsheet.com/

Trying every possible permutation Growth of functions Trying every possible subset Processing every element in a square array, Comparing every element to every other element Finding the nearest Pokemon stop from every other stop with a k-d tree Linear Search Finding an element in a balanced tree, Simple arithmetic, simple comparison, http://bigocheatsheet.com/ array access

Pop quiz! public static int foo(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum = sum + i; } return sum; } public static int bar(int n) { int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k < j; k++) { sum = sum + i + k + k; } } } return sum; } public static int foobar(int n) { return foo(n) + bar(n); } Classic Linear Scan: O(n) Nested for loop, where each loop (indirectly) depends on n: O(n 3 ) O(n) + O(n 3 ) = O(n 3 )

Pop quiz! public static int baz(int n) { if (n <= 0) { return 0; } int sum = 0; for (int i = 0; i < n; i++) { sum = sum + i; } return sum + baz(n/2) + baz(n/2-1); } T(n) = n + T(n/2) + T(n/2) Follow the recursion tree O(n lg n) public static int buz(int n) { if (n <= 1) { return 1; } return buz(n-1) + buz(n-2); } T(n) = 1 + T(n-1) + T(n-2) Follow the recursion tree O(2 n ) public static int fastbuz(int n) { int [] s = new int[n+1]; s[1] = 1; s[2] = 1; for (int i = 3; i <= n; i++) { s[i] = s[i-1] + s[i-2]; } return s[n]; } Linear Scan: O(n) Same result just exponentially faster!

Part 2: Sorting Part 2

Selection Sort Quickly sort these numbers into ascending order: 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 Find the minimum 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 Flip it into the right place 6, 29, 14, 31, 39, 64, 78, 50, 13, 63, 61, 19 Find the next smallest 6, 29, 14, 31, 39, 64, 78, 50, 13, 63, 61, 19 Flip it into the right place 6, 13, 14, 31, 39, 64, 78, 50, 29, 63, 61, 19 Find the next smallest 6, 13, 14, 31, 39, 64, 78, 50, 29, 63, 61, 19 Flip it into the right place 6, 13, 14, 31, 39, 64, 78, 50, 29, 63, 61, 19 O(N) O(1) O(N-1) O(1) O(N-2) O(1)

Selection Sort Analysis Analysis Outer loop: i = 0 to n Inner loop: j = i to n Total Running time: Outer * Inner = O(n 2 ) T = n +(n 1) + (n 2) + +3+2+1= nx i = i=1 n(n + 1) 2 = O(n 2 )

Selection Sort Analysis Formally you would prove the recurrence using induction: Discrete Math class! Requires almost no extra space: In-place algorithm Analysis Outer loop: i = 0 to n Inner loop: j = i to n Total Running time: Outer * Inner = O(n 2 ) T = n +(n 1) + (n 2) + +3+2+1= nx i = i=1 Feels slow since inner loop only seeks out one number (the next biggest) n(n + 1) 2 = O(n 2 )

Bubble sort Sort these values by bubbling up the next largest value 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 [14, 29], 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 [14, 29], 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 14, [29, 6], 31, 39, 64, 78, 50, 13, 63, 61, 19 14, [6, 29], 31, 39, 64, 78, 50, 13, 63, 61, 19 14, 6, [29, 31], 39, 64, 78, 50, 13, 63, 61, 19 14, 6, [29, 31], 39, 64, 78, 50, 13, 63, 61, 19 14, 6, 29, [31, 39], 64, 78, 50, 13, 63, 61, 19 14, 6, 29, [31, 39], 64, 78, 50, 13, 63, 61, 19 14, 6, 29, 31, [39, 64], 78, 50, 13, 63, 61, 19 14, 6, 29, 31, [39, 64], 78, 50, 13, 63, 61, 19 14, 6, 29, 31, 39, [64, 78], 50, 13, 63, 61, 19 14, 6, 29, 31, 39, [64, 78], 50, 13, 63, 61, 19 14, 6, 29, 31, 39, 64, [78, 50], 13, 63, 61, 19 14, 6, 29, 31, 39, 64, [50, 78], 13, 63, 61, 19 14, 6, 29, 31, 39, 64, 50, [78, 13], 63, 61, 19 14, 6, 29, 31, 39, 64, 50, [13, 78], 63, 61, 19 14, 6, 29, 31, 39, 64, 50, 13, [78, 63], 61, 19 14, 6, 29, 31, 39, 64, 50, 13, [63, 78], 61, 19 14, 6, 29, 31, 39, 64, 50, 13, 63, [78, 61], 19 14, 6, 29, 31, 39, 64, 50, 13, 63, [61, 78], 19 14, 6, 29, 31, 39, 64, 50, 13, 63, 61, [78, 19] 14, 6, 29, 31, 39, 64, 50, 13, 63, 61, [19, 78] 14, 6, 29, 31, 39, 64, 50, 13, 63, 61, 19, 78 On the first pass, sweep list to bubble up the largest element (also move smaller items down)

Bubble sort Sort these values by bubbling up the next largest value 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 [14, 6], 29, 31, 39, 64, 50, 13, 63, 61, 19, 78 [6, 14], 29, 31, 39, 64, 50, 13, 63, 61, 19, 78 6, [14, 29], 31, 39, 64, 50, 13, 63, 61, 19, 78 6, [14, 29], 31, 39, 64, 50, 13, 63, 61, 19, 78 6, 14, [29, 31], 39, 64, 50, 13, 63, 61, 19, 78 6, 14, [29, 31], 39, 64, 50, 13, 63, 61, 19, 78 6, 14, 29, [31, 39], 64, 50, 13, 63, 61, 19, 78 6, 14, 29, [31, 39], 64, 50, 13, 63, 61, 19, 78 6, 14, 29, 31, [39, 64], 50, 13, 63, 61, 19, 78 6, 14, 29, 31, [39, 64], 50, 13, 63, 61, 19, 78 6, 14, 29, 31, 39, [64, 50], 13, 63, 61, 19, 78 6, 14, 29, 31, 39, [50, 64], 13, 63, 61, 19, 78 6, 14, 29, 31, 39, 50, [64, 13], 63, 61, 19, 78 6, 14, 29, 31, 39, 50, [13, 64], 63, 61, 19, 78 6, 14, 29, 31, 39, 50, 13, [64, 63], 61, 19, 78 6, 14, 29, 31, 39, 50, 13, [63, 64], 61, 19, 78 6, 14, 29, 31, 39, 50, 13, 63, [64, 61], 19, 78 6, 14, 29, 31, 39, 50, 13, 63, [61, 64], 19, 78 6, 14, 29, 31, 39, 50, 13, 63, 61, [64, 19], 78 6, 14, 29, 31, 39, 50, 13, 63, 61, [19, 64], 78 6, 14, 29, 31, 39, 50, 13, 63, 61, 19, 64, 78 On the second pass, sweep list to bubble up the second largest element (also move smaller items down)

Bubble sort Sort these values by bubbling up the next largest value 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 [6, 14], 29, 31, 39, 50, 13, 63, 61, 19, 64, 78 6, [14, 29], 31, 39, 50, 13, 63, 61, 19, 64, 78 6, 14, [29, 31], 39, 50, 13, 63, 61, 19, 64, 78 6, 14, 29, [31, 39], 50, 13, 63, 61, 19, 64, 78 6, 14, 29, 31, [39, 50], 13, 63, 61, 19, 64, 78 6, 14, 29, 31, 39, [50, 13], 63, 61, 19, 64, 78 6, 14, 29, 31, 39, [13, 50], 63, 61, 19, 64, 78 6, 14, 29, 31, 39, 13, [50, 63], 61, 19, 64, 78 6, 14, 29, 31, 39, 13, 50, [63, 61], 19, 64, 78 6, 14, 29, 31, 39, 13, 50, [61, 63], 19, 64, 78 6, 14, 29, 31, 39, 13, 50, 61, [63, 19], 64, 78 6, 14, 29, 31, 39, 13, 50, 61, [19, 63], 64, 78 6, 14, 29, 31, 39, 13, 50, 61, 19, 63, 64, 78 On the third pass, sweep list to bubble up the third largest element How many passes will we need to do? O(n) How much work does each pass take? O(n) What is the total amount of work? n passes, each requiring O(n) => O(n 2 ) Note, you might get lucky and finish much sooner than this Feels faster : multiple swaps on the inner loop, but Feels slow because inner loop sweeps entire list to move one number into sorted position

Insertion Sort Quickly sort these numbers into ascending order: 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 50, 64, 78, 13, 63, 61, 19 6, 13, 14, 29, 31, 39, 50, 64, 78, 63, 61, 19 6, 13, 14, 29, 31, 39, 50, 63, 64, 78, 61, 19 6, 13, 14, 29, 31, 39, 50, 61, 63, 64, 78, 19 6, 13, 14, 19, 29, 31, 39, 50, 61, 63, 64, 78 Sorted elements To be sorted Base Case: Declare the first element as a correctly sorted array Repeat: Iteratively add the next unsorted element to the partially sorted array at the correct position Slide the unsorted element into the correct position: 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 14, 6, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 6, 14, 29, 31, 39, 64, 78, 50, 13, 63, 61, 19 Feels fast because you always have a partially sorted list, but some insertions will be expensive

Insertion Sort Quickly sort these numbers into ascending order: 14, 29, 6, 31, 39, 64, 78, 50, 13, 63, 61, 19 sorted unsorted rest of array sorted? i unsorted rest of array <? i > unsorted rest of array <? i > unsorted rest of array <? i > unsorted rest of array <? i > unsorted rest of array sorted sorted i unsorted rest of array unsorted rest of array Outer loop: n elements to move into correct position Inner loop: O(n) work to move element into correct position Total Work: O(n 2 )

Quadratic Sorting Algorithms Selection Sort Move next smallest into position Bubble Sort Swap up bigger values over smaller Insertion Sort Slide next value into correct position Asymptotically all three have the same performance but can perform significantly different on some datasets

Sorting Race!

Sorting Race!

Part 3: Stacks

Fibonacci Sequence public static int fib(int n) { if (n <= 1) { return 1; } return fib(n-1) + fib(n-2); } f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0) The computer has to keep track of all of these function calls and keep everything in order!

Introducing the call stack 100: int f(int a) { 101: int y = 42; 102: int z = 13; 400: int x = g(13 + a); 497: return x + y + z; 498: } 499: 500: int g(double a) { 501: return h(a*2); 502: } 503: 504: int h(double x) { 505: int k = (int) x; 506: return k+1; 507: } 508: 509: static public void main() { 510: int val = f(15); 510: System.out.println(val); 511: } h Return val to g line 501 x=56 k=56 val = 57 g Return val to f line 400 a=28 val = <h(56)> f Return val to main line 510 a = 15 y = 42 z = 13 x = <g(28)> main val = <f(15)>

Introducing the call stack The call stack keeps track of the local variables and return location for the current function. This makes it possible for program execution to jump from function to function without loosing track of where the program should resume A stack frame records the information for each function call, with local variables and the address of where to resume processing after this function is complete. => Take a computer architecture course for more info Importantly the computer only needs to add or remove items from the very top of the stack, making it easy for the computer to keep track of where to go next! More generally, stacks are a very useful data structure for Last-In-First-Out (LIFO) processing h Return val to g line 501 x=56 k=56 val = 57 g Return val to f line 400 a=28 val = <h(56)> f Return val to main line 510 a = 15 y = 42 z = 13 x = <g(28)> main val = <f(15)>

Stacks Stacks are very simple but surprisingly useful data structures for storing a collection of information Any number of items can be stored, but you can only manipulate the top of the stack: Push: adds a new element to the top Pop: takes off the top element Top: Lets you peek at top element s value without removing it from stack Many Applications In hardware call stack Memory management systems Parsing arithmetic instructions: ((x+3) / (x+9)) * (42 * sin(x)) Back-tracing, such as searching within a maze

Fibonacci Sequence f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0)

Fibonacci Sequence f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) a = F(5) f(1) f(0)

Fibonacci Sequence f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) F(5) a = F(4) a = F(5) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0)

Fibonacci Sequence f(5) f(4) f(4) f(3) f(3) f(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0)

Fibonacci Sequence f(5) f(4) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0)

Fibonacci Sequence F(2) a = F(1) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0)

Fibonacci Sequence F(1) return 1 F(2) a = F(1) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 f(0)

Fibonacci Sequence F(2) b = F(0) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 f(0)

Fibonacci Sequence F(0) return 1 F(2) b = F(0) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence F(2) return 2 F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) F(3) a = F(2) F(4) a = F(3) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) F(3) b = F(1) F(4) a = F(3) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 f(1) f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence F(1) return 1 F(3) b = F(1) F(4) a = F(3) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) F(3) return 3 F(4) a = F(3) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) f(4) f(3) f(3) f(2) F(4) b = F(2) F(5) a = F(4) a = F(5) 2 1 f(1) f(0) f(1) f(0) f(1) f(0) 1 1 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0)

Fibonacci Sequence f(5) f(4) F(2) A = F(1) F(4) b = F(2) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 f(1) f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence F(1) return 1 F(2) A = F(1) F(4) b = F(2) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 1 f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) F(2) b = F(0) F(4) b = F(2) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 1 f(0) f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence F(0) return 1 F(2) b = F(0) F(4) b = F(2) F(5) a = F(4) a = F(5) f(5) f(4) f(4) f(3) f(3) f(2) 3 f(2) f(2) f(1) f(2) f(1) f(1) f(0) 2 1 1 1 f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) F(2) return 2 F(4) b = F(2) F(5) a = F(4) a = F(5) f(4) f(3) f(3) f(2) 3 2 f(2) f(1) f(2) f(1) f(1) f(0) 2 1 1 1 f(1) f(0) f(1) f(0) 1 1

Fibonacci Sequence f(5) f(4) 5 f(3) f(3) f(2) F(4) return 5 F(5) a = F(4) a = F(5) 2 1 1 1 f(1) f(0) f(1) f(0) 1 1 3 2 f(2) f(1) f(2) f(1) f(1) f(0)

Fibonacci Sequence Equivalent to running through a maze and always keeping your right hand on the wall Notice we only look at the top of the stack to keep track of where we have been and were we should go next! f(5) f(4) 5 f(3) f(3) f(2) 3 2 f(2) f(1) f(2) f(1) f(1) f(0) F(5) a = F(4) a = F(5) 2 1 1 1 f(1) f(0) f(1) f(0) 1 1

Stack ADT adt Stack uses Any, Boolean defines Stack<T: Any> operations new: ---> Stack<T> push: Stack<T> x T ---> Stack<T> pop: Stack<T> -/-> Stack<T> top: Stack<T> -/-> T empty: Stack<T> ---> Boolean preconditions pop(s): not empty(s) top(s): not empty(s) axioms empty(new()) not empty(push(s, t)) top(push(s, t)) = t pop(push(s, t)) = s

Stack ADT adt Stack uses Any, Boolean defines Stack<T: Any> operations new: ---> Stack<T> push: Stack<T> x T ---> Stack<T> pop: Stack<T> -/-> Stack<T> top: Stack<T> -/-> T empty: Stack<T> ---> Boolean preconditions pop(s): not empty(s) top(s): not empty(s) axioms empty(new()) not empty(push(s, t)) top(push(s, t)) = t pop(push(s, t)) = s Note: pop(push(s,t)) = s is comparing the whole stack, but we havent defined what it means for one stack to equal another! Relax this flaw by considering the axioms as rewrite rules: top(pop(push(push(new(), 1), 2))) = 1 Can be rewritten as top(push(new(), 1)) = 1 and finally to 1=1

Stack ADT adt Stack uses Any, Boolean defines Stack<T: Any> operations new: ---> Stack<T> push: Stack<T> x T ---> Stack<T> pop: Stack<T> You might be -/-> tempted Stack<T> to try to derive a correct equality top: Stack<T> operation, or -/-> add additional T constraints (like maximum empty: number Stack<T> of elements ---> that Boolean the stack can store) but the ADT preconditions gets very messy pop(s): not empty(s) top(s): not empty(s) See lecture notes for details axioms empty(new()) Instead, lets focus on implementation instead not empty(push(s, t)) top(push(s, t)) = t pop(push(s, t)) = s

Stack Interface public interface Stack<T> { // checks if empty boolean empty(); // peeks at top value without removing T top() throws EmptyException; // removes top element void pop() throws EmptyException; } // adds new element to top of stack void push(t t); How would you implement this interface? Why?

Stack JUnit Tests (1/2) import org.junit.test; import org.junit.before; import static org.junit.assert.assertequals; public class TestStack { private Stack<Integer> stack; @Before public void makestack() { stack = new ListStack<Integer>(); } @Test public void testnewempty() { assertequals(true, stack.empty()); } @Test public void testpushnotempty() { stack.push(12); assertequals(false, stack.empty()); } @Test public void testpushpopempty() { stack.push(12); stack.pop(); assertequals(true, stack.empty()); }

Stack JUnit Tests (2/2) @Test public void testpushtopequal() { stack.push(12); int hack = stack.top(); assertequals(12, hack); } } @Test public void testlotsofpush() { for (int i = 0; i < 100; i++) { stack.push(i); } int pops; for (pops = 0;!stack.empty(); pops++) { int hack = stack.top(); assertequals(99-pops, hack); stack.pop(); } assertequals(100, pops); }

ListStack vs. ArrayStack Which is better? Why?

Next Steps 1. Reflect on the magic and power of stacks! 2. Finish Assignment 3: Due Sunday Sept 25 @ 10:00 pm

Welcome to CS 600.226 http://www.cs.jhu.edu/~cs226/ Questions?