Consider the following algorithm:
a. What does this algorithm compute?
b. Compute the running time of this algorithm.
a) Lets trace The algorithm for n = 5;
The for loop wil go from i = 1 to i=5;
So at start S = 0;
then for i=1, S = S + 1*1 = 1
for i=2, S= S + 2*2 = 5
for i=3, S = S + 3*3 = 14
for i=4, S=S+4*4 = 30
for i=5; S = S+5*5 = 55
So At the end, S is nothing but sum of squares from numbers
from 1 to n..
55 = 1^2 + 2^2 + 3^2 + 4^2 + 5^2
So algorithm computes sum of squares of first n numbers.
b) The running time of the algorithm:
As the for loop will run once for each of the numbers starting from
1 till n, hence the for loop will be running for n times.. Hence
the running time of O(n)
In the above example, you can verify that the loop ran for 5 times
for n=5.
Consider the following algorithm: a. What does this algorithm compute? b. Compute the running time of...
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow 0 to n - 2 do for j leftarrow i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.
What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-
Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index mystery (index low, index high, const Array S[], number x) if low S high then mid = (low + high) / 2 if x = Smid] then return mid elsif x < s[mid] then return mystery (low, mid-1, s, x) else return mystery (mid+1, high, s, x) else return 0 end What does the recursive algorithm above compute? Explain why?
Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for i = 1 to 5n^2 do p = p times i return p (b) Algorithm for-loop(n): s = 0 for i = 1 to n do for j = I to n do s = s + i return s (c) Algorithm WhileLoop(n): x = 0; j = 2; while (j = n){x = x+ 1; j =j times 2;}
pleas answer asap 3. (20 points) Algorithm Analysis and Recurrence There is a mystery function called Mystery(n) and the pseudocode of the algorithm own as below. Assume that n 3* for some positive integer k21. Mystery (n) if n<4 3 for i1 to 9 5 for i-1 to n 2 return 1 Mystery (n/3) Print "hello" 6 (1) (5 points) Please analyze the worst-case asymptotic execution time of this algorithm. Express the execution time as a function of the input...
Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...
EC2 (5 Points): The running time of Algorithm Ais (1) n? + 1300, and the running time of another Algorithm B for solving the same problem is 112n - 8. Assuming all other factors equal, at what input sizes) would we prefer one algorithm to the other? 7.5 EC3 (2.5 Points): What is the recurrence relation (an equation that recursively defines) of the Towers of Hanoi problem? Remember, the base case is T(1) = 1 BIVAAI EE11
Consider the merge sort algorithm. (a) Write a recurrence relation for running time function for the merge sort. (b) Use two methods to solve the recurrence relation. (c) What is the best, worst and average running time of the merge sort algorithm? Justify your answer.
In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain your answer. Assume that n is a power of 2. Algorithm rec(n) Input: Integer value n ≥ 0 if n = 0 then return 1 else { c ← 0 For i ← 0 to n−1 do c ← c + i c ← c + rec(n/2) return c }
What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n on the same machine?