CS Asymptotic Notations for Algorithm Analysis

Similar documents
CS Asymptotic Notations for Algorithm Analysis Outline

CS Non-recursive and Recursive Algorithm Analysis

CS Analysis of Recursive Algorithms and Brute Force

CSC Design and Analysis of Algorithms. Lecture 1

CS483 Design and Analysis of Algorithms

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

MATH 501 Discrete Mathematics. Lecture 6: Number theory. German University Cairo, Department of Media Engineering and Technology.

Rings and modular arithmetic

Simultaneous Linear, and Non-linear Congruences

MTH 346: The Chinese Remainder Theorem

Chuck Garner, Ph.D. May 25, 2009 / Georgia ARML Practice

Principles of Algorithm Analysis

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CS Data Structures and Algorithm Analysis

All variables a, b, n, etc are integers unless otherwise stated. Each part of a problem is worth 5 points.

Direct Proof Divisibility

CSC 474 Network Security. Outline. GCD and Euclid s Algorithm. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

Direct Proof Divisibility

cse547, math547 DISCRETE MATHEMATICS Short Review for Final

Algorithm Efficiency. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University

Outline. Some Review: Divisors. Common Divisors. Primes and Factors. b divides a (or b is a divisor of a) if a = mb for some m

Outline. AIT 682: Network and Systems Security. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

CS483 Design and Analysis of Algorithms

About complexity. We define the class informally P in the following way:

Module 1: Analyzing the Efficiency of Algorithms

Section 19 Integral domains

4 Powers of an Element; Cyclic Groups

WORKSHEET ON NUMBERS, MATH 215 FALL. We start our study of numbers with the integers: N = {1, 2, 3,...}

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

CMPUT 403: Number Theory

The Euclidean Algorithm and Multiplicative Inverses

Lecture 6: Introducing Complexity

Number Theory Alex X. Liu & Haipeng Dai

Lecture 11: Cantor-Zassenhaus Algorithm

Wednesday, February 21. Today we will begin Course Notes Chapter 5 (Number Theory).

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Solution Sheet (i) q = 5, r = 15 (ii) q = 58, r = 15 (iii) q = 3, r = 7 (iv) q = 6, r = (i) gcd (97, 157) = 1 = ,

Discrete Math, Fourteenth Problem Set (July 18)

Homework 3, solutions

Homework #2 solutions Due: June 15, 2012

2.3 In modular arithmetic, all arithmetic operations are performed modulo some integer.

About complexity. Number theoretical algorithms

Number Theory Proof Portfolio

Chapter 9 Basic Number Theory for Public Key Cryptography. WANG YANG

Cpt S 223. School of EECS, WSU

Divisibility. Chapter Divisors and Residues

1. multiplication is commutative and associative;

Module 1: Analyzing the Efficiency of Algorithms

MATH 420 FINAL EXAM J. Beachy, 5/7/97

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Admin. ! Will post sample questions soon. 1. Choose a bit-length k. 2. Choose two primes p and q which can be represented with at most k bits

An Algorithm for Prime Factorization

The Time Complexity of an Algorithm

Ch 4.2 Divisibility Properties

CS483 Design and Analysis of Algorithms

MATH 433 Applied Algebra Lecture 4: Modular arithmetic (continued). Linear congruences.

(a) 14 and 42 (b) 33 and 18 (c) 45 and 108 (d) 24 and 2039, given that 2039 is a prime number

CS 5319 Advanced Discrete Structure. Lecture 9: Introduction to Number Theory II

Number Theory. Everything else. Misha Lavrov. ARML Practice 10/06/2013

2 Elementary number theory

Lecture notes: Algorithms for integers, polynomials (Thorsten Theobald)

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

2.2 Inverses and GCDs

1 Overview and revision

2301 Assignment 1 Due Friday 19th March, 2 pm

Math Circle Beginners Group February 28, 2016 Euclid and Prime Numbers

Algorithmic number theory. Questions/Complaints About Homework? The division algorithm. Division

CS2800 Questions selected for fall 2017

Exercises Exercises. 2. Determine whether each of these integers is prime. a) 21. b) 29. c) 71. d) 97. e) 111. f) 143. a) 19. b) 27. c) 93.

CS250: Discrete Math for Computer Science

CS483 Design and Analysis of Algorithms

CSE 417: Algorithms and Computational Complexity

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Number Theory. Zachary Friggstad. Programming Club Meeting

Introduction to Sets and Logic (MATH 1190)

CSCE 750, Spring 2001 Notes 1 Page 1 An outline of the book 1. Analyzing algorithms 2. Data abstraction 3. Recursion and induction 4. Sorting 5. Selec

CMSC Discrete Mathematics SOLUTIONS TO SECOND MIDTERM EXAM November, 2005

Math 3012 Applied Combinatorics Lecture 4

The Time Complexity of an Algorithm

Number Theory and the abc Conjecture

Modular Arithmetic Instructor: Marizza Bailey Name:

Chapter 2: The Basics. slides 2017, David Doty ECS 220: Theory of Computation based on The Nature of Computation by Moore and Mertens

Factoring. there exists some 1 i < j l such that x i x j (mod p). (1) p gcd(x i x j, n).

Math 4400 First Midterm Examination September 21, 2012 ANSWER KEY. Please indicate your reasoning and show all work on this exam paper.

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

Advanced Algorithms and Complexity Course Project Report

Computational Complexity - Pseudocode and Recursions

Optimisation and Operations Research

Some Facts from Number Theory

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Numbers and their divisors

NOTES ON SIMPLE NUMBER THEORY

Q 2.0.2: If it s 5:30pm now, what time will it be in 4753 hours? Q 2.0.3: Today is Wednesday. What day of the week will it be in one year from today?

MAS114: Solutions to Exercises

Lecture 11 - Basic Number Theory.

Integers and Division

Proof 1: Using only ch. 6 results. Since gcd(a, b) = 1, we have

Transcription:

CS483-02 Asymptotic Notations for Algorithm Analysis Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483 fall07/ This lecture note is based on Dr. J.M. Lien s lecture notes. CS483 Design and Analysis of Algorithms 1 Lecture 02, August 30, 2007

Outline Review Time efficiency Worst-case, best-case, and average case Order of growth O, Ω and Θ Examples and exercises CS483 Design and Analysis of Algorithms 2 Lecture 02, August 30, 2007

Review In last class What is an algorithm? Definition and properties Why do we study algorithms? Theoretical importance and practical importance There may exist multiple algorithms for the same problem. (Refer to Greatest Common Divisor Problem.) CS483 Design and Analysis of Algorithms 3 Lecture 02, August 30, 2007

Review In last class What is an algorithm? Its definition and properties Why do we study algorithms? Theoretical importance and practical importance There may exist multiple algorithms for the same problem. (Refer to Greatest Common Divisor Problem.) In this class Given an algorithm, how do we analyze it? Is the algorithm correct? What is its running time? (similar for space efficiency analysis) CS483 Design and Analysis of Algorithms 4 Lecture 02, August 30, 2007

Example: Greatest Common Divisor gcd(m, n) 3 algorithms calculating gcd(m, n). Assume m > n > 0. 1. From gcd(m, n) s definition: check each number in decreasing order by 1 starting from min{m, n}. n, n 1, n 2,... 2. From gcd(m, n) s definition and prime numbers definition: gcd(m, n) is the product of all common factors of m and n. List all prime numbers n. (Sieve method) Prime number factorization of m and n. Multiply all the common factors. 3. Euclid s algorithm: gcd(m, n) = gcd(n, m mod n) CS483 Design and Analysis of Algorithms 5 Lecture 02, August 30, 2007

Example: Greatest Common Divisor gcd(m, n) 3 algorithms calculating gcd(m, n). Assume m > n > 0. 1. From gcd(m, n) s definition: check each number in decreasing order by 1 since min{m, n}. n, n 1, n 2,... 2. From gcd(m, n) s definition and prime numbers definition: gcd(m, n) is the product of all common factors of m and n. List all prime numbers n. (Sieve method) Prime number factorization of m and n. Multiply all the common factors. 3. Euclid s algorithm: gcd(m, n) = gcd(n, m mod n) Question: Which one runs the fastest? CS483 Design and Analysis of Algorithms 6 Lecture 02, August 30, 2007

Outline Review Time efficiency Worst-case, best-case, and average case Order of growth O, Ω and Θ Examples and exercises CS483 Design and Analysis of Algorithms 7 Lecture 02, August 30, 2007

Empirical Analysis vs. Theoretical Analysis of Time Efficiency Approach of estimating the running time 1. Select a typical sample of inputs 2. Count actual number of basic executions or record the running time 3. Analyze the collected data CS483 Design and Analysis of Algorithms 8 Lecture 02, August 30, 2007

Empirical Analysis vs. Theoretical Analysis of Time Efficiency Approach of estimating the running time 1. Select a typical sample of inputs 2. Count actual number of basic executions or record the running time 3. Analyze the collected data Drawbacks Difficult to decide on how many samples/tests are needed to be done Hardware/environmental dependent Implementation dependent CS483 Design and Analysis of Algorithms 9 Lecture 02, August 30, 2007

Theoretical Analysis of Time Efficiency Goal Provide a machine independent measurement Common sense The size of input is increased algorithms run longer CS483 Design and Analysis of Algorithms 10 Lecture 02, August 30, 2007

Theoretical Analysis of Time Efficiency Goal Provide a machine independent measurement We are satisfied with this goal. Common sense The size of input is increased algorithms run longer Measurement is based on input size basic operation order of growth of running time CS483 Design and Analysis of Algorithms 11 Lecture 02, August 30, 2007

Input size 1. sort {a 1, a 2,, a n } Theoretical Analysis of Time Efficiency 2. 2 6 4 a 11 a 1m....... 3 2 7 6 5 4 b 11 b 1k....... 3 7 5 a n1 a nm b m1 b mk 3. prime(n) 4. Basic operation The operation that contributes most towards the running time CS483 Design and Analysis of Algorithms 12 Lecture 02, August 30, 2007

Theoretical Analysis of Time Efficiency Input size Basic operation Running time T(n) T(n) c op C(n), where n is the input size C(n) is the total number of basic operations for input of size n. c op is the time needed to execute one single basic operation. CS483 Design and Analysis of Algorithms 13 Lecture 02, August 30, 2007

Outline Review Time efficiency Worst-case, best-case, and average case Order of growth O, Ω and Θ Examples and exercises CS483 Design and Analysis of Algorithms 14 Lecture 02, August 30, 2007

Worst-case, Best-case, Average-case Input size Basic operation Running time T(n) T(n) c op C(n), For some algorithms efficiency depends on form of input: Worst case: C worst (n) maximum over inputs of size n Best case: C best (n) minimum over inputs of size n Average case: C avg (n) average over inputs of size n CS483 Design and Analysis of Algorithms 15 Lecture 02, August 30, 2007

Example: Greatest Common Divisor Algorithm 0.1: gcd(a, b) for i = {min(a, b),, 1} if a mod i = 0 and b mod i = 0 do then return (i) Input size: n = min(a, b) Basic operation: a mod i CS483 Design and Analysis of Algorithms 16 Lecture 02, August 30, 2007

Example: Greatest Common Divisor Algorithm 0.2: gcd(a, b) for i = {min(a, b),, 1} if a mod i = 0 and b mod i = 0 do then return (i) Input size: n = min(a, b) Basic operation: a mod i Worst case (worst case analysis provides an upper bound): When does the worst case happen? (a and b are relatively prime) C worst (n) = n CS483 Design and Analysis of Algorithms 17 Lecture 02, August 30, 2007

Example: Greatest Common Divisor Algorithm 0.3: gcd(a, b) for i = {min(a, b),, 1} if a mod i = 0 and b mod i = 0 do then return (i) Input size: n = min(a, b) Basic operation: a mod i Best case When does the best case happen? (gcd(a, b) = min(a, b)) C best (n) = 1 CS483 Design and Analysis of Algorithms 18 Lecture 02, August 30, 2007

Example: Greatest Common Divisor Algorithm 0.4: gcd(a, b) for i = {min(a, b),, 1} if a mod i = 0 and b mod i = 0 do then return (i) Input size: n = min(a, b) Basic operation: a mod i Average case Things need to be pay attention to Number of times of the basic operation will be executed on typical instances CS483 Design and Analysis of Algorithms 19 Lecture 02, August 30, 2007

NOT the average of worst and best cases Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs Assumptions Assume that a and b are two randomly chosen integers Assume that all integers have the same probability of being chosen hint: The probability that an integer i is a and b s greatest common divisor is P a,b (i) = 6 π 2 i 2 gcd(a, b) as the integer i such that (i a, b) and x := a/i and ó y := b/i are co-prime. ó The probability of two integers sharing a factor i is i 2. The probability that two integers are co-prime is 6/π 2.) CS483 Design and Analysis of Algorithms 20 Lecture 02, August 30, 2007

Example: Greatest Common Divisor Algorithm 0.5: gcd(a, b) for i = {min(a, b),, 1} if a mod i = 0 and b mod i = 0 do then return (i) Average case Denote n = min(a, b) C avg (n) = 1 P a,b (n) + 2 P a,b (n 1) + + n P a,b (1) = 6 π 2 ( 1 n 2 + 2 (n 1) 2 + + n 1 2 ) When n = 10, C avg (10) = 8.583. CS483 Design and Analysis of Algorithms 21 Lecture 02, August 30, 2007

Outline Review Time efficiency Worst-case, best-case, and average case Order of growth O, Ω and Θ Examples and exercises CS483 Design and Analysis of Algorithms 22 Lecture 02, August 30, 2007

Orders of Growth Input size Basic operation Running time T(n) T(n) c op C(n), where n is the input size, C(n) is the total number of basic operations for input of size n, and c op is the time needed to execute one single basic operation. Examples Given that C(n) = 1 2n(n 1), how much the performance will be affected if the input size n is doubled? growth = T(2n) T(n) c op C(2n) c op C(n) = 4n 2 n 1 4 CS483 Design and Analysis of Algorithms 23 Lecture 02, August 30, 2007

CS483 Design and Analysis of Algorithms 24 Lecture 02, August 30, 2007