268 views

Uploaded by api-26091603

save

- Skill Set
- List and Table Sort, Sorting Algorithm Analysis
- 10.1.1.34
- PP Lab Manual
- Chap 10
- Howto Sorting
- Assignment Eso207!2!1
- Internal Sorting Methods
- ccads
- Appti and Tech
- Datastructures-CS1151
- Randomized Quicksort Performance Analysis
- Quiz1-2017
- 7_sorting
- 02-filesandindexes
- Duplicate Detection
- Algorithms - Chapter 3 Bruteforce
- Jquery.cheatsheet.1.4.3
- Aspiring Minds Sample Papers
- C Questions and Answer-libre
- SWIFT Story of Sustainable Change: Upgrading the water system in Namorputh, Turkana
- Service Cloud Omni-Channel Winter '16 (Customers)
- Data Structure First Home Work
- 53 DynHypStopModel (MTITS2009)
- Data Structure
- NI Tutorial 3023
- Joint Prediction of Queuing Manners Using Mobile Phones
- Chapter 2
- Data Structure
- Data Structure
- Urdu Dictionary
- Linked List
- Salatul Janaza Ka Masnoon Tariqa
- Topi,Pagri Ya Nangay Sar Nimaz
- Jannat Main La Jana Wala 40 Aamaal
- Linked Lists
- Chanaroon K Aansoo by Tariq Ismail Sagar
- One Minute for Allah
- Be-namazi Kay 15 Azab
- Hadith-eman Ka Maza
- Namaz e Witar o Tahajud
- AL-HADITH
- Qaza e Umari Ki Namazain

You are on page 1of 13

© 2004 Goodrich, Tamassia

Priority Queues

1

**Priority Queue ADT (§ 7.1.3)
**

A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT

Additional methods

min() returns, but does not remove, an entry with smallest key size(), isEmpty()

insert(k, x) inserts an entry with key k and value x removeMin() removes and returns the entry with smallest key

Applications:

Standby flyers Auctions Stock market

© 2004 Goodrich, Tamassia

Priority Queues

2

**Total Order Relations (§ 7.1.1)
**

Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct entries in a priority queue can have the same key

© 2004 Goodrich, Tamassia

**Mathematical concept of total order relation ≤
**

Reflexive property: x≤x Antisymmetric property: x≤y∧y≤x⇒x=y Transitive property: x≤y∧y≤z⇒x≤z

Priority Queues

3

Entry ADT (§ 7.1.2)

An entry in a priority queue is simply a key-value pair Priority queues store entries to allow for efficient insertion and removal based on keys Methods:

**As a Java interface:
**

/** * Interface for a key-value * pair entry **/ public interface Entry { public Object key(); public Object value(); }

key(): returns the key for this entry value(): returns the value associated with this entry Priority Queues © 2004 Goodrich, Tamassia

4

Comparator ADT (§ 7.1.2)

A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses an auxiliary comparator The comparator is external to the keys being compared When the priority queue needs to compare two keys, it uses its comparator

© 2004 Goodrich, Tamassia

**The primary method of the Comparator ADT:
**

compare(x, y): Returns an integer i such that i < 0 if a < b, i = 0 if a = b, and i > 0 if a > b; an error occurs if a and b cannot be compared.

Priority Queues

5

Example Comparator

Lexicographic comparison of 2-D points: Point objects:

/** Comparator for 2D points under /** Class representing a point in the standard lexicographic order. the plane with integer */ coordinates */ public class Lexicographic public class Point2D { implements Comparator { protected int xc, yc; // int xa, ya, xb, yb; coordinates public int compare(Object a, public Point2D(int x, int y) Object b) throws { ClassCastException { xc = x; xa = ((Point2D) a).getX(); yc = y; ya = ((Point2D) a).getY(); } xb = ((Point2D) b).getX(); public int getX() { yb = ((Point2D) b).getY(); return xc; if (xa != xb) } return (xb - xa); public int getY() { else return yc; return (yb - ya); } } } Priority Queues 6 © 2004} Goodrich, Tamassia

**Priority Queue Sorting (§ 7.1.4)
**

We can use a priority queue to sort a set of comparable elements

Insert the elements one by one with a series of insert operations Remove the elements in sorted order with a series of removeMin operations

The running time of this sorting method depends on the priority queue implementation

Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P ← priority queue with comparator C while ¬S.isEmpty () e ← S.removeFirst () P.insert (e, 0) while ¬P.isEmpty() e ← P.removeMin().key() S.insertLast(e)

7

© 2004 Goodrich, Tamassia

Priority Queues

**Sequence-based Priority Queue
**

Implementation with an unsorted list

4 5 2 3 1

**Implementation with a sorted list
**

1 2 3 4 5

Performance:

insert takes O(1) time since we can insert the item at the beginning or end of the sequence removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key

Performance:

insert takes O(n) time since we have to find the place where to insert the item removeMin and min take O(1) time, since the smallest key is at the beginning

8

© 2004 Goodrich, Tamassia

Priority Queues

Selection-Sort

Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort:

Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to

**1 + 2 + …+ n Selection-sort runs in O(n2) time
**

© 2004 Goodrich, Tamassia Priority Queues

9

**Selection-Sort Example
**

Input: Phase 1 (a) (b) .. . (g) Phase 2 (a) (b) (c) (d) (e) (f) (g)

© 2004 Goodrich, Tamassia

Sequence S (7,4,8,2,5,3,9) (4,8,2,5,3,9) (8,2,5,3,9) .. .. . . () (2) (2,3) (2,3,4) (2,3,4,5) (2,3,4,5,7) (2,3,4,5,7,8) (2,3,4,5,7,8,9)

Priority Queues

Priority Queue P () (7) (7,4) (7,4,8,2,5,3,9) (7,4,8,5,3,9) (7,4,8,5,9) (7,8,5,9) (7,8,9) (8,9) (9) ()

10

Insertion-Sort

Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort:

Inserting the elements into the priority queue with n insert operations takes time proportional to

1 + 2 + …+ n

Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time

**Insertion-sort runs in O(n2) time
**

© 2004 Goodrich, Tamassia Priority Queues 11

**Insertion-Sort Example
**

Input: Phase 1 (a) (b) (c) (d) (e) (f) (g) Phase 2 (a) (b) .. . (g)

© 2004 Goodrich, Tamassia

**Sequence S Priority queue P (7,4,8,2,5,3,9) () (4,8,2,5,3,9) (8,2,5,3,9) (2,5,3,9) (5,3,9) (3,9) (9) () (2) (2,3) .. . (2,3,4,5,7,8,9)
**

Priority Queues

(7) (4,7) (4,7,8) (2,4,7,8) (2,4,5,7,8) (2,3,4,5,7,8) (2,3,4,5,7,8,9) (3,4,5,7,8,9) (4,5,7,8,9) .. . ()

12

**In-place Insertion-sort
**

Instead of using an external data structure, we can implement selectionsort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertionsort

5 5 4 2 2

4 4 5 4 3 2 2

2 2 2 5 4 3 3

3 3 3 3 5 4 4

1 1 1 1 1 5 5

13

1 We keep sorted the initial portion of the sequence 1 We can use swaps Priority Queues © 2004 Goodrich,instead of modifying Tamassia

- Skill SetUploaded byTavishi Singh
- List and Table Sort, Sorting Algorithm AnalysisUploaded byhardworking_student
- 10.1.1.34Uploaded byluftkissen
- PP Lab ManualUploaded byparasuraman
- Chap 10Uploaded byyasinmhd
- Howto SortingUploaded byRanu Games
- Assignment Eso207!2!1Uploaded byDeepak Tholia
- Internal Sorting MethodsUploaded byKeith Tanaka Magaka
- ccadsUploaded bya2199347
- Appti and TechUploaded byjeyajoy
- Datastructures-CS1151Uploaded byctvenkat
- Randomized Quicksort Performance AnalysisUploaded byWlad
- Quiz1-2017Uploaded bynorbihaver
- 7_sortingUploaded bysudhircarpenter
- 02-filesandindexesUploaded byAbhinav Agnihotri
- Duplicate DetectionUploaded bysnaveenkumarbe
- Algorithms - Chapter 3 BruteforceUploaded byKata Vizi
- Jquery.cheatsheet.1.4.3Uploaded byswordenium
- Aspiring Minds Sample PapersUploaded bybatchno3
- C Questions and Answer-libreUploaded byAshik A Nezzilianz
- SWIFT Story of Sustainable Change: Upgrading the water system in Namorputh, TurkanaUploaded byOxfam
- Service Cloud Omni-Channel Winter '16 (Customers)Uploaded byatifhassansiddiqui
- Data Structure First Home WorkUploaded bynarinder pal saini
- 53 DynHypStopModel (MTITS2009)Uploaded byguido gentile
- Data StructureUploaded bysanmax007
- NI Tutorial 3023Uploaded bymirko_italy
- Joint Prediction of Queuing Manners Using Mobile PhonesUploaded byInternational Journal for Scientific Research and Development - IJSRD
- Chapter 2Uploaded byJyoti Patil
- Data StructureUploaded byAbhishek Vk
- Data StructureUploaded bygspriyadarshini

- Urdu DictionaryUploaded byapi-26091603
- Linked ListUploaded byapi-26091603
- Salatul Janaza Ka Masnoon TariqaUploaded byapi-26091603
- Topi,Pagri Ya Nangay Sar NimazUploaded byapi-26091603
- Jannat Main La Jana Wala 40 AamaalUploaded byapi-26091603
- Linked ListsUploaded byapi-26091603
- Chanaroon K Aansoo by Tariq Ismail SagarUploaded byapi-26091603
- One Minute for AllahUploaded byapi-26091603
- Be-namazi Kay 15 AzabUploaded byapi-26091603
- Hadith-eman Ka MazaUploaded byapi-26091603
- Namaz e Witar o TahajudUploaded byapi-26091603
- AL-HADITHUploaded byapi-26091603
- Qaza e Umari Ki NamazainUploaded byapi-26091603