Class Revision
Class Revision
Class Revision
Omowunmi Isafiade
Email: omowunmi.isafiade@uct.ac.za
Class Exercises
Hash Table
Priority Queue
Summary
Hash Table
If the last index of a hash table is p, what is the size of the hash
table?
What is primary clustering and why does it occur?
What is the relevance of lazy deletion?
Suppose in quadratic probing, a hash function hashes to an index k
which is already occupied, what are the next three possible positions
to examine?
The average number of cells probed during an unsuccessful search
using linear probing is estimated at (1 + 1/(1-α)2)/2, where α is the
load factor, what is the expected number of probes for an
unsuccessful search when the table contains 6 elements?(m= 10)
Hash Table
Given the input {437, 123, 617, 87, 24, 199}, a fixed table size of 11,
and a hash function H(X) = X mod 11, show the resulting
a. Linear Probing Hash Table
b. Quadratic Probing Hash Table
13 6 13 6 13 6
2 4 9
11 9 4 11 9 11
D-heap after
Final d-heap after D-heap after second deleteMin
complete insertion first deleteMin
BuildHeap vs Sequential Insertion
Exercise: Show the result of inserting the following sequence
{5, 10, 12, 3, 2, 7, 1} one at a time, in an initially empty heap.
Then show the result of using the linear time buildHeap algorithm
instead.
What is the relevance of the buildHeap operation?
Insert the following sequence of items {6, 7, 8, 10, 14, 19} into a
min-max heap.
BuildHeap vs Sequential Insertion
What is the relevance of the buildHeap operation?
***The build heap operation takes a heap that violates the
ordering property and reinstates it. Furthermore, it reduces the
cost of insertion from O(NlogN) to O(N). So buildHeap achieves
linear time operation.
1 1
3 2 2 5
10 5 12 7 3 10 7 12
Notice that at the end of the 2nd iteration there are no more changes
made to any of the vertices, even though we are supposed to do |V| -
1 iterations. So we can immediately terminate the process because
subsequent iterations would not cause any further update.
Exercise – In Class
Consider the graph shown below, compute the shortest path from
5
P to T
-2 R Note: |V| - 1 = 5 - 1= 4
Q
This means that we need to do the
6 iteration 4 times in order to compute
8 -4 -3
7 the shortest path.
P 2
7
S T
9
Vertex P Q R S T
P 0P 6P ∞ 7P ∞ First
7P 2Q Iteration
Q 6P 11Q
T 9T 7P 2Q
S 4S 7P
R 2R 4S
Exercise In Class - Solution
Consider the graph shown below, compute the shortest path from
P to T 5
2 4
Q -2 R
6
0 8 -4 -3
P 7
2
7 -2
7
S T
9
Vertex P Q R S T
P 0P 2R 4S 7P 2Q Second
4S 7P Iteration
Q 2R -2Q
T 4S 7P -2Q
S 4S 7P
R 4S 7P
Exercise In Class - Solution
Consider the graph shown below, compute the shortest path from
P to T 5
4 Note: We can terminate the
2
-2 process at the 3rd iteration since
Q R there are no changes in the
6 2nd and 3rd iterations.
0 8 -4 -3
P 7
2 So shortest path from P to T is
7 2 P → S → R → Q → T: -2
7
S T
9
Vertex P Q R S T
P 0P 2R 4S 7P 2Q Third
4S 7P Iteration
T 2R -2Q
Q 4S 7P -2Q
R 4S 7P
S 4S 7P
Path Problems in Acyclic Graphs - Recall
Basically, the path problem in a DAG can be summarised as
follows:
“Given a directed acyclic graph G = (V,E) find the shortest
path (measured by total cost) from a designated vertex S
to every vertex in an acyclic graph. Edge costs are
unrestricted.”
U V
Topological Sorting - Recall
** A graph can have several topological orders
0 2
a a
d 2 d
c c
c c
1
a a
0 d d
c c