Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.

Similar presentations


Presentation on theme: "Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."— Presentation transcript:

1 Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

2 » Rationale » Implementation Layer » Exercise 2 © Sekolah Tinggi Teknik Surabaya

3 » A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. » Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3. 3 © Sekolah Tinggi Teknik Surabaya

4 » A priority queue is a container in which access or deletion is of the highest- priority item, according to some way of assigning priorities to items. 4 © Sekolah Tinggi Teknik Surabaya

5 » For example, suppose a hospital emergency room has the following four patients: » In what order should the patients be treated? 5 © Sekolah Tinggi Teknik Surabaya NameAgeInjury Matt20Sprained Ankle Andrew45Broken Leg Samira20Active Labor Kerem83Heart Attack

6 » Scheduling » Data Compression ˃Huffman Encoding » JPEG Encoding 6 © Sekolah Tinggi Teknik Surabaya

7 » Rationale » Implementation Layer » Exercise 7 © Sekolah Tinggi Teknik Surabaya

8 » A comparison ADT encapsulates the action of comparing two objects according to a given total order relation » Comparison ADT in C# ˃ IComparer ˃ IComparable » A generic priority queue uses a IComparer as a template argument, to define the comparison function ( ) » The IComparer is external to the keys being compared. Thus, the same objects can be sorted in different ways by using different comparers » Data types such as String and Integer already have default implementation of IComparer ( Comparer.Default ) 8 © Sekolah Tinggi Teknik Surabaya

9 » Default sort order for your object. int IComparable.CompareTo(object obj) { car c=(car)obj; return String.Compare(this.make,c.make); } » String.Compare is used in this example because the property that is chosen for the comparison is a string 9 © Sekolah Tinggi Teknik Surabaya

10 » Provide additional comparison mechanism private class sortYearAscendingHelper : IComparer { int IComparer.Compare(object a, object b) { car c1=(car)a; car c2=(car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; } » IComparer.Compare method requires a tertiary comparison (1, 0, or -1) 10 © Sekolah Tinggi Teknik Surabaya

11 » Array.Sort(array); » Array.Sort(array, ComparerObject); 11 © Sekolah Tinggi Teknik Surabaya

12 Implementation 1Implementation 2 » Implementation with an unsorted list » Performance: ˃push takes O(1) time since we can insert the item at the beginning or end of the sequence ˃pop, top take O(n) time since we have to traverse the entire sequence to find the smallest key » Implementation with a sorted list » Performance: ˃push takes O(n) time since we have to find the place where to insert the item ˃pop, top take O(1) time since the smallest key is at the beginning of the sequence © Sekolah Tinggi Teknik Surabaya 12

13 PriorityQueue weights = new PriorityQueue (); weights.Insert(95.3); weights.Insert(47.6); weights.Insert(98.2); weights.Insert(88.7); while (!weights.IsEmpty()) { Console.WriteLine(weights.Delete()) } // 98.2 95.3 88.7 47.6 13 © Sekolah Tinggi Teknik Surabaya

14 » Important Methods ˃Length ˃IsEmpty() ˃IsFull() ˃Insert() ˃Delete() ˃Peek() 14 © Sekolah Tinggi Teknik Surabaya

15 » Rationale » Implementation Layer » Exercise 15 © Sekolah Tinggi Teknik Surabaya

16 » Wicked Carrefour ˃Customer who buys more, served first ˃5 Cashiers ˃Customer may choose any cashier 16 © Sekolah Tinggi Teknik Surabaya

17 » How to use the IComparable and IComparer interfaces in Visual C#, http://support.microsoft.com/kb/32072 7 http://support.microsoft.com/kb/32072 7 » Briana B. Morrison, Priority Queues (Lecture Notes) 17 © Sekolah Tinggi Teknik Surabaya


Download ppt "Lecture 7 Priority Queue Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1."

Similar presentations


Ads by Google