CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Similar documents
Data Structures. Algorithm Performance and Big O Analysis

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Analysis of Binary Search algorithm and Selection Sort algorithm

The Running Time of Programs

Zabin Visram Room CS115 CS126 Searching. Binary Search

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Binary Heap Algorithms

CS/COE

MBA Jump Start Program

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Binary search algorithm

Faster deterministic integer factorisation

Lecture 3: Finding integer solutions to systems of linear equations

Mathematical Induction. Lecture 10-11

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Algorithms. Margaret M. Fleck. 18 October 2010

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Homework until Test #2

FINAL EXAM SECTIONS AND OBJECTIVES FOR COLLEGE ALGEBRA

Loop Invariants and Binary Search

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Lecture 1: Course overview, circuits, and formulas

csci 210: Data Structures Recursion

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

CS 103X: Discrete Structures Homework Assignment 3 Solutions

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

FACTORING POLYNOMIALS IN THE RING OF FORMAL POWER SERIES OVER Z

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The program also provides supplemental modules on topics in geometry and probability and statistics.

minimal polyonomial Example

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Section IV.1: Recursive Algorithms and Recursion Trees

Precalculus Orientation and FAQ

Chapter 3. if 2 a i then location: = i. Page 40

recursion, O(n), linked lists 6/14

Notes on Factoring. MA 206 Kurt Bryan

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CHAPTER FIVE. Solutions for Section 5.1. Skill Refresher. Exercises

The Factor Theorem and a corollary of the Fundamental Theorem of Algebra

Indiana State Core Curriculum Standards updated 2009 Algebra I

The Goldberg Rao Algorithm for the Maximum Flow Problem

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Sample Questions Csci 1112 A. Bellaachia

Binary Multiplication

MATH 22. THE FUNDAMENTAL THEOREM of ARITHMETIC. Lecture R: 10/30/2003

Sequence of Mathematics Courses

Analysis of a Search Algorithm

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Determining the Optimal Combination of Trial Division and Fermat s Factorization Method

Some facts about polynomials modulo m (Full proof of the Fingerprinting Theorem)

3.2 LOGARITHMIC FUNCTIONS AND THEIR GRAPHS. Copyright Cengage Learning. All rights reserved.

Mathematics 31 Pre-calculus and Limits

6. Standard Algorithms

Review of Fundamental Mathematics

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Analysis of Computer Algorithms. Algorithm. Algorithm, Data Structure, Program

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Math 115 Spring 2011 Written Homework 5 Solutions

Equations. #1-10 Solve for the variable. Inequalities. 1. Solve the inequality: Solve the inequality: 4 0

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Chapter 4 -- Decimals

8 Primes and Modular Arithmetic

MATH 095, College Prep Mathematics: Unit Coverage Pre-algebra topics (arithmetic skills) offered through BSE (Basic Skills Education)

3. Mathematical Induction

Lecture 13 - Basic Number Theory.

Discrete Mathematics: Homework 7 solution. Due:

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

JUST THE MATHS UNIT NUMBER 1.8. ALGEBRA 8 (Polynomials) A.J.Hobson

Math 0980 Chapter Objectives. Chapter 1: Introduction to Algebra: The Integers.

1 Review of Least Squares Solutions to Overdetermined Systems

11 Multivariate Polynomials

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

PURSUITS IN MATHEMATICS often produce elementary functions as solutions that need to be

CS 575 Parallel Processing

parent ROADMAP MATHEMATICS SUPPORTING YOUR CHILD IN HIGH SCHOOL

Math 55: Discrete Mathematics

Chemical Kinetics. Reaction Rate: The change in the concentration of a reactant or a product with time (M/s). Reactant Products A B

FACTORING LARGE NUMBERS, A GREAT WAY TO SPEND A BIRTHDAY

CS473 - Algorithms I

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

Algebra and Geometry Review (61 topics, no due date)

5.2 The Master Theorem

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Algorithm Design and Recursion

MATH 2 Course Syllabus Spring Semester 2007 Instructor: Brian Rodas

Curve Fitting, Loglog Plots, and Semilog Plots 1

Nonlinear Regression Functions. SW Ch 8 1/54/

Number Systems and Radix Conversion

Lecture Notes on Linear Search

MEP Y9 Practice Book A

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

THE NUMBER OF REPRESENTATIONS OF n OF THE FORM n = x 2 2 y, x > 0, y 0

Integer Factorization using the Quadratic Sieve

Transcription:

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro

Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential to algorithm analysis Proof by induction (review example) Exponents and logarithms Floor and ceiling functions Begin algorithm analysis CSE 373: Data Structures & Algorithms 2

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: Data Structures & Algorithms 3

Review Example P(n) = the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1 2 0 + 2 1 + 2 2 + + 2 n-1 = 2 n - 1. in other words: 1 + 2 + 4 + + 2 n-1 = 2 n - 1. CSE 373: Data Structures & Algorithms 4

Review Example P(n) = the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1 We will show that 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. CSE 373: Data Structures & Algorithms 5

Review Example P(n) = the sum of the first n powers of 2 (starting at 2 0 ) is 2 n -1 Inductive case: Assume P(k) is true i.e. the sum of the first k powers of 2 is 2 k -1 Show P(k+1) is true i.e. the sum of the first (k+1) powers of 2 is 2 k+1-1 Using our assumption, we know the first k powers of 2 is 2 0 + 2 1 + 2 2 + + 2 k-1 = 2 k - 1 Add the next power of 2 to both sides 2 0 + 2 1 + 2 2 + + 2 k-1 + 2 k = 2 k - 1 + 2 k We have what we want on the left; massage the right a bit: 2 0 + 2 1 + 2 2 + + 2 k-1 + 2 k = 2(2 k ) 1 = 2 k+1 1 Success! CSE 373: Data Structures & Algorithms 6

Mathematical Preliminaries The following N slides contain basic mathematics needed for analyzing algorithms. You should actually know this stuff. Hang in there! CSE 373: Data Structures & Algorithms 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 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: Data Structures & Algorithms 8

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: Data Structures & Algorithms 9

Logarithms and Exponents n CSE 373: Data Structures & Algorithms 10

Logarithms and Exponents n CSE 373: Data Structures & Algorithms 11

Logarithms and Exponents n CSE 373: Data Structures & Algorithms 12

Logarithms and Exponents See Excel file for plot data play with it! n CSE 373: Data Structures & Algorithms 13

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: Data Structures & Algorithms 14

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) I use this because my calculator doesn t have log 2. CSE 373: Data Structures & Algorithms 15

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: Data Structures & Algorithms 16

Facts about floor and ceiling 1. 2. 3. X 1< X n/2 X + X < X + 1 n/2 X = n n is an integer if CSE 373: Data Structures & Algorithms 17

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: Data Structures & Algorithms 18

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: Data Structures & Algorithms 19

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 = 1/2n 2 + 1/2n 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: Data Structures & Algorithms 20

Lower-order terms don t matter n*(n+ 1)/2 vs. just n 2 /2 We just say O(n 2 ) CSE 373: Data Structures & Algorithms 21

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: Data Structures & Algorithms 22

Big-O running times For a processor capable of one million instructions per second CSE 373: Data Structures & Algorithms 23

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: Data Structures & Algorithms 24

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 ) : Result is O(n 2 ) Examples: 4n + 5 0.5n log n + 2n + 7 n 3 + 2 n + 3n 365 = O(n) = O(n log n) = O(2 n ) = O(1) CSE 373: Data Structures & Algorithms 25

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: Data Structures & Algorithms 26