Last time
(Midterm) Implementation § Stacks § Queues Linked Lists Unbounded arrays Amortized analysis
Today Next
Hash tables
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
(Midterm) Implementation § Stacks § Queues Linked Lists Unbounded arrays Amortized analysis
Hash tables
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
A data structure that combines the best properties of arrays and linked lists
Example: 100 operations at cost 1, followed by 1 operation at cost 100 The amortized cost per operation is: 200/101
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
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
# 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)
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
A data structure that combines the best properties of arrays and linked lists
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;
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)
Action Start with [4, 7] § add(5) § rem() § add(9) User View Implementation View
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
bool is_uba(uba* A) { return A != NULL && is_array_expected_length(A->data, A->limit) && 0 <= A->size && A->size < A->limit; }
Each array write costs 1 New array allocation does not cost anything
“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
“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
a l c
t c
t
t h i s a d d
“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”
#
a d d s t
a l c
t c
t
t h i s a d d
“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
a l t
e n s t
a l c
t c
t
t h i s a d d b a n k a c c
n t 2 2 4 6 2
“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"
“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"
“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"
“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"
“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"
“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’’
Amortized analysis guarantees the average cost of each operation in the worst case
expression must be true to ensure safety?
whether this queue is empty?
y Q
front back
Go to
cs.cmu.edu/~1 5122/quiz
data next data next data next data next data next