1 Abstract Data Types Queue + Dequeue Amortized analysis.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
1 Persistent data structures. 2 Ephemeral: A modification destroys the version which we modify. Persistent: Modifications are nondestructive. Each modification.
Data Structures and Algorithms (60-254)
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
CSE332: Data Abstractions Lecture 21: Amortized Analysis Dan Grossman Spring 2010.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
1 Abstract Data Types Stack, Queue Amortized analysis Cormen: Ch 10, 17 (11, 18)
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
1 Persistent data structures. 2 Ephemeral: A modification destroys the version which we modify. Persistent: Modifications are nondestructive. Each modification.
The Potential Method. Deque with stacks size=7 13 S1S1 S2S2 push(x,D): push(x,S 1 ) push(2,D)
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
CSE332: Data Abstractions Lecture 26: Amortized Analysis Tyler Robison Summer
Stack and Queue.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks and Queues Introduction to Computing Science and Programming I.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Stacks and Queues. Announcements USACO Open competition results are out o Congrats to Johnny for scoring 2nd in the US USACO Finalists are also announced.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Parasol Lab, Dept. CSE, Texas A&M University
Algorithms and Data Structures Lecture VI
CSE373: Data Structures & Algorithms Lecture 8: Amortized Analysis Dan Grossman Fall 2013.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at.
Cpt S 122 – Data Structures Abstract Data Types
Elementary data structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Double-Ended Queues Chapter 5.
Week 4 - Friday CS221.
Linked List Stacks, Linked List Queues, Dequeues
Queues Queues Queues.
Stacks and Queues.
Abstract Data Types Stack, Queue Amortized analysis
Stacks, Queues, and Deques
Persistent deques.
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks and Queues.
Abstract Data Types Stack, Queue Amortized analysis
Abstract Data Types Stack, Queue Amortized analysis
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queues Model Operations Pointer Implementations Array Implementation
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Abstract Data Types Stack, Queue Amortized analysis
Data Structures & Programming
Presentation transcript:

1 Abstract Data Types Queue + Dequeue Amortized 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 inject

4 The Queue Data Abstraction inject pop First in, First out (FIFO).

5 Using an array A 5 A[0] A[1] t pop(Q) A[2] A[N-1]

6 Using an array 1425 A A[0] A[1] t pop(Q) A[2] A[N-1]

7 Using an array 1425 A 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

8 Using an array A 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

9 Using an array A 5 A[0] A[1] r pop(Q) A[2] A[N-1] f

10 Using an array 142 A 5 A[0] A[1] A[2] A[N-1] f r pop(Q) inject(5,Q)

11 Using an array 142 A 55 A[0] A[1] A[2] A[N-1] f r pop(Q) inject(5,Q)

12 Using an array 142 A 555 A[0] A[1] A[2] A[N-1] f r pop(Q) inject(5,Q) pop(Q)

13 Using an array 2 A 555 A[0] A[1] A[2] A[N-1] f r pop(Q) inject(5,Q) pop(Q) pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….

14 Using an array A 5555 A[0] A[1] r A[2] A[N-1] f pop(Q) inject(5,Q) pop(Q) pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….

15 Make the array “circular” 5 5 A 55 A[0] A[1] r A[2] A[N-1] f Pop(Q), inject(5,Q), pop(Q), inject(5,Q),……….

16 Operations empty?(Q): return (f = r) top(Q): if empty?(Q) then error else return A[f] 142 A 5 A[0] A[1] A[2] A[N-1] f r

17 Operations size(Q): if (r >= f) then return (r-f) else return N-(f-r) 142 A 5 A[0] A[1] A[2] A[N-1] f r

18 Operations size(Q): if (r >= f) then return (r-f) else return N-(f-r) 5 5 A 55 A[0] A[1] r A[2] A[N-1] f

19 Pop pop(Q) 142 A 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)

20 Pop pop(Q): if empty?(Q) then error else e ← A[f] f ← (f + 1) mod N return (e) pop(Q) 142 A 5 A[0] A[1] A[2] f r

21 Push inject(x,Q): if size(Q) = N-1 then error else A[r] ← x r ← (r+1) mod N inject(5,Q) 42 A 5 A[0] A[1] A[2] f r

22 Push inject(x,Q): if size(Q) = N-1 then error else A[r] ← x r ← (r+1) mod N inject(5,Q) 42 A 55 A[0] A[1] A[2] f r

23 Implementation with lists head size=3 tail inject(4,Q)

24 Implementation with lists head size=3 tail inject(4,Q) 4

25 Implementation with lists head size=3 tail inject(4,Q) 4 Complete the details by yourself

26 Can one Implement A Queue with stacks? S2S2 You are given the STACK ABSTRACT data structure (1, 2.. as many as you want) Can you use it to implement a queue S1S Q

27 Implementation of Queue with stacks size=5 13 S1S1 S2S2 inject(x,Q): push(x,S 2 ); size ← size inject(2,Q)

28 Implementation with stacks size=5 13 S1S1 S2S2 inject(x,Q): push(x,S 2 ); size ← size inject(2,Q)

29 Implementation of a Queue with stacks size=6 13 S1S1 S2S inject(2,Q) inject(x,Q): push(x,S 2 ); size ← size + 1

30 Pop size=6 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q) 13

31 Pop size=6 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q)

32 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q) pop(Q)

33 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q) pop(Q) 2

34 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q) pop(Q) 2 21

35 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size pop(Q) pop(Q)

36 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size -1 5 pop(Q) pop(Q)

37 Pop size=5 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size -1 pop(Q) pop(Q)

38 Pop size=4 S1S1 S2S2 pop(Q): if empty?(Q) error if empty?(S 1 ) then move(S 2, S 1 ) pop( S 1 ); size ← size -1 pop(Q) pop(Q)

39 move(S 2, S 1 ) while not empty?(S 2 ) do x ← pop(S 2 ) push(x,S 1 )

40 Analysis O(n) worst case time per operation

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

42 Key Observation An expensive operation cannot occur too often !

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 once 2.Moves from S1 to S2 at most once 3.Exits S2 at most once  #ops per element ≤ 3 m operations  #elements ≤ m  work ≤ 3 m S2S2 S1S

44 The potential formalism The potential is in fact the bank

45 Amortized(op) = actual(op) +  Define Define: a potential function 

46 Amortized(op 1 ) = actual(op 1 ) +  1 -  0 Amortized(op 2 ) = actual(op 2 ) +  2 -  1 Amortized(op n ) = actual(op n ) +  n -  (n-1) ………… +  i Amortized(op i ) =  i actual(op i ) +  n -  0  i Amortized(op i )   i actual(op i ) if  n -  0  0

47 Potential based Proof (on your own) Consider Recall that: Amortized(op) = actual(op) + ΔΦ This is O(1) if a move does not occur Say we move S 2 : Then the actual time is |S 2 | + O(1) ΔΦ = -|S 2 | So the amortized time is O(1) Think of Φ as accumulation of easy operations covering for future potential “damage”

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

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

50 Implementation with doubly linked lists 5 head size=2 tail 13 x.next x.element x.prev x

51 Empty list head size=0 tail We use two sentinels here to make the code simpler

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 head size=1 tail

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 head size=1 tail push(4,D) 4

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 head size=1 tail push(4,D) 4

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 head size=1 tail push(4,D) 4

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 head size=2 tail push(4,D) 4

57 Implementations of the other operations are similar Try by yourself

58 Back to deques Alternative implementation using stacks

59 Implementation with stacks size=5 13 S1S1 S2S2 push(x,D): push(x,S 1 ) push(2,D)

60 Implementation with stacks size= S1S1 S2S2 push(x,D): push(x,S 1 ) push(2,D)

61 Pop size=6 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D)

62 Pop size=5 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D) pop(D)

63 Pop size=4 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D) pop(D)

64 Pop size=4 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D)

65 Pop size=4 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D)

66 Pop size=4 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D) 5 4

67 Pop size=4 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D) 4

68 Pop size=3 S1S1 S2S2 pop(D): if empty?(D) error if empty?(S 1 ) then split(S 2, S 1 ) pop( S 1 ) pop(D) 4

69 Split S1S1 S2S2 S3S3

70 Split S1S1 S2S2 S3S3 21

71 Split 5 4 S1S1 S2S2 S3S

72 Split 5 S1S1 S2S2 S3S

73 Split S1S1 S2S2 S3S

74 Split S1S1 S2S2 S3S

75 Split S1S1 S2S2 S3S

76 Split (same thing in reverse) 5 4S1S1 S2S2 S3S3

77 split(S 2, S 1 ) S 3 ← make-stack() d ← size(S 2 ) while (i ≤ ⌊ d/2 ⌋ ) do x ← pop(S 2 ) push(x,S 3 ) i ← i+1 while (i ≤ ⌈ d/2 ⌉ ) do x ← pop(S 2 ) push(x,S 1 ) i ← i+1 while (i ≤ ⌊ d/2 ⌋ ) do x ← pop(S 3 ) push(x,S 2 ) i ← i+1

78 Analysis O(n) worst case time per operation

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

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

81 Key Observation An expensive operation cannot occur too often !

82 A better bound Consider This is O(1) if no splitting occurs Say we split S 1 : Then the actual time is |S 1 | + O(1) ΔΦ = -|S 1 | (S 2 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”