You are on page 1of 26

CSC2001F: General Revision

Omowunmi Isafiade

Email: omowunmi.isafiade@uct.ac.za

Office: Room 306


Outline
General Revision

Class Exercises

Hash Table

Priority Queue

Graphs and Paths

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

In quadratic probing hash table, suppose the first three attempts


made to insert a key into a table failed. Assume a hash function of k
mod m, table size is 10 and key to be inserted is 53.
(a) What location would be considered at the next probe.
(b) What could have been responsible for the failed attempts?
(c) Any suggestions for resolving the problem?
Hash Table
Is it possible for a load factor to be greater than 1? Discuss briefly?
What can be done when the table is too small and we can no longer
insert an item?
Give the contents of the hash table that results when you insert the
following keys “B E H” in that order into an initially empty linear
probing table of size 5. Use the hash function 3k mod M to
transform the kth letter of the alphabet into a table index, e.g., hash
(F) = hash (6) = 18 mod 5 = 3.
Revision Exercise
Why is rehashing useful?
Suppose we need to store information about 100 students in a class
and that d1 and d2 represent the first and second digits of a student
ID respectively. Provide an explanation for why using the hash
function 2(10d1 + d2) is a better choice over using d1 + d2?
Revision Exercise
A telecommunication company wants to implement caller ID
capability (i.e identification of users). Suppose the range (r) of
possible phone numbers is 108 – 1. There are k phone numbers
currently in use by the company's customers (i.e k << r). Advise the
company on the appropriate data structure to adopt.
Priority Queue - Exercises
Discuss with illustration examples the two key properties associated
with a binary heap
Differentiate between single-ended and double-ended priority queues
A d-heap is similar to a binary heap, except that nodes have d-
children. Suppose d = 3, insert the following items into a d-heap
(assume min-heap). {6, 11, 13, 4, 2, 9, 1}
Show the result of two deleteMin operations on the input in the
resulting d-heap
Determine the running times (in terms of d and N) of the insertion and
DeleteMin operations for a d-heap
In a binary heap, for an item in the ith position, where are the parent,
left child and right child located? If you translate this to a d-ary heap
what result do you get?
Priority Queue

A d-heap is similar to a binary heap but has d maximum number of


children. Suppose d = 3, insert the following items into a d-heap
(assume a min-heap). {6, 11, 13, 4, 2, 9, 1}

1 If you follow the procedure


correctly you should get the
6 following d-heap.
2 13
Remember this is a d-heap
not a binary-heap (d = 3)
11 9 4
Priority Queue
A d-heap is similar to a binary heap but has d maximum number of
children. Suppose d = 3, insert the following elements into a d-heap
(assume a min-heap). {6, 11, 13, 4, 2, 9, 1}
Show the result of two deleteMin operations on the input in the d-
heap
1 1st deleteMin 2 2nd deleteMin 4

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

Result: sequential insertion Result: buildHeap


Priority Queue - Heapsort

Describe in detail how a set of items can be sorted more efficiently in


increasing order using heap sort (use illustrations as necessary)???

The sequence BA*E* is applied to an initially empty priority queue,


where a letter means insert and an asterisk means removing the
minimum. Consider that the elements are evaluated in terms of
alphabetical order where “A” is the minimum and “Z” the maximum.
Show the binary heap that is obtained by applying this algorithm
(sequence) using a min heap
Bellman-Ford Algorithm – Exercise In Class
Note that the results of the first iteration became our starting
values in the second iteration
A 5 B
3
2 4
-1 Note that this is the
second iteration from the
outer loop (|V| -1 times) in
C D the Bellman Ford algorithm
6
Vertex A B C D
Second
A 0A 5A 2A 8C Iteration
C 5A 2A 8C
B 5A 8C
D 8C

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

Note: Even though a particular method is not specified, you should


know that the appropriate technique to use is the Bellman-Ford
algorithm since negative edge weights exist in the graph
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 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.”

Recall: a directed acyclic graph has no cycles


This implies there is nothing like negative-cost cycles, since
there are no cycles in the first place
Path Problems in Directed Acyclic Graphs
Topological Sorting (Recall):
“A topological sort orders vertices in a directed acyclic graph
such that if there is a path from u to v then v appears after u in
the ordering”
A graph with a cycle cannot have a topological order
Because for two vertices u and v on a cycle, there is a path from
u to v and from v to u
So any ordering of u and v would contradict one of the 2 paths

U V
Topological Sorting - Recall
** A graph can have several topological orders

Topological sorting works by scanning a graph to progressively


reduce the number of incoming edges to a vertex to 0

Definition: “The indegree of a vertex v is defined as the number of


incoming edges (u,v)”
In this graph, v has an
indegree of 1 and u an
indegree of 0
U V
Topological Sorting Algorithm - Recall
1) For all vertices in the graph, compute all the indegrees
2) Find the vertices with indegree of 0
3) If none exist then we have a graph with a cycle
4) Else remove the vertices with indegree of 0 as well as their edges

Note: In real-life applications removing the edges really just means


reducing the count of the number of incoming edges for each vertex
adjacent to the one with indegree 0
“The algorithm produces the correct result and detects cycles if the
graph is not acyclic”
Topological Sorting Algorithm - Example
1
Recall
b b

0 2
a a
d 2 d

c c

Graph labelled with associated


Initial State of Graph indegree per vertex

Note: the indegree of a vertex is the number of incoming edges (u,v)


Topological Sorting Algorithm - Example
Recall 1 0
b b
0
2 2
a a
2 d 1 d

c c

Removing edges to b and c Removing edges to c and d

Note: repeatedly and logically removing vertices that have no incoming


edges
Topological Sorting Algorithm - Example
Recall
b b

1
a a
0 d d

c c

Removing edges to d All vertices are finally sorted

***A cycle is guaranteed to exist if


Topological order: a, b, c, d at any point there are unseen
vertices and none of them
have an indegree of 0
Bellman Ford – Identifying Negative Cycle
Extending the notion of topological sorting to the identification of
negative cycle in a shortest path problem
1. Compute the indegree of each node
2. If there is no node with an indegree of zero, then a cycle exist
3. Else progressively remove nodes with indegree 0 to identify where
the cycle is...
4. Then compute the cost of the cycle (if < 0, then a negative-cost
cycle exist)
Recall: a cycle is not a problem but a negative cycle is. So you
need to compute the sum of the edge weights in the cycle before
you can reach a conclusion. ***A cycle is guaranteed to exist if
at any point there are unseen
vertices and none of them
have an indegree of 0

You might also like