CSE 2010: Algorithms and Data Structures Algorithms
Worst case vs. Average case Next, we will look at the concept of rate of growth (or order of growth). Average case is often as bad as the worst case. When analyzing algorithms, we will mostly focus on the worst case.
Linear search example
Linear/sequential search
Linear search var doLinearSearch = function(array) { for (var guess = 0; guess < array.length; guess++) { if (array[guess] === targetValue) { return guess; // found it! } return -1; // didn't find it }; https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Worst case: loop runs n times where n is the length of the input array. Best case: loop runs 1 time.
Linear search var doLinearSearch = function(array) { for (var guess = 0; guess < array.length; guess++) { if (array[guess] === targetValue) { return guess; // found it! } return -1; // didn't find it }; https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Each iteration of the loop performs a fixed number of instructions. Each of these instructions runs in fixed amount of time, i.e., constant time. Extra instruction: return -1 also runs in constant time.
Linear search var doLinearSearch = function(array) { for (var guess = 0; guess < array.length; guess++) { if (array[guess] === targetValue) { return guess; // found it! } return -1; // didn't find it }; Rate of growth is linear https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Running time of a program as a function of the size of its input.
Search in a matrix for i = 0 to N for j = 0 to N print matrix[i,j] print “Completed.” Rate of growth is quadratic https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Running time of a program as a function of the size of its input.
Asymptotic notations for i = 0 to N for j = 0 to N print matrix[i,j] print “Completed.” Rate of growth is quadratic Drop the slow-growing terms == keep only the fastest growing term. https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Running time of a program as a function of the size of its input.
Rate of growth: Example functions that often appear in algorithm analysis Constant » 1 Logarithmic » log n Linear » n N-Log-N » n log n Quadratic » n2 Cubic » n3 Exponential » 2n T(n) = n2 T(n) = n T(n) = log2 n T(n) = 1
Asymptotic notations Concept: upper bound and lower bound
Asymptotic notations Concept: upper bound and lower bound
Asymptotic notations upper bound Concept: upper bound and lower bound
Asymptotic notations tight bound lower bound upper bound Concept: upper bound and lower bound
https://youtu.be/ddsP7NecEBk Asymptotic notations tight bound lower bound upper bound Concept: upper bound and lower bound Less or equal We can then say that: https://youtu.be/ddsP7NecEBk
https://youtu.be/ddsP7NecEBk Asymptotic notations tight bound lower bound upper bound Concept: upper bound and lower bound Less or equal We can then say that: https://youtu.be/ddsP7NecEBk
https://youtu.be/ddsP7NecEBk Asymptotic notations tight bound lower bound upper bound Concept: upper bound and lower bound Less or equal We can then say that: and https://youtu.be/ddsP7NecEBk
https://youtu.be/ddsP7NecEBk Asymptotic notations tight bound lower bound upper bound Concept: upper bound and lower bound Less or equal We can then say that: and https://youtu.be/ddsP7NecEBk
Asymptotic Notation f(n) = O(g(n) means f(n) in O(g(n))…. abuse of notation…
Asymptotic Notation
Asymptotic Notation
Asymptotic Notation
Asymptotic Notation
Asymptotic Notation
Asymptotic notations
Asymptotic notations for i = 0 to M for j = 0 to N if a == b count = count + 1 print “Completed.” Rate of growth is quadratic Drop the slow-growing terms == keep only the fastest growing term. https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation Running time of a program as a function of the size of its input. O( M N )
Confusing worst case with upper bound Upper bound refers to a growth rate. Worst case refers to the worst input from among the choices for possible inputs of a given size. http://web.mit.edu/16.070/www/lecture/lecture_5_2.pdf