0% found this document useful (0 votes)
2 views

Lecture9 Algorithm

Uploaded by

Junk Dummy1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture9 Algorithm

Uploaded by

Junk Dummy1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Algorithm With Python

What is algorithm?
“An algorithm is any well-defined computational
procedure that takes some value, or set of values, as
input and produces some value, or set of values, as
output.”

Inputs Outputs
Algorithm
Procedure

x f( )
f(x)
12/05/2024 2
Properties of algorithm?
A good algorithm should meet some requirements:

1. Correctness: be able to output the correct answer

2. Readability: help human understand

3. Robustness: when input is illegal, give proper reaction

4. Efficiency: Time complexity and Space complexity

12/05/2024 3
Why algorithm?
• Examples
• Compute summation from 1 to 100,
• i.e., Sum = 1 + 2 + , … + 99 + 100

• Simple way: 1+2+3…+99+100, 99 addition

• Better way: (1+100), (2+99),…..(50+51), 50 addition


50*101=5050, 1
multiplication

12/05/2024 4
Why algorithm?
• Examples
• Find the shortest distance from
GSU campus to ATL airport

How to effectively and efficiently


obtain solutions?
12/05/2024 5
Algorithm runtime

• An algorithm's runtime is the time the algorithm takes to execute.


• A function of the input size. f(n)

A sample algorithm 1~n sum


sum_v1 /
sum_v1(n) sum_v2
1+2+3+ … +n # n steps
sum_v2(n)
n*(n+1)/2 # 1 step
Runtime varies based on your implementation!
What is big-O notation means
• Big-O notation is a mathematical notation for upper-bounding a function's
growth rate
• Informally, can be found by ignoring constants and non-dominant growth terms.
• Examples: an2+bn+c=O(n2)
n + 137 = O(n)
n2 + 3n – 2 = O(n2)
n3 + 10n2log(n) – 15n = O(n3)
2n + n! = O(?)
12/05/2024 8
Asymptotic Analysis
• Simple operations (e.g., insert, move, add, etc.) are usually assumed to run in O(1)
time
• Ignore all constants which are dependent on machine and programming languages.

O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n)
9
Algorithm Runtime Analysis

Best-case

Cases Average-case

Worst-case
Algorithm Runtime Analysis:
Best-case
 A situation where the algorithm performs optimally
 Often represents the most favorable or ideal input for
the algorithm
 The best-case analysis helps us understand the lower
bound on the algorithm's performance
Algorithm Runtime Analysis:
Average-case
 Refers to the expected or typical
performance of the algorithm on random or
average input
 Provides a more realistic assessment of the algorithm's
efficiency
 Determining the average-case runtime requires
knowledge of the statistical properties of the expected
data inputs.
Algorithm Runtime Analysis:
Worst-case
 Refers to the scenario that causes the
algorithm to perform the most poorly
 Helps to understand the algorithm's behavior under
unfavorable conditions
 Algorithm runtime analysis often focuses on the worst-
case runtime complexity
Analysis: Finding the max
Counting constant time operations
 An operation can be any statement (or constant number of statements)
that has a constant runtime complexity, O(1)

 In big-O notation, any constant number of constant time operations is


O(1)

 Example:
Algorithm ~ 5 operations > 3 operations in the loop > 6 operation
~ 5 + 3N + 6
~ O(1) + O(N) + O(1)
~ O(N)
Runtime of nested for loop
Runtime of nested for loop

You might also like