CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

Similar documents
CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

An analogy from Calculus: limits

Algorithms and Their Complexity

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Growth of Functions (CLRS 2.3,3)

CSC236 Week 4. Larry Zhang

CSE 373: Data Structures and Algorithms. Asymptotic Analysis. Autumn Shrirang (Shri) Mare

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

IS 709/809: Computational Methods in IS Research Fall Exam Review

CS 310 Advanced Data Structures and Algorithms

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Lecture 3. Big-O notation, more recurrences!!

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Analysis of Algorithms

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

Lecture 3: Big-O and Big-Θ

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS361 Homework #3 Solutions

CSC236 Week 3. Larry Zhang

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Winter 2013

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

Review Asympto&c Bounds

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Data Structures and Algorithms. Asymptotic notation

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Asymptotic Analysis 1

Computational Complexity

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Lecture 2: Asymptotic Notation CSCI Algorithms I

Algorithms and Programming I. Lecture#1 Spring 2015

Ch 01. Analysis of Algorithms

CPSC 221 Basic Algorithms and Data Structures

Great Theoretical Ideas in Computer Science. Lecture 7: Introduction to Computational Complexity

CS1210 Lecture 23 March 8, 2019

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Data Structures and Algorithms CSE 465

Ch01. Analysis of Algorithms

Asymptotic Analysis Cont'd

Problem-Solving via Search Lecture 3

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Time Analysis of Sorting and Searching Algorithms

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

Problem Set 1. CSE 373 Spring Out: February 9, 2016

How many hours would you estimate that you spent on this assignment?

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

CS383, Algorithms Spring 2009 HW1 Solutions

CSE373: Data Structures and Algorithms Lecture 2: Proof by & Algorithm Analysis. Lauren Milne Summer 2015

Theory of Computation

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Announcements. CompSci 230 Discrete Math for Computer Science. The Growth of Functions. Section 3.2

Divide-and-Conquer Algorithms Part Two

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014

csci 210: Data Structures Program Analysis

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithms

Principles of Algorithm Analysis

Data Structures and Algorithms

The Time Complexity of an Algorithm

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

csci 210: Data Structures Program Analysis

Algorithms and Data S tructures Structures Complexity Complexit of Algorithms Ulf Leser

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Lecture 8 HASHING!!!!!

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

2. Limits at Infinity

Review Of Topics. Review: Induction

Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

Assignment 5 Bounding Complexities KEY

Advanced Algorithmics (6EAP)

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

P, NP, NP-Complete, and NPhard

Computational Complexity - Pseudocode and Recursions

Big , and Definition Definition

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

3.1 Asymptotic notation

The Time Complexity of an Algorithm

Transcription:

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis Riley Porter

Announcements Op4onal Java Review Sec4on: PAA A102 Tuesday, January 10 th, 3:30-4:30pm. Any materials covered will be posted online. TAs will be around ader the session to answer ques4ons. TA office hours will be posted on the website later today HW1 released ader class today Extension: Due Tuesday, January 17 th at 11:00PM 2

Assumed Knowledge (Pep Talk) Objects in Java: fields, methods, encapsula4on, inheritance, interfaces, classes Being the Client of Data Structures: List, Set, Stack, Queue, Map, Trees Being the implementer of Data Structures: ArrayList, LinkedList, Binary Trees, tes$ng your objects Binary Search, and some awareness of how sorkng works: merge sort, selec4on sort. Some basic analysis about above. Examples: when to use a HashSet vs a TreeSet? when does adding into an ArrayList become expensive? why does sor4ng an array help for searching? 3

NOT Assumed Knowledge Full understanding of the difference between the defini4ons of ADTs vs Data Structures vs ImplementaKons Big O analysis. Maybe you have some awareness, but we don t expect mastery of Big O yet. Indepth analysis or mastery of sorkng or hashing. Anything at all about Graphs, Heaps, AVL Trees, Union Find, Disjoint Sets, Hashing, Topological Sort, Parallelism Any advanced algorithms, dynamic programming, P vs NP, complexity theory, proofs, induc4on 4

Review of last time: Heaps Heaps follow the following two properties: Structure property: A complete binary tree Heap order property: The priority of the children is always a greater value than the parents (greater value means less priority / less importance) 10 1 10 20 30 15 not a heap 80 450 50 75 60 3 8 not a heap 10 40 20 50 700 60 85 a heap 80 99 5

Today Algorithm Analysis Review math for algorithm analysis Exponents and logarithms, floor and ceiling Analyzing code Big- O defini4on Using asympto4c analysis (con4nue next 4me) Set ourselves up to analyze why we use Heaps for Priority Queues (con4nue later this week) 6

Review of Logarithms log 2 x=y if x=2 y (so, log 2 1,000,000 = a lihle under 20 ) Just as exponents grow very quickly, logarithms grow very slowly Log base B compared to log base 2 doesn t maher so much In computer science we use base 2 because it works nicely with binary and how a computer does math. we are about to stop worrying about constant factors In particular, log 2 x = 3.22 log 10 x 7

Review of log properties log(a*b) = log A + log B So log(n k )= k log N log(a/b) = log A log B log(log x) is written log log x Grows as slowly as 2 2 grows quickly y (log x)(log x) is written log 2 x It is greater than log x for all x > 2 It is not the same as log log x 8

Review of floor and ceiling X Floor function: the largest integer < X 2.7 = 2 2.7 = 3 2 = 2 X Ceiling function: the smallest integer > X 2.3 = 3 2.3 = 2 2 = 2 9

Comparing Algorithms When is one algorithm (not implementa4on) beher than another? Various possible answers (clarity, security, ) But a big one is performance: for sufficiently large inputs, runs in less 4me or less space Large inputs (n) because probably any algorithm is fine for small inputs Answer will be independent of CPU speed, programming language, coding tricks, etc. 10

Comparing Algorithms Example 4 2 5 1 8 6 10 9 3 7 Given the above list, search for 3, which is beher? binary search linear search 1 2 3 4 5 6 7 8 9 10 Given the above list, search for 3, which is beher? binary search linear search 11

Analyzing Algorithms As the size of an algorithm s input grows: How much longer does the algorithm take (4me) How much more memory does the algorithm need (space) Ignore constant factors, think about large input: there exists some input size n 0, that for all input sizes n larger than n 0, binary search is beher than linear search on sorted input Analyze code to compute run4me, then look at how the run4me behaves as n gets really large (asympto4c run4me) 12

Constant Kme operakons: Analyzing Code Arithme4c, Variable Assignment, Access one Java field or array index, etc Complex operakons (approximakon): Consecu4ve Statements: Sum of +me of each statement Condi4onals: Time of condi+on + max(ifbranch, elsebranch) Loops: Number of itera+ons Time for Loop Body Func4on Calls: Time of func+on s body 13

Example What is the run4me of this pseudocode: x := 0 for i=1 to N do for j=1 to N do x := x + 3 return x 14

Example Solution What is the run4me of this pseudocode: x := 0 for i=1 to N do for j=1 to N do x := x + 3 return x 1 assignment + (N itera4ons of loop * (N itera4ons of loop * 1 assignment and math)) 1 return 1 + (N * (N * 1)) + 1 = N 2 + 2 However, what we care about here is the N 2 part. Let s look at asympto4c run4mes to see why. 15

Asymptotic Intuition with Pictures Are these the same? 16

Asymptotic Intuition with Pictures What about now that we compare them to y=n 2? 17

Asymptotic Intuition with Pictures What about these? One starts off much lower than the other one, but grows much faster. 18

Asymptotic Notation About to show formal definition, which amounts to saying: 1. Calculate Runtime by analyzing code 2. Eliminate low-order terms 3. Ignore constants and coefficients Examples: 4n + 5 0.5n log n + 2n + 7 n 3 + 2 n + 3n n log (10n 2 ) CSE373: Data Structure & Algorithms 19

Examples with Big-O Asymptotic Notation True or False? 1. 3n+10 O(n) 2. 4+2n O(1) 3. 20-3n O(n 2 ) 4. n+2logn O(logn) 5. logn O(n+2logn) 20

Examples with Big-O Asymptotic Notation Solutions True or False? 1. 3n+10 O(n) True (n = n) 2. 4+2n O(1) False: (n >> 1) 3. 20-3n O(n 2 ) True: (n n 2 ) 4. n+2logn O(logn) False: (n >> logn) 5. logn O(n+2logn) True: (logn n+2logn) 21

Formally Big-O Definition: g(n) is in O( f(n) ) if there exist constants c and n 0 such that g(n) c f(n) for all n n 0 To show g(n) is in O( f(n) ), pick a c large enough to cover the constant factors and n 0 large enough to cover the lower-order terms Example: Let g(n) = 3n 2 +17 and f(n) = n 2 c=5 and n 0 =10 is more than good enough This is less than or equal to So 3n 2 +17 is also O(n 5 ) and O(2 n ) etc. CSE373: Data Structure & Algorithms 22

Big-O We use O on a function f(n) (for example n 2 ) to mean the set of functions with asymptotic behavior less than or equal to f(n) So (3n 2 +17) is in O(n 2 ) 3n 2 +17 and n 2 have the same asymptotic behavior What it means: For your runtime, asymptotically, O(function) is the family of functions that defines the upper bound. There is a size of input (n 0 ) and a constant factor (c) you can use to make O(function) strictly larger than your runtime. CSE373: Data Structure & Algorithms 23

Examples using formal definition A valid proof is to find valid c and n 0 : Let g(n) = 1000n and f(n) = n 2. The cross-over point is n=1000 So we can choose n 0 =1000 and c=1 Many other possible choices, e.g., larger n 0 and/or c Let g(n) = n 4 and f(n) = 2 n. We can choose n 0 =20 and c=1 Definition: g(n) is in O( f(n) ) if there exist constants c and n 0 such that g(n) c f(n) for all n n 0 CSE373: Data Structure & Algorithms 24

What s with the c The constant multiplier c is what allows functions that differ only in their largest coefficient to have the same asymptotic complexity Example: g(n) = 7n+5 and f(n) = n For any choice of n 0, need a c > 7 (or more) to show g(n) is in O( f(n) ) Definition: g(n) is in O( f(n) ) if there exist constants c and n 0 such that g(n) c f(n) for all n n 0 CSE373: Data Structure & Algorithms 25

Big-O: Common Names O(1) constant (same as O(k) for constant k) O(log n) logarithmic O(n) linear O(n log n) n log n O(n 2 ) quadra4c O(n 3 ) cubic O(n k ) polynomial (where is k is any constant: linear, quadra4c and cubic all fit here too.) O(k n ) exponen4al (where k is any constant > 1) Note: exponen4al does not mean grows really fast, it means grows at rate propor4onal to k n for some k>1. Example: a savings account accrues interest exponen4ally (k=1.01?). 26

Intuition of Common Runtimes Even for small N, these look pretty different very quickly. 27

Intuition of Common Runtimes Now y=n and y=logn look a lot more similar in comparison to other runtimes. 28

Intuition of Common Runtimes Asymptotically, y=2 N looks way different than the rest and the rest all look roughly the same. 29

More Asymptotic Notation Big- O Upper bound: O( f(n) ) is the set of all func4ons asympto4cally less than or equal to f(n) g(n) is in O( f(n) ) if there exist constants c and n 0 such that g(n) c f(n) for all n n 0 Big- Omega Lower bound: Ω( f(n) ) is the set of all func4ons asympto4cally greater than or equal to f(n) g(n) is in Ω( f(n) ) if there exist constants c and n 0 such that g(n) c f(n) for all n n 0 Big- Theta Tight bound: θ( f(n) ) is the set of all func4ons asympto4cally equal to f(n) Intersec4on of O( f(n) ) and Ω( f(n) ) (use different c values) CSE373: Data Structure & Algorithms 30

A Note on Big- O Terms A common error is to say O( func4on ) when you mean θ( func4on ): People oden say Big- O to mean a 4ght bound Say we have f(n)=n; we could say f(n) is in O(n), which is true, but only conveys the upper- bound Since f(n)=n is also O(n 5 ), it s temp4ng to say this algorithm is exactly O(n) Somewhat incomplete; instead say it is θ(n) That means that it is not, for example O(log n) 31

What We re Analyzing The most common thing to do is give an O or θ bound to the worst- case running 4me of an algorithm Example: True statements about binary- search algorithm Common: θ(log n) running- 4me in the worst- case Less common: θ(1) in the best- case (item is in the middle) Less common (but very good to know): the find- in- sorted array problem is Ω(log n) in the worst- case No algorithm can do beher (without parallelism) 32

Today s Takeaways Algorithm Analysis Lots of ways to compare algorithms, today we analyzed run4me and asympto4c behavior Intui4on of how the different types of run4mes compare asympto4cally Big- O, Big- Theta, and Big- Omega defini4ons. Being able to prove them for a given run4me. 33