implementing abstractions · a bounded stack 4 0 allocated size logical size element array the...

140
Implementing Abstractions Part Two

Upload: others

Post on 17-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Implementing AbstractionsPart Two

Page 2: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Previously, on CS106B...

Page 3: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

0

allocatedsize

logicalsize

elementarray

Page 4: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

0

allocatedsize

logicalsize

elementarray

The stack’s allocated size is the number of slots in the

array. Remember – arrays in C++ cannot grow or shrink.

The stack’s allocated size is the number of slots in the

array. Remember – arrays in C++ cannot grow or shrink.

Page 5: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

0

allocatedsize

logicalsize

elementarray

The stack’s allocated size is the number of slots in the

array. Remember – arrays in C++ cannot grow or shrink.

The stack’s allocated size is the number of slots in the

array. Remember – arrays in C++ cannot grow or shrink.

The stack’s logical size is the number of elements actually

stored in the stack. This lets us track how much space we’re

actually using.

The stack’s logical size is the number of elements actually

stored in the stack. This lets us track how much space we’re

actually using.

Page 6: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

1

allocatedsize

logicalsize

elementarray

137

Page 7: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

2

allocatedsize

logicalsize

elementarray

137 42

Page 8: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

3

allocatedsize

logicalsize

elementarray

137 42 2718

Page 9: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

4

allocatedsize

logicalsize

elementarray

137 42 2718 512

Page 10: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

3

allocatedsize

logicalsize

elementarray

137 42 2718 512

Page 11: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

3

allocatedsize

logicalsize

elementarray

137 42 2718 512

Arrays cannot grow or shrink, so this older value is still technically there in the array. We’re just going to

pretend it isn’t.

Arrays cannot grow or shrink, so this older value is still technically there in the array. We’re just going to

pretend it isn’t.

Page 12: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

2

allocatedsize

logicalsize

elementarray

137 42 5122718

Page 13: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

3

allocatedsize

logicalsize

elementarray

137 42 161 512

Page 14: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Bounded Stack

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 15: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

New Stuff!

Page 16: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Running out of Space

● Our current implementation very quickly runs out of space to store elements.

● What should we do when this happens?

Page 17: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

2

allocatedsize

logicalsize

elementarray

137 42

Page 18: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

3

allocatedsize

logicalsize

elementarray

137 42 161

Page 19: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 20: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 21: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137

Page 22: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42

Page 23: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161

Page 24: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161 314

Page 25: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161 314

delete[]

DynamicDeallocation!

Page 26: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 27: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 28: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 29: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 30: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 31: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137

Page 32: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137 42

Page 33: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137 42 161

Page 34: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137 42 161 314

Page 35: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137 42 161 314 159

Page 36: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

137 42 161 314 159

delete[]

DynamicDeallocation!

Page 37: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 38: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 39: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

6

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 40: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

6

6

allocatedsize

logicalsize

elementarray

137 42 161 314 159 265

Page 41: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Ready... set... grow!

Page 42: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

class OurStack {public: OurStack(); ~OurStack();

void push(int value); int pop(); int peek() const;

int size() const; bool isEmpty() const;

private: void grow();

int* elems; int allocatedSize; int logicalSize;};

Page 43: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

class OurStack {public: OurStack(); ~OurStack();

void push(int value); int pop(); int peek() const;

int size() const; bool isEmpty() const;

private: void grow();

int* elems; int allocatedSize; int logicalSize;};

This is a private member function. It’s a helper function only

the implementation can call.

This is a private member function. It’s a helper function only

the implementation can call.

Page 44: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elems

137 42 161 314

Page 45: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

4

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

Page 46: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

Page 47: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

Page 48: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

Page 49: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

Page 50: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

Page 51: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

delete[]

DynamicDeallocation!

Page 52: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

Page 53: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

Page 54: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

newElems

If a and b are pointers, then

a = b;

makes a point where b is pointing.

If a and b are pointers, then

a = b;

makes a point where b is pointing.

Page 55: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

An Initial Idea

5

4

allocatedsize

logicalsize

elems

137 42 161 314

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

void OurStack::grow() { allocatedSize++;

int* newElems = new int[allocatedSize];

for (int i = 0; i < size(); i++) { newElems[i] = elems[i]; }

delete[] elems; elems = newElems;}

Page 56: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing Our Approach

● We now have a working solution, but is it an efficient solution?

● Let's analyze the big-O complexity of the five operations.● size: O(1)● isEmpty: O(1)● push: O(n)● pop: O(1)● peek: O(1)

Page 57: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing Our Approach

● We now have a working solution, but is it an efficient solution?

● Let's analyze the big-O complexity of the five operations.● size: O(1)● isEmpty: O(1)● push: O(n)● pop: O(1)● peek: O(1)

Page 58: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing Our Approach

● We now have a working solution, but is it an efficient solution?

● Let's analyze the big-O complexity of the five operations.● size: O(1)● isEmpty: O(1)● push: O(n)● pop: O(1)● peek: O(1)

Page 59: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing Our Approach

● We now have a working solution, but is it an efficient solution?

● Let's analyze the big-O complexity of the five operations.● size: O(1)● isEmpty: O(1)● push: O(n)● pop: O(1)● peek: O(1)

Page 60: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

Cost of the pushes:

1 + 2 + 3 + 4 + … + n = O(n2)

Cost of the pops:

1 +1 + 1 + 1 + … + 1 = O(n)

Total cost: O(n2)

Page 61: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

Cost of the pops:

1 +1 + 1 + 1 + … + 1 = O(n)

Total cost: O(n2)

Page 62: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

Cost of the pops:

1 +1 + 1 + 1 + … + 1 = O(n)

Total cost: O(n2)

Page 63: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

● Cost of the pops:● 1 + 1 + 1 + 1 + … + 1 = O(n)

Total cost: O(n2)

Page 64: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

● Cost of the pops:● 1 + 1 + 1 + 1 + … + 1 = O(n)

Total cost: O(n2)

Page 65: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

● Cost of the pops:● 1 + 1 + 1 + 1 + … + 1 = O(n)

● Total cost: O(n2)

Page 66: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What This Means

● What is the complexity of pushing n elements and then popping them?

● Cost of the pushes:● 1 + 2 + 3 + 4 + … + n = O(n2)

● Cost of the pops:● 1 + 1 + 1 + 1 + … + 1 = O(n)

● Total cost: O(n2)

Page 67: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Validating Our Model

Page 68: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Time-Out for Announcements!

Page 69: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Assignment 4

● Assignment 4 is due on Friday.● You can use a late day to extend the deadline to

Wednesday (there’s no class on Monday), but we don’t recommend this.● That will eat into your time for studying for the exam.● Topics from Assignment 4 are fair game for the exam.

● YEAH Hours for Assignment 5 will be on Friday at 3:30PM in 380-380Y.

Page 70: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Midterm Exam

● The midterm exam is next Tuesday, February 19 from 7:00PM – 10:00PM. Locations are divvied up by last (family) name:● A – K: Go to Bishop Auditorium● L – Z: Go to Hewlett 200

● It covers topics from Lectures 01 – 12 (up through and including big-O notation) and Assignments 0 – 4.

● The exam is closed-book and limited-note. You may bring one double-sided sheet of 8.5” × 11” of notes to the exam with you.

Page 71: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Midterm Exam

● We will be administering the exam using a software tool called BlueBook.

● Visit the CS106B website, click the “BlueBook” link under the “Resources” tab, then download the BlueBook software.

● This week’s section handout will be done through BlueBook so that you get a chance to test it out.

● Need a laptop for the exam? We can help out with that. Please contact us ASAP so we can make appropriate arrangements.

Page 72: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Practice Midterm Exam

● There’s a practice midterm exam up on the course website. It’s a minimally-modified version of the exam we gave out in Winter 2017.

● The password is

maplesyrup

and you’ll see why when you start the exam. 😃

Page 73: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Back to the Stack!

Page 74: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Speeding up the Stack

Page 75: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Key Idea: Plan for the Future

Page 76: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 77: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 78: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137

Page 79: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42

Page 80: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161

Page 81: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161 314

Page 82: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

137 42 161 314

delete[]

DynamicDeallocation!

Page 83: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 84: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 85: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

6

4

allocatedsize

logicalsize

elementarray

137 42 161 314

Page 86: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

6

5

allocatedsize

logicalsize

elementarray

137 42 161 314 159

Page 87: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Better Idea

6

6

allocatedsize

logicalsize

elementarray

137 42 161 314 159 265

Page 88: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

What Just Happened?

● Half of our pushes are now “easy” pushes, and half of our pushes are now “hard” pushes.

● Hard pushes still take time O(n).● Easy pushes only take time O(1).● Worst-case is still O(n).● What about the average case?

Page 89: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing the Work

Page 90: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing the Work

Page 91: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Analyzing the Work

We cut down the amount of work by roughly one

half!

We cut down the amount of work by roughly one

half!

Page 92: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 93: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 94: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 95: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 96: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 97: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 98: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

Page 99: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Different Analysis

We cut down the amount of work by roughly one

half!

We cut down the amount of work by roughly one

half!

Page 100: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

How does it stack up?

Page 101: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

Page 102: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

Page 103: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

137

Page 104: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

137 42

Page 105: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

137 42

delete[]

DynamicDeallocation!

Page 106: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

Page 107: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

2

2

allocatedsize

logicalsize

elementarray

137 42

Page 108: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

2

allocatedsize

logicalsize

elementarray

137 42

Page 109: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

3

allocatedsize

logicalsize

elementarray

137 42 271

Page 110: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

Page 111: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

Page 112: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

137

Page 113: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

137 42

Page 114: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

137 42 271

Page 115: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

137 42 271 828

Page 116: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

137 42 271 828

delete[]

DynamicDeallocation!

Page 117: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

Page 118: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

4

4

allocatedsize

logicalsize

elementarray

137 42 271 828

Page 119: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

8

4

allocatedsize

logicalsize

elementarray

137 42 271 828

Page 120: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

8

5

allocatedsize

logicalsize

elementarray

137 42 271 828 182

Page 121: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

8

6

allocatedsize

logicalsize

elementarray

137 42 271 828 182 845

Page 122: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

8

7

allocatedsize

logicalsize

elementarray

137 42 271 828 182 845 904

Page 123: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

A Much Better Idea

8

8

allocatedsize

logicalsize

elementarray

137 42 271 828 182 845 904 5

Page 124: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Let's Give it a Try!

Page 125: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

How do we analyze this?

Page 126: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 127: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 128: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 129: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 130: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 131: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 132: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 133: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 134: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 135: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

Page 136: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Spreading the Work

On average, we do just 3 units of work!

This is O(1) work on average!

On average, we do just 3 units of work!

This is O(1) work on average!

Page 137: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Sharing the Burden

● We still have “heavy” pushes taking time O(n) and “light” pushes taking time O(1).

● Worst-case time for a push is O(n).● Heavy pushes become so rare that the

average time for a push is O(1).● Can we confirm this?

Page 138: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Amortized Analysis

● The analysis we have just done is called an amortized analysis.

● We reason about the total work done, not the word done per operation.

● In an amortized sense, our implementation of the stack is extremely fast!

● This is one of the most common approaches to implementing Stack.

Page 139: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Your Action Items

● Download BlueBook● Hopefully you’ve already done this; if not,

please do that soon.● Finish Assignment 4

● Need help? Stop by the LaIR!

Page 140: Implementing Abstractions · A Bounded Stack 4 0 allocated size logical size element array The stack’s allocated size is the number of slots in the array. Remember – arrays in

Next Time

● Linked Lists● A different way to represent sequences of

elements.● Dynamic Allocation Revisited

● What else can we allocate?