DS L9: Queues

Similar documents
Data Structures, Algorithms & Data Science Platforms

L10: Dictionary & Skip List

L6,7: Time & Space Complexity

DS ,21. L11-12: Hashmap

DS & 26. L4: Linear Lists

L20-21: Graph Data Structure

L22-23: Graph Algorithms

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Data Structures, Algorithms & Data Science Platforms

Data Structures, Algorithms & Data Science Platforms

8. Fundamental Data Structures

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Spark Programming with RDD

Data Structures and Algorithms. Roberto Sebastiani

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

.:: UNIT 4 ::. STACK AND QUEUE

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

1 P age DS & OOPS / UNIT II


Linked Structures. See Section 3.2 of the text.

16. Dynamic Data Structures

Linked List. April 2, 2007 Programming and Data Structure 1

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CSC212:Data Structure

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Concurrent Programming

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CSCA48 Term Test 1 Seminar

Memory Organization Redux

CAP Theorem, BASE & DynamoDB

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Ashish Gupta, Data JUET, Guna

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Programming. Lists, Stacks, Queues

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

DNHI Homework 3 Solutions List, Stacs and Queues

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Introduction to Computer Science II CS S-20 Linked Lists III

csci 210: Data Structures Stacks and Queues

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Laboratorio di Algoritmi e Strutture Dati

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Dynamic Data Structures

Information Science 2

Your Topic Goes Here Queue

Abstract Data Types. Abstract Data Types

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Stack and Queue. Stack:

ITI Introduction to Computing II

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Data Structures and Algorithms for Engineers

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Lecture 12: Doubly Linked Lists

Project 2 (Deques and Randomized Queues)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Data Structures Lecture 5

Unit 4 Basic Collections

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Chapter 9 STACK, QUEUE

L3: Spark & RDD. CDS Department of Computational and Data Sciences. Department of Computational and Data Sciences

CS171 Midterm Exam. October 29, Name:

Tutorial: Apache Storm

15. Stacks and Queues

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

Introduction to Data Structures

CS350 - Exam 1 (100 Points)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Abstract Data Types. Definitions, Implementations, and Uses

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Name CPTR246 Spring '17 (100 total points) Exam 3

Solution for Data Structure

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Chapter 18: Stacks And Queues

Cpt S 122 Data Structures. Data Structures

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Basic Data Structures

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

DATA STRUCTURE UNIT I

Data Structures and Algorithms

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

L5-6:Runtime Platforms Hadoop and HDFS

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Transcription:

Indian Institute of Science Bangalore, India भ रत य व ज ञ न स स थ न ब गल र, भ रत Department of Computational and Data Sciences DS286 2016-09-09 L9: Queues Yogesh Simmhan s i m m h a n @ c d s. i i s c. a c. i n Slides courtesy Venkatesh Babu, CDS Department of Computational and Data Science, IISc, 2016 This work is licensed under a Creative Commons Attribution 4.0 International License Copyright for external content used with attribution is retained by their original authors

Queue ADT FIFO Principle Elements inserted only at rear (enqueued) end and removed from front (dequeued) Also called Head and Tail Dequeue() 2 Enqueue(6) 2 5 4 7 7 2 8 0 9 6 Front Front Rear Rear

Queue -Methods queue New() Creates and returns an empty queue Enqueue(item v) Inserts object v at the rear of the queue item Dequeue() Removes the object from front of the queue. Error occurs if the queue is empty item Front() Returns, but does not remove the front element. An error occurs if the queue is empty

Queue Methods & Invariants int Size() number of items in queue boolean IsEmpty() is size == 0 Axioms/invariants Front(Enqueue(New(),v)) = v Dequeue(Enqueue(New(), v)) = New() Front(Enqueue(Enqueue(Q, w), v)) = Front(Enqueue(Q, w)) Dequeue(Enqueue(Enqueue(Q, w), v))= Enqueue(Dequeue(Enqueue(Q, w)), v)

Array Implementation of Queue Using array in circular fashion Wraparound using mapping function (recollect from List ADT discussion) A max size N is specified Q consists of an N element array and 2 integer variables having array index: f: index of the front element (head, for dequeue) r: index of the element after the rear one (tail, for enqueue) Q 0 N-1 f r

Array Implementation of Queue Q 0 N-1 r f What does f=r mean? Resolve Ambiguity: We will never add n th element to Queue (declare full if the size of queue is N-1).

Pseudo Code int size() Return (N-f+r) mod N bool isempty() Return(f==r) int front() If isempty() then Return QueueEmptyException Else Return Q[f]

Pseudo Code int Dequeue() If isempty() then Return QueueEmptyException v = Q[f] Q[f] = null f = (f+1) mod N Return v Enqueue(v) If size()==n-1 then Return QueueFullException Q[r] = v r = (r+1) mod N Compute Complexity? Storage Complexity?

Linked List Problem with array: Requires the number of elements a priori. DATA DATA NULL

Implementation with linked List NOTE: Different from what was mentioned in class. Nodes (data, pointer) connected in a chain by links Head Tail FRONT of Queue REAR of Queue Φ Maintain two pointers, to head and tail of linked list. The head of the list is FRONT of the queue, the tail of the list is REAR of the queue. Why not the opposite?

Example CDS.IISc.ac.in Department of Computational and Data Sciences Tail of linked list is REAR of queue Enqueue at tail Head is FRONT of queue Dequeue at head TAIL = Φ, HEAD = Φ Enqueue 7 7 Φ HEAD, TAIL FRONT, REAR Enqueue 8 7 8 Φ HEAD FRONT TAIL REAR

Example Enqueue 6 CDS.IISc.ac.in Department of Computational and Data Sciences Tail of linked list is REAR of queue Enqueue at tail Head is FRONT of queue Dequeue at head 7 8 6 Φ HEAD FRONT TAIL REAR TAIL REAR Node n = new Node(6) n.next = null TAIL.next = n TAIL = n // NEW REAR O(1) complexity to enqueue

Example Dequeue 7 CDS.IISc.ac.in Department of Computational and Data Sciences Tail of linked list is REAR of queue Enqueue at tail Head is FRONT of queue Dequeue at head 7 8 6 Φ HEAD FRONT HEAD FRONT TAIL REAR int v = HEAD.value tmp = HEAD.next delete(head) HEAD = tmp // NEW HEAD return v // 7 O(1) complexity to dequeue

Example What if Head of linked list is REAR of queue, Tail the FRONT? 6 8 Φ Enqueue 9 HEAD REAR TAIL FRONT 9 6 8 Φ HEAD REAR TAIL FRONT

Example Enqueue 9 What if Head of linked list is REAR of queue, Tail the FRONT? 9 6 8 Φ HEAD REAR HEAD REAR TAIL FRONT Node n = new Node(9) n.next = HEAD HEAD = n O(1) complexity to enqueue

Example Dequeue 8 What if Head of linked list is REAR of queue, Tail the FRONT? 9 6 8 Φ HEAD REAR TAIL FRONT 9 6 Φ 8 Φ HEAD REAR TAIL FRONT TAIL FRONT

Example Dequeue 8 What if Head of linked list is REAR of queue, Tail the FRONT? Things don t work well! 9 6 Φ 8 Φ HEAD REAR TAIL FRONT TAIL int v = TAIL.value // 8 // tail s previous (6) should point to null! n = HEAD // Can head be null? while(n.next!= TAIL) n = n.next; n.next = null delete(tail) TAIL = n return v FRONT O(N) complexity to dequeue

Linked List VALUE NEXT typedef struct Node { int value; struct Node *next; } a_node; // pointer to Node Any linked list is a pointer to a node typedef Node *list; // head of list

Double-Ended Queue (Dequeue) Supports insertion & deletion from front & rear Supports six methods InsertFirst(item o) Inserts o at the beginning of deque InsertLast(item o) Inserts o at the end of deque item RemoveFirst() removes the 1 st element item RemoveLast() removes the last element item First() return first element item Last() - return last element Problem in implementing using single linked list with O(1) e.g. If head is front and tail is rear, RemoveLast will require traversal from head to tail s previous

Dequeue as Doubly Linked List prev value next Φ 5 6 8 Φ HEAD TAIL Nodes of doubly linked list have a next and a prev link All the methods of a dequeue using doubly linked list have a constant running time O(1) How?

Doubly Linked List PREV VALUE NEXT typedef struct Node { int value; struct Node *next, *prev; } a_node;

Delete Element from Doubly Linked List HEAD TAIL Φ 5 6 6 8 5 6 8 2 8 2 Φ HEAD Delete(8) TAIL Φ 5 6 5 6 2 2 6 Φ Node n = HEAD while(n.next.value!= v) n = n.next; tmp = n.next // n = 6, tmp = 8 n.next = tmp.next // 6.next = 2 tmp.next.prev = n // 2.prev = 6 delete(tmp) // delete 8

Implement Stacks with Deques Stack Method Size() IsEmpty() Top() Push() Pop() Deque Method Size() IsEmpty() Last() insertlast() removelast()

Implement Queue with Deques Stack Method Size() IsEmpty() front() enqueue() dequeue() Deque Method Size() IsEmpty() first() insertlast() removefirst()

CodeChef: FLOW004 9/Sep 20-Sep-16 28

Tasks Solve sanity check problem on CodeChef by Sep 14 https://www.codechef.com/problems/flow004 Self study (Sahni Textbook) Check: Have you read Chapter 8 Stacks? Solved exercises? Read: Chapter 9, Queues from textbook Try: Exercise 4, 14, 18 from Chapter 9 of textbook 20-Sep-16 29

Questions? Department of Computational and Data Science, IISc, 2016 This work is licensed under a Creative Commons Attribution 4.0 International License Copyright for external content used with attribution is retained by their original authors 20-Sep-16 30