please i need answer for b cd e and f For each of the code s

please i need answer for b, c,d, e and f.

For each of the code segments below, determine an equation for the worst-ease computing time T(n) (expressed as a function of n. i.e 2n + 4) and the order of magnitude (expressed using big O notation, i.e. O(n)). b.//Matrix addition for (int i = 0; i

Solution

Ans.

b. Matrix Adddition :

First for loop executes n no of times and inner for loop executes n no of times.

Therefore, T(n)= n2 + c where c is a positive constant required to execute the assignment operation inside nested for loop.

O(n)=n2

(In the worst case, both for loops would run for n no of times, and since O() represents the worst case scenario, hence the highest order of T(n) is taken into consideration, please keep this in mind for the next subproblems as well)

c. Matrix Multiplication :

Since the first assignment operation, i.e, c[i][j] = 0, takes place for n2 no. of times and the second assigment operation c[i][j] += a[i][k] * b[k][j] , takes place n3 no of times, hence we can deduce:

T(n) = n3 + n2 + c

Since O(n) takes the highest magnitude of T(n) (since its the worst case scenario)

O(n) = n3

d. Bubble Sort :

Here outer for loop executes (n-1) times and inner loop also executes (n-1) times.

At worst case the if condition gets executed (n-1)*(n-1) times and if we assume that the swapping of vairables take linear time then the assignment operations can be expressed in terms of some constants say c.

Thus T(n) = (n-1)2 + c     =>   n2 -2n+1+c    => n2-2n+c

O(n) = n2 (Taking highest magnitude of T(n)).

e. In worst case if we assume n is very large then for each execution of while loop value of n is getting halved.

For 1st iteration : n/2

For 2nd iteration: n/22

For kth iteration : n/2k

Thus : T(n) = n/2k + c

Now, let n/2k = i

Applying log on both sides , log(n/2k) = log(i)

                                       => log(n) - log(2k) = log(i)

                                       => log(n) - k = log(i)

   => k = log(n) - log(i) => k = log(n) + c , since log(i) is a constant

Hence we can deduce, O(n) = log(n).

f. Since the first for loop exectes for (n-1) times and the second for loop can be explained as below:

1st iteration : 1

2nd iteration: 2

3rd iteration: 4

nth iteration: 2n

Hence, T(n) = (n-1)*2n + c => T(n) = n2n - 2n + c => T(n) = nlog(n) - log(n) + c

Now considering highest magnitude for worst case scenario O(n)

O(n) = nlog(n)     

please i need answer for b, c,d, e and f. For each of the code segments below, determine an equation for the worst-ease computing time T(n) (expressed as a func
please i need answer for b, c,d, e and f. For each of the code segments below, determine an equation for the worst-ease computing time T(n) (expressed as a func

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site