Last time Today Next (Midterm) Hash tables Unbounded arrays - - PowerPoint PPT Presentation

last time today next
SMART_READER_LITE
LIVE PREVIEW

Last time Today Next (Midterm) Hash tables Unbounded arrays - - PowerPoint PPT Presentation

Last time Today Next (Midterm) Hash tables Unbounded arrays Implementation Amortized analysis Stacks Queues Linked Lists Toward unbounded arrays Unsorted Arrays Linked Lists O(1) access Self-resizing PROS Built-in support


slide-1
SLIDE 1

Last time

(Midterm) Implementation § Stacks § Queues Linked Lists Unbounded arrays Amortized analysis

Today Next

Hash tables

slide-2
SLIDE 2

Unsorted Arrays Linked Lists PROS CONS O(1) access Built-in support Self-resizing O(1) insertion (given right pointers) Fixed size O(n) insertion O(n) access No built-in support

Toward unbounded arrays

A data structure that combines the best properties of arrays and linked lists

slide-3
SLIDE 3

Amortized analysis

§ Average frequent cheap operations with infrequent expensive operations

Example: 100 operations at cost 1, followed by 1 operation at cost 100 The amortized cost per operation is: 200/101

slide-4
SLIDE 4

Binary Counter

slide-5
SLIDE 5

Binary Counter

slide-6
SLIDE 6

Binary Counter

slide-7
SLIDE 7

Binary Counter

slide-8
SLIDE 8

Binary Counter

slide-9
SLIDE 9

n-bit counter

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 Per press Cost $ 1 $ 2 $ 1 $ 3 $ 1 $ 2 $ 1 $ 4 Press # 1 2 3 4 5 6 7 8

slide-10
SLIDE 10

n-bit counter

1 0 1 0 1 1 _ _ _ _ _ _ Per press Cost $ ?

slide-11
SLIDE 11

n-bit counter

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 Per press Cost 1 2 1 3 1 2 1 4 Press # 1 2 3 4 5 6 7 8 Total Cost 1 3 4 7 8 10 11 15 $1 / press Total Revenue 1 2 3 4 5 6 7 8 Bank Account

  • 1
  • 1
  • 3
  • 3
  • 4
  • 4
  • 7
slide-12
SLIDE 12

n-bit counter

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 Per press Cost 1 2 1 3 1 2 1 4 Press # 1 2 3 4 5 6 7 8 Total Cost 1 3 4 7 8 10 11 15 $2 / press Total Revenue 2 4 6 8 10 12 14 16 Bank Account 1 1 2 1 2 2 3 1

slide-13
SLIDE 13

n-bit counter

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 Per press Cost 1 2 1 3 1 2 1 4 Press # 1 2 3 4 5 6 7 8 Total Cost 1 3 4 7 8 10 11 15 $2 / press Total Revenue 2 4 6 8 10 12 14 16 Bank Account 1 1 2 1 2 2 3 1 Average cost $2, O(1) Will this keep working? Can we prove it?

slide-14
SLIDE 14

Invariant?

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 Per press Cost 1 2 1 3 1 2 1 4 Press # 1 2 3 4 5 6 7 8 Total Cost 1 3 4 7 8 10 11 15 $2 / press Total Revenue 2 4 6 8 10 12 14 16 Bank Account 1 1 2 1 2 2 3 1

slide-15
SLIDE 15

000000 000001 000010 000011 000100 000101 000110 000111 001000 charge 2 tokens Pay 1 1 in reserve charge 2 tokens Pay 2 1 in reserve charge 2 tokens Pay 1 2 in reserve charge 2 tokens Pay 3 1 in reserve charge 2 tokens Pay 1 2 in reserve charge 2 tokens Pay 2 2 in reserve charge 2 tokens Pay 1 3 in reserve charge 2 tokens Pay 4 1 in reserve

n-bit counter

slide-16
SLIDE 16

000000 000001 000010 000011 000100 000101 000110 000111 001000 charge 2 tokens Pay 1 1 in reserve charge 2 tokens Pay 2 1 in reserve charge 2 tokens Pay 1 2 in reserve charge 2 tokens Pay 3 1 in reserve charge 2 tokens Pay 1 2 in reserve charge 2 tokens Pay 2 2 in reserve charge 2 tokens Pay 1 3 in reserve charge 2 tokens Pay 4 1 in reserve

n-bit counter

slide-17
SLIDE 17

Invariant: Preservation

0 1 0 1 0 1 1 _ _ _ _ _ _ _

  • 1. Assume invariant

k tokens for k ones

  • 2. Prove preservation

k’ tokens for k’ ones

slide-18
SLIDE 18

Amortized Analysis

# of times the counter has been incremented # of total bit flips Budgeted by amortized analysis (2 flips per increment) Actual number (will never be bigger than budget)

slide-19
SLIDE 19

Unbounded arrays

slide-20
SLIDE 20

Unsorted Arrays Linked Lists PROS CONS O(1) access Built-in support Self-resizing O(1) insertion (given right pointers) Fixed size O(n) insertion O(n) access No built-in support

Recall: Toward unbounded arrays

A data structure that combines the best properties of arrays and linked lists

slide-21
SLIDE 21

Unbounded arrays interface

int uba_len(uba_t A) /*@requires A != NULL; @*/ /*@ensures \result >= 0; @*/ ; uba_t uba_new(int size) /*@requires 0 <= size; @*/ /*@ensures \result != NULL; @*/ /*@ensures uba_len(\result) == size; @*/ ; string uba_get(uba_t A, int i) /*@requires A != NULL; @*/ /*@requires 0 <= i && i < uba_len(A); @*/; void uba_set(uba_t A, int i, string x) /*@requires A != NULL; @*/ /*@requires 0 <= i && i < uba_len(A); @*/; // typedef ______* uba_t;

slide-22
SLIDE 22

string uba_rem(uba_t A) /*@requires A != NULL; @*/ /*@requires 0 < uba_len(A); @*/ ; void uba_add(uba_t A, string x) /*@requires A != NULL; @*/ ;

new functionality Increase and decrease the array’s size by 1 Goal: All operations in the interface are O(1)

slide-23
SLIDE 23

Unbounded Array

Action Start with [4, 7] § add(5) § rem() § add(9) User View Implementation View

slide-24
SLIDE 24

Data structure

typedef struct uba_header uba; struct uba_header { int size; // 0 <= size && size < limit int limit; // 0 < limit string[] data; // \length(data) == limit }; reported to user actual size

slide-25
SLIDE 25

Data structure invariant

bool is_uba(uba* A) { return A != NULL && is_array_expected_length(A->data, A->limit) && 0 <= A->size && A->size < A->limit; }

slide-26
SLIDE 26

Cost assumptions

Each array write costs 1 New array allocation does not cost anything

slide-27
SLIDE 27

Resizing the array

  • Make uba_add have constant amortized cost
  • Arrange so that resizing happens rarely
  • making new array of length limit+1 won’t work. Why?
slide-28
SLIDE 28

“a” “b” “c” “d” “e” “f” “g” “h”

size limit data 7 8 size limit data 8 16 size limit data 9 16

“a” “b” “c” “d” “e” “f” “g” “h” “I” “a” “b” “c” “d” “e” “f” “g” “h” “a” “b” “c” “d” “e” “f” “g”

size limit data 6 8

“a” “b” “c” “d” “e” “f”

size limit data 5 8

“a” “b” “c” “d” “e” “a” “b” “c” “d”

size limit data 4 8

“a” “b” “c” “d”

size limit data 3 4

“a” “b” “c” “a” “b”

size limit data 2 4

“a” “b”

size limit data 1 2

1 3 3 2 4 1 3 9 5 4 10 1 5 11 1 6 12 1 7 21 9 8 22 1

“a”

cost of this add

Resize 2x

slide-29
SLIDE 29

“a” “b” “c” “d” “e” “f” “g” “h”

size limit data 7 8 size limit data 8 16 size limit data 9 16

“a” “b” “c” “d” “e” “f” “g” “h” “I” “a” “b” “c” “d” “e” “f” “g” “h” “a” “b” “c” “d” “e” “f” “g”

size limit data 6 8

“a” “b” “c” “d” “e” “f”

size limit data 5 8

“a” “b” “c” “d” “e” “a” “b” “c” “d”

size limit data 4 8

“a” “b” “c” “d”

size limit data 3 4

“a” “b” “c” “a” “b”

size limit data 2 4

“a” “b”

size limit data 1 2

3 3 4 1 9 5 10 1 11 1 12 1 21 9 22 1

“a”

t

  • t

a l c

  • s

t c

  • s

t

  • f

t h i s a d d

slide-30
SLIDE 30

“a” “b” “c” “d” “e” “f” “g” “h”

size limit data 7 8 size limit data 8 16 size limit data 9 16

“a” “b” “c” “d” “e” “f” “g” “h” “I” “a” “b” “c” “d” “e” “f” “g” “h” “a” “b” “c” “d” “e” “f” “g”

size limit data 6 8

“a” “b” “c” “d” “e” “f”

size limit data 5 8

“a” “b” “c” “d” “e” “a” “b” “c” “d”

size limit data 4 8

“a” “b” “c” “d”

size limit data 3 4

“a” “b” “c” “a” “b”

size limit data 2 4

“a” “b”

size limit data 1 2

1 3 3 2 4 1 3 9 5 4 10 1 5 11 1 6 12 1 7 21 9 8 22 1

“a”

#

  • f

a d d s t

  • t

a l c

  • s

t c

  • s

t

  • f

t h i s a d d

slide-31
SLIDE 31

“a” “b” “c” “d” “e” “f” “g” “h”

size limit data 7 8 size limit data 8 16 size limit data 9 16

“a” “b” “c” “d” “e” “f” “g” “h” “I” “a” “b” “c” “d” “e” “f” “g”

size limit data 6 8

“a” “b” “c” “d” “e” “f”

size limit data 5 8

“a” “b” “c” “d” “e” “a” “b” “c” “d”

size limit data 4 8 size limit data 3 4

“a” “b” “c” “a” “b”

size limit data 2 4 size limit data 1 2

3 3 3 6 4 1 9 9 5 12 10 1 15 11 1 18 12 1 21 21 9 24 22 1

“a”

t

  • t

a l t

  • k

e n s t

  • t

a l c

  • s

t c

  • s

t

  • f

t h i s a d d b a n k a c c

  • u

n t 2 2 4 6 2

slide-32
SLIDE 32

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i"

Unit of cost: array add 3 tokens

slide-33
SLIDE 33

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i"

Unit of cost: array add 3 tokens

slide-34
SLIDE 34

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i"

Unit of cost: array add 3 tokens

slide-35
SLIDE 35

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i"

Unit of cost: array add 3 tokens

slide-36
SLIDE 36

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i"

Unit of cost: array add 3 tokens

slide-37
SLIDE 37

“r” “o” “b” size limit data 3 6 “r” “o” “b” “e” size limit data 4 6 “r” “o” “b” “e” “r” size limit data 5 6 “r” “o” “b” “e” “r” “t” size limit data 6 12 size limit data 7 12 “r” “o” “b” “e” “r” “t” “r” “o” “b” “e” “r” “t” “s” size limit data 8 12 “r” “o” “b” “e” “r” “t” “s” “i’’

Unit of cost: array add 3 tokens

slide-38
SLIDE 38

Amortized analysis guarantees the average cost of each operation in the worst case

slide-39
SLIDE 39
  • 1. If you access Q->front, what Boolean

expression must be true to ensure safety?

  • 2. What Boolean expression indicates

whether this queue is empty?

  • 3. Write the statements in the correct
  • rder that will make node y the front
  • f this non-empty queue.
  • 4. TRUE or FALSE: In C0, setting y->next = y is allowed.
  • 5. If y is of type list*, the type of *y is ___________
  • 6. Q->back->data contains ______________________

y Q

front back

Go to

  • r

cs.cmu.edu/~1 5122/quiz

data next data next data next data next data next