abstract data types stack, queue amortized analysis
DESCRIPTION
Abstract Data Types Stack, Queue Amortized analysis. ADT is an interface. It defines the type of the data stored operations, what each operation does (not how) parameters of each operation. ADT. Application. חוזה בין מתכנת האפליקציה ומיישם מבנה הנתונים. ממשק. Implementation of the - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/1.jpg)
1
Abstract Data TypesStack, Queue
Amortized analysis
![Page 2: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/2.jpg)
2
ADT is an interface
• It defines– the type of the data stored– operations, what each operation does
(not how)– parameters of each operation
![Page 3: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/3.jpg)
3
ADT
Application
Implementation of theData structure
חוזה בין מתכנת האפליקציה
ומיישם מבנה הנתונים
ממשק
![Page 4: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/4.jpg)
4
Example: Stacks
• Push(x,S) : Insert element x into S• Pop(S) : Delete the last element inserted into
S• Empty?(S): Return yes if S is empty• Top(S): Return the last element inserted into
S• Size(S)• Make-stack()
![Page 5: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/5.jpg)
5
The Stack Data Abstraction
push
push
push
![Page 6: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/6.jpg)
6
The Stack Data Abstraction
push
push
push
pop
push
Last in,First out.
![Page 7: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/7.jpg)
7
• Evaluate an expression in postfix or Reverse Polish Notation
Infix Postfix
(2+ 3) * 5 2 3 + 5 *
( (5 * (7 / 3) ) – (2 * 7) ) 5 7 3 / * 2 7 *-
A stack application
![Page 8: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/8.jpg)
8
2 3 + 5 *
A stack application
23
![Page 9: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/9.jpg)
9
2 3 + 5 *
A stack application
55
![Page 10: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/10.jpg)
10
2 3 + 5 *
A stack application
25
![Page 11: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/11.jpg)
11
S ← make-stack()while ( not eof ) do B ← read the next data;
if B is an operand then push(B,S) else X ← pop(S)
Y ← pop(S) Z ← Apply the operation B on X and Y push(Z,S)return(top(S))
Pseudo-code
![Page 12: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/12.jpg)
12
Implementation
• We will be interested in algorithms to implement the ADT..
• And their efficiency..
![Page 13: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/13.jpg)
13
Using an array
12 1 3A
A[0] A[1]
t
The stack is represented by the array A and variable t
A[2] A[N-1]
1213
![Page 14: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/14.jpg)
14
Using an array
12 1 3A
A[0] A[1]
t
make-stack(): Allocates the array A, which is of some fixed size N, sets t ← -1
The stack is represented by the array A and variable t
A[2] A[N-1]
![Page 15: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/15.jpg)
15
Operations
12 1 3A
t
size(S): return (t+1)
empty?(S): return (t < 0)
top(S): if empty?(S) then error else return A[t]
A[N-1]A[0] A[1]
A[2]
![Page 16: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/16.jpg)
16
Pop
12 1 3A
t
pop(S): if empty?(S) then error else e ←A[t] t ← t – 1 return (e)
A[N-1]A[0] A[1]
A[2]
pop(S)
![Page 17: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/17.jpg)
17
Pop
12 1 3A
t
A[N-1]A[0] A[1]
A[2]
pop(S): if empty?(S) then error else e ←A[t] t ← t – 1 return (e)
pop(S)
![Page 18: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/18.jpg)
18
Push
12 1 3A
t
push(x,S): if size(S) = N then error else t ←t+1 A[t] ← x
A[N-1]A[0] A[1]
A[2]
push(5,S)
![Page 19: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/19.jpg)
19
Push
12 1 5A
t
push(x,S): if size(S) = N then error else t ←t+1 A[t] ← x
A[N-1]A[0] A[1]
A[2]
push(5,S)
![Page 20: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/20.jpg)
20
Implementation with lists
12 1 5
top
size=3
x.element
x.nextx
![Page 21: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/21.jpg)
21
Implementation with lists
12 1 5
top
make-stack(): top ← null size ← 0
size=3
![Page 22: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/22.jpg)
22
Operations
12 1 5
top
size(S): return (size)
empty?(S): return (top = null)
top(S): if empty?(S) then error else return top.element
size=3
![Page 23: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/23.jpg)
23
Pop
12 1 5
top
pop(S): if empty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e)
pop(S)
size=3
![Page 24: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/24.jpg)
24
Pop
12 1 5
top
pop(S): if empty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e)
pop(S)
size=2
![Page 25: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/25.jpg)
25
Garbage collection
1 5
top
pop(S): if empty?(S) then error else e ←top.element top ← top.next size ← size-1 return (e)
pop(S)
size=2
![Page 26: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/26.jpg)
26
Push
1 5
topsize=2
push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)
![Page 27: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/27.jpg)
27
Push
1 5
topsize=2
push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)
5
![Page 28: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/28.jpg)
28
Push
1 5
topsize=2
push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)
5
![Page 29: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/29.jpg)
29
Push
1 5
topsize=3
push(x,S): n = new node n.element ←x n.next ← top top ← n size ← size + 1 push(5,S)
5
![Page 30: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/30.jpg)
30
Analysis
• Bound the running time of an operation on the worst-case
• As a function of the “size”, n, of the data structure
• T(n) < 4n+7• Too detailed, we are just interested
in the order of growth
![Page 31: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/31.jpg)
31
Big-O
) ו- כך ש:c - קיים ) ( ( ))T n O f n0n
0)()( nnnfcnT
דוגמא:2
( ) ( 1)T n n
)(4)1(222nOnn
![Page 32: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/32.jpg)
32
Big-O
))(()( ngOnf
cg(n)
f(n)
n0
![Page 33: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/33.jpg)
33
• 4n O(n2)
• 4n2 O(n2)
• 2n O(n10)
• 10 O(1)
• 100n3+10n O(n3)
• log2(n) O(log10(n))
More examples
![Page 34: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/34.jpg)
34
The running time of our stack and queue operations
• Each operation takes O(1) time
![Page 35: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/35.jpg)
35
Stacks via extendable arrays
• We do not want our implementation using arrays to be limited to only N elements
• When the array is full we will double its size
![Page 36: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/36.jpg)
36
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x
12 1A
t
A[N-1]A[0] A[1]
![Page 37: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/37.jpg)
37
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x
12 1 3 3A 4 5 7 3 2 8 1
A[N-1]A[0] A[1]
t
push(5,S)
![Page 38: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/38.jpg)
38
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x
push(5,S)
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
![Page 39: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/39.jpg)
39
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x push(5,S)
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
12 1 3 3 4 5 7 3 2 8 1
t
![Page 40: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/40.jpg)
40
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x
t
A[2N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
push(5,S)
![Page 41: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/41.jpg)
41
Push
push(x,S): if size(S) = N then allocate a new array of size 2N copy the old array to the new one t ←t+1 A[t] ← x
5
A[2N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
push(5,S)
![Page 42: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/42.jpg)
42
Analysis
• An operation may take O(n) worst case time !
• But that cannot happen often..
![Page 43: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/43.jpg)
43
Amortized Analysis
• How long it takes to do m operations ?
• Well, O(nm)
• Yes, but can it really take that long ?
![Page 44: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/44.jpg)
44
![Page 45: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/45.jpg)
45
x
![Page 46: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/46.jpg)
46
x x
![Page 47: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/47.jpg)
47
x x
x x x
![Page 48: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/48.jpg)
48
x x
x x x x
![Page 49: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/49.jpg)
49
x x
x x x x
x x x x x
![Page 50: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/50.jpg)
50
x x
x x x x
x x x x x x
![Page 51: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/51.jpg)
51
x x
x x x x
x x x x x x x
![Page 52: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/52.jpg)
52
x x
x x x x
x x x x x x x x
![Page 53: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/53.jpg)
53
x x
x x x x
x x x x x x x x
x x x x x x x x x
![Page 54: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/54.jpg)
54
x x
x x x x
x x x x x x x x
x x x x x x x x x x
![Page 55: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/55.jpg)
55
x x
x x x x
x x x x x x x x
x x x x x x x x x x x
![Page 56: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/56.jpg)
56
x x
x x x x
x x x x x x x x
x x x x x x x x x x x
Only after z-1 pushes that cost 1 we will have a push that costs 2z+1
Let z be the size of the array we just copied
![Page 57: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/57.jpg)
57
x x
x x x x
x x x x x x x x
x x x x x x x x x x x
3+(2∙4+1)=3∙4
7+(2∙8+1)= 3∙8
Total cost O(m) !!
Start from the second row: z=2
1+(2∙2+1)=3∙2
z=4 z=8
![Page 58: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/58.jpg)
58
Theorem: A sequence of m operations on a stack takes O(m) time
proof.
![Page 59: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/59.jpg)
59
Amortized Analysis (The bank’s view)
• We will have a bank
• An operation can either pay a token for a unit of work by itself, or take a token from the bank to pay for it
• An operation can put tokens in the bank
• If the bank never has a negative balance then the total # of tokens spent by the operations bounds the total work
![Page 60: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/60.jpg)
60
For a proof:
• Define exactly how each operation pays for its work, and how many tokens it puts in the bank
• Prove that the balance in the bank is always non-negative
• Count the total # of tokens – that’s your bound
![Page 61: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/61.jpg)
61
“Easy” push
• when we push an item, we also put ≤ two tokens, one on the item which we insert, and one on another item if there is some item without a token
A[N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
![Page 62: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/62.jpg)
62
5
A[N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
“Easy” push
• when we push an item, we also put ≤ two tokens, one on the item which we insert, and one on another item if there is some item without a token
![Page 63: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/63.jpg)
63
5 6
A[N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
![Page 64: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/64.jpg)
64
5 6 6 7 1 10 4 67 2 5 7
A[N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
![Page 65: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/65.jpg)
65
“hard” push
• Tokens from the bank “pay” for copying the array into the larger array
5 6 6 7 1 10 4 67 2 5 7
A[N-1]
12 1 3 3A 4 5 7 3 2 8 1
A[0] A[1]
t
![Page 66: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/66.jpg)
66
• Then you pay a token and put two in the bank as an “easy” push
5 6 6 7 1 10 4 67 2 5 712 1 3 3 4 5 7 3 2 8 1 A
t
5 6 6 7 1 10 4 67 2 5 712 1 3 3 4 5 7 3 2 8 1 4
t
“hard” push
![Page 67: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/67.jpg)
67
Need to prove
• The balance is never negative:
• When we get to an expensive push there are enough tokens to pay for copying
• By induction: prove that after the i-th push following a copying there are 2i tokens in the bank
![Page 68: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/68.jpg)
68
How many tokens we spent ?
• Each operation spent 3 tokens
![Page 69: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/69.jpg)
69
Summary
• So the total # of tokens is 3mTHM: A sequence of m operations takes
O(m) time !! (we finished the proof)
• Each operation takes O(1) amortized time
![Page 70: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/70.jpg)
70
Theorem: A sequence of m operations on a stack takes O(m) time
proof (2) .
• We formalize the bank as a potential function
![Page 71: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/71.jpg)
71
Amortized(op) = actual(op) +
Define
Define: a potential function
![Page 72: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/72.jpg)
72
Amortized(op1) = actual(op1) + 1- 0
Amortized(op2) = actual(op2) + 2- 1
Amortized(opn) = actual(opn) + n- (n-1)
……
+
iAmortized(opi) = iactual(opi) + n- 0
iAmortized(opi) iactual(opi) if n- 0 0
![Page 73: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/73.jpg)
73
Example: Our extendable arrays
Define a potential of the stack
2( 1) 2( 1) 0( )
0
t N if t NS
Otherwise
![Page 74: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/74.jpg)
74
Amortized(push) = actual(push) + =
1 + 2(t+2) – N - (2(t+1) – N) = 3
Amortized(push) = N + 1 + =
N + 1 + (2 - N) = 3
Amortized(pop) = 1 + =
1 + 2(t-1) – N - (2t – N) = -1
With copying:
Without copying:
![Page 75: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/75.jpg)
75
Amortized(op1) = actual(op1) + 1- 0
Amortized(op2) = actual(op2) + 2- 1
Amortized(opn) = actual(opn) + n- (n-1)
……
+
iAmortized(opi) = iactual(opi) + n- 0
iAmortized(opi) iactual(opi) if n- 0 03m ≥
![Page 76: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/76.jpg)
76
Summary
• So the total # of tokens is 3m THM: A sequence of m operations
starting from an empty stack takes O(m) time !!
• Each operations take O(1) amortized time
![Page 77: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/77.jpg)
77
Queue
• Inject(x,Q) : Insert last element x into Q• Pop(Q) : Delete the first element in Q• Empty?(Q): Return yes if Q is empty• Front(Q): Return the first element in Q• Size(Q)• Make-queue()
![Page 78: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/78.jpg)
78
The Queue Data Abstraction
inject
inject
![Page 79: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/79.jpg)
79
The Queue Data Abstraction
inject
inject
inject
inject
popFirst in,First out (FIFO).
![Page 80: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/80.jpg)
80
Using an array
12 1 4 2A 5
A[0] A[1]
t
pop(Q)
A[2] A[N-1]
![Page 81: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/81.jpg)
81
Using an array
1 4 2 5A
A[0] A[1]
t
pop(Q)
A[2] A[N-1]
![Page 82: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/82.jpg)
82
Using an array
1 4 2 5A
A[0] A[1]
t
A[2] A[N-1]
This would be inefficient if we insist that elements span a prefix of the array
![Page 83: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/83.jpg)
83
Using an array
12 1 4 2A 5
A[0] A[1]
r
A[2] A[N-1]
f
A
A[0] A[1]
r
A[2]
f
Empty queue f=r
![Page 84: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/84.jpg)
84
Using an array
12 1 4 2A 5
A[0] A[1]
r
pop(Q)
A[2] A[N-1]
f
![Page 85: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/85.jpg)
85
Using an array
1 4 2A 5
A[0] A[1]
A[2] A[N-1]
f r
pop(Q)inject(5,Q)
![Page 86: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/86.jpg)
86
Using an array
1 4 2A 5 5
A[0] A[1]
A[2] A[N-1]
f r
pop(Q)inject(5,Q)inject(5,Q)
![Page 87: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/87.jpg)
87
Using an array
1 4 2A 5 5 5
A[0] A[1]
A[2] A[N-1]
f r
pop(Q)inject(5,Q)inject(5,Q)pop(Q)pop(Q)
![Page 88: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/88.jpg)
88
Using an array
2A 5 5 5
A[0] A[1]
A[2] A[N-1]
f r
pop(Q)inject(5,Q)inject(5,Q)pop(Q)pop(Q)pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….
![Page 89: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/89.jpg)
89
Using an array
A 5 5 5 5
A[0] A[1]
r
A[2] A[N-1]
f
pop(Q)inject(5,Q)inject(5,Q)pop(Q)pop(Q)pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….
![Page 90: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/90.jpg)
90
Make the array “circular”
5 5 A 5 5
A[0] A[1]
r
A[2] A[N-1]
f
Pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….
![Page 91: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/91.jpg)
91
Operations
empty?(Q): return (f = r)
top(Q): if empty?(Q) then error else return A[f]
1 4 2A 5
A[0] A[1]
A[2] A[N-1]
f r
![Page 92: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/92.jpg)
92
Operations
size(Q): if (r >= f) then return (r-f) else return N-(f-r)
1 4 2A 5
A[0] A[1]
A[2] A[N-1]
f r
![Page 93: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/93.jpg)
93
Operations
size(Q): if (r >= f) then return (r-f) else return N-(f-r)
5 5 A 5 5
A[0] A[1]
r
A[2] A[N-1]
f
![Page 94: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/94.jpg)
94
Pop
pop(Q)
1 4 2A 5
A[0] A[1]
A[2]
f r
pop(Q): if empty?(Q) then error else e ←A[f] f ← (f + 1) mod N return (e)
![Page 95: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/95.jpg)
95
Pop
pop(Q): if empty?(Q) then error else e ←A[f] f ← (f + 1) mod N return (e)
pop(Q)
1 4 2A 5
A[0] A[1]
A[2]
f r
![Page 96: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/96.jpg)
96
Inject
inject(x,Q): if size(Q) = N-1 then error else A[r] ← x r ← (r+1) mod N
inject(5,Q)
4 2A 5
A[0] A[1]
A[2]
f r
![Page 97: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/97.jpg)
97
Inject
inject(x,Q): if size(Q) = N-1 then error else A[r] ← x r ← (r+1) mod N
inject(5,Q)
4 2A 5 5
A[0] A[1]
A[2]
f r
![Page 98: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/98.jpg)
98
Implementation with lists
12 1 5
headsize=3
tail
inject(4,Q)
![Page 99: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/99.jpg)
99
Implementation with lists
12 1 5
headsize=3
tail
inject(4,Q)
4
![Page 100: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/100.jpg)
100
Implementation with lists
12 1 5
headsize=3
tail
inject(4,Q)
4
Complete the details by yourself
![Page 101: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/101.jpg)
101
Double ended queue (deque)
• Push(x,D) : Insert x as the first in D• Pop(D) : Delete the first element of D• Inject(x,D): Insert x as the last in D• Eject(D): Delete the last element of D• Size(D)• Empty?(D)• Make-deque()
![Page 102: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/102.jpg)
102
Implementation with doubly linked lists
5
head
size=2
tail
13
x.next
x.element
x.prev
x
![Page 103: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/103.jpg)
103
Empty list
head
size=0
tail
We use two sentinels here to make the code simpler
![Page 104: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/104.jpg)
104
Push
push(x,D): n = new node n.element ←x n.next ← head.next (head.next).prev ← n head.next ← n n.prev← head size ← size + 1
5
head
size=1
tail
![Page 105: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/105.jpg)
105
push(x,D): n = new node n.element ←x n.next ← head.next (head.next).prev ← n head.next ← n n.prev← head size ← size + 1
5
head
size=1
tail
push(4,D)
4
![Page 106: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/106.jpg)
106
push(x,D): n = new node n.element ←x n.next ← head.next (head.next).prev ← n head.next ← n n.prev← head size ← size + 1
5
head
size=1
tail
push(4,D)
4
![Page 107: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/107.jpg)
107
push(x,D): n = new node n.element ←x n.next ← head.next (head.next).prev ← n head.next ← n n.prev← head size ← size + 1
5
head
size=1
tail
push(4,D)
4
![Page 108: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/108.jpg)
108
push(x,D): n = new node n.element ←x n.next ← head.next (head.next).prev ← n head.next ← n n.prev← head size ← size + 1
5
head
size=2
tail
push(4,D)
4
![Page 109: Abstract Data Types Stack, Queue Amortized analysis](https://reader030.vdocuments.site/reader030/viewer/2022020111/568143a1550346895db02312/html5/thumbnails/109.jpg)
109
Implementations of the other operations are similar
• Try by yourself