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

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

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

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

CS 310 Advanced Data Structures and Algorithms

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

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

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

Growth of Functions (CLRS 2.3,3)

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

csci 210: Data Structures Program Analysis

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

CS383, Algorithms Spring 2009 HW1 Solutions

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

Topic 17. Analysis of Algorithms

csci 210: Data Structures Program Analysis

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

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 Running Time of Algorithms

CSC2100B Data Structures Analysis

Algorithm efficiency analysis

Recursion: Introduction and Correctness

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

CISC 235: Topic 1. Complexity of Iterative Algorithms

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

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

Data Structures and Algorithms

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

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

Remainders. We learned how to multiply and divide in elementary

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

CS361 Homework #3 Solutions

Asymptotic Analysis 1

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

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

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

Algorithms and Their Complexity

4.4 Recurrences and Big-O

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

Introduction. An Introduction to Algorithms and Data Structures

Computational Complexity - Pseudocode and Recursions

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

CSE 311: Foundations of Computing. Lecture 14: Induction

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

Definition of a Logarithm

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Data Structures and Algorithms

CS1210 Lecture 23 March 8, 2019

CSE 21 Practice Exam for Midterm 2 Fall 2017

Analysis of Algorithms

Why do we need math in a data structures course?

Data Structures and Algorithms Chapter 2

It ain t no good if it ain t snappy enough. (Efficient Computations) COS 116, Spring 2011 Sanjeev Arora

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

The Time Complexity of an Algorithm

Analysis of Algorithms

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

Algorithms (II) Yu Yu. Shanghai Jiaotong University

Ch01. Analysis of Algorithms

COMP 250 Fall Midterm examination

Copyright 2000, Kevin Wayne 1

Algorithms Design & Analysis. Analysis of Algorithm

What is Performance Analysis?

CSE 241 Class 1. Jeremy Buhler. August 24,

CSCE 222 Discrete Structures for Computing

Math101, Sections 2 and 3, Spring 2008 Review Sheet for Exam #2:

Fall Lecture 5

Definition of a Logarithm

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

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

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Lecture 2: Divide and conquer and Dynamic programming

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

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

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

Analysis of Algorithms

Induction and Recursion

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

Math 391: Midterm 1.0 Spring 2016

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Cpt S 223. School of EECS, WSU

Introduction to Cryptology. Lecture 20

Neville s Method. MATH 375 Numerical Analysis. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Neville s Method

POLYNOMIAL EXPRESSIONS PART 1

CPSC 490 Problem Solving in Computer Science

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

Ch 01. Analysis of Algorithms

Before we do that, I need to show you another way of writing an exponential. We all know 5² = 25. Another way of writing that is: log

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

5. Sequences & Recursion

1 Substitution method

COMP 355 Advanced Algorithms

COMP Analysis of Algorithms & Data Structures

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Transcription:

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

Today Registration should be done. Homework 1 due 11:59pm next Wednesday, April 8 th. Review math essential to algorithm analysis Proof by induction (another example) Exponents and logarithms Floor and ceiling functions Begin algorithm analysis CSE 373 Spring 2015 2

Homework 1 Clarifications You should have numoracles Queues for the questions to be added to (if the number of answers changes, so should the number of oracles) The Oracles answers are stored in the answers array and when you dequeue a question from Oracle 0, you can get the answer to the question from answers[0] CSE 373 Spring 2015 3

Mathematical induction Suppose P(n) is some statement (mentioning integer n) Example: n n/2 + 1 We can use induction to prove P(n) for all integers n n 0. We need to 1. Prove the base case i.e. P(n 0 ). For us n 0 is usually 1. 2. Assume the statement holds for P(k). 3. Prove the inductive case i.e. if P(k) is true, then P(k+1) is true. Why we care: To show an algorithm is correct or has a certain running time no matter how big a data structure or input value is (Our n will be the data structure or input size.) CSE 373 Spring 2015 4

Example P(n) = n n/2 + 1 We will show that P(n) holds for all n 2 Proof: By induction on n Base case: n=2. 2 2/2 + 1 2 1+1 2 2 CSE 373 Spring 2015 5

Example P(n) =n n/2 + 1, n 2 Inductive case: Assume P(k) is true i.e. k k/2 + 1 Show P(k+1) is true i.e. k+1 (k+1)/2 + 1 Using our assumption, we know k k/2 + 1 so: k+1 (k/2 + 1) + 1 k+1 k/2 + 2 k+1 k/2 + 2 (k+1)/2 + 1* *(k+1)/2 + 1 = k/2 + 1.5 k+1 (k+1)/2 + 1 Success! CSE 373 Spring 2015 6

Logarithms and Exponents Definition: x = 2 y if log 2 x = y 8 = 2 3, so log 2 8 = 3 65536= 2 16, so log 2 65536 = 16 The exponent of a number says how many times to use the number in a multiplication. e.g. 2 3 = 2 2 2 = 8 (2 is used 3 times in a multiplication to get 8) A logarithm says how many of one number to multiply to get another number. It asks "what exponent produced this? e.g. log 2 8 = 3 (2 makes 8 when used 3 times in a multiplication) CSE 373 Spring 2015 7

Logarithms and Exponents Definition: x = 2 y if log 2 x = y 8 = 2 3, so log 2 8 = 3 65536= 2 16, so log 2 65536 = 16 Since so much is binary in CS, log almost always means log 2 log 2 n tells you how many bits needed to represent n combinations. So, log 2 1,000,000 = a little under 20 Logarithms and exponents are inverse functions. Just as exponents grow very quickly, logarithms grow very slowly. CSE 373 Spring 2015 8

Logarithms and Exponents See Excel file for plot data play with it! n CSE 373 Spring 2015 9

Logarithms and Exponents See Excel file for plot data play with it! n CSE 373 Spring 2015 10

Logarithms and Exponents See Excel file for plot data play with it! n CSE 373 Spring 2015 11

Logarithms and Exponents See Excel file for plot data play with it! n CSE 373 Spring 2015 12

Properties of logarithms log(a*b) = log A + log B 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 CSE 373 Spring 2015 13

Log base doesn t matter much! Any base B log is equivalent to base 2 log within a constant factor And we are about to stop worrying about constant factors! In particular, log 2 x = 3.22 log 10 x In general we can convert log bases via a constant multiplier To convert from base B to base A: log B x = (log A x) / (log A B) CSE 373 Spring 2015 14

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 CSE 373 Spring 2015 15

Facts about floor and ceiling 1. 2. 3. X 1 X X X X X 1 n/2 n/2 n if n is an integer CSE 373 Spring 2015 16

Algorithm Analysis As the size of an algorithm s input grows (integer, length of array, size of queue, etc.), we want to know How much longer does the algorithm take to run? (time) How much more memory does the algorithm need? (space) Because the curves we saw are so different, often care about only which curve we are like Separate issue: Algorithm correctness does it produce the right answer for all inputs Usually more important, naturally CSE 373 Spring 2015 17

Algorithm Analysis: A first example Consider the following program segment: x:= 0; for i = 1 to n do for j = 1 to i do x := x + 1; What is the value of x at the end? i j x 1 1 to 1 1 2 1 to 2 3 3 1 to 3 6 4 1 to 4 10 n 1 to n? Number of times x gets incremented is = 1 + 2 + 3 + + (n-1) + n = n*(n+1)/2 CSE 373 Spring 2015 18

Analyzing the loop Consider the following program segment: x:= 0; for i = 1 to n do for j = 1 to i do x := x + 1; The total number of loop iterations is n*(n+1)/2 This is a very common loop structure, worth memorizing This is proportional to n 2, and we say O(n 2 ), big-oh of n*(n+1)/2 = (n 2 + n)/2 For large enough n, the lower order and constant terms are irrelevant, as are the assignment statements See plot (n 2 + n)/2 vs. just n 2 /2 CSE 373 Spring 2015 19

Lower-order terms don t matter n*(n+ 1)/2 vs. just n 2 /2 We just say O(n 2 ) CSE 373 Spring 2015 20

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 ) O(n 3 ) O(n k ) quadratic cubic polynomial (where is k is any constant) O(k n ) exponential (where k is any constant > 1) O(n!) factorial Note: exponential does not mean grows really fast, it means grows at rate proportional to k n for some k>1 CSE 373 Spring 2015 21

Big-O running times For a processor capable of one million instructions per second CSE 373 Spring 2015 22

Analyzing code Basic operations take some amount of constant time Arithmetic (fixed-width) Assignment Access one Java field or array index Etc. (This is an approximation of reality: a very useful lie.) Consecutive statements Conditionals Loops Calls Recursion Sum of times Time of test plus slower branch Sum of iterations Time of call s body Solve recurrence equation (next lecture) CSE 373 Spring 2015 23

Analyzing code 1. Add up time for all parts of the algorithm e.g. number of iterations = (n 2 + n)/2 2. Eliminate low-order terms i.e. eliminate n: (n 2 )/2 3. Eliminate coefficients i.e. eliminate 1/2: (n 2 ) Examples: 4n + 5 0.5n log n + 2n + 7 n 3 + 2 n + 3n n log (10n 2 ) nlog(10) + n log(n 2 ) nlog(10) + 2n log(n) = O(n) = O(n log n) = O(2 n ) = O(n log n) CSE 373 Spring 2015 24

Try a Java sorting program private static void bubblesort(int[] intarray) { int n = intarray.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ } } } if(intarray[j-1] > intarray[j]){ //swap the elements! temp = intarray[j-1]; intarray[j-1] = intarray[j]; intarray[j] = temp; } CSE 373 Spring 2015 25