F28da - May - P2021-2022 - TZ1
F28da - May - P2021-2022 - TZ1
Semester 2 – 2021-2022
1. Topic: graphs
Let G be an undirected graph whose vertices are the letters from A to G, and let the
adjacent vertices of each vertex be given by the table below:
Vertex Adjacent vertices
A B, C
B D, G
C A, D
D B, C, E, G
E C, D, F
F E, G
G B, D, F
(a) Give the mathematical definition of the graph G in terms of the set of vertices V and
the set of edges E.
[4 marks]
C D C D
E F G E F G
γ: A B δ: A B
C D C D
E F G E F G
(i) For each of the four graphs α, β , γ, and δ , indicate if it represents a spanning
tree resulting from a depth-first traversal of G starting at vertex A, if it rep-
resents a spanning tree resulting from a breadth-first traversal of G starting at
vertex A, and give the rationale to your answer.
Page 1 of 4
This paper contains three questions.
[8 marks]
(ii) Could DFS traversals of G starting from vertex A result in different spanning
trees? Could BFS traversals of G starting from vertex A result in different
spanning trees? If yes, give examples not found among α, β , γ, and δ . If no,
explain why not.
[3 marks]
[3 marks]
(d) Does the graph G have an Eulerian cycle? Justify your answer.
[2 marks]
Page 2 of 4
This paper contains three questions.
2. Topic: hashing
Consider a hash table of size 11, storing integers using the hash function h(x) = x mod 11
with open addressing and linear probing. (NB: x mod 11 is the remainder of the division
of x by 11.)
(a) Starting with the empty table, sketch the state of the map after the following numbers
were inserted in the order listed: 20, 13, 31, 10, 9. Which elements experienced
collision during the insertion attempt? How many probes were necessary when
inserting the number 9?
[8 marks]
(b) What is the load factor of the has table after executing the operations from question
2a? What will be the expected number of probes if we wanted to insert another
element into the table?
[2 marks]
(c) Given the state after executing the operations from question 2a, how many probes
will be needed when searching for value 10? How many probes when searching for
value 11?
[2 marks]
(d) Starting from the state after executing the commands in question 2a, sketch the state
of the hash table after removing the value 10 from the table. After the removal,
how many probes are needed when searching for value 10? How many probes when
searching for 9? Did you need to introduce a special value to execute removal, and
if yes, why?
[5 marks]
(e) What would the hash table look like if instead of open addressing, we wanted to
use separate chaining? Sketch the state of the hash table after executing the same
insertions and removals described in questions 2a and 2d.
[3 marks]
Page 3 of 4
This paper contains three questions.
(a) (i) Describe (using pseudocode) an algorithm, isDescending which tests, in O(n)
time, if a sequence of length n is sorted in the descending order. Give a brief
explanation why the algorithm is of the correct time complexity.
[4 marks]
(ii) Give an example input of length 8 for which isDescending reaches its worst
case execution time. What is the reason the given example causes the algorithm
to run for the longest possible time?
[2 marks]
(iii) Give an example input of length 8 for which isDescending reaches its best
case execution time. What is the reason the given example causes the algorithm
to run for the shortest possible time?
[2 marks]
(b) (i) When starting the quicksort algorithm on the sequence pictured below, what
would be the best and the worst choices for the initial pivot element? Explain
your answers.
6 9 18 7 4 3 12 11
[6 marks]
(ii) Sketch a possible way the values in the sequence pictured above would get
rearranged around the pivot, if the chosen pivot was the value 11. (Assuming
the sorting is done in the ascending order.)
[1 mark]
(c) Let A and B be two algorithms for solving a particular problem. The time complexity
of A is O(n2 ) and the time complexity of B is O(n3 ). However, for all practical
purposes B outperforms A. Give a possible explanation for such a situation.
[5 marks]
Page 4 of 4