Question

In: Computer Science

Improve the algorithm by using two for loops instead of three, and analyze the running time...

Improve the algorithm by using two for loops instead of three, and analyze the running time of your improved algorithm asymptotically. Show the modified code.

Algorithm FactorSum(A, n)

Input: Array A of n real numbers.

S ← 0

for i ← 0 to n - 1

   for j ← i to n - 1

      P ← 1

      for k ← i to j

         P ← P x A[k]

S ← S + P

return S

Solutions

Expert Solution

Algorithm:

  1. First of all, make an array having cumulative multiplication of elements.
  2. Cumulative multiplication at any particular index is just the multiplication of all numbers from starting index to that index.
  3. Use the two outer loops as it is.
  4. For the third loop, we can simply replace it with
    int p;
    if(i!=0)
       p = cumulative_multiplication[j]/cumulative_multiplication[i-1];
    else
       p = cumulative_multiplication[j];
  • Below I have used a runnable code to explain you the algorithm:
  • You can also test them by running the program with both the algorithms. If the answer from both the algorithms are same, it means we have successfully reduced one loop.

Old Program ( 3 loops ):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int s = 0;
    int n = 8;
    int A[n] = {1,2,4,5,45,6,78,9};  // Input array A of size n

    // 1st loop
    for(int i=0;i<=n-1;i++)
    {
        // 2nd Loop
        for(int j=i;j<=n-1;j++)
        {
            // 3rd Loop
            int p = 1;
            for(int k=i;k<=j;k++)
            {
                p = p * A[k];
            }
            s = s + p;
        }

    }

     // Finally printing the value of s
    cout << "With 3-loops\n" << endl;
    cout << "Value of s: " << s << endl;
    return 0;
}

Output:

New Program ( 2 loops ):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int s = 0;
    int n = 8;
    int A[n] = {1,2,4,5,45,6,78,9};  // Input array A of size n

    long int cumulative_multiplication[n]; // Declaring an array to store cumulative multiplication

    // Finding the cumulative multiplication
    cumulative_multiplication[0] = A[0];
    for(int i=1;i<=n-1;i++)
    {
        cumulative_multiplication[i] = A[i] * cumulative_multiplication[i-1];
    }
    // 1st loop
    for(int i=0;i<=n-1;i++)
    {
        // Second loop
        for(int j=i;j<=n-1;j++)
        {

            // Replacing thirst loop with cumulative multiplication
            int p;
            if(i!=0)
                p = cumulative_multiplication[j]/cumulative_multiplication[i-1];
            else
                p = cumulative_multiplication[j];

            s = s + p;
        }

    }

    // Finally printing the value of s
    cout << "With 2-loops\n" << endl;
    cout << "Value of s: " << s << endl;
    return 0;
}

Output:

Please let me know in the comments in case of any confusion. Also, please UPVOTE if you like.


Related Solutions

In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the recurrence T (n) = 3T (n/2) + n. We then used the substitution method to solve the recurrence. When using the substitution method, it seems difficult to make a guess about the upper bound on the running time. However, if we use the recursion tree method, it would be a lot easier. In this question, you are asked to solve this recurrence using the...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
As stated in the chapter, many different factors affect the running time of an algorithm. Your...
As stated in the chapter, many different factors affect the running time of an algorithm. Your task is to conduct experiments to see how sorting algorithms perform in different environments. To construct this experiment you must have a program or two programs that sorts sets of data. The program or programs should sort a set of number using a quick sort and using a merge sort. To expedite this, example code that performs a quick sort and a merge sort...
Provide and implement three completely different algorithms of different running time that will check if two...
Provide and implement three completely different algorithms of different running time that will check if two strings are anagrams.
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the straightforward algorithm) to multiply a pair of 2 by 2 matrices. Explain why it is not possible to further reduce this number, 7, to anything less than 4, in multiplying a pair of 2 by 2 matrices.
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the straightforward algorithm) to multiply a pair of 2 by 2 matrices. Explain why it is not possible to further reduce this number, 7, to anything less than 4, in multiplying a pair of 2 by 2 matrices.
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and found the median of medians x by making groups of 5 elements. What is the maximum number of elements which are guaranteed to be greater than equals to x (without counting x, itself)?
Does every algorithm have a running-time equation? In other words, are the upper and lower bounds...
Does every algorithm have a running-time equation? In other words, are the upper and lower bounds for the running time (on any specified class of inputs) always the same?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT