Priority Queue.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues and Linked Lists
Data Structures ADT List
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.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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++)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks, Queues & Deques CSC212.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
7.2 Priority Queue ADTs Priority queue concepts
Priority Queues Dr. David Matuszek
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues Chapter 4.
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
Double-Ended Queues Chapter 5.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Marcus Biel, Software Craftsman
Queues Rem Collier Room A1.02
Csc 2720 Data structure Instructor: Zhuojun Duan
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues, Deques and Priority Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Queues.
General Trees & Binary Trees
Queue.
Data Structures ADT List
Data Structures ADT List
Queues: Implemented using Arrays
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Queues FIFO Enqueue Dequeue Peek.
Stacks and Queues 1.
Collections Framework
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
Copyright © Aiman Hanna All rights reserved
ADT Queue (Array Implementation)
Header and Trailer Sentinels
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues Definition of a Queue Examples of Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues: Implemented using Linked Lists
Queues.
Stacks and Linked Lists
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Priority Queue

Priority Queue Each data element has a priority associated with it. Highest priority item is served first. Real World Priority Queues: hospital emergency rooms…most sick patients treated first, events in a computer system, etc. Priority Queue can be viewed as: View 1: Priority queue as an ordered list. View 2: Priority queue as a set.

ADT Priority Queue Structure: the elements are linearly arranged, and may be ordered according to a priority value, highest priority element is called the front or head and least priority element the tail. Domain: the number of nodes in the queue is bounded therefore the domain is finite. Type of elements: PriorityQueue

ADT Priority Queue Operations: Method Enqueue (Type e, Priority p) requires: PQ is not full. input: e, p. results: Element e is added to the queue according to its priority. output: none. Method Serve (Type e, Priority p) requires: PQ is not empty. input: results: the element at the head of PQ is removed and returned. output: e, p. Method Length (int length) input: results: The number of element in the PQ is returned. output: length.

ADT Priority Queue Operations: Method Full (boolean flag). requires: input: results: If PQ is full then flag is set to true, otherwise flag is set to false. output: flag.

ADT Priority Queue Array Implementation el 10 8 7 5 Head Tail el el el Linked Implementation Insert Where? el 10 8 7 5 Head Tail

ADT Priority Queue Specification: Elements: The elements are of type PQNode. Each node has in it a data element of variable type <Type> and priority of type Priority ( which could be int type). public class PQNode<T> { private T data; private Priority priority; public PQNode<T> next; public PQNode() { next = null; } public PQNode(T e, Priority p) { data = e; priority = p; }

ADT Priority Queue (Linked) public class LinkPQ<T> { private int size; private PQNode<T> head, tail; /* tail is of no use here. */ public LinkPQ() { head = tail = null; size = 0; } public int length (){ return size; }

ADT Priority Queue (Linked) public int length (){ return size; } public boolean full () { return false;

ADT Priority Queue (Linked) public void enqueue(T e, int pty) { PQNode<T> p, q, tmp; if ((size == 0) || (pty > head.Priority())) { tmp = new PQNode<T>(e, pty); tmp.next = head; head = tmp; } else { p = head; q = null; while ((p != null) && (p.Priority() > pty)) { q = p; p = p.next; } tmp.next = p; q.next = tmp; } }

ADT Priority Queue (Linked) public T serve (Priority pty){ T e = head.get_data(); pty.set_value(head.get_priority().get_value()); head = head.next; size--; return(e); }

ADT Priority Queue Implementations Array Implementation: Enqueue is O(n), Serve is O(1). Linked List: Enqueue is O(n), Serve is O(1). Heap: Enqueue is O(log n), Serve is O(log n)  Heaps to be discussed later.

Double-Ended Queues Double ended queue (or a deque) supports insertion and deletion at both the front and the tail of the queue. Supports operations: addFirst( ), addLast(), removeFirst( ) and removeLast( ). Can be used in place of a queue or a stack.

Double-Ended Queues Operations: (Assume all operations are performed on deque DQ) Method addFirst (Type e) requires: DQ is not full. input: e. results: Element e is added to DQ as first element. output: none. Method addLast (Type e) requires: DQ is not full. input: e results: Element e is added to DQ as last element. output: none. Method removeFirst (Type e) requires: DQ is not empty. input: none results: Removes and returns the first element of DQ. output: e.

Double-Ended Queues Method removeLast (Type e) requires: DQ is not empty. input: none. results: Removes and returns the last element of DQ. output: e. Method getFirst (Type e) requires: DQ is not empty. input: none results: Returns the first element of DQ. output: e. Method getLast (Type e) results: Returns the last element of DQ. output: e Method size (int x) input: none results: Returns the number of elements in DQ. output: x

Double-Ended Queues Method isEmpty (boolean x) input: none results: if DQ is empty returns x as true otherwise false. output: x