CS 2210B Assignment 1: Concept assignment
Question 1:
Instead of proving n is not Big O of 1/n , we will use proof by contradiction and assume
that the claim is false.
Let’s say that n is O(1/n), there are constants where c > 0 and no such that
nscc's for all men
We will then simplify the inequality and multiply both sides by n:
M2 E C
In this case, for any value of C we choose, n will always be equal or less than C.
Speci cally, this inequality cannot be true for all values of n larger than some constant
n Therefore, we reached a contraction since there are no constant values c > 0 and
8
n > 1 such that n2 < c n for all n > n .
o
Question 2:
We need to nd constants c > 0 and n > 1 such that
o
f n xh n Ec gin x h n
Since h(n) is non negative, then h(n) < max( h(n) )
Let C C x max h al
h IC heh
f n x n x
gin x h n s Cragen x max
Since C and C xmax h al are both positive
For all men
f n xh n EC x hea C x x match
al
gn x max
gn
Therefore, f(n) x h(n) is O(g(n) x h(n)) since the inequality of f(n) x h(n) < C x g(n) x h(n) is true,
Question 3:
Algorithm: SymmetryChecker(L)
Input: Array L of size n
Output: 1 if true that the array is fully symmetrical or -1 for false.
Notes: this algorithm uses 2 for loops to compare 1 pair of integer ( mpair meaning main pair)
to all other pairs of integer (cpair meaning compared pair). They will check if the pair has a
symmetrical pair and break the inner loop if found, or loop through the entire list if not found. It
then moves to the next pair to determine whether it is symmetrical or not.
For i=0 to n–1 by 2
Set mpair = [ L[i], L[i+1]]
Ic Oct
For j = 0 to n – 1 by 2
Set cpair = [L[j], L[j+1]
If mpair[0] == cpair[1] && mpair[1] == cpair[0]
Set isSymm = true
Exit inner loop Else
else
Set isSymm = false
C2 012
If isSymm == true
Return 1
Else
Return -1
3 OCI
n
a)
For my particular algorithm, the worse case is when the list is not fully symmetric or becomes fully
symmetric towards the end of the list. In this case, my algorithm needs to iterate through the entire list
every time it compares a pair.
b)
If we assign the time complexity of the outer loop as C1 and the inner loop as C2 and the
return condition as C3, we will notice that both loops will run n/2 times, iterating over the list in
steps of 2 elements and C3 is of time complexity O(1). Within both loops, the operations for
comparing the elements and setting the Boolean isSymm has the time complexity of O(1)
While the overall time complexity can be approximated as O((n/2) * (n/2)) = O (n^2/4) = O(n^2),
the best case time complexity is much faster at O(n). This can be done if the algorithm
determines that the list is not fully symmetric in the early iterations of the outer loop. For
example, if the rst mpair does not have a symmetrical pair in the list, the inner loop (C2) would
iterate n/2 time, while the outer loop (C1) would be O(1) and C(3) would also be O(1). And since
n/2 is in O(n), we can say the best case time complexity for this algorithm is O(n).
Question 4:
Notes:
This algorithm uses a for loop and a comp_num (steadily increases from 1 depends on
condition) to compare with the list of integers. It then has several if statements to check for
di erent conditions:
1. If they are equal, do nothing (empty block)
2. If the integer in the list is one number higher than comp_num, increase comp_num to
match
3. If there are missing integers between two number in the list, add missing integer to
missing_num ( an array of missing numbers), increase comp_num
4. Else, add missing integer to missing_num; this covers the case that initial numbers are
missing.
A. For example, 4,5,6,7,8,9,9,9 ; missing initial 1, 2, 3
Algorithm: ndMissingNum(L)
Input: Integer array L of size n
Output: An array list of all missing integers (missing_num) or empty list if none are missing.
Set comp_num - 1
I CI OCD
For i from 0 to n-1
if num_list[i] == comp_num
(empty block)
else if ( i >= 1 && num_list[i-1] == comp_num && num_list[i] == comp_num +1)
comp_num + = 1
if (( i ==0) && (comp_num == 1) && (num_list[i] != 1)
add comp_num to missing_num
C2
else if ( i > = 1 && num_list[i-1] == comp_num && num_list[i] != comp_num +1)
comp_num += 1
Oln
for j from comp_num to num_list[i] - 1
I C3 0 n
add comp_num to missing_num comp num
comp_num + = 1
else
add comp_num to missing_num
comp_num += 1
Return missing_num
14 OCI
b)
The worse case time complexity of the algorithm is O(n^2) where n is the size of the num_list
and best case time complexity is O(1).
This code includes a single loop that iterates through the num_list, each iteration, the
conditional checks and operations are of time complexity O(1). However, that is the best case
in which no missing number appeared between 2 integers from the list. In this case, another
inner loop will be called making the worse case time complexity O(n^2).
c)
L = [1,1,3,3,4,6,6,6,9,9]
i num listed comp num missing num
E I
o 1 I I J
1 I I I I
2 3 missing I I I
3 3 3 12 J
4 4 3 12 I
5 6 missing 4 I 21
6 6 6 52,51
7 6 6 52,51
8 9 Egging 6 5251
9 9 9 52,5 7,81
Correct algorithm which returns 2, 5, 7, 8 as missing numbers.
Another example:
L = [ 4,4,5,5,5,6,6,6,10,10]
i num list fit comp num missing num
O 4 missing I I I
23
1 4 4 I 1,231
2 5 4 51,231
3 5 5 1,231
4 5 5 51,231
5 6 5 51,231
6 6 6 51,231
7 6 6 51,231
8 10 51 6 51,231
9 10 10 11,213,7 8,91
This example handles special case where the rst integer in the list starts with missing number.
Regardless, the algorithm produces the correct output with the array 1,2,3,7,8,9 as missing
number.