

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures are Data Structures, Dynamic Programming, First-In-First-Out, Implementation, Python Code. Main points of this lecture are: Priority, Queue, Dequeued Next, Integer, Determine, Queue Implementations, Unzip, File, Python List Order, Methods
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Objective: To understand priority queue implementations in Python including being able to determine the big-oh of each operation.
To start the lab: Download and unzip the file at:
Part A:
a) Suppose that we have a priority queue with integer priorities such that the smallest integer corresponds to the highest priority. For the following priority queue, which item would be dequeued next?
40
30
10
5
79 13
priority queue:
b) The ListPriorityQueue implementation in lab4/list_priority_queue.py uses an unorder Python list.
_items:
ListPriorityQueue Object
List Object
40 30 10 13 5 79
0 1 2 3 4 5
What would be the big-oh notation for each of the following methods: (justify your answer)
enqueue:
dequeue:
c) The SortedListPriorityQueue implementation in lab4/sorted_list_priority_queue.py uses a Python list order by priorities in decending order.
_items:
SortedListPriorityQueue Object
List Object
79 40 30 13 10 5
0 1 2 3 4 5
What would be the big-oh notation for each of the following methods: (justify your answer)
enqueue:
dequeue
d) Why would it be a bad idea to implement a priority queue using a Python list order by priorities in reverse (ascending) order?
_items:
SortedListPriorityQueue Object
List Object
5 10 13 30 40 79
0 1 2 3 4 5
Answer the above questions, then raise your hand. Explain your answers to an instructor or TA.
Part B: (Lecture 7 and) Section 6.6 discusses a very “non-intuitive”, but powerful list/array-based approach to implement a priority queue, call a binary heap. The list/array is used to store a complete binary tree (a full tree with any additional leaves as far left as possible) with the items being arranges by heap-order property , i.e., each node is ≤ either of its children. An example of a min heap “viewed” an a complete binary tree would be:
6
15 10
(^114 20 )
300 125 117
[1]
[2] [3]
[4] [5] [6] [7]
[8] [9] (^) [10]
0 1 2 3 4 5 6 7 8 9 10 Python List actually used used not 6 15 10 114 20 20 50 300 125 117 to store heap items
a) For the above heap, the list/array indexes are indicated in [ ]'s. For a node at index i , what is the index of:
its left child if it exists: its right child if it exists: its parent if it exists: Recall the General Idea of insert(newItem): append newItem to the end of the list (easy to do, but violates heap-order property) restore the heap-order property by repeatedly swapping the newItem with its parent until it percolates up to the correct spot b) What would the above heap look like after inserting 30 and then 8? (show the changes on above tree)
c) What is the big-oh notation for inserting a new item in the heap?
Now let us consider the delMin operation that removes and returns the minimum item. Recall the General Idea of delMin():
remember the minimum value so it can be returned later (easy to find - at index 1) copy the last item in the list to the root, delete it from the right end, decrement size restore the heap-order property by repeatedly swapping this item with its smallest child until it percolates down to the correct spot return the minimum value
6
10 15
(^114 20 )
(^300 )
[1]
[2] [3]
[4] [5] (^) [6] [7]
[8] [9]
d) What would the above heap look like after delMin? (show the changes on above tree)
Answer the above questions, then raise your hand. Explain your answers to an instructor or TA.