CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2009W1 1.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Analysis of Algorithms
CSE 373 Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CS420 lecture one Problems, algorithms, decidability, tractability.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
CPSC 320: Intermediate Algorithm Design & Analysis Asymptotic Notation: (O, Ω, Θ, o, ω) Steve Wolfman 1.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Asymptotic Growth Rate
CSE 326 Asymptotic Analysis David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
CSE 326: Data Structures Lecture #2 Analysis of Algorithms Alon Halevy Fall Quarter 2000.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Analysis of Recursive Algorithms October 29, 2014
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
Algorithm Analysis (Big O)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2014W1 1.
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
CS 3343: Analysis of Algorithms
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2011W2 1.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2013W2 1.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
1 CSE 326 Data Structures: Complexity Lecture 2: Wednesday, Jan 8, 2003.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Algorithm Analysis Part of slides are borrowed from UST.
Sorting.
Module #7: Algorithmic Complexity Rosen 5 th ed., §2.3.
CS 615: Design & Analysis of Algorithms Chapter 2: Efficiency of Algorithms.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 326: Data Structures Asymptotic Analysis Hannah Tang and Brian Tjaden Summer Quarter 2002.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CSE 326: Data Structures Lecture #3 Asymptotic Analysis Steve Wolfman Winter Quarter 2000.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Analysis of Algorithms
CS 3343: Analysis of Algorithms
More Computational Theory
CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2010W2.
Module #7: Algorithmic Complexity
Analysis of Algorithms
Discrete Mathematics and its Applications
Module #7: Algorithmic Complexity
CSE 326: Data Structures Lecture #6 Asymptotic Analysis
Introduction To Algorithms
Analysis of Algorithms
Presentation transcript:

CS221: Algorithms and Data Structures Lecture #1 Complexity Theory and Asymptotic Analysis Steve Wolfman 2009W1 1

Today’s Outline Programming Project #1 and Forming Teams Brief Proof Reminder Asymptotic Analysis, Briefly Silicon Downs and the SD Cheat Sheet Asymptotic Analysis, Proofs and Programs Examples and Exercises 2

Learning Goals By the end of this unit, you will be able to… Define which program operations we measure in an algorithm in order to approximate its efficiency. Define “input size” and determine the effect (in terms of performance) that input size has on an algorithm. Give examples of common practical limits of problem size for each complexity class. Give examples of tractable, intractable, and undecidable problems. Given code, write a formula which measures the number of steps executed as a function of the size of the input (N). Continued… 3

Learning Goals By the end of this unit, you will be able to… Compute the worst-case asymptotic complexity of an algorithm (e.g., the worst possible running time based on the size of the input (N)). Categorize an algorithm into one of the common complexity classes. Explain the differences between best-, worst-, and average- case analysis. Describe why best-case analysis is rarely relevant and how worst-case analysis may never be encountered in practice. Given two or more algorithms, rank them in terms of their time and space complexity. 4

Prog Proj #1 & Teams 5

Proof by... Counterexample –show an example which does not fit with the theorem –QED (the theorem is disproven) Contradiction –assume the opposite of the theorem –derive a contradiction –QED (the theorem is proven) Induction –prove for a base case (e.g., n = 1) –assume for an anonymous value (n) –prove for the next value (n + 1) –QED 6

Example Proof by Induction A number is divisible by 3 iff the sum of its digits is divisible by three Base Case: 7

Example Still Continued Induction Step: 8

Example Proof by Induction (Worked) “A number is divisible by 3 iff the sum of its digits is divisible by three.” First, some definitions: Consider a positive integer x to be made up of its n digits: x 1 x 2 x 3..x n. For convenience, let’s call SD(x). 9

Example Proof by Induction (Worked) “A number is divisible by 3 iff the sum of its digits is divisible by three.” There are many ways to solve this, here’s one. We’ll prove a somewhat stronger property, that for a non-negative integer x with any positive integral number of digits n, SD(x) mod 3 = x mod 3. 10

Example Proof by Induction (Worked) Base case: Consider any number x with one digit (0-9). SD(x) = = x 1 = x. So, it’s trivially true that SD(x) mod 3 = x mod 3. 11

Example Proof by Induction (Worked) Induction hypothesis: Assume for an arbitrary integer n > 0 that for any non-negative integer x with n digits: SD(x) mod 3 = x mod 3. 12

Example Proof by Induction (Worked) Inductive step: Consider an arbitrary number y with n + 1 digits. We can think of y as being made up of its digits: y 1 y 2 …y n y n+1. Clearly, y 1 y 2 …y n (which we’ll call z) is itself an n digit number; so, the induction hypothesis applies: SD(z) mod 3 = z mod 3. 13

Example Proof by Induction (Worked) Inductive step continued: Now, note that y = z*10 + y n+1. So:y mod 3 = (z*10 + y n+1 ) mod 3 = (z*9 + z + y n+1 ) mod 3 z*9 is divisible by 3; so, it has no impact on the remainder of the quantity when divided by 3: = (z + y n+1 ) mod 3 14

Example Proof by Induction (Worked) Inductive step continued: By the IH, we know z mod 3 = SD(z) mod 3. So: = (z + y n+1 ) mod 3 = (SD(z) + y n+1 ) mod 3 = (y 1 + y 2 + … + y n + y n+1 ) mod 3 = SD(y) mod 3 QED! 15

Today’s Outline Programming Project #1 and Forming Teams Brief Proof Reminder Asymptotic Analysis, Briefly Silicon Downs and the SD Cheat Sheet Asymptotic Analysis, Proofs and Programs Examples and Exercises 16

A Task to Solve and Analyze Find a student’s name in a class given her student ID 17

Analysis of Algorithms Analysis of an algorithm gives insight into how long the program runs and how much memory it uses –time complexity –space complexity Analysis can provide insight into alternative algorithms Input size is indicated by a number n (sometimes there are multiple inputs) Running time is a function of n (Z 0  R 0 ) such as T(n) = 4n + 5 T(n) = 0.5 n log n - 2n + 7 T(n) = 2 n + n 3 + 3n But... 18

Asymptotic Analysis Hacks Eliminate low order terms –4n + 5  4n –0.5 n log n - 2n + 7  0.5 n log n –2 n + n 3 + 3n  2 n Eliminate coefficients –4n  n –0.5 n log n  n log n –n log (n 2 ) = 2 n log n  n log n 19

Rates of Growth Suppose a computer executes ops per second: 10 4 s = 2.8 hrs10 18 s = 30 billion years 20

Order Notation T(n)  O(f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 21

Order Notation T(n)  O(f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 T(n)   (f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 T(n)   (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) T(n)  o(f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) T(n)  (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) 22

Examples 10,000 n n   (n 2 ) n 2   (n 2 ) n log n  O(n 2 ) n log n   (n) n  o(n 4 ) n  (n 2 ) 23

Today’s Outline Programming Project #1 and Forming Teams Brief Proof Reminder Asymptotic Analysis, Briefly Silicon Downs and the SD Cheat Sheet Asymptotic Analysis, Proofs and Programs Examples and Exercises 24

Silicon Downs Post #1 n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / lg n mn 3 Post #2 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n 2 m n For each race, which “horse” is “faster”. Note that faster means smaller, not larger! a.Left b.Right c.Tied d.It depends e.I am opposed to algorithm racing. 25

Race I n 3 + 2n 2 100n vs. a.Left b.Right c.Tied d.It depends 26

Race II n 0.1 log n vs. a.Left b.Right c.Tied d.It depends 27

Race III n + 100n 0.1 2n + 10 log n vs. a.Left b.Right c.Tied d.It depends 28

Race IV 5n 5 n! vs. a.Left b.Right c.Tied d.It depends 29

Race V n n / n 15 vs. a.Left b.Right c.Tied d.It depends 30

Race VI 8 2lg(n) 3n 7 + 7n vs. a.Left b.Right c.Tied d.It depends 31

Race VII mn 3 2mn2mn vs. a.Left b.Right c.Tied d.It depends 32

Silicon Downs Post #1 n 3 + 2n 2 n 0.1 n + 100n 0.1 5n 5 n n / lg n mn 3 Post #2 100n log n 2n + 10 log n n! 1000n 15 3n 7 + 7n 2 m n Winner O(n 2 ) O(log n) TIE O(n) O(n 5 ) O(n 15 ) O(n 6 ) IT DEPENDS 33

Mounties Find Silicon Downs Fixed The fix sheet (typical growth rates in order) –constant:O(1) –logarithmic:O(log n)(log k n, log n 2  O(log n)) –poly-log:O(log k n) –linear:O(n) –log-linear:O(n log n) –superlinear:O(n 1+c )(c is a constant > 0) –quadratic:O(n 2 ) –cubic:O(n 3 ) –polynomial:O(n k )(k is a constant) –exponential:O(c n )(c is a constant > 1) “tractable” “intractable” 34

Today’s Outline Programming Project #1 and Forming Teams Brief Proof Reminder Asymptotic Analysis, Briefly Silicon Downs and the SD Cheat Sheet Asymptotic Analysis, Proofs and Programs Examples and Exercises 35

Terminology Given an algorithm whose running time is T(n) –T(n)  O(f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 1, log n, n, 100n  O(n) –T(n)   (f(n)) if there are constants c and n 0 such that T(n)  c f(n) for all n  n 0 n, n 2, n, n 3 log n   (n) –T(n)   (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) n, 2n, 100n, 0.01 n + log n   (n) –T(n)  o(f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) 1, log n, n 0.99  o(n) –T(n)   (f(n)) if T(n)  O(f(n)) and T(n)   (f(n)) n 1.01, n 2, n, n 3 log n   (n) 36

Types of analysis Orthogonal axes –bound flavor upper bound (O, o) lower bound ( ,  ) asymptotically tight (  ) –analysis case worst case (adversary) average case best case “common” case –analysis quality loose bound (any true analysis) tight bound (no better bound which is asymptotically different) 37

Analyzing Code C++ operations - constant time consecutive stmts- sum of times conditionals- sum of branches, condition loops- sum of iterations function calls- cost of function body Above all, use your head! 38

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 1: What’s the input size n ? 39

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 2: What kind of analysis should we perform? Worst-case? Best-case? Average-case? Expected-case, amortized, … 40

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 3: How much does each line cost? (Are lines the right unit?) 41

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 4: What’s T(n) in its raw form? 42

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 5: Simplify T(n) and convert to order notation. (Also, which order notation: O, o, , ,  ?) 43

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 6: Casually name-drop the appropriate complexity class in order to sound bracingly cool to colleagues: “Oh, linear search? That’s tractable, polynomial time. What polynomial? Linear, duh. See the name?! I hear it’s sub- linear on quantum computers, though. Wild, eh?” 44

Analyzing Code // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Step 7: Prove your complexity class by finding constants c and n 0 such that for all n  n 0, T(n)  cn. You usually won’t do this in practice. 45

Today’s Outline Programming Project #1 and Forming Teams Brief Proof Reminder Asymptotic Analysis, Briefly Silicon Downs and the SD Cheat Sheet Asymptotic Analysis, Proofs and Programs Examples and Exercises 46

More Examples Than You Can Shake a Stick At (#0) // Linear search find(key, array) for i = 1 to length(array) - 1 do if array[i] == key return i return -1 Here’s a whack-load of examples for us to: 1.find a function T(n) describing its runtime 2.find T(n) ’s complexity class 3.find c and n 0 to prove the complexity class 47

METYCSSA (#1) for i = 1 to n do for j = 1 to n do sum = sum + 1 Time complexity: a.O(n) b.O(n lg n) c.O(n 2 ) d.O(n 2 lg n) e.None of these 48

METYCSSA (#2) i = 1 while i < n do for j = i to n do sum = sum + 1 i++ Time complexity: a.O(n) b.O(n lg n) c.O(n 2 ) d.O(n 2 lg n) e.None of these 49

METYCSSA (#3) i = 1 while i < n do for j = 1 to i do sum = sum + 1 i += i Time complexity: a.O(n) b.O(n lg n) c.O(n 2 ) d.O(n 2 lg n) e.None of these 50

METYCSSA (#4) Conditional if C then S 1 else S 2 Loops while C do S 51

METYCSSA (#5) Recursion almost always yields a recurrence Recursive max: if length == 1: return arr[0] else: return larger of arr[0] and max(arr[1..length-1]) T(1) <= b T(n) 1 Analysis T(n) <= c + c + T(n - 2) (by substitution) T(n) <= c + c + c + T(n - 3) (by substitution, again) T(n) <= kc + T(n - k) (extrapolating 0 < k  n ) T(n) <= (n – 1)c + T(1) = (n – 1)c + b (for k = n - 1 ) T(n)  52

METYCSSA (#6): Mergesort Mergesort algorithm –split list in half, sort first half, sort second half, merge together T(1) <= b T(n) 1 Analysis T(n) <= 2T(n/2) + cn <= 2(2T(n/4) + c(n/2)) + cn = 4T(n/4) + cn + cn <= 4(2T(n/8) + c(n/4)) + cn + cn = 8T(n/8) + cn + cn + cn <= 2 k T(n/2 k ) + kcn (extrapolating 1 < k  n ) <= nT(1) + cn lg n (for 2 k = n or k = lg n ) T(n)  53

METYCSSA (#7): Fibonacci Recursive Fibonacci: int Fib(n) if (n == 0 or n == 1) return 1 else return Fib(n - 1) + Fib(n - 2) Lower bound analysis T(0), T(1) >= b T(n) 1 Analysis let  be (1 +  5)/2 which satisfies  2 =  + 1 show by induction on n that T(n) >= b  n

Example #7 continued Basis: T(0)  b > b  -1 and T(1)  b = b  0 Inductive step: Assume T(m)  b  m - 1 for all m < n T(n)  T(n - 1) + T(n - 2) + c  b  n-2 + b  n-3 + c  b  n-3 (  + 1) + c = b  n-3  2 + c  b  n-1 T(n)  Why? Same recursive call is made numerous times. 55

Example #7: Learning from Analysis To avoid recursive calls –store all basis values in a table –each time you calculate an answer, store it in the table –before performing any calculation for a value n check if a valid answer for n is in the table if so, return it This strategy is called “memoization” and is closely related to “dynamic programming” How much time does this version take? 56

Final Concrete Example (#8): Longest Common Subsequence Problem: given two strings (m and n), find the longest sequence of characters which appears in order in both strings –lots of applications, DNA sequencing, blah, blah, blah Example: –“ search me ” and “ insane method ” = “ same ” 57

Abstract Example (#9): It’s Log! Problem: find a tight bound on T(n) = lg(n!) Time complexity: a.O(n) b.O(n lg n) c.O(n 2 ) d.O(n 2 lg n) e.None of these 58

Log Aside log a b means “the exponent that turns a into b ” lg x means “ log 2 x ” (our usual log in CS) log x means “ log 10 x ” (the common log) ln x means “ log e x ” (the natural log) But… O(lg n) = O(log n) = O(ln n) because: log a b = log c b / log c a (for c > 1 ) so, there’s just a constant factor between log bases 59

Asymptotic Analysis Summary Determine what characterizes a problem’s size Express how much resources (time, memory, etc.) an algorithm requires as a function of input size using O(),  (),  () –worst case –best case –average case –common case –overall 60

Some Well-Known Horses from the Downs For general problems (not particular algorithms): We can prove lower bounds on any solution. We can give example algorithms to establish “upper bounds” for the best possible solution. Searching an unsorted list using comparisons: provably  (n), linear search is O(n). Sorting a list using comparisons: provably  (n lg n), mergesort is O(n lg n). 61

Aside: Who Cares About  (lg (n!))? Can You Beat O(n lg n) Search? Chew these over: 1.How many values can you represent with n bits? 2.Comparing two values (x < y) gives you one bit of information. 3.There are n! possible ways to reorder a list. We could number them: 1, 2, …, n! 4.Sorting basically means choosing which of those reorderings/numbers you’ll apply to your input. 5.How many comparisons does it take to pick among n! numbers? 62

Some Well-Known Horses from the Downs Searching and Sorting: polynomial time, tractable Traveling Salesman Problem: non-deterministic polynomial… can check a guess in polynomial time, maybe exponential time to solve. Are problems in NP really in P? $1M prize to prove yea or nay. 63

Some Well-Known Horses from the Downs Searching and Sorting numbers: P, tractable Traveling Salesman Problem: NP, intractable Kolmogorov Complexity: uncomputable The Kolmogorov Complexity of a string is its shortest representation in a given language. In other words, it tells us how much the string can possibly be compressed. Can’t be computed. Pithy but hand-wavy proof: What’s: “The smallest positive integer that cannot be described in fewer than fourteen words.” 64

Learning Goals By the end of this unit, you will be able to… Define which program operations we measure in an algorithm in order to approximate its efficiency. Define “input size” and determine the effect (in terms of performance) that input size has on an algorithm. Give examples of common practical limits of problem size for each complexity class. Give examples of tractable, intractable, and undecidable problems. Given code, write a formula which measures the number of steps executed as a function of the size of the input (N). Continued… 65

Learning Goals By the end of this unit, you will be able to… Compute the worst-case asymptotic complexity of an algorithm (e.g., the worst possible running time based on the size of the input (N)). Categorize an algorithm into one of the common complexity classes. Explain the differences between best-, worst-, and average- case analysis. Describe why best-case analysis is rarely relevant and how worst-case analysis may never be encountered in practice. Given two or more algorithms, rank them in terms of their time and space complexity. 66

To Do Sign up as a pair on Vista… with someone from your lab! Start HW 1 Start Project 1 Read Epp and Koffman 2.6 Prepare for upcoming labs 67

Coming Up Recursion and Induction Loop Invariants and Proving Program Correctness Call Stacks and Tail Recursion First Written Homework due (Sep 23) First Programming Project due (Sep 30) 68