Data Structures and Analysis of Algorithms
EECE 330
Lab 7 – Fall 2020
Guidelines
• This assignment is due on Thursday, November 19, 2020 at 11:55 pm.
• Build your code incrementally. Make sure you keep a backup of your working code. Build
and test your program for each task.
• Topics: Priority queues, hash tables, and reasoning problems.
? You are supposed to submit your own work. We have a zero tolerance policy for cheating.
Penalties may include one or more of the following: zero grades on programming assignments,
failing the course, disciplinary committee, Dean’s warning, and suspension.
? Lab attendance is mandatory. Violating this rule can lead to a failing grade.
Coding problem 1: Priority queues
Write a pqa t data structure that implements a priority queue using an array based implementation
of a binary heap. The structure should support the following.
• A constructor that takes an array a and its size n and constructs the priority queue with all
elements in a.
– For pqa t, implement the heapify algorithm as a separate method and call it from the
constructor.
• Method insert(e) that takes an element e and inserts it in the priority queue while preserving
order.
– Pay attention to the array used for storing the elements in pqa t. Use a dynamic array
methodology. Hint: you may use vector.
• Method extract() that removes the top element in the priority queue and returns it.
Test your code as follows.
• Use the pqa t constructor to build a heap for a randomly generated large array N = 10000
and measure the time needed to construct the priority queue.
• Use method insert to construct a pqa t object from the same array and measure the time
needed.
• Use the STL priority queue class to construct a priority queue with the same array and
measure the time needed. Try both approaches.
1
– Use the constructor that takes an array (or a vector) container to construct the priority
queue.
– Use the typical constructor with the push back method to construct the priority queue.
• Based on the time measurements, what do you think the implementation in the STL priority queue
is?
For the following, provide written and scanned solutions only.
Problem 2: Merge k sorted arrays
. Given two sorted linked lists (or arrays) A and B of sizes m and n, respectively, you have already
seen (and impelemented) an algorithm to merge the elements of A and B into a sorted linked list
(or aray) in O(n + m). Suppose instead of just two linked lists, we are given k linked lists, say each
of size n. Describe an algorithm to merge k linked lists in O(nk log k). Bonus: implement your
algorithm.
Problem 3: Exercise 13.3-2 from CLRS.
Show the red-black trees that result after successively inserting the keys 41, 38, 31, 12, 19, 8 into
an initially empty red-black tree.
Problem 4: CLRS 11.3-3
Consider the following hash function: given a string k of length n, h(k) = k mod m, where
mP= 2p − 1 for some prime p, and k is a character string interpreted in radix 2p . That is, h(k) =
n−1 pi
i=0 ki 2 mod (2p − 1).
Show that if string x can be derived from string y by permuting its characters, then x and y hash
to the same value. Give an example of an application in which this property would be undesirable
in a hash function.