Question

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-

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

Solution:

(1)

Explanation:

Finding time complexity of abc algorithm:

=>We can see in the abc() method, there is a while loop starting from i = 1 and ending with condition i \leq n doubling the value of i each time using i = 2i

=>Number of times loops will be executed = log(n) times

=>Time complexity of abc algorithm = O(logn)

(2)

(c)

Explanation:

=>In procedure foo() we can see that there are 2 for loops.

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

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

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

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

=>Let say cost of comparisons and other operation takes time = C where C is some constant.

=>Cost of all operations = n(n+1)/2 + C

(d)

Explanation:

=>Cost of all operations = n(n+1)/2 + C

=>Hence time complexity in big-O = O(n^2)

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

Add a comment
Know the answer?
Add Answer to:
What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1...
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
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