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

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 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

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

CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction. Linda Shapiro Spring 2016

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

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: Proof by Nicki Dell Spring 2014

CSE 311: Foundations of Computing. Lecture 14: Induction

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

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

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

CS383, Algorithms Spring 2009 HW1 Solutions

CS 310 Advanced Data Structures and Algorithms

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

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

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

CS1210 Lecture 23 March 8, 2019

CSC236 Week 3. Larry Zhang

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

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

Topic 17. Analysis of Algorithms

Discrete Mathematics. Spring 2017

Recursion: Introduction and Correctness

Mathematical Induction. EECS 203: Discrete Mathematics Lecture 11 Spring

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

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

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

CSC236 Week 4. Larry Zhang

Math 31 Lesson Plan. Day 2: Sets; Binary Operations. Elizabeth Gillaspy. September 23, 2011

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

Mathematical Induction

Introduction. How can we say that one algorithm performs better than another? Quantify the resources required to execute:

CS361 Homework #3 Solutions

CSE 241 Class 1. Jeremy Buhler. August 24,

Algorithm efficiency analysis

Computational Complexity - Pseudocode and Recursions

Lecture 27: Theory of Computation. Marvin Zhang 08/08/2016

Growth of Functions (CLRS 2.3,3)

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

CSE 311: Foundations of Computing I Autumn 2014 Practice Final: Section X. Closed book, closed notes, no cell phones, no calculators.

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

What is Performance Analysis?

Time-bounded computations

CS173 Lecture B, September 10, 2015

Intractable Problems Part Two

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

Advanced topic: Space complexity

Ch 01. Analysis of Algorithms

Math 391: Midterm 1.0 Spring 2016

Data Structures and Algorithms

Climbing an Infinite Ladder

CS173 Strong Induction and Functions. Tandy Warnow

Why do we need math in a data structures course?

Loop Convergence. CS 536: Science of Programming, Fall 2018

Math 31 Lesson Plan. Day 5: Intro to Groups. Elizabeth Gillaspy. September 28, 2011

Ch01. Analysis of Algorithms

Math.3336: Discrete Mathematics. Mathematical Induction

Induction and Recursion

Homework 5: Sampling and Geometry

Algorithms and Their Complexity

4.4 Recurrences and Big-O

csci 210: Data Structures Program Analysis

Asymptotic Running Time of Algorithms

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

N/4 + N/2 + N = 2N 2.

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

The following are generally referred to as the laws or rules of exponents. x a x b = x a+b (5.1) 1 x b a (5.2) (x a ) b = x ab (5.

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Running Time Evaluation

Induction. Announcements. Overview. Defining Functions. Sum of Squares. Closed-form expression for SQ(n) There have been some corrections to A1

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

Math 38: Graph Theory Spring 2004 Dartmouth College. On Writing Proofs. 1 Introduction. 2 Finding A Solution

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

CISC 235: Topic 1. Complexity of Iterative Algorithms

Mathematical Induction

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

Introduction to Cryptology. Lecture 20

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

Cpt S 223. School of EECS, WSU

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

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

Climbing an Infinite Ladder

1 What is the area model for multiplication?

Course: Math 111 Pre-Calculus Summer 2016

Asymptotic Analysis 1

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

Counting Strategies: Inclusion/Exclusion, Categories

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Mathematical Induction Assignments

Midterm 1. Name: TA: U.C. Berkeley CS70 : Algorithms Midterm 1 Lecturers: Anant Sahai & Christos Papadimitriou October 15, 2008

CS 4349 Lecture August 30th, 2017

What is proof? Lesson 1

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

Mathematical Fundamentals

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

Mathematical Induction

Transcription:

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

Today Finish discussing stacks and queues Review math essential to algorithm analysis Proof by induction Powers of 2 Binary numbers Exponents and logarithms Begin analyzing algorithms Using asymptotic analysis (continue next time) 2

Prove that 1 + 2 + 4 + 8 + + 2 n = 2 n+1-1 3

Background on Induction Type of mathematical proof Typically used to establish a given statement for all natural numbers (integers > 0) Proof is a sequence of deductive steps 1. Show the statement is true for the first number. 2. Show that if the statement is true for any one number, this implies the statement is true for the next number. 3. If so, we can infer that the statement is true for all numbers. Summer 2016 CSE373: Data Structures 4 & Algorithms

Think about climbing a ladder 1. Show you can get to the first rung (base case) 2. Show you can get between rungs (inductive step) 3. Now you can climb forever. Summer 2016 CSE373: Data Structures 5 & Algorithms

5 steps to inductive proofs 1. State what you re trying to prove. Suppose that P(n) is some predicate (mention n) Ex: Let P(n) be 2. Prove the base case Show that P(x) is true 3. Inductive Hypothesis (IH) Will prove that P(n) is true for every n >= x Assume that P(k) is true for some arbitrary integer k in the set of integers you re looking at 4. Inductive Step Show that P(k + 1) is true. Be sure to use the Inductive Hypothesis, and point out where you use it! 5. Conclusion 6

Why you should care Induction turns out to be a useful technique AVL trees Heaps Graph algorithms Can also prove things like 3 n > n 3 for n 4 Exposure to rigorous thinking Summer 2016 CSE373: Data Structures 7 & Algorithms

Prove that 1 + 2 + 4 + 8 + + 2 n = 2 n+1-1 8

Example P(n) = the sum of the first n powers of 2 (starting at 0) is 2 n -1 Theorem: P(n) holds for all n 1 Proof: By induction on n Base case: n=1. Sum of first 1 power of 2 is 2 0, which equals 1. And for n=1, 2 n -1 equals 1. Inductive case: Assume the sum of the first k powers of 2 is 2 k -1 Show the sum of the first (k+1) powers of 2 is 2 k+1-1 Using assumption, sum of the first (k+1) powers of 2 is (2 k -1) + 2 (k+1)-1 = (2 k -1) + 2 k = 2 k+1-1 9

Powers of 2 A bit is 0 or 1 (just two different letters or symbols ) A sequence of n bits can represent 2 n distinct things For example, the numbers 0 through 2 n -1 2 10 is 1024 ( about a thousand, kilo in CSE speak) 2 20 is about a million, mega in CSE speak 2 30 is about a billion, giga in CSE speak Java: an int is 32 bits and signed, so max int is about 2 billion a long is 64 bits and signed, so max long is 2 63-1 10

Therefore Could give a unique id to Every person in the U.S. with 29 bits Every person in the world with 33 bits Every person to have ever lived with 38 bits (estimate) Every atom in the universe with 250-300 bits So if a password is 128 bits long and randomly generated, do you think you could guess it? 11

Logarithms and Exponents Since so much is binary login CS almost always means log 2 Definition: log 2 x = y if x = 2 y So, log 2 1,000,000 = a little under 20 Just as exponents grow very quickly, logarithms grow very slowly See Excel file for plot data play with it! 12

Logarithms and Exponents Since so much is binary log in CS almost always means log 2 Definition: log 2 x = y if x = 2 y So, log 2 1,000,000 = a little under 20 Just as exponents grow very quickly, logarithms grow very slowly See Excel file for plot data play with it! 13

Logarithms and Exponents Since so much is binary log in CS almost always means log 2 Definition: log 2 x = y if x = 2 y So, log 2 1,000,000 = a little under 20 Just as exponents grow very quickly, logarithms grow very slowly See Excel file for plot data play with it! 14

Logarithms and Exponents Since so much is binary log in CS almost always means log 2 Definition: log 2 x = y if x = 2 y So, log 2 1,000,000 = a little under 20 Just as exponents grow very quickly, logarithms grow very slowly See Excel file for plot data play with it! 15

Properties of logarithms 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 16

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, log B x = (log A x) / (log A B) 17

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 18

Floor and ceiling properties 1. 2. 3. X 1< X X X < X X + 1 n/2 + n/2 = n if n is an integer 19

Algorithm Analysis As the size of an algorithm s input grows (integer, length of array, size of queue, etc.): How much longer does the algorithm take (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 20

Example What does this pseudocode return? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; Correctness: For any N 0, it returns 21

Example What does this pseudocode return? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; Correctness: For any N 0, it returns 3N(N+1)/2 Proof: By induction on n P(n) = after outer for-loop executes n times, x holds 3n(n+1)/2 Base: n=0, returns 0 Inductive: From P(k), x holds 3k(k+1)/2 after k iterations. Next iteration adds 3(k+1), for total of 3k(k+1)/2 + 3(k+1) = (3k(k+1) + 6(k+1))/2 = (k+1)(3k+6)/2 = 3(k+1)(k+2)/2 22

Example How long does this pseudocode run? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; Running time: For any N 0, Assignments, additions, returns take 1 unit time Loops take the sum of the time for their iterations So: 2 + 2*(number of times inner loop runs) And how many times is that 23

Example How long does this pseudocode run? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; The total number of loop iterations is N*(N+1)/2 This is a very common loop structure, worth memorizing Proof is by induction on N, known for centuries This is proportional to N 2, and we say O(N 2 ), big-oh of For large enough N, the N and constant terms are irrelevant, as are the first assignment and return See plot N*(N+1)/2 vs. just N 2 /2 24

Lower-order terms don t matter N*(N+1)/2 vs. just N 2 /2 25

Geometric interpretation N i = N*N/2+N/2 i=1 for i=1 to N do for j=1 to i do // small work Area of square: N*N Area of lower triangle of square: N*N/2 Extra area from squares crossing the diagonal: N*1/2 As N grows, fraction of extra area compared to lower triangle goes to zero (becomes insignificant) 26

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 ) quadratic O(n 3 ) cubic O(n k ) polynomial (where is k is any constant) O(k n ) exponential (where k is any constant > 1) Note: exponential does not mean grows really fast, it means grows at rate proportional to k n for some k>1 A savings account accrues interest exponentially (k=1.01?) If you don t know k, you probably don t know it s exponential 27

Announcements TA office hours have been decided Held at the 4 th floor breakouts in CSE Whiteboard area near the stairs/elevator HW1 released Due Friday, July 2 at 11:00PM See late day policy Optional *section* Thursdays 2:00 3:00pm Room TBD Getting started on HW1, Induction, Eclipse Bring Questions! Materials will be posted online 28