Algorithm: a set of steps and calcultions to obtain a goal
Theoretically: steps to take and corresponding reasoning to obtain a goal
Practically: A function or series of functions
Algorithms have properties
------------------
Algorithms have properties
Arithmetic complexity: How many operations (of a certain type) occur
Time complexity: How long does the algorithm run?
Arithmetic complexity: How many operations (of a certain type) occur
Time complexity: How long does the algorithm run?
Best case: fastest or least amount of operations
Worst case: longest or most amount of operations
int arr[3];
arr[0] = 5;
How many assignment operations?
This is considered constant time
int minimum(int[] arr){
int min = arr[0];
for(int i = 1; i < arr.size(); i++)
if(arr[i] < min) min = arr[i];
return min;
How many comparison operations?
How many assignmnets?
Best case, worse case?
Average case?
Average case?
Could mean looking at all possible input and taking the average
Could mean looking at practical input and taking the average
Average case?
int minimum(int[] arr){
int min = arr[0];
for(int i = 1; i < arr.size(); i++)
if(arr[i] < min) min = arr[i];
return min;
Suppose arr.size() will always be 3
At most 3
What about when $P(arr[i] < min) = \frac{1}{i}$
int minimum(int[] arr){
int min = arr[0];
for(int i = 1; i < arr.size(); i++)
if(arr[i] < min) min = arr[i];
return min;
What if comparison = 2 secs, assignments = 1 sec
Best case, worse case?
Average case?
int minimum(int[][] arr){
int min = arr[0][0];
for(int i = 0; i < arr.size(); i++)
for(int j = 0; j < arr.size(); j++)
if(arr[i][j] < min) min = arr[i][j];
return min;
How many comparisons?
How many assignments best case?
How many assignments worst case?
int minimum(int[][] arr){
int min = arr[0][0];
for(int i = 0; i < arr.size(); i++)
for(int j = 0; j < arr.size(); j++)
if(arr[i][j] < min) min = arr[i][j];
return min;
Suppose that $min = A[i,j]$ takes $i + 2^j$ seconds
How long?
int f(int n){
if(n == 0) return 1;
return factorial(n)+ f(n-1);
How many additions?
How many multiplications?/p>
Note: different runtimes same algorithm
Algorithm can be broken up
int f(int n){
if(n == 0) return 1;
return factorial(n)+ f(n-1);
Addition takes 2 seconds, multiplication 6 seconds
Suppose input is a equally likely random number between 1 and 100 inclusive
Average runtime? (aka expected value?)