























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























Announcements ❖ Schedule for Drop-in Times (finally!!!! 🙄) posted
Lecture Outline ❖ Algorithm Analysis Concepts ❖ Asymptotic Analysis Case Study: dup1 Worst Case ❖ Asymptotic Analysis Simplifications ❖ Case Analysis vs Asymptotic Analysis ❖ Takeaways
Review: What is Runtime Analysis? ❖ What does it mean for something to be “slow” or “fast”? ❖ Let’s run it and measure the (wallclock) time! Oh wait … ▪ Input can affect time ▪ Hardware can affect time ▪ Other programs running on the machine can affect time ▪ … and so much more! ❖ Count how many steps a program takes to execute on an input of size N
Comprehension ❖ What is the data structure or algorithm doing? public static boolean dup1(int[] A) { for (int i = 0 ; i < A.length; i += 1 ) { for (int j = i + 1 ; j < A.length; j += 1 ) { if (A[i] == A[j]) { return true; } } } return false; }
Cost Model: Time ❖ An algorithm’s runtime is dictated by the number of CPU instructions it executes. This is why we “count steps”: each “step” is an idealized instruction ▪ Example CPU instructions: comparing integers (<, ==), incrementing
public static boolean dup1(int[] A) { for (int i = 0 ; i < A.length; i += 1 ) { for (int j = i + 1 ; j < A.length; j += 1 ) { if (A[i] == A[j]) { return true; } } } return false; }
Asymptotic Analysis ❖ Asymptotic Analysis is how the algorithm or data structure behaves as N→∞ ▪ Simulating billions of particles ▪ Social network with billions of users ▪ Logging billions of transactions ▪ Encoding billions of bytes of video data ❖ Why ∞? ▪ We need a way to characterize data structures’ and algorithms’
Asymptotic Analysis Means “Infinity”: Graphically ❖ Asymptotic analysis is non-intuitive because it deals with infinity! ▪ What if you know your algorithm will only run on N < 100 elements? ▪ Why do we “throw away constants”? ▪ How come lower-order terms “don’t matter”? ▪ ❖ Linear-time algorithms scale better than quadratic-time algorithms (parabolas).^11
Lecture Outline ❖ Asymptotic Analysis Concepts ❖ Asymptotic Analysis Case Study: dup1 Worst Case ❖ Asymptotic Analysis Simplifications ❖ Case Analysis vs Asymptotic Analysis ❖ Takeaways
Counting Steps: Asymptotic Worst Case ❖ Assume N = A.length = 6 ▪ I’ve broken up the for-loops to make it easier to count ▪ Cheatsheet: 1 + 2 + 3 + 4 + 5 + 6 = 21 public static boolean dup1(int[] A) { int i = 0 ; for ( ; i < A.length; ) { int j = i + 1 ; for ( ; j < A.length; ) { if (A[i] == A[j]) { return true; } j += 1 ; } i += 1 ; } return false; }
Counting Steps: Asymptotic Worst Case ❖ Cheatsheet: 1 + 2 + 3 + … (N – 1) = ??? Operation Number of Executions (N = 6) Symbolic Expression i = 0 1 Less-than (<) 28 Increment (+=) 21 Equals-to (==) 21 Array accesses ([]) 42
Counting Steps: Double-checking Our Work ❖ Demo
Symbolic Expression, Intuitively == == == == == == == == == == == == == == == 0 1 2 3 4 5 0 1 2 3 4 5
Counting Steps: Asymptotic Worst Case ❖ Cheatsheet: 1 + 2 + 3 … N = (N