abstract data types queue + dequeue amortized analysis

50
1 Abstract Data Types Queue + Dequeue Amortized analysis

Upload: lilli

Post on 21-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

Abstract Data Types Queue + Dequeue Amortized analysis. 5 4 17 21. Q. 5 4 17 21. Can one Implement A Queue with stacks?. You are given the STACK ABSTRACT data structure (1, 2 .. as many as you want) Can you use it to implement a queue. S 2. S 1. Implementation of Queue with stacks. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Abstract Data Types Queue + Dequeue Amortized analysis

1

Abstract Data TypesQueue + DequeueAmortized analysis

Page 2: Abstract Data Types Queue + Dequeue Amortized analysis

26

Can one Implement A Queue with stacks?

S2

•You are given the STACK ABSTRACT data structure (1, 2 .. as many as you want)

•Can you use it to implement a queue

S1

5 4 17 21Q5 4 17 21

Page 3: Abstract Data Types Queue + Dequeue Amortized analysis

27

Implementation of Queue with stacks

size=5

13S1S2

inject(x,Q): push(x,S2); size ← size + 1

5 4 17 21

inject(2,Q)

Page 4: Abstract Data Types Queue + Dequeue Amortized analysis

28

Implementation with stacks

size=5

13S1S2

inject(x,Q): push(x,S2); size ← size + 1

5 4 17 21 2

inject(2,Q)

Page 5: Abstract Data Types Queue + Dequeue Amortized analysis

29

Implementation of a Queue with stacks

size=6

13S1S25 4 17 21 2

inject(2,Q)

inject(x,Q): push(x,S2); size ← size + 1

Page 6: Abstract Data Types Queue + Dequeue Amortized analysis

30

Pop

size=6

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4 17 21 2

pop(Q)

13

Page 7: Abstract Data Types Queue + Dequeue Amortized analysis

31

Pop

size=6

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4 17 21 2

pop(Q)

Page 8: Abstract Data Types Queue + Dequeue Amortized analysis

32

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4 17 21 2

pop(Q)

pop(Q)

Page 9: Abstract Data Types Queue + Dequeue Amortized analysis

33

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4 17 21

pop(Q)

pop(Q)

2

Page 10: Abstract Data Types Queue + Dequeue Amortized analysis

34

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4 17

pop(Q)

pop(Q)

221

Page 11: Abstract Data Types Queue + Dequeue Amortized analysis

35

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5 4

pop(Q)

pop(Q)

22117

Page 12: Abstract Data Types Queue + Dequeue Amortized analysis

36

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

5

pop(Q)

pop(Q)

221174

Page 13: Abstract Data Types Queue + Dequeue Amortized analysis

37

Pop

size=5

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

pop(Q)

pop(Q)

2211745

Page 14: Abstract Data Types Queue + Dequeue Amortized analysis

38

Pop

size=4

S1S2

pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1

pop(Q)

pop(Q)

221174

Page 15: Abstract Data Types Queue + Dequeue Amortized analysis

39

move(S2, S1)

while not empty?(S2) do x ← pop(S2) push(x,S1)

Page 16: Abstract Data Types Queue + Dequeue Amortized analysis

40

Analysis

• O(n) worst case time per operation

Page 17: Abstract Data Types Queue + Dequeue Amortized analysis

41

Amortized Analysis

• How long it takes to perform m operations on the worst case ?

• O(nm)

• Is this tight ?

Page 18: Abstract Data Types Queue + Dequeue Amortized analysis

42

Key Observation• An expensive operation cannot

occur too often !

Page 19: Abstract Data Types Queue + Dequeue Amortized analysis

43

Amortized complexity

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

Proof:• No element moves from S2 to S1• Entrance at S1, exit at S2.

• Every element:1. Enters S1 exactly once2. Moves from S1 to S2 at most once3. Exits S2 at most once

#ops per element ≤ 3 • m operations #elements ≤ m work ≤ 3 m

S2S15 42217

Page 20: Abstract Data Types Queue + Dequeue Amortized analysis

47

Potential based Proof (on your own)

Consider2( ) | |D S

Recall that: Amortized(op) = actual(op) + ΔΦ

This is O(1) if a move does not occur

Say we move S2:Then the actual time is |S2| + O(1)

ΔΦ = -|S2|

So the amortized time is O(1)

Think of Φ as accumulation of easy operations covering for future potential “damage”

Page 21: Abstract Data Types Queue + Dequeue Amortized analysis

49

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 22: Abstract Data Types Queue + Dequeue Amortized analysis

50

Implementation with doubly linked lists

5

head

size=2

tail

13

x.next

x.element

x.prev

x

Page 23: Abstract Data Types Queue + Dequeue Amortized analysis

51

Empty list

head

size=0

tail

We use two sentinels here to make the code simpler

Page 24: Abstract Data Types Queue + Dequeue Amortized analysis

52

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 25: Abstract Data Types Queue + Dequeue Amortized analysis

53

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 26: Abstract Data Types Queue + Dequeue Amortized analysis

54

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 27: Abstract Data Types Queue + Dequeue Amortized analysis

55

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 28: Abstract Data Types Queue + Dequeue Amortized analysis

56

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 29: Abstract Data Types Queue + Dequeue Amortized analysis

59

Implementation with stacks

size=5

13S1S2

push(x,D): push(x,S1)

5 4 17 21

push(2,D)

Page 30: Abstract Data Types Queue + Dequeue Amortized analysis

60

Implementation with stacks

size=6

2 13

S1S2

push(x,D): push(x,S1)

5 4 17 21

push(2,D)

Page 31: Abstract Data Types Queue + Dequeue Amortized analysis

61

Pop

size=6

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

2 13

5 4 17 21

pop(D)

Page 32: Abstract Data Types Queue + Dequeue Amortized analysis

62

Pop

size=5

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

13 5 4 17 21

pop(D)

pop(D)

Page 33: Abstract Data Types Queue + Dequeue Amortized analysis

63

Pop

size=4

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

5 4 17 21

pop(D)

pop(D)

Page 34: Abstract Data Types Queue + Dequeue Amortized analysis

64

Pop

5 4 17 21

size=4

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

pop(D)

Page 35: Abstract Data Types Queue + Dequeue Amortized analysis

65

Pop

5 4 17 21

size=4

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

pop(D)

Page 36: Abstract Data Types Queue + Dequeue Amortized analysis

66

Pop

17 21

size=4

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

pop(D)

5 4

Page 37: Abstract Data Types Queue + Dequeue Amortized analysis

67

Pop

17 21

size=4

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

pop(D)

4

Page 38: Abstract Data Types Queue + Dequeue Amortized analysis

68

Pop

17 21

size=3

S1S2

pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1)

pop(D)

4

Page 39: Abstract Data Types Queue + Dequeue Amortized analysis

69

Split

5 4 17 21S1S2

S3

Page 40: Abstract Data Types Queue + Dequeue Amortized analysis

70

Split

5 4 17S1S2

S3

21

Page 41: Abstract Data Types Queue + Dequeue Amortized analysis

71

Split

5 4S1S2

S3

2117

Page 42: Abstract Data Types Queue + Dequeue Amortized analysis

72

Split

5 S1S2

S3

2117

4

Page 43: Abstract Data Types Queue + Dequeue Amortized analysis

73

Split

S1S2

S3

2117

5

4

Page 44: Abstract Data Types Queue + Dequeue Amortized analysis

74

Split

S1S2

S3

21

17

5

4

Page 45: Abstract Data Types Queue + Dequeue Amortized analysis

75

Split

S1S2

S3

2117

5

4

Page 46: Abstract Data Types Queue + Dequeue Amortized analysis

76

Split (same thing in reverse)

5 4S1S2

S3

Page 47: Abstract Data Types Queue + Dequeue Amortized analysis

77

split(S2, S1)

S3 ← make-stack() d ← size(S2) while (i ≤ ⌊d/2⌋) do x ← pop(S2) push(x,S3) i ← i+1 while (i ≤ ⌈d/2⌉) do x ← pop(S2) push(x,S1) i ← i+1 while (i ≤ ⌊d/2⌋) do x ← pop(S3) push(x,S2) i ← i+1

Page 48: Abstract Data Types Queue + Dequeue Amortized analysis

78

Analysis

• O(n) worst case time per operation

Page 49: Abstract Data Types Queue + Dequeue Amortized analysis

79

Thm: If we start with an empty deque and perform m operations then it takes O(m) time

Page 50: Abstract Data Types Queue + Dequeue Amortized analysis

82

A better boundConsider

1 2( ) || | | ||D S S

This is O(1) if no splitting occursSay we split S1:Then the actual time is |S1| + O(1)

ΔΦ = -|S1| (S2 empty)

So the amortized time is O(1)

Recall that: Amortized(op) = actual(op) + ΔΦ

Think of Φ as accumulation of easy operations covering for future potential “damage”

Cs
upto here