0% found this document useful (0 votes)
32 views18 pages

3) Algorithms, Data Structures and Computability

Uploaded by

bacexam229
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32 views18 pages

3) Algorithms, Data Structures and Computability

Uploaded by

bacexam229
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

*M2691906F1PV1*

M2691906F1PV1

M269/K

Module Examination 2019

ALGORITHMS, DATA STRUCTURES


AND COMPUTABILITY

Friday, 14 June 2019 2:30 pm – 5:30 pm

Time allowed: 3 hours

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.

At the end of the examination


Check that you have written your personal identifier and examination
number on each answer book used. Failure to do so will mean that
your work cannot be identified.
Examination number

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.

Copyright © 2019 The Open University


PART 1 Answer every question in this Part.
Answers to questions in this Part should be written on this paper in the spaces
or on the lines provided, or in the case of multiple-choice questions you should
tick the appropriate box(es). If you tick more boxes than indicated for a
multiple-choice question, you will receive no marks for that question.
Use the provided answer books for any rough working.

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:

2 M269 June 2019


Question 3 You are given two data structures. The first is an unordered list of integer values
(6 marks) without duplicates, the second is an ordered list of integer values without
duplicates.
Here are examples of such lists, you will not need to use these lists to answer the
question:

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 ]

For a list with n items


(a) What are the best-case and worst-case Big-O complexities of a linear
search over an un-ordered list?
(b) What are the best-case and worst-case Big-O complexities of a binary
search over an ordered list?

Best case Big-O Worst case Big-O


Linear search over un-
ordered list with n items.
Binary search over ordered
list with n items.

In each of the four cases, briefly explain your answer:

M269 June 2019 TURN OVER 3


Question 4 A Python program contains a loop with the following guard
(5 marks) while (not (i == 3) or not (j < 2)) and (i == 3):
...
Complete the following truth table, where:
P represents i == 3
Q represents j < 2

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.)

A.  It simplifies to not (j < 2).


B.  The loop will never be entered.
C.  The loop will never terminate.
D.  It simplifies to (i == 3) and not (j < 2).
E.  It simplifies to i == 3.

4 M269 June 2019


Question 5 Consider the diagrams in A–H, where nodes are represented by dots and edges
(4 marks) by arrows. The numbers are the keys for the corresponding nodes.
Note that in diagrams C and D the arrows in the bottom level show leaf 3 to be to
the left of its parent node (a left branch), and leaf 1 to be to the right of its parent
node (a right branch).

On each line, write one or more letters, or write "None".

(a) Which of A, B, E and F, if any, are not a tree? ____________

(b) Which of E, F, G and H, if any, are binary trees? ____________

(c) Which of B, C, F and G, if any, are complete binary trees? ____________

(d) Which of A, D, E and H, if any, are a heap? ____________

M269 June 2019 TURN OVER 5


Question 6 Consider the following function, which takes a non-negative integer as an
(6 marks) argument.
def evaluate(n):
a = 1
b = 0
c = 0
for i in range (n):
a = a + i
for j in range (n):
a = a + j
b = b + a
for k in range (n):
a = a + k
b = b + a
c = c + a + b
resolution = (c * b) / a
return resolution

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.

6 M269 June 2019


Question 7 (a) Which one of the following statements about hash tables is true? (Tick one
(4 marks) box.)

A. Hash tables don’t always require key values to be unique.


B. Linear probing always examines every storage location in a hash table.
C. The load factor is a measure of how many collisions have occurred
when using a hash table.
D.  A collision occurs when two key values map to the same location in a
hash table.

(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

M269 June 2019 TURN OVER 7


Question 8 a) Draw the binary search tree that results from the insertion of the following
(6 marks) values, in order, in an initially empty tree: 55, 59, 34, 29, 86, 65, 68.
b) For the tree you produce, annotate each node with its balance factor and so
explain whether the tree you produced is balanced or unbalanced.

8 M269 June 2019


Question 9 (a) In Python, a dictionary of dictionaries can be used to represent a directed
(4 marks) graph’s adjacency lists.

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.

(b) Complete the following statements about your graph.

The graph is ____________________ (choose from Cyclic/Acyclic) because

The graph is ____________________ (choose from Sparse/Dense) because

M269 June 2019 TURN OVER 9


Question 10 Consider the following undirected graph:
(3 marks)
3
5 6

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:

10 M269 June 2019


Question 11 In propositional logic, what does it mean to say that two well-formed formulae are
(5 marks) ‘equivalent’?

Are the following well-formed formulae equivalent to each other?


( P  ¬Q )
¬( ¬P  Q )
Justify your answer by showing appropriate truth tables.

M269 June 2019 TURN OVER 11


Question 12 Consider the following particular interpretation I for predicate logic allowing facts
(6 marks) to be expressed about boats, their homeport and the ports at which they have
docked.
The domain of elements is D = { Dover, Poole, Portsmouth, Plymouth,
Endeavour, Serenity, Adagio }.
The constants dover, poole, portsmouth, plymouth, endeavour, serenity and
adagio are assigned to the corresponding elements.

Two predicate symbols are assigned binary relations as follows:


I(homeport) = { (Endeavour, Dover), (Serenity, Plymouth),
(Adagio, Dover) }
I(has_docked_at) = { (Endeavour, Poole), (Endeavour, Dover),
(Endeavour, Plymouth), (Serenity, Plymouth),
(Serenity, Portsmouth), (Adagio, Dover),
(Adagio, Plymouth) }
For both relations, the first element of each tuple is the boat’s name and the
second is the port’s name.
(a) Consider the English sentence:
“There is at least one boat that has a home port of Dover and that has
docked at Poole.”
Write this as a well-formed predicate logic formula.

(b) This formula is ______________ (choose from TRUE/FALSE), under the


interpretation given above.

12 M269 June 2019


(c) Explain your answer to (b) in the box below. You need to consider any
relevant values for the variables, and show, using the domain and
interpretation on the previous page, whether they make the formula TRUE or
FALSE. In your explanation, make sure that you use formal notation. For
example, instead of stating ‘Serenity hasn’t docked at Poole’ you would
need to write (Serenity, Poole)  I(has_docked_at).

(d) Give an appropriate English translation of the well-formed formula:


X.( has_docked_at(X, poole)  homeport(X, dover)) .

M269 June 2019 TURN OVER 13


Question 13 The interpretation of the previous question can also be represented by a
(4 marks) database containing the following tables and data:

homeport has_docked_at

boatname home name port


Serenity Plymouth Endeavour Poole
Adagio Dover Endeavour Dover
Endeavour Dover Endeavour Plymouth
Serenity Plymouth
Serenity Portsmouth
Adagio Dover
Adagio Plymouth

(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;

(b) Write the request that the above query is answering.

14 M269 June 2019


Question 14 Consider the statements (numbered 1-4) below and write the letter of the most
(4 marks) appropriate term (labelled A-E) into the underlined empty spaces.
1. A decision problem that is not computable is: ________________.
2. A tractable decision problem is: ________________.
3. A computational problem with co-domain {0,1} is: ________________.
4. The inability to re-write a program and know if it behaves the same as the
original program is a consequence of: ________________.

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 15 Consider the following computational problems (numbered 1-5):


(4 marks)
1. Deciding whether or not a given integer is even.
2. Finding the largest value in an unsorted list of integers.
3. Deciding whether a given algorithm will terminate for every possible input.
4. The decision version of the travelling salesman problem.
5. The equivalence problem.
On each line below, write one or more of the above problem numbers, or write
"None".
A: Which problems, if any, are computable?___________________________
B: Which problems, if any, are definitely in the class P? _________________
C: Which problems, if any, are in the class NP? ________________________
D: Which problems, if any, are NP-complete?__________________________

M269 June 2019 TURN OVER 15


PART 2 Answer every question in this part. Answers to this part must be written in the
separate answer books, which you should also use for your rough working.

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)

16 M269 June 2019


(b) A list is a palindrome if the list is the same as its reverse.
So, [1,2,3,2,1] is a palindrome, ['a', 'b', 'b', 'a'] is a
palindrome, [1] is a palindrome, [['Fred'],['Freda'], ['Freya']]
is not a palindrome.
(i) Specify the problem of deciding if a given list is a palindrome. You may
write the specification in English and/or formally with mathematical
notation.
Name: palindrome
Inputs:
Preconditions:
Outputs:
Postconditions:
(ii) Give your initial insight for an algorithm that uses a stack to solve the
problem. You must use some or all of the Stack ADT’s operations
(pop, push, new, and is_empty). You can also use any of the basic
list operations: create an empty list, append an item, calculate the
length, access an item by position (indexing), iterate over the items or
over the indices, check if two lists are equal.
(8 marks)
(c) A training company, TComp, offers short courses to members of the public.
Someone seeking information on a course on the TComp website will be
shown a list of ‘prior knowledge courses’ (which we will abbreviate to ‘prior
courses’ from now on). TComp needs to hold information about the
courses, and which are prior courses for each other.
So, course X has prior courses A, B and C, is TComp’s way of saying that to
do course X you should already know about the content of courses A and B
and C. Those prior courses may also have prior courses, and a course may
be a prior course for more than one other course.
When a customer applies for a place on a course, they complete an
application form on the website. This is sent electronically to a processing
centre in which the applications are processed in the order they arrive.
Courses have a limited number of places, so places are allocated to each
course in the order the processed applications are received until the course
is full. Any remaining applications are stored in case vacancies arise later.
State, and briefly justify, the choice of abstract data types that you could
adopt or adapt to represent:
(i) the course descriptions and the links to their prior knowledge courses;
(ii) the applications submitted for places on the courses.
State explicitly any assumptions you make.
(5 marks)

M269 June 2019 TURN OVER 17


Question 17 A TV programme is planning to screen a public lecture on aspects of
(15 marks) Computability. To help shape the lecture, you’ve been asked to prepare a short
report for the producers on the topics ‘decision problems’ and ‘undecidability’
with a particular focus on the equivalence problem.
You should assume that the producers do not have a background in computer
science and that the programme’s intended audience is the general public.
Your report must have the following structure:
1. A suitable title and a short paragraph defining computability.
2. A paragraph introducing decision problems.
3. A paragraph in which you describe the issue of undecidability.
4. A paragraph describing how undecidability relates to the equivalence
problem.
5. A summary that describes why the equivalence problem is important in
computing.
Some marks will be awarded for a clear coherent text that is appropriate for its
audience, so avoid unexplained technical jargon and abrupt changes of topic,
and make sure your sentences fit together to tell an overall ‘story’. As a guide,
you should aim to write roughly two to five sentences per paragraph.

[END OF QUESTION PAPER]

18 M269 June 2019

You might also like