CNS 2102 - Data Structures and Algorithms - July 2022
CNS 2102 - Data Structures and Algorithms - July 2022
a. For each functions f(n) below, give an asymptotic upper bound using “big-Oh” notation. (6
Marks)
i. f(n) = 100n3 − 7n 3 + 14n2
ii. f(n) = 100 − 100n3 + 7n2
iii. f(n) = log (7n 2)
iv. f(n) = .001n + 100 ꞏ 2 n
v. f(n) = n3 (1 + 6n + 2014n2)
vi. f(n) = (log n) x (n + n2)
b. Evaluate the following. (ab*cd/+) where a=3, b=5, c=8, d=2. (4 Marks)
c. Explain Data abstraction? (2 Marks)
d. A loop invariant is some predicate (condition) that holds for every iteration in the loop.
Discuss the three properties of a loop invariant. (3 Marks)
e. Distinguish between iterative algorithms and recursive algorithms with regards to Execution
Approach, Time Efficiency and Usage. (2 Marks)
f. Depth first traversal (DFT) can be used to perform various tasks including network analysis.
In full detail, describe the DFT algorithm. (4 Marks)
g. Describe any three differences between Arrays and Vectors (3 Marks).
h. Complete the bellow Java code by writing a method that can be used to add a new node with
a string data type at the head position of a linked-list. Ensure you name the method ‘element
_insert’ (4 Marks)
QUESTION 2 15 Marks
a. An algorithm can be classified as either totally or partially correct. Explain the difference
between these two classifications. (2 Marks)
b. Use the Principle of Mathematical Induction to verify that, the nth Odd number is given by,
2𝑛 1 . (6 Marks)
c. State the formal Big O definition and show that: 4x3 + 12 is O(x3) (4 Marks)
d. Consider the table below with variable k and pointer i
Name Value Memory Address
i (pointer) 000RT4 0000RQ
k 200 000RT4
Given the following sequence of commands, find the final value of integer variable k. Show
your working. (3 Marks)
*i = *i + 100
*i = k - 150
k = *i / 50 + k
QUESTION 3 15 Marks
a. Fill in the table below using the relevant Space Complexity notations for each of the listed
algorithms (3 Marks).
b. The Java code snippet bellow shows how the core logic of a Bubble sort algorithm can be
implemented to achieve a complexity of O(n2). Discuss the drawbacks of this approach and
suggest one way of improving the implementation. (3 Marks)
c. Explain why Selection Sort and Insertion Sort algorithms are considered improvements of
Bubble Sort? (2 Marks)
d. Using relevant example, discuss the difference between an In-Place and Out-of-Place
algorithm? (4 Marks)
e. In a step-by-step order, list the steps involved in a Quick Sort algorithm. (3 Marks)
QUESTION 4 15 Marks
a. Describe any two applications of Breadth First Search. (2 Marks)
b. Explain the conditions for implementing Dijkstra’s algorithm? (2 Marks)
c. Consider the non-linear structure below.
7 6
Source P Q R
3
2 1 6
S T
2
i. Apply Dijkstra’s algorithm to find the shortest distances from the source to each node
in the table below. (5 Marks)
ii. State the shortest path to get to node R, assuming node R acts as a destination. (1 Mark)
d. Create an adjacency matrix to represent the undirected-weighted graph given below. (5 Marks)
QUESTION 5 15 Marks