Produce an equation that asymptotically describes the following algorithms runtime: define algorithm_1(input): x = 2 y = 2 x = ((y + z) * 80)/4 print x, y, z define algorithm_2(input): x = 1 y = 5 loop from x to size(input) * 4: y = y +5 print x, y define algorithm_3(input): x = user_input() y = user_input() z = 0 loop i = 0 to size(input): if x

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Please solve and show all steps. This is for C++ problem.

**Title: Understanding the Runtime of Algorithms**

**Objective:**
Analyze and produce equations that asymptotically describe the runtime of the given algorithms.

**Algorithm Descriptions:**

1. **Algorithm 1:**
   - **Definition:** `define algorithm_1(input)`
   - **Initial Values:**
     - `x = 2`
     - `y = 2`
   - **Computation:**
     - `x = ((y + z) * 80) / 4`
   - **Output:**
     - `print x, y, z`

2. **Algorithm 2:**
   - **Definition:** `define algorithm_2(input)`
   - **Initial Values:**
     - `x = 1`
     - `y = 5`
   - **Loop Structure:**
     - `loop from x to size(input) * 4:`
       - Increment `y` by 5: `y = y + 5`
       - Output: `print x, y`

3. **Algorithm 3:**
   - **Definition:** `define algorithm_3(input)`
   - **User Input:**
     - `x = user_input()`
     - `y = user_input()`
   - **Initial Value:**
     - `z = 0`
   - **Loop Structure:**
     - `loop i = 0 to size(input):`
       - Conditional Check:
         - If `x < y`: Increment `z` by 1: `z = z + 1`
         - Else: Decrement `z` by 1: `z = z - 1`

**Analysis:**

- **Algorithm 1** involves constant time operations with no loops over the input, suggesting O(1) runtime.
- **Algorithm 2** includes a loop that iterates `size(input) * 4` times, indicating a runtime of O(n), where n is `size(input)`.
- **Algorithm 3** iterates from `0 to size(input)`, and the number of operations inside stays consistent regardless of input, leading to a runtime of O(n).

**Conclusion:**
These algorithms demonstrate different patterns of efficiency, with their runtime complexities being O(1), O(n), and O(n), respectively. Understanding these will aid in predicting their performance on varying input sizes.
Transcribed Image Text:**Title: Understanding the Runtime of Algorithms** **Objective:** Analyze and produce equations that asymptotically describe the runtime of the given algorithms. **Algorithm Descriptions:** 1. **Algorithm 1:** - **Definition:** `define algorithm_1(input)` - **Initial Values:** - `x = 2` - `y = 2` - **Computation:** - `x = ((y + z) * 80) / 4` - **Output:** - `print x, y, z` 2. **Algorithm 2:** - **Definition:** `define algorithm_2(input)` - **Initial Values:** - `x = 1` - `y = 5` - **Loop Structure:** - `loop from x to size(input) * 4:` - Increment `y` by 5: `y = y + 5` - Output: `print x, y` 3. **Algorithm 3:** - **Definition:** `define algorithm_3(input)` - **User Input:** - `x = user_input()` - `y = user_input()` - **Initial Value:** - `z = 0` - **Loop Structure:** - `loop i = 0 to size(input):` - Conditional Check: - If `x < y`: Increment `z` by 1: `z = z + 1` - Else: Decrement `z` by 1: `z = z - 1` **Analysis:** - **Algorithm 1** involves constant time operations with no loops over the input, suggesting O(1) runtime. - **Algorithm 2** includes a loop that iterates `size(input) * 4` times, indicating a runtime of O(n), where n is `size(input)`. - **Algorithm 3** iterates from `0 to size(input)`, and the number of operations inside stays consistent regardless of input, leading to a runtime of O(n). **Conclusion:** These algorithms demonstrate different patterns of efficiency, with their runtime complexities being O(1), O(n), and O(n), respectively. Understanding these will aid in predicting their performance on varying input sizes.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Use previous solution to Derive Big Oh for the algorithms from Problem 1. 

 
This is the pervious solution starting with Step 1 and Step 2:
Step 1

- We need to provide the asymptotic equation that will describe the complexity of the provided code snippets.

 
Step 2 ::

1. First code :: 

-> In this we have 3 operations and all the operation take constant time.

We are considering (input = n). 

Lets take time taken for :: 

  • x = 2 , y = 2  -> a    (Constant time)
  • x = ((y + z) * 80) /4   -> b  (Constant time)
  • print x ,y, z   -> c  (Constant time)

Here all are constant operations. So,

T(n) = a + b + c , which can also be written as ::

T(n) = k (Constant)

 

2. Second code :: 

-> In this code we have a loop and so the operation will take linear time. 

We are considering (input = n). 

Lets take time taken for :: 

  • x = 1 , y = 5  -> a   (Constant time)
  • loop, (y = y + 5) ,  (print x, y) -> 4*n  (Linear time)

T(n) = a + 4*n       or,

T(n) = a + b*n     ( a, b = constant) 

 

3. Third code :: 

-> In this code we have a loop and so the operation will take linear time. 

We are considering (input = n). 

Lets take time taken for :: 

  • x and y input -> e (Constant time)
  • z = 0 -> f (constant time)
  • loop -> g* n  (linear time)
  • if x < y   -> h* n  (linear time)
  • Other operations -> k  (linear time)

T(n) = (e + f) + (g+h) *n + k     

- We assume :: (e + f) = a , (g+h) = b

T(n) = a + bn + k    ( a, b, n are constants) 

Derive Big Oh for the algorithms from Problem 1.
Transcribed Image Text:Derive Big Oh for the algorithms from Problem 1.
Produce an equation that asymptotically describes the following algorithms runtime:
define algorithm_1(input):
x = 2
y = 2
x = ((y+z) * 80)/4
print x, y, z
define algorithm_2(input):
x = 1
y = 5
loop from x to size(input) * 4:
y = y +5
print x, y
Derive Big Oh for the algorithms from Problem 1.
define algorithm_3(input):
x = user_input()
y = user_input()
z = 0
loop i = 0 to size (input):
if x <y:
z=z+1
else
Z=Z-1
Transcribed Image Text:Produce an equation that asymptotically describes the following algorithms runtime: define algorithm_1(input): x = 2 y = 2 x = ((y+z) * 80)/4 print x, y, z define algorithm_2(input): x = 1 y = 5 loop from x to size(input) * 4: y = y +5 print x, y Derive Big Oh for the algorithms from Problem 1. define algorithm_3(input): x = user_input() y = user_input() z = 0 loop i = 0 to size (input): if x <y: z=z+1 else Z=Z-1
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education