Abstract Data Types Stack, Queue Amortized analysis

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
1 Abstract Data Types Stack, Queue Amortized analysis Cormen: Ch 10, 17 (11, 18)
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)
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.
1 Abstract Data Types Queue + Dequeue Amortized analysis.
Algorithms and Data Structures Representing Sequences by Arrays and Linked Lists.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000.
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.
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.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
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
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
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
Queues.
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Week 4 - Friday CS221.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Linked List Stacks, Linked List Queues, Dequeues
CE 221 Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
Stacks and Queues CMSC 202.
Queue data structure.
Stacks and Queues.
Priority Queue.
Abstract Data Types Stack, Queue Amortized analysis
Algorithms Part III. Data Structures
Stacks, Queues, and Deques
Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we.
Stacks, Queues, and Deques
Persistent deques.
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Stacks and Queues.
Abstract Data Types Stack, Queue Amortized analysis
Abstract Data Types Stack, Queue Amortized analysis
Haim Kaplan, Uri Zwick March 2018
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 Stacks CSCI 240
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Abstract Data Types Stack, Queue Amortized analysis

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

Implementation with lists head size=3 12 1 5 tail inject(4,Q)

Implementation with lists head size=3 12 1 5 4 tail inject(4,Q)

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

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

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

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

Push size=1 5 push(x,D): n = new node n.element ←x n.next ← head.next tail size=1 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

4 size=1 5 push(x,D): n = new node n.element ←x n.next ← head.next tail size=1 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)

4 size=1 5 push(x,D): n = new node n.element ←x n.next ← head.next tail size=1 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)

4 size=1 5 push(x,D): n = new node n.element ←x n.next ← head.next tail size=1 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)

4 size=2 5 push(x,D): n = new node n.element ←x n.next ← head.next tail size=2 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)

Implementations of the other operations are similar Try by yourself

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

Implementation with stacks 13 5 4 17 21 size=5 inject(x,Q): push(x,S2); size ← size + 1 inject(2,Q)

Implementation with stacks 13 5 4 17 21 2 size=5 inject(x,Q): push(x,S2); size ← size + 1 inject(2,Q)

Implementation with stacks 13 5 4 17 21 2 size=6 inject(x,Q): push(x,S2); size ← size + 1 inject(2,Q)

Pop S1 S2 13 5 4 17 21 2 size=6 pop(Q): if empty?(Q) error 5 4 17 21 2 size=6 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q)

Pop S2 S1 5 4 17 21 2 size=6 pop(Q): if empty?(Q) error 5 4 17 21 2 size=6 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q)

Pop S2 S1 5 4 17 21 2 size=5 pop(Q): if empty?(Q) error 5 4 17 21 2 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 2 5 4 17 21 size=5 pop(Q): if empty?(Q) error 5 4 17 21 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 21 2 5 4 17 size=5 pop(Q): if empty?(Q) error 5 4 17 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 17 21 2 5 4 size=5 pop(Q): if empty?(Q) error 5 4 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 4 17 21 2 5 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 5 4 17 21 2 size=5 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

Pop S1 S2 4 17 21 2 size=4 pop(Q): if empty?(Q) error if empty?(S1) then move(S2, S1) pop( S1); size ← size -1 pop(Q) pop(D)

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

Analysis O(n) worst case time per operation

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

Key Observation An expensive operation cannot occur too often !

The potential formalism The potential is in fact the bank

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

+ Amortized(op1) = actual(op1) + 1- 0 … Amortized(opn) = actual(opn) + n- (n-1) iAmortized(opi) = iactual(opi) + n- 0 iAmortized(opi)  iactual(opi) if n- 0  0

A better bound Consider 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)

Conclusion If we start with an empty queue and perform m operations then its takes O(m) time

Back to deques Alternative implementation using stacks

Implementation with stacks 13 5 4 17 21 size=5 push(x,D): push(x,S1) push(2,D)

Implementation with stacks 2 13 5 4 17 21 size=6 push(x,D): push(x,S1) push(2,D)

Pop S1 S2 2 13 5 4 17 21 size=6 pop(D): if empty?(D) error 5 4 17 21 size=6 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Pop S1 S2 13 5 4 17 21 size=5 pop(D): if empty?(D) error 5 4 17 21 size=5 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D) pop(D)

Pop S2 S1 5 4 17 21 size=4 pop(D): if empty?(D) error 5 4 17 21 size=4 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D) pop(D)

Pop S2 S1 5 4 17 21 size=4 pop(D): if empty?(D) error 5 4 17 21 size=4 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Pop S2 S1 5 4 17 21 size=4 pop(D): if empty?(D) error 5 4 17 21 size=4 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Pop S2 S1 5 4 17 21 size=4 pop(D): if empty?(D) error 5 4 S2 17 21 size=4 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Pop S1 S2 4 17 21 size=4 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Pop S1 S2 4 17 21 size=3 pop(D): if empty?(D) error if empty?(S1) then split(S2, S1) pop( S1) pop(D)

Split S1 S2 5 4 17 21 S3

Split S1 S2 5 4 17 S3 21

Split S1 S2 5 4 S3 17 21

Split S1 S2 4 5 S3 17 21

Split S1 S2 5 4 S3 17 21

Split S1 17 S2 5 4 S3 21

Split S1 S2 5 4 17 21 S3

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 x ← pop(S3) push(x,S2) i ← i+1

Analysis O(n) worst case time per operation

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

Key Observation An expensive operation cannot occur too often !

A better bound Consider Recall that: Amortized(op) = actual(op) + ΔΦ This is O(1) if no splitting occurs Say we split S1: Then the actual time is |S1| + O(1) ΔΦ = -|S1| So the amortized time is O(1)

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