CSCM043 Computational and Mathematical
Methods
Programming Strand
Coursework Part 5: Asymptotic Analysis
This final sheet of your Scientific Programming coursework challenges you to find the
complexity, i.e. T() function and O() order of various algorithms. In some cases, there
might be different ways to approach the problem, and you should use your own
discernment.
THE DEADLINE IS 16TH DECEMBER 2022
SUBMIT VIA ON MOODLE TURNITIN TO “SP Coursework
Part 5”.
In this exercise, unless otherwise stated, assume the following mapping between
operation/function and number of time units (T.U.):
(TOTAL 50 MARKS)
Op./Func. T.U
.
Addition 3
Multiplication 10
Assignment 1
f 8
g 30
1. Calculate the time complexity T(n) and asymptotic complexity O(.). Briefly discuss
its order of complexity. (5 MARKS)
a := 2
b := 3
c := a + b
For i = 1 to n, do,
a := a * b
c := c ^ 4
End For
ANS
1) (d) Program P3 is slower than P1 and P2 for very large inputs.
2) (E) f(n) = log n, g(n) = 1/n, all others can be made O(g(n)) and g(n) is O(f(n))
3) (B) O(n) time: Because it may happen that all the keys are mapped to Same Row and
Chained across
4) (E) O(n^3) [because foo runs for n times and for n times foofoo runs n^2 times, So
n^3]
5) (E) It computes the number of internal nodes in the tree. [For Leaf it returns 0 and for
other nodes its 1, so Number of internal nodes is the answer]
7) (B) If ki is the largest key, then in every binary search tree storing the above set of
keys, the right child of the node storing ki is a leaf because that largest element will be at
the bottom right and it will always be a leaf
8) (B) c is definitely at distance 3 from root because b is root, its left is g and g's right is a
Now if we see In order it says c is left of a and it will be at distance 3
2. Compute T(n) and O(.) for the following algorithm. Briefly discuss its order of
complexity. (5 MARKS)
p := 10
p := f(p)
q := p squared
For i = 1 to n, do,
For j = 1 to n, do,
q := f(q)
p := p – 1
End For
End For
result := g(p, q)
3. Compute T(n) and O(.) for the following algorithm. Briefly discuss its order of
complexity. (5 MARKS)
p := 50
n := 10
For j = 1 to n-1, do,
a := p – q
For i = 1 : n-1, do,
q := p * n * n
End For
End For
4. Compute T(n) and O(.) for the following algorithm. Briefly discuss its order of
complexity. (5 MARKS)
x := 10
For j = 1 to x
x := x + 1
y := x cubed
For i = 1 to n-1
(x, y) = f(x, y)
End For
End For
5. Given that Operation 1 takes 15 time units and Operation 2 takes 9 time units,
compute T(n) and O(.) for the following algorithm. Briefly discuss its order of
complexity. (5 MARKS)
For i = 1 to n*log(n)
For j = 1 to i
Do operation 1
Do operation 2
End For
End For
6. Compute T(n) and O(.) for the following algorithm. Briefly discuss its order of
complexity and any issues with space complexity. (5 MARKS)
m := all possible permutations of 1,…,n
x := 1
For i = 1 to m
x := 2x
End For
7. Observe Algorithms A and B below and do the following:
1. Asymptotically analyse Algorithm A.
(3 MARKS)
2. Asymptotically analyse Algorithm B.
(3 MARKS)
3. Calculate which of the two algorithms is better for large values of n, using the
graphical method. As part of your answer, you should identify the approximate
size (n) of the problem that delineates small and large.
(7 MARKS)
4. Calculate which of the two algorithms is better for large values of n, using the
algebraic method. As part of your answer, you should identify the exact size of
the problem that delineates small and large.
(7 MARKS)
Algorithm A Algorithm B
x := 10 x := 10
Array A[] := f(x) //80 T.U. Array A[] := f(x) //300 T.U.
Read in n elements into A. Read in n elements into A.
For i = 1 to n, do,
Do Operation 1 //10 T.U. For i = 1 to n, do,
Do Operation 2 //5 T.U. Do Operation 1 //5 T.U.
Do Operation 3 //20 T.U. Do Operation 2 //3 T.U.
End For End For