Algorithm and Data Structure Part II Dr. Naimah Yaakob

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
Lecture - 9 On Queues. Prepared by, Jesmin Akhter, Lecturer, IIT,JU QUEUES A Queue is a linear list of elements in which deletions can take place only.
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
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.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CHP-4 QUEUE.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Stacks And Queues Chapter 18.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Computer Engineering Rabie A. Ramadan Lecture 6.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
UNIT II Queue.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Arrays.
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
Priority Queues and Heaps
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Data Structures Interview / VIVA Questions and Answers
Algorithm and Data Structure Part III Dr. Naimah Yaakob
QUEUE.
CS 583 Analysis of Algorithms
Introduction to Data Structure
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks, Queues, and Deques
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Priority Queues (Chapter 6.6):
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Presentation transcript:

Algorithm and Data Structure Part II Dr. Naimah Yaakob EKT 334 Algorithm and Data Structure Chapter 5 – Queues Part II Dr. Naimah Yaakob

Circular queues Used to rectify the limitations in linear queue As the name indicates a circular queue is not linear in structure but instead circular. In other words, the FRONT and REAR variables which displayed a linear (left to right) movement over a queue, display a circular movement (clock wise) over the queue data structure.

C moves towards the front

REAR FRONT ak+n a1 ak+l a2 ... ... ak+2 ... ak+1 ak Fig. 5.5: circular MOVEMENT of FRONT and REAR variables in a circular queue

INSERT OPERATION.. Algorithm 5.3: Implementation of insert operation on a circular queue procedure INSERT_CIRCQ(CIRC_Q, FRONT,REAR, n, ITEM) REAR=(REAR + 1) mod n; If (FRONT = REAR) then CIRCQ_FULL; /* Here CIRCQ_FULL tests for the queue full condition and if so, retracts REAR to its previous value*/ CIRC_Q [REAR]= ITEM; end INSERT_CIRCQ.

DELETE OPERATION.. Algorithm 5.4: Implementation of a delete operation on a circular queue. Procedure DELETE_CIRCQ(CIRC_Q, FRONT,REAR, n, ITEM) If (FRONT = REAR) then CIRCQ_EMPTY; /* CIRC_Q is physically empty*/ FRONT = (FRONT+1) mod n; ITEM = CIRC_Q [FRONT]; end DELETE_CIRCQ

Priority queues A priority queue is a queue in which insertion or deletion of items from any position in the queue are done based on some property (such as priority of task) A common method of implementation of a priority queue is to open as many queues as there are priority factors. A low priority queue will be operated for deletion (service) only when all its high priority predecessors are empty. Let P be a priority queue with three different elements a,b,c, whose priority factors are 2,1,1. The higher the number, the higher is the priority. When a new element d with priority 4 is inserted, d joins at the head (front) of the queue superceding the remaining elements. The same priority elements follow the FIFO or FCFS.

A Scenario... A file of patients waiting for their turn in a queue to have an appointment with a doctor. All patients are recorded equal priority and follow the FCFS by appointment. Suppose however, when a patient with bleeding injuries is brought in, he/she is accorded high priority and is immediately moved to the head of the queue for immediate attention by the doctor. This is priority queue at work. A low priority queue will be operated for deletion (service) only when all its high priority predecessors are empty. i.e: The deletion of an element in a priority queue Q1 with priority Pi is possible only when those queues Qj with priorities Pj (Pj>Pi) are empty. ***However, the insertion still obeys the FIFO scheme with regards to the q1 alone. Another method of implementation could be to sort the elements in the queue according to the descending order of priorities every time an insertion takes place. The top priority element at the head of the queue is the one to be deleted.

Performance Trade-off While the first method (cluster (group) of queue) consumes space, the time complexity for this method is only O(1). - in the case of deletion of an element in a specific queue with a specific priority, it calls for the checking of all other queues preceding it in priority, to be empty. The second method (sorting) consumes less space since it handles just a single queue. However, the insertion of every element, calls for sorting all the queue elements in the descending order, the most efficient of which report a time complexity of O(n.logn). High High Medium Low Let say we want to delete an item from a low-priority queue, we need to check all other higher-priority queues to ensure they are empty High Low Delete Sorted-Priority Queue

Same priority: FCFS

PRIORITY QUEUE AS AN ARRAY... Rows : Number of priorities accorded to the data elements Columns : Maximum number of elements that can be accommodated in the queues corresponding to the priority queues If PRIO_QUE[1:m, 1:n] is an array representing a priority queue; Then, The priority numbers are ranging from 1 to m Maximum number of elements is n E.g: [1:3, 1:2]

Deques A deque (double ended queue) is a linear list in which all insertions and deletions are made at the end of the list. A deque is therefore more general than a stack or queue and is a sort of FLIFLO (first in last in or first out last out). Thus while one speaks of the top or bottom of a stack, or front or rear of a queue, one refers to the right end or left end of a deque.

A Comparison: Stack, queue, dequeue