CSC Design and Analysis of Algorithms. Lecture 1

Similar documents
Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithms

Principles of Algorithm Analysis

Cpt S 223. School of EECS, WSU

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

CS 4407 Algorithms Lecture 2: Growth Functions

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

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

COMPUTER ALGORITHMS. Athasit Surarerks.

EECS 477: Introduction to algorithms. Lecture 5

CS Non-recursive and Recursive Algorithm Analysis

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Running Time Evaluation

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

Data Structures and Algorithms Running time and growth functions January 18, 2018

Data Structures and Algorithms. Asymptotic notation

CS473 - Algorithms I

Introduction to Algorithms

Growth of Functions (CLRS 2.3,3)

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

CS Asymptotic Notations for Algorithm Analysis

Introduction to Algorithms and Asymptotic analysis

Computational Complexity

3.1 Asymptotic notation

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

The Time Complexity of an Algorithm

The Time Complexity of an Algorithm

Analysis of Algorithms

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

data structures and algorithms lecture 2

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

with the size of the input in the limit, as the size of the misused.

Ch01. Analysis of Algorithms

Asymptotic Analysis of Algorithms. Chapter 4

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

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

COMP 9024, Class notes, 11s2, Class 1

Algorithms Design & Analysis. Analysis of Algorithm

Asymptotic Analysis 1

The Growth of Functions and Big-O Notation

Analysis of Algorithms

Foundations II: Data Structures and Algorithms

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

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

Analysis of Algorithms

CSE 417: Algorithms and Computational Complexity

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

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

Lecture 2: Asymptotic Notation CSCI Algorithms I

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

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

CS Asymptotic Notations for Algorithm Analysis Outline

Agenda. We ve discussed

Data Structures and Algorithms

CS 380 ALGORITHM DESIGN AND ANALYSIS

Asymptotic Analysis Cont'd

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

Data Structures and Algorithms CSE 465

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Analysis Fundamentals

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

What is Performance Analysis?

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

CS Data Structures and Algorithm Analysis

Algorithms and Their Complexity

COMP Analysis of Algorithms & Data Structures

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

Big-O Notation and Complexity Analysis

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Written Homework #1: Analysis of Algorithms

Big , and Definition Definition

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

COMP Analysis of Algorithms & Data Structures

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Ch 01. Analysis of Algorithms

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

2. ALGORITHM ANALYSIS

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

COMP 382: Reasoning about algorithms

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

Data Structures and Algorithms Chapter 2

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

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

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

Big O (Asymptotic Upper Bound)

Runtime Complexity. CS 331: Data Structures and Algorithms

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

Lecture 1 - Preliminaries

P, NP, NP-Complete, and NPhard

Reading 10 : Asymptotic Analysis

Comparison of functions. Comparison of functions. Proof for reflexivity. Proof for transitivity. Reflexivity:

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Analysis of Algorithms Review

csci 210: Data Structures Program Analysis

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

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

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

Transcription:

CSC 8301- Design and Analysis of Algorithms Lecture 1 Introduction Analysis framework and asymptotic notations What is an algorithm? An algorithm is a finite sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input. The word algorithm stems from the Latin translation of the name of Muhammad ibn Musa al-khwarizmi the 9 th century Islamic mathematician who was principally responsible for spreading the Indian-Arabic numerals throughout Middle East and Europe and advocated systematic approach to solving linear and quadratic equations (see http://en.wikipedia.org/wiki/khwarazmi) 1

What is an algorithm? (cont.) problem algorithm input computer output Knuth s 5 important features of an algorithm finiteness terminates after a finite number of steps definiteness must be precisely defined has 0 or more inputs from predefined sets of objects has 1 or more outputs effectiveness each operation can be executed exactly in a finite length of time using pencil and paper (Donald E. Knuth, The Art of Computer Programming, vol.1) 2

Euclid s algorithm Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers m and n Euclid s algorithm is based on repeated application of equality gcd(m, n) = gcd(n, m mod n) Ex.: gcd(60,24) = while n 0 do r m mod n m n n r return m 5 Why study algorithms? Theoretical importance The cornerstone of computer science Practical importance a practitioner s toolkit of known algorithms frameworks for designing and analyzing algorithms for new problems 3

Two main issues related to algorithms How to design algorithms How to analyze algorithm efficiency Major Algorithm Design Techniques/Strategies Brute force Greedy approach Decrease and conquer Divide and conquer Transform and conquer Space-time tradeoff Dynamic programming Itera;ve improvement Backtracking Branch and Bound 4

Analysis of Algorithms How good is the algorithm? correctness (accuracy for approximation alg.) time efficiency space efficiency optimality Approaches: empirical (experimental) analysis theoretical (mathematical) analysis Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of times the algorithm s basic operation is executed as a function of input size Input size: number of input items or, if matters, their size Basic operation: the operation contributing the most toward the running time of the algorithm input size T(n) c op C(n) running time execution time for basic operation number of basic operation s executions 5

Example: Searching for a Key in a List Input Size? Basic Operation? Example: Multiplying two nxn Matrices Input Size? Basic Operation? 6

Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of list s items, i.e. n Key comparison Multiplication of two n n matrices Matrix order n or total number of elements N=2n 2 Multiplication of two numbers Typical graph problem #vertices (and edges) Visiting a vertex or traversing an edge Best-case, average-case, worst-case 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 Number of times the basic operation is executed on typical input NOT the average of worst and best case Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs 7

Example: Searching for a Key in a List Worst Case? Best Case? Average Case? (assuming probability of successful search is p and probability of the first match in each position i is same) Example: Multiplying two nxn Matrices Worst Case? Best Case? Average Case? 8

Types of formulas for basic operation count Exact formula e.g., C(n) = n(n-1)/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n 2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn 2 Order of growth Most important: Order of growth within a constant multiple as n Example: C(n) = 3n(n-1) Suppose we double the input size. How much longer will the algorithm take? 9

Values of several functions important for analysis of algorithms n log 2 n n n log 2 n n² n³ 2ⁿ n! 10 3.3 10 1 3.3 10 1 10 2 10 3 10 3 3.6 10 6 10 2 6.6 10 2 6.6 10 2 10 4 10 6 1.3 10 30 9.3 10 157 10 3 10 10 3 1.0 10 4 10 6 10 9 10 4 13 10 4 1.3 10 5 10 8 10 12 10 5 17 10 5 1.7 10 6 10 10 10 15 10 6 20 10 6 2.0 10 7 10¹² 10 18 Example Order these functions according to their order of growth (from lowest to highest): 2 n n 4/3 n(lg n) 3 n lg n 10

Main Points of the Analysis Framework Both time and space efficiencies are measured as functions of the algorithm s input size. Time efficiency is measured by counting the number of times the algorithm s basic operation is executed. Space efficiency is measured by counting the number of extra memory units (beyond input and output) used by the algorithm. For some algorithms, one should distinguish among the worst, best and average case efficiencies. The main concern is the order of growth of the algorithm s running time and extra memory units consumed as input size goes to infinity. Asymptotic order of growth A way to classify functions according to their order of growth - practical way to deal with complexity functions Big-O O(g(n)): class of functions f(n) that grow no faster than g(n) Big-Theta Θ (g(n)): class of functions f(n) that grow at same rate as g(n) Big-Omega Ω(g(n)): class of functions f(n) that grow at least as fast as g(n) 11

Big-O (asymptotic ) Definition: f(n) is in O(g(n)) if order of growth of f(n) order of growth of g(n) (within constant multiple), i.e., there exist positive constant c and non-negative integer n 0 such that f(n) c g(n) for every n n 0 Examples: 10n 2 is O(n 2 ) 10n is O(n 2 ) 5n+20 is O(n) Ω (Omega, asymptotic ) Definition: f(n) is in Ω (g(n)) if there exist positive constant c and non-negative integer n 0 such that These are all Ω(n 2 ) : n 2 n 2 + 100n 1000n 2-1000 n n 3 These are not: n 1.999 n lg n f(n) c g(n) for every n n 0 12

Θ (Theta, asymptotic =) Definition: f(n) is in Θ(g(n)) if there exist positive constants c 1, c 2 and non-negative integer n 0 such that Example: n 2-2n is Θ(n 2 ) c 1 g(n) f(n) c 2 g(n) for every n n 0 pick c 1 = 0.5, c 2 = 1, n 0 = 4 Find a tight Θ-bound for: 4n 3 4n 3 +2n Establishing order of growth High level idea: ignore: - constant factors (too system-dependent) - low-order terms (irrelevant for large inputs) For example, 6n log n + 2n becomes 26 13

Establishing order of growth using limits 0 order of growth of f(n) < order of growth of g(n) lim n f(n)/g(n) = c > 0 order of growth of f(n) = order of growth of g(n) order of growth of f(n) > order of growth of g(n) Examples: 10n vs. n 2 n(n+1)/2 vs. n 2 L Hôpital s Rule and Stirling s Formula L Hôpital s rule: If lim n f(n) = lim n g(n) = and the derivatives f, g exist, then lim n f(n) g(n) = lim n f (n) g (n) Example: log 2 n vs. n Stirling s formula: n! (2πn) 1/2 (n/e) n 14

Orders of growth of some important functions All logarithmic functions log a n belong to the same class Θ(log n) no matter what the logarithm s base a>1 is. All polynomials of the same degree k belong to the same class: a k n k + a k-1 n k-1 + + a 0 Θ(n k ). Exponential functions a n have different orders of growth for different a. order log n < order n α (α>0) < order a n < order n! < order n n Some properties of asymptotic order of growth f(n) O(f(n)) f(n) O(g(n)) iff g(n) Ω(f(n)) If f (n) O(g (n)) and g(n) O(h(n)), then f(n) O(h(n)) Note similarity with a b If f 1 (n) O(g 1 (n)) and f 2 (n) O(g 2 (n)), then f 1 (n) + f 2 (n) O(max{g 1 (n), g 2 (n)}) 15

Homework Reading: Preface and Chapter 1 (Sections 1.1-1.4) Exercises 1.1: 6, 9a, 12 1.2: 1, 2 1.3: 4, 5 Reading: Sections 2.1 and 2.2 + handout (Sec. 18.1) in Cormen et al. s book (1st ed.) Exercises 2.1: 3, 5a, 8, 9 2.2: 3, 5, 9, 12 16