Professional Documents
Culture Documents
20UCS173 Assignment3
20UCS173 Assignment3
20UCS173 Assignment3
Q2)
Part a
To perform a simple long division operation and to calculate the total bit operations performed,
consider the divisor as b and the dividend as a.
Here a, is divided by b as a result of which quotient q and the remainder r are observed. There
are mainly three operations:
1) Division operation
2) 2) Calculating quotient operation
3) Calculating remainder operation
So, the total number of operations required to perform long division using paper and pencil
algorithm is equal to O((1+log(a))(log(b)))
Part b
For calculating remainder and swapping numbers total no. of bit operations required are
(1+log(a))(1+log(b))
For gcd(a,b)=(1+log(a))(1+log(b))
So for
gcd(a,b)->gcd(b, a mod b)
Ques 7
S is the set of integers ranging from 1 to n. Clearly it is a one-one objective mapping such that
f(n corresponds to x belonging to R.
Ques 8
Let us assume that m x n grid of boxes in 2D array of size m x n and each element of array is
box.
DAA Assignment 3
Pseudocode
1) Travel list using loop till n-2
2) 2) Compare ith element with (i+1)th element
3) If arr[i+1]<arr[i] then return value of i
In worst case, all the boxes will be opened till the second last box because the last box has
smallest number
b) if we suppose m=n then the size of array is n x n which is 2D array, here we will use
differential algo
In the worst case all the boxes will be opened till the second box because the last box has the
smallest number.
Ques 15
Phase 1: Cop C1 moves up or down till he can match his y coordinates with thief’s y coordinate
Similarly cop C2 moves right or left until he matches his x coordinates with the thiefs
When both the cops match their x and y coordinates with the thiefs we have a situation like the
one shown in figure
Note that this situation can be reached in less than n moves by alice
Phase 2:
From this situation alice can win in at most 2n turns the key is to maintain C1’s x coordinate and
C2’s y coordinate matched with thief’s, while getting close to him. Here’s the strategy
Assume that the t’s position is (x,y) if t stays still, the distance b/w c2 and t reduces by 1 is that
turn . If t moves down then the distance between c2 and t reduces by 2 since y is max distance
between c2 and t, we have
S+2D<= y
Theif’s y coordinate increases with each u and decreases with each d, since the grid size is n,
we have
y+U-D<=n
Hence the thief is caught in 2n moves after phase 1. Phase 1 takes n moves
Ques 18
Part a
Lets say that there are g<n/2 good chips and n-g bad chips
From this assumption we can always find a set of good chips G and a set of bad chips B of
equal size g since n-g>=g
Now assume that chips in B always conspire to fool the professor in the following
“For any test made by the professor, chips in B declare chips in B as good and chips in G as
bad”
DAA Assignment 3
Since the chips in G always report correct answers thus there exists symmetric behaviours it is
not possible to distinguish bad chips from good ones
Part B
Generalize the original problem to “Assume there are more good chips than bad chips”
Algorithm
1) Pairwise test them, and leave the last one alone if the number of chis is odd. If the report
says at least one of them is bad, throw both chips away. Otherwise throw one away from
each pair
2) Recursively find one good chip among the remaining chips. The recursion ends when
the number of remaining chips is 1 or 2. If there is only 1 chip left, then it is the good chip
we desire. If there are 2 chips left, we make a pairwise test between them. If the report
says both are good, we can conclude that both are good chips. Otherwise, one is good
and the other is bad and we throw both away. The chip we left alone at step-1 is a good
chip.
Explanation:
1) If the no. of chips is od, from assumption we know the no. of good chips must be greats
then the no. of bad chips we randomly leave one chip alne from the chips, in which good
chips are not less then bad chips
2) Chip pairs that do not say each other is good either have one bad chip or have 2 bad
chips, throwing them away doesn’t change the fact that good chips are not less than bad
chips
3) The remaining chip pairs are either both good chips or bad chips, after throwing one chip
away in every one of those pairs, we have reduced the size of the problem to at most
half of the original problem size.
4) If the no. of good chps is n more than that of bad chips, we just throw away the chip we
left alone when the no. of chips is odd. In this case the no. of good chips is at least one
more than that of bad chips, and we can eventually find a good chip as our algorithm
claims.
5) If the no. of good chips is exactly one more than that of bad chps, there are 2 cases:-
a) We left alone the good chip, and the remaining chips are one half good and one
half bad. In this case, all the chips will be thrown away eventually. And the chip
left alone is the one we desire.
b) We left alone the bad chip, there are mre good chips than bad chips in the
remaining chips. In this case, we can recursively find a good chip in the
remaining chips and the left bad chips will be thrown away at the end.
Part c
By the master theorem, we have T(n)=O(n) after finding a good chip , we can identify all good
chips with that good chp we just found in n-1 tests, so the total number of tests is
O(n)+n-1=O(n)