Sie sind auf Seite 1von 9

Assignment 1

1. Suppose that an algorithm A runs in time fA(n) = 2n2 + 7n and that an algorithm B runs in
time fB(n) = 45n + 4, for problems sizes of n. For what values of n is algorithm A faster than
B. That is, for what n is fA(n) < fB(n)?

Sol:
2

Algorithm runs in time given by expression fA(n)=2n +7n


Algorithm runs in time given by expression fB(n)=45n+4
For what values of n fA(n) < fB(n)

2n +7n < 45n+4


2

2n +7n - 45n+4 < 0


2

2n - 38n 4 < 0

n - 19n 2 < 0 (2 taken common)

ax + bx + c < 0
It looks like a quadratic in-equation, and to find the for what values of n its true we have to find
the roots of in-equation.

a = 1, b=-19, c=-2
=>

24

(19)361+8

1919.20

1919.20

= -19.1,

19+19.20

So, (n-19.1) * (n+0.1) < 0


n lies in the range of {n < 19.1, n > 0.1}
i.e. for all n = {0.1,0.2,0.3.19.1} algorithm A is faster than algorithm B

= 0.1

2. Prove part 7 of Lemma 1.1 about logarithms. Use one of the earlier parts of Lemma 1.1 and
consider taking the log of both sides.

Sol:

Part 7 of Lemma 1.1 is


=

log

log

= ylog

log

Taking log on both sides


log

log

= log

log

From part 6 of lemma 1.1 log= a. log

There fore log

log

= log

log

log log = log log

Thus Left hand side is equal to right hand side


Hence proved

Another simple method


To prove

log

log

Lets look at basic log notation log = L; =

So = ; log

=L

Therefore, x=

log

substituting this in

log

= (

Thus

log

log

log

log

log

log

log

log

since (y=

log

3. How many different arrangements for a 52 card deck are there?


How does this compare to the 10

18

seconds that have passed since the Big Bang?

Sol: Different arrangements for a 52 card deck is 52! = 52*51*50*1 = 8.0658*10

67

Age of universe since big bang is 13.82 * 10 years


1 year = 31536000 seconds
18

age of universe in seconds = 435827520* 10

18

==> In times of 10
0.4358 * 10
18
Comparing different arrangements for a 52 card deck with the 10 seconds that have
passed since the Big Bang
8.065810

67

18.508 * 1049

1.85 * 1050

18

0.4358 10

Thus the arrangement of 52 is 1.85 * 1050 times the 1018 times seconds since big bang.

4. Show from definitions that P(S) = P (S | T) P(T) + P (S | not T) P (not T).


Sol: Given two events S and T which are stochastically independent or simply independent
Then conditional probability in entire population based on value T. P(S) = P (S | T)
P(S) =
P (S= )

This denotes the conditional probability on entire population


S

Not T

This denotes the conditional


probability of stochastic variable
based on variable T and the
population is divided based on T
and not T

Thus P(S) = P(P (S | T)) =

( = Ti) P (S/Ti)

Thus
Thus P(S) = P(P (S | T)) = ( = Ti) P (S/Ti) = P (Ti = T) * P (S/T) + P ( Ti = notT) * P(S / not T)

5. Prove equation 1.6 of your textbook (page 21).


=

Sol:

+3

=1

Taking sum of cubes for instance

=1

Writing in the form of sum of integers from 0 to (i+1) and subtracting the last term
==> =1 3 = (( +
1)3 ( + 1)3)
3
+ 1) 3 ( +2 1)3

3
3

= (
3

==> ( + 1) = 3

==>

+ 3

=1

=((

+3

+ 3 + 1) ( + 1)

+ 3 + 1

+3

+ 3 + 1 = 3

+ 3 + 1 {We know =

( +1)
2

( +1)

There fore

+3

+3 +1-

( + 1) = 3
2

2 3+3 2+

= 3

22

2 =

2 3+3 2+
6

,1 = +1}

6. Write an algorithm to find the second largest integer in a list of n integers. How many
comparisons does your algorithm do in the worst case?

Sol: Comparing each element to the largest element found element found so far and replacing the
current big variable with newly found big element which gives the largest element in the
list/array. After finding the largest element in the element assign it to variable biggest, and repeat
the above process by not considering the biggest variable which gives the second largest integer
in the list
Algorithm:
N = number of elements in list
for ( i=0; i<n; i++);
{
If (List [i] > List [i+1]);
{
Biggest1 = List[i];
}
Else
{
Biggest1 = List [i+1];
}
}
for ( i=0; i<n; i++);
{
if (List[i] ==! Biggest1)
{
If (List [i] > List [i+1]);
{
Biggest2 = List[i];
}
Else

{
Biggest2 = List[i+1];
}
}
Print Biggest 1, Biggest 2;

The number of comparisons this algorithm does in worst case is N-1 comparisons for finding
largest integer, N comparisons for eliminating biggest1 from list and N-1-1 for finding second
largest number
So in worst case my algorithm does N-1+N+N-2 = 3 N -3 comparisons.

7. Prove Lemma 1.10 of your textbook (page 51).


Sol: Lemma 1.10
1. f O(g) if and only if g (f).

Suppose f O(g) There are two variables c1 and n1 such that c1 > 0 and n1 >0 such that for n 1<= n,

f (n) < = c*g(n). Then for n 1<= n,


g(n) < = (1/c)* f (n).
The other direction is proved similarly.
2. if f (g) then g (f).
f (g) means f O(g) (g) By 1, g O(f) (f) Thus g (f).

3. defines an equivalence relation on the functions. (Section 1.3.1 explains what is


necessary to demonstrate.) Each set (F) Is an equivalence class, we call it
complex class.
If
f O(G) and g O(H) then F O(H); ie, O is transitive. So they are , , o and w. For all f, f (g) then g (f) gives symmetry.
For any f, f (f) and for any g (g) gives reflexivity.

4. O(f + g) = O(mx(f, g)).Similar equations hold for and . (They are useful
to analyze
complex algorithms where f and g could describe the work done by different parts of
the algorithm.)

For O (f+g) = O(max(f,g)), There are c1 and n1 such that n > n1 , h(n) <= c*( f +g)(n)
for c>0 and n> n1
Then h(n) < = 2cmax( f ,g)(n).
8. Prove or disprove =1

( )
2

Sol: From 5
=1

+3

+
6

means running time of the algorithm as n (input size) gets larger is


proportional
to 2 but the sum of squares is having a 3 term which is not proportional to 2 so the sum of squares over an interval 1 to n does not belong to
2
( )

( 2)

9. Draw a
55-56) for n = 17.

Hence disproved =1 2 ( 2).

decision tree for the binary search algorithm (Algorithm 1.4 on pages

Algorithm:
int binarySearch(int[] E, int first, int last, int K)
1. if (last < first)
2. index = -1;
3. else if (last == first)
4. if (K == E[first])
5. index = first;
6. else
7. index = -1;
8. else
9. int mid = (first + last) / 2;
10. if (K < = E[mid])
11. index = binarySearch(E, first, mid, K);
12. else
13. index = binarySearch(E, mid+1, last, K);
14. return index;

Decision Tree for n = 17

10. Add a row to Table 1.1 (page 49) showing the maximum input size that can be solved in one
hour.
Sol:

maximum input size that can be solved in one hour


1 hour

108000000

4615384

27272

1063

32

Das könnte Ihnen auch gefallen