Assignment 2 - Article on
Alogrithms
Data structure (University of Central Punjab)
, lOMoARcPSD|3013804
COMP 3051: Algorithm Design and Analysis A2-1
Assignment 2
Instructions
According to our suggested 16 week “Course Schedule,” you should complete and
submit Assignment 2 by the Sunday at the end of Week 6. This assignment is worth
10% of your final grade. To receive full marks, answer each of the following questions
in a clear and comprehensive manner. You can find the assignment marking criteria at
the end of this document.
Questions
1. Show that if f(n) is O(h(n)) and g(n) is O(i(n)), then f(n) + g(n) is O(h(n) + i(n)).
[2 marks]
f(n) = O(h(n))
g(n) = O(i(n))
from the addition property of equality
f(n) + g(n) = O(h(n) + i(n))
2. Show that 3(n + 1)7 + 2n log n is O(n7). Hint: Try applying the rules of Theorem 1.7.
You will have to use the insert equations to answer this question. [2 marks]
According to rules of Proposition:
log n = O(n)
2nlog n = O(2n2)
3(n + 1)7 is a polynomial of degree 7, therefore it is O(n7)
3(n + 1)7 + 2nlog n = O(n7 + 2n2)
3(n + 1)7 + 2nlog n = O(n7)
3. Give an O(n)-time algorithm for computing the depth of each node of a tree T, where
n is the number of nodes of T. Assume the existence of methods setDepth (v,d) and
getDepth(v) that run in O(1) time. [2 marks]
depth (Tree, node):
if (Tree.isRoot(node)) then
setDepth(node, 0)