Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

L04: Algorithm Analysis I, Schemes and Mind Maps of Algorithms and Programming

CSE373, Winter 2020. L04: Algorithm Analysis I. Counting Steps: Asymptotic Worst Case. ❖ Cheatsheet: 1 + 2 + 3 + 4 + 5 + 6 = 21. 15. Operation. Number of.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 05/11/2023

shanthi_48
shanthi_48 🇺🇸

4.8

(36)

901 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE373, Winter 2020L04: Algorithm Analysis I
Algorithm Analysis I: Intro
CSE 373 Winter 2020
Instructor:Hannah C. Tang
Teaching Assistants:
Aaron Johnston Ethan Knutson Nathan Lipiarski
Amanda Park Farrell Fileas Sam Long
Anish Velagapudi Howard Xiao Yifan Bai
Brian Chan Jade Watkins Yuma Tou
Elena Spasova Lea Quan
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download L04: Algorithm Analysis I and more Schemes and Mind Maps Algorithms and Programming in PDF only on Docsity!

Algorithm Analysis I: Intro

CSE 373 Winter 2020

Instructor: Hannah C. Tang

Teaching Assistants:

Aaron Johnston Ethan Knutson Nathan Lipiarski

Amanda Park Farrell Fileas Sam Long

Anish Velagapudi Howard Xiao Yifan Bai

Brian Chan Jade Watkins Yuma Tou

Elena Spasova Lea Quan

Announcements ❖ Schedule for Drop-in Times (finally!!!! 🙄) posted

Lecture OutlineAlgorithm 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

numbers (+=, *), accessing an array

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; }

“Computers are really dumb. They can only do a few things like shuffling around

numbers, but they do them really really fast so that they appear smart.”

  • Hal Perkins, UW CSE

Asymptotic AnalysisAsymptotic 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’

complexity, which can vary for every finite N

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

i

j

  • We want the area of the right triangle
  • If its side length is N, its order of growth is N^2. 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; }

Counting Steps: Asymptotic Worst Case ❖ Cheatsheet: 1 + 2 + 3 … N = (N

  • N)/ Operation Number of Executions (N = 6) Symbolic Expression i = 0 1 1 Less-than (<) 28 (N^2 + 3N + 2)/ Increment (+=) 21 (N^2 + N)/ Equals-to (==) 15 (N^2 - N)/ Array accesses 30 N
  • N +2N +N