Question

[10 marks] consider the following pseudocode: p := 0 x := 2fori:= 2ton p := (p...

  1. [10 marks] consider the following pseudocode:

    p := 0
    x := 2fori:= 2ton

    p := (p + i ) * x

    a) How many addition(s) and multiplication(s) are performed by the above pseudocode?b) Express the time-complexity of the pseudocode using the big-Θ notation.) Trace the algorithm, below, then answer (c) and (d):   Procedure xyz(n: integer) s := 0, for i:= 1 to n, for j:= 1to i, s:= s+ j*(i− j+ 1) return s

c) What is the time-complexity for the algorithm xyz? d) Express the number of operations carried out by the algorithm using the summation notation

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

(a)

Explanation:

=>We can see that in the given code loop is running from i = 2 to i = n means total number of times loop is executed = n-1 times.

=>For each iteration of for loop statement p := (p+i)*x is executed and the statement includes one addition and one multiplication.

=>Hence total number of additions = n-1 additions

=>Total number of multiplications = n-1 multiplications

(b)

Explanation:

=>As there is only one for loop and which is running n-1 times so time complexity = \Theta (n)

(c)

Explanation:

=>In xyz algorithm we can see that there are two for loops.

=>Outer for loop is independent and will run n times from i = 1 to i = n incrementing value of i by 1 each time.

=>Inner for loop is dependent for loop and will run from j = 1 to j = i incrementing the value of j by 1 each time.

=>When i = 1 inner loop will executed only once for j = 1, when i = 2 inner loop will run twice for j = 1, 2 and so on.

=>Total number of times loops will be executed = 1 + 2 + 3 + .. + n

=>Total number of times loops will be executed = n(n+1)/2

=>Hence time complexity = O(n^2)

(d)

Explanation:

=>Let say the cost of calcualting s := s + j*(i-j+1) = C where C is some constant.

=>We can write the number of operations using summation operations = \sum (i = 1 to i = n)\sum(j = 1 to j = i) (C)

I have explained each and every part with the help of statements attached to it.

Add a comment
Know the answer?
Add Answer to:
[10 marks] consider the following pseudocode: p := 0 x := 2fori:= 2ton p := (p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1...

    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-

  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

  • Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function...

    Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsorted integer array A of size n. For example, given the array A=[6,-6,3,2,1,2,0,4,3,5] and k=3, the function should return -5. a. [3 marks) Write an algorithm in pseudocode for SumKSmallest using the brute force paradigm. Indicate and justify (within a few sentences) the time complexity of your algorithm. b....

  • Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1...

    Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...

  • URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an...

    URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an empty array For i = 1 to n j = 1 While ij <= n Addj to the end of output j - j + 1 Return output Answer the following questions about the ArrayMystery algorithm above. a) How many times will the inner while loop iterate? You should express your answer in terms of i and n, using Big-Oh notation. Briefly justify your...

  • Perform the following to the algorithm below: - - Express T(n) as a function of n...

    Perform the following to the algorithm below: - - Express T(n) as a function of n Find a best approximation for the Big O function for T(n) Perform a time complexity analysis Define the basic operation of the algorithm Correctness Efficiency - - Procedure maxMin (n, A, I, h) integer h, I, A (1:n), n integer j j-2 IA (1) hS (1) while (i <=n) do if (Ali) < 1) then TEA (0) if(Ali) >h) then h A() j+į+1 repeat...

  • Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate...

    Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.

  • (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...

    (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...

  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT