Abstract Data Types Stack, Queue Amortized analysis - PowerPoint PPT Presentation

About This Presentation
Title:

Abstract Data Types Stack, Queue Amortized analysis

Description:

Abstract Data Types Stack, Queue Amortized analysis – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 53
Provided by: peopleCsa2
Category:

less

Transcript and Presenter's Notes

Title: Abstract Data Types Stack, Queue Amortized analysis


1
Abstract Data TypesStack, QueueAmortized
analysis
2
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()

3
The Queue Data Abstraction
4
The Queue Data Abstraction
inject
inject
First in, First out (FIFO).
pop
5
Using an array
t
12
1
4
2
5
A
A1
A2
AN-1
A0
pop(Q)
6
Using an array
t
1
4
2
5
A
A1
A2
AN-1
A0
pop(Q)
7
Using an array
t
1
4
2
5
A
A1
A2
AN-1
A0
This would be inefficient if we insist that
elements span a prefix of the array
8
Using an array
r
f
12
1
4
2
5
A
A1
A2
AN-1
A0
r
f
A
A1
A2
A0
Empty queue fr
9
Using an array
r
f
12
1
4
2
5
A
A1
A2
AN-1
A0
pop(Q)
10
Using an array
r
f
1
4
2
5
A
A1
A2
AN-1
A0
pop(Q)
inject(5,Q)
11
Using an array
r
f
1
4
2
5
5
A
A1
A2
AN-1
A0
pop(Q)
inject(5,Q)
inject(5,Q)
12
Using an array
r
f
1
4
2
5
5
5
A
A1
A2
AN-1
A0
pop(Q)
inject(5,Q)
inject(5,Q)
pop(Q)
pop(Q)
13
Using an array
r
f
2
5
5
5
A
A1
A2
AN-1
A0
pop(Q)
inject(5,Q)
inject(5,Q)
pop(Q)
pop(Q)
pop(Q), inject(5,Q), pop(Q), inject(5,Q),.
14
Using an array
r
f




5
5
5
5

A
A1
A2
AN-1
A0
pop(Q)
inject(5,Q)
inject(5,Q)
pop(Q)
pop(Q)
pop(Q), inject(5,Q), pop(Q), inject(5,Q),.
15
Make the array circular
r
f
5
5



5
5
A
A1
A2
AN-1
A0
Pop(Q), inject(5,Q), pop(Q), inject(5,Q),.
16
Operations
r
f
1
4
2
5
A
A1
A2
AN-1
A0
empty?(Q) return (f r)
top(Q) if empty?(Q) then error
else return Af
17
Operations
r
f
1
4
2
5
A
A1
A2
AN-1
A0
size(Q) if (r gt f) then return (r-f)
else return N-(f-r)
18
Operations
r
f
5
5



5
5
A
A1
A2
AN-1
A0
size(Q) if (r gt f) then return (r-f)
else return N-(f-r)
19
Pop
r
f
1
4
2
5
A
A1
A2
A0
pop(Q) if empty?(Q) then error
else e ?Af f ? (f 1) mod
N return (e)
pop(Q)
20
Pop
r
f
1
4
2
5
A
A1
A2
A0
pop(Q) if empty?(Q) then error
else e ?Af f ? (f 1) mod
N return (e)
pop(Q)
21
Push
r
f

4
2
5
A
A1
A2
A0
inject(x,Q) if size(Q) N-1 then error
else Ar ? x
r ? (r1) mod N
inject(5,Q)
22
Push
r
f

4
2
5
5
A
A1
A2
A0
inject(x,Q) if size(Q) N-1 then error
else Ar ? x
r ? (r1) mod N
inject(5,Q)
23
Implementation with lists
head
size3
5
12
1
tail
inject(4,Q)
24
Implementation with lists
head
size3
4
5
12
1
tail
inject(4,Q)
25
Implementation with lists
head
size3
4
5
12
1
tail
inject(4,Q)
Complete the details by yourself
26
Implementation with stacks
S2
S1
13
5 4 17 21
size5
inject(x,Q) push(x,S2) size ? size 1

inject(2,Q)
27
Implementation with stacks
S2
S1
13
5 4 17 21 2
size5
inject(x,Q) push(x,S2) size ? size 1

inject(2,Q)
28
Implementation with stacks
S2
S1
13
5 4 17 21 2
size6
inject(x,Q) push(x,S2) size ? size 1

inject(2,Q)
29
Pop
S2
S1
5 4 17 21 2
13
size6
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
30
Pop
S2
S1
5 4 17 21 2
size6
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
31
Pop
S2
S1
5 4 17 21 2
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
32
Pop
S2
S1
5 4 17 21
2
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
33
Pop
S2
S1
5 4 17
2
21
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
34
Pop
S2
S1
5 4
2
21
17
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
35
Pop
S2
S1
5
2
21
17
4
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
36
Pop
S2
S1
2
21
17
4
5
size5
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
37
Pop
S2
S1
2
21
17
4
size4
pop(Q) if empty?(Q) error if
empty?(S1) then move(S2, S1) pop(
S1) size ? size -1
pop(Q)
pop(Q)
38
move(S2, S1) while not empty?(S2) do
x ? pop(S2) push(x,S1)

39
Analysis
  • O(n) worst case time per operation

40
Amortized Analysis
  • How long it takes to perform m operations on the
    worst case ?
  • O(nm)
  • Is tha tight ?

41
Key Observation
  • An expensive operation cannot occur too often !

42
  • THM If we start with an empty queue and perform
    m operations then it takes O(m) time

43
The potential formalism
  • The potential is in fact the bank

44
Define a potential function ?
Define
Amortized(op) actual(op) ??
45
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
46
Proof
  • Consider

Recall that Amortized(op) actual(op) ?F
This is O(1) if a move does not occur
Say we move S2
Then the actual time is S2 O(1) ?F -S2
So the amortized time is O(1)
47
Conclusion
  • THM If we start with an empty queue and perform
    m operations then it takes O(m) time

48
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()

49
Implementation with doubly linked lists
head
tail
size2
13
5
50
Empty list
head
tail
size0
We use two sentinels here to make the code simpler
51
Push
head
tail
size1
5
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
52
head
tail
size1
5
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
push(4,D)
53
head
tail
size1
5
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
push(4,D)
54
head
tail
size1
5
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
push(4,D)
55
head
tail
size2
5
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
push(4,D)
56
Implementations of the other operations are
similar
  • Try by yourself

57
Back to deques
  • Alternative implementation using stacks

58
Implementation with stacks
S2
S1
13
5 4 17 21
size5
push(x,D) push(x,S1)
push(2,D)
59
Implementation with stacks
S2
S1
2 13
5 4 17 21
size6
push(x,D) push(x,S1)
push(2,D)
60
Pop
S2
S1
2 13
5 4 17 21
size6
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
61
Pop
S2
S1
13
5 4 17 21
size5
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
pop(D)
62
Pop
S2
S1
5 4 17 21
size4
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
pop(D)
63
Pop
S2
S1
5 4 17 21
size4
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
64
Pop
S2
S1
5 4 17 21
size4
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
65
Pop
S2
5 4
S1
17 21
size4
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
66
Pop
S2
S1
17 21
4
size4
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
67
Pop
S2
S1
17 21
4
size3
pop(D) if empty?(D) error if
empty?(S1) then split(S2, S1) pop(
S1)
pop(D)
68
Split
S2
S1
5 4 17 21
S3
69
Split
S2
S1
5 4 17
S3
21
70
Split
S2
S1
5 4
S3
17
21
71
Split
S2
S1
5
4
S3
17
21
72
Split
S2
S1

5
4
S3
17
21
73
Split
S2
S1
17

5
4
S3
21
74
Split
S2
S1
21
17

5
4
S3
75
split(S2, S1) S3 ? make-stack() d ?
size(S2) while (i ?d/2?) do x ?
pop(S2) push(x,S3) i ? i1 while (i
?d/2?) do x ? pop(S2) push(x,S1) i ?
i1 while (i ?d/2?) do x ?
pop(S3) push(x,S2) i ? i1

76
Analysis
  • O(n) worst case time per operation

77
  • Thm If we start with an empty deque and perform
    m operations then its takes O(m) time

78
Amortized Analysis
  • How long it takes to perform m operations on the
    worst case ?
  • O(nm)
  • Really ?

79
Key Observation
  • An expensive operation cannot occur too often !

80
A better bound
  • Consider

Recall that Amortized(op) actual(op) ?F
This is O(1) if no splitting occurs
Say we split S1
Then the actual time is S1 O(1) ?F -S1
So the amortized time is O(1)
Write a Comment
User Comments (0)
About PowerShow.com