3) Algorithms, Data Structures and Computability
3) Algorithms, Data Structures and Computability
M2691906F1PV1
M269/K
There are TWO parts to this examination. You should attempt all
questions in both parts.
Part 1 carries 65 marks. Answers to this part must be written in the
spaces provided on this examination paper.
Part 2 carries 35 marks. Answers to this part must be written in the
answer books.
Although Part 1 carries more marks, it consists of short questions that
can be answered quickly. We therefore advise you to apportion more
time for Part 2, e.g. 80 minutes for Part 1, 90 minutes for Part 2 and 10
minutes for checking your answers.
Personal identifier
Attach this examination paper to the front of the answer books you have
used for Part 2, put your signed desk record on top and fix them all
together with the fastener provided.
This question paper must not be removed from the examination room;
you must attach it to your answer book(s) at the end of the examination.
Question 1 Which one of the following statements is true? (Tick one box.)
(2 marks)
A. A specification of an algorithm in structured English is a formal
description of the inputs and outputs for that algorithm.
B. A problem is computable if it can be expressed as an algorithm.
C. An algorithm will always terminate.
D. A problem for which the result is yes or no, is a decision problem.
Question 2 Complete the following diagram by filling the empty boxes labelled A, B and C
(2 marks) with the appropriate terms from the following list:
Abstract Data Type, Data, Data Structure.
A: B:
is
encapsulated implements
by
C:
Unordered List: [ 17, 99, 25, 31, 39, 77, 83, 89, 41, 52, 67, 69, 2, 16, 91 ]
Ordered List: [ 2, 16, 17, 25, 31, 39, 41, 52, 67, 69, 77, 83, 89, 91, 99 ]
P Q ¬P ¬Q ¬P ¬Q (¬P ¬Q) P
F F
F T
T F
T T
Use the results from your truth table to choose which one of the following
statements is true about the above guard.
(Tick one box.)
From the options below, select the correct combination of T(n) and Big-O
complexity for this function. You may assume that a step (i.e. the basic unit of
computation) is the assignment statement.
(Tick one box for T(n) and one box for Big-O complexity.)
A. T(n) = 3n3 + 2n2 + n + 4 I. O(n)
B. T(n) = 3n3 + 2n2 + n + 3 II. O(n2)
C. T(n) = 3n2 + 2n + 3 III. O(3n2)
D. T(n) = 3n2 + 2n + 4 IV. O(n3)
E. T(n) = n + 10 V. O(3n3)
Explain how you arrived at T(n) and the associated Big-O complexity.
(b) Calculate the load factor for the hash table below. Explain and show your
working.
0 1 2 3 4 5 6 7 8
Germaine Anu Eric
Directedgraph1 = {
0: {'neighbours': [1,4] },
1: {'neighbours': [3,4] },
2: {'neighbours': [] },
3: {'neighbours': [0] },
4: {'neighbours': [2,1] },
5: {'neighbours': [] }
}
Draw the graph that corresponds to the adjacency list given above.
2 1
4
In the space below, draw one of the spanning trees that could be generated from
a Breadth First Search (BFS) of the above graph, starting at vertex 1:
homeport has_docked_at
(a) For the following SQL query, give the table returned by the query.
SELECT home, boatname
FROM homeport CROSS JOIN has_docked_at
WHERE home = port AND boatname = name;
List of terms:
A: a decision problem,
B: the totality problem,
C: an undecidable problem,
D: the equivalence problem,
E: a class P problem.
Question 16 The Stack Abstract Data Type describes a data structure in which items are
(20 marks) available in last-in, first-out order. Items are pushed onto the top of a stack and
popped off the top. The ADT for a Stack includes these operations:
new, which creates a new, empty stack.
push, which adds an item to the top of a stack.
pop, which removes and returns the top item from the stack.
is_empty, which tests to see if the stack is empty.
(a) The following stand-alone Python function accepts a stack and returns a list
with the stack’s items but in reverse order. It assumes the ADT is
implemented as a Python class, i.e. using Stack()as the new operation.
def reverse(in_stack):
inverted_stack = Stack()
while not in_stack.is_empty():
inverted_stack.push(in_stack.pop())
reversed_list = []
while not inverted_stack.isempty():
reversed_list.append(inverted_stack.pop())
return reversed_list
(i) What is the Big-O complexity of the reverse function for an input
stack with n items? Explain your answer. Take each stack and list
operation as a basic step of computation.
(ii) If the reverse operation is added to the Stack ADT it would be a
modifier. Explain why.
(iii) What would you change in order to make the reverse operation an
inspector? Don’t write code, just describe the changes needed.
(7 marks)