Professional Documents
Culture Documents
Analysis
Analysis of Algorithms
Formally measure how fast a program/algorithm runs
Efficiency of an algorithm can be measured in terms of:
- Execution time (time complexity)
complexity)
Time Complexity
Time complexity (T(n)) : A measure of the
amount of time required to execute an
algorithm
Factors that should not affect time complexity
analysis:
The programming language chosen to implement
the algorithm
The quality of the compiler
The speed of the computer on which the
algorithm is to be executed
one to implement
TCA - Motivation
If you make an algorithm for a web application that works
well for 1000 users and you measure its running time,
using algorithm complexity analysis, you can have a
good idea of what will happen once we get 2000 users
instead!
Conclusion - If you have measured your algo/programs
behavior for a small input, you can get a good idea of
how it will behave for larger inputs.
Time Complexity
Three possible states in algorithm analysis:
Worst-case
An upper bound on the running time for any input of given
size
A determination of the maximum amount of time that an
algorithm requires to solve problems of size n
Average-case
Assume all inputs of a given size are equally likely
A determination of the average amount of time that an
Best-case
The lower bound on the running time
A determination of the minimum amount of time that an
n
Worst-case: Find or cannot find the target after compare every
Big-O Notation
Time complexity of algorithm can be
represented by Big O notation. (O
order of)
Big O notation is denoted as O(f(n))
f(n)- algorithms growth-rate function /
instruction-counting function
Time complexity (Big-O)
Running time
O(1)
Constant
O(n)
Linear
O(n2)
Quadratic
O(log(n))
Logarithmic
f(n)
This function f(n) gives us the number of
instructions that would be needed by an
algorithm in the worst-case.
Example :
If an algorithm requires 6n+4 number of
instructions.. you may say that the
algorithm is defined to have f(n)= 6n+4
f(n)
Two terms found in the previous function :
f(n) = 6n + 4
bigger
term
f(n)
Two terms found in the previous function :
f(n) = 6n + 4
dominant
term
i=1
= n(n+1)/2
Algo_A:
T(n) = n
= O(n)
i
1+2
1+2+3
1+2+3++n
Algo_B
When i is 1, inner loop executes 1 time,
when i is 2, inner loop executes 2 times;
when i is n, inner loop executes n
times.
In all the inner loop executes
=1+2+3++n = n2/2 + n/2 times.
The algorithm is O(n2).
Algo_C
Algorithms whose
solutions are
independent of the
size of the problems
inputs are said to
have constant time
complexity : O(1)
Growth Rates
A growth function shows the relationship between the size of
the problem (n) and the time it takes to solve the problem
worstTime(n)
O(2n)
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
n
Constant :
The algorithm requires the same fixed number of steps regardless of the size of the task.
A Linked Implementation
Adding an entry to a bag, an O(1) method,
A Linked Implementation
Searching a bag for a given entry, O(1) best
case, O(n) worst case , Thus an O(n)
method overall
Objectives
Sort array into ascending order using
Insertion sort
Selection sort
merge sort
Insertion Sort
Arranging things into either ascending or
descending order is called sorting
When book found taller than one to the right
Remove book to right
Slide taller book to right
Insert shorter book into that spot
Continue
Insertion Sort
Insertion Sort
Selection Sort
Example of sorting books by height
Take all books off shelf
Select shortest , replace on shelf
Continue until all books
Alternative
Look down shelf, select shortest
Swap first with selected shortest
Move to second slot, repeat process
Selection Sort
Before and after exchanging the shortest book and the first book
After n-1repetitions of this process, the last item has automatically fallen into place
Merge Sort
Divide array into two halves
Sort the two parts
Merge them into one sorted array
smaller tasks
The effect of the recursive calls and the merges during a merge sort:
- Recursively sort 1st half of input array
- Recursively sort 2nd half of the input array
-Merge two sorted sub-lists into one
Best Case
Worst Case
Insertion sort
O(n)
O(n2)
Selection sort
O(n2)
O(n2)
n Log2 n
n Log2 n
Merge sort