Lecture9 Algorithm
Lecture9 Algorithm
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:
12/05/2024 3
Why algorithm?
• Examples
• Compute summation from 1 to 100,
• i.e., Sum = 1 + 2 + , … + 99 + 100
12/05/2024 4
Why algorithm?
• Examples
• Find the shortest distance from
GSU campus to ATL airport
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)
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