Question

In: Computer Science

Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...

Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise.

for i := 1 to n
  for j := 1+1 to n
    for k:= i+1 to j
      m[i][j] := min(m[i][j], a[k])
    end for
  end for
end for
return m

a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M.

b.) Show that this algorithm uses Ω(?3)Ω(n3) comparisons to compute the matrix M. using this fact and part (a), conclude that the algorithms uses Θ(?3)Θ(n3) comparions. [Hint: Only consider the cases where ?≤?/4i≤n/4 and ?≥3?/4j≥3n/4 in the two outer loops in the algorithm.]

Solutions

Expert Solution

a.) The innermost assignment will take constant time to do the comparison and assignment.
The innermost loop (with k as the index) will run no more than n times. [The minimum possible
value to initialize k is i + 1, and the minimum possible value for i is 1 and the maximum value k
will ever go to is j and the maximum value that j will go to is n.]
The middle loop (with j as the index) will run no more than n times. [The minimum possible
value to initialize j is i + 1 which is 2. The maximum value j will go to is n]
The outermost loop (with i as the index) will run n times.
Therefore the runtime of the nested loops will take O(n) * O(n) * O(n) time
The initialization step of M will do one assignment for every element in M. M has n2 elements,
so this step will take O(n2) time.
Therefore the total runtime will be O(n3 + n2) = O(n3).

b.) First decide whether the function is big omega or not.

Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is Omega(g(x)) if there are constants C and k such that whenever f(x) >= C|g(x)|. [This is read as "f(x) is big-omega of g(x)="] One must note that in particular, f(x) is Omega(g(x)) if and only if g(x) is (f(x)).

The constants C and k in the definition of big-O notation are called witnesses to the relationship

The outer loop is executed at least n/4 times, once for each value of i from 1 to n/4. The middle loop is executed at least n/4 times, once for each value of j from 3n/4 to n. The inner loop for these values of i and j is executed at least (3n/4)- (n/4) n/2 times. Therefore the statement within the inner loop, which requires one comparison, is executed at least (n/4)(n/4)(n/2) = (n^3)/32 times.

Hence, the complexity of the algorithm is Omega(n^3).

From both the parts we can conclude that the complexity of the algorithm is .


Related Solutions

Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers...
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it. Write its corresponding program using your favorite programming language
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum...
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum of all integers = 2(n-1) Write an algorithm that, starting with a sequence (a1,a2,a3,...an) of positive integers, either constructs a tree with this degree sequence or concludes that none is possible.
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1...
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1 + a2 = b1 + b2 mod n, (2) a1 − a2 = b1 − b2 mod n, and (3) a1a2 = b1b2 mod n.
For each of the following sequences find a functionansuch that the sequence is a1, a2, a3,...
For each of the following sequences find a functionansuch that the sequence is a1, a2, a3, . . .. You're looking for a closed form - in particular, your answer may NOT be a recurrence (it may not involveany otherai). Also, while in general it is acceptable to use a "by cases"/piecewise definition, for this task you must instead present a SINGLE function that works for all cases.(Hint: you may find it helpful to first look at the sequence of...
Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give...
Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give him 2M integers, L1,L2,...,LM and R1,R2,...,RM. For each i from 1 to M, his teacher asked him to calculate the sum of odd index numbers from index Li to Ri. For example if Li = 3 and Ri = 7, then he has to calculate the value of (A3 + A5 + A7). Help him by making the program to calculate it quickly! Format...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT