Download presentation
Presentation is loading. Please wait.
1
CSE 2010: Algorithms and Data Structures Algorithms
2
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.
3
Linear search example
4
Linear/sequential search
5
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 }; Worst case: loop runs n times where n is the length of the input array. Best case: loop runs 1 time.
6
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 }; 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.
7
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 Running time of a program as a function of the size of its input.
8
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 Running time of a program as a function of the size of its input.
9
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. Running time of a program as a function of the size of its input.
10
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
11
Asymptotic notations Concept: upper bound and lower bound
12
Asymptotic notations Concept: upper bound and lower bound
13
Asymptotic notations upper bound Concept: upper bound and lower bound
14
Asymptotic notations tight bound lower bound upper bound
Concept: upper bound and lower bound
15
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:
16
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:
17
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
18
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
19
Asymptotic Notation f(n) = O(g(n) means f(n) in O(g(n))…. abuse of notation…
20
Asymptotic Notation
21
Asymptotic Notation
22
Asymptotic Notation
23
Asymptotic Notation
24
Asymptotic Notation
25
Asymptotic notations
26
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. Running time of a program as a function of the size of its input. O( M N )
27
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.