COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Similar documents
CS Non-recursive and Recursive Algorithm Analysis

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

CS473 - Algorithms I

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

Analysis of Algorithms

Asymptotic Analysis and Recurrences

Data Structures and Algorithms CSE 465

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

CS 310 Advanced Data Structures and Algorithms

Reductions, Recursion and Divide and Conquer

3.1 Asymptotic notation

EECS 477: Introduction to algorithms. Lecture 5

CS173 Running Time and Big-O. Tandy Warnow

CS 4407 Algorithms Lecture 2: Growth Functions

COMPUTER ALGORITHMS. Athasit Surarerks.

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

CSC Design and Analysis of Algorithms. Lecture 1

Asymptotic Algorithm Analysis & Sorting

CSC2100B Data Structures Analysis

Cpt S 223. School of EECS, WSU

Asymptotic Analysis 1

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

Growth of Functions (CLRS 2.3,3)

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Computational Complexity

CS Analysis of Recursive Algorithms and Brute Force

data structures and algorithms lecture 2

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

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

Module 1: Analyzing the Efficiency of Algorithms

Data Structures and Algorithms. Asymptotic notation

COMP Analysis of Algorithms & Data Structures

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

COMP Analysis of Algorithms & Data Structures

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

COMP 9024, Class notes, 11s2, Class 1

Advanced Counting Techniques. Chapter 8

Advanced Counting Techniques

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

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

Analysis of Algorithms - Using Asymptotic Bounds -

The Time Complexity of an Algorithm

Algorithms Design & Analysis. Analysis of Algorithm

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

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

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

Analysis of Algorithms

Running Time Evaluation

Copyright 2000, Kevin Wayne 1

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

The Time Complexity of an Algorithm

Topic 17. Analysis of Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Omega notation. Transitivity etc.

CS481: Bioinformatics Algorithms

Ch01. Analysis of Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

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

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

Analysis of Algorithms

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

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

An analogy from Calculus: limits

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CISC 235: Topic 1. Complexity of Iterative Algorithms

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Analysis of Algorithms

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Why do we need math in a data structures course?

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Practical Session #3 - Recursions

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

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

Advanced Counting Techniques. 7.1 Recurrence Relations

Review Of Topics. Review: Induction

Lecture 1 - Preliminaries

COMP 355 Advanced Algorithms

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

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

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

Asymptotic Running Time of Algorithms

CS483 Design and Analysis of Algorithms

Data Structures and Algorithms Chapter 2

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

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

Introduction to Algorithms

Recursion: Introduction and Correctness

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

Divide and Conquer Problem Solving Method

Introduction to Algorithms and Asymptotic analysis

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Introduction to Algorithms: Asymptotic Notation

CSC236 Week 4. Larry Zhang

Agenda. We ve discussed

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

Ch 01. Analysis of Algorithms

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

Transcription:

COMP 555 Bioalgorithms Fall 2014 Lecture 3: Algorithms and Complexity Study Chapter 2.1-2.8

Topics Algorithms Correctness Complexity Some algorithm design strategies Exhaustive Greedy Recursion Asymptotic complexity measures 2

What is an algorithm? An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated problem. input algorithm problem output Problem: Complexity Algorithm: Correctness Complexity 3

Problem: US Coin Change Input an amount of money 0 M < 100 in cents Output: M cents in US coins using the minimal number of coins Example 72 cents Is it correct? Two quarters Two dimes Two pennies 4

Algorithm 1: Greedy strategy 72 cents Greedy coin alg Use large denominations as long as possible Two quarters, 22 cents left Algorithm description r M q r / 25 r r 25 q d r /10 r r 10 d n r / 5 p r r r 5 n Two dimes, 2 cents left Is it correct? Two pennies Can we generalize it? 5

Algorithm 2: Exhaustive strategy Enumerate all combinations of coins. Record the combination totaling to M with fewest coins All is impossible. Limit the multiplicity of each coin! First try (80,000 combinations) coin Quarter Dime Nickel Penny multiplicity 0..3 0..9 0..19 0..99 Better (200 combinations) coin Quarter Dime Nickel Penny multiplicity 0.. 3 0.. 4 0.. 1 0.. 4 Is it correct? 6

Correctness An algorithm is correct only if it produces correct result for all input instances. If the algorithm gives an incorrect answer for one or more input instances, it is an incorrect algorithm. US coin change problem It is easy to show that the exhaustive algorithm is correct The greedy algorithm is correct but we didn t really show it 7

Observations Given a problem, there may be many correct algorithms. They give identical outputs for the same inputs They give the expected outputs for any valid input The costs to perform different algorithms may be different. US coin change problem The exhaustive algorithm checks 200 combinations The greedy algorithm performs just a few arithmetic operations 8

Change Problem: generalization Input: an amount of money M an array of denominations c = (c 1, c 2,,c d ) in order of decreasing value Output: the smallest number of coins To show an algorithm was incorrect we showed an input for which it produced the wrong result. How do we show that an algorithm is correct? M = M 40 c = c (25, = (c 1 20,, c 2 10,,,c 5, d 1) ) r M n 0 for k 1 to d i k return n r / c n n + i k r r i c k k k? n 3 The correct answer should be 2. Is it correct? Incorrect algorithm! 9

How to Compare Algorithms? Complexity the cost of an algorithm can be measured in either time and space Correct algorithms may have different complexities. How do we assign cost for time? Roughly proportional to number of instructions performed by computer Exact cost is difficult to determine and not very useful Varies with computer, particular input, etc. How to analyze an algorithm s complexity Depends on algorithm design 10

Recursive Algorithms Recursion is an algorithm design technique for solving problems in terms of simpler subproblems The simplest versions, called base cases, are merely declared. Recursive definition: factorial( n) = n factorial( n 1) Base case: factorial( 1) = 1 Easy to analyze Thinking recursively 11

Towers of Hanoi There are three pegs and a number of disks with decreasing radii (smaller ones on top of larger ones) stacked on Peg 1. Goal: move all disks to Peg 3. Rules: When a disk is moved from one peg it must be placed on another peg. Only one disk may be moved at a time, and it must be the top disk on a tower. A larger disk may never be placed upon a smaller disk. 12

A single disk tower 1 2 3 13

A single disk tower 1 2 3 14

A two disk tower 1 2 3 15

Move 1 1 2 3 16

Move 2 1 2 3 17

Move 3 1 2 3 18

A three disk tower 1 2 3 19

Move 1 1 2 3 20

Move 2 1 2 3 21

Move 3 1 2 3 22

Move 4 1 2 3 23

Move 5 1 2 3 24

Move 6 1 2 3 25

Move 7 1 2 3 26

Simplifying the algorithm for 3 disks 1 2 3 Step 1. Move the top 2 disks from 1 to 2 using 3 as intermediate 27

Simplifying the algorithm for 3 disks 1 2 3 Step 2. Move the remaining disk from 1 to 3 28

Simplifying the algorithm for 3 disks 1 2 3 Step 3. Move 2 disks from 2 to 3 using 1 as intermediate 29

Simplifying the algorithm for 3 disks 1 2 3 30

The problem for N disks becomes A base case of a one-disk move. A recursive step for moving n-1 disks. To move n disks from Peg 1 to Peg 3, we need to Move (n-1) disks from Peg 1 to Peg 2 Move the n th disk from Peg 1 to Peg 3 Move (n-1) disks from Peg 2 to Peg 3 The number of disk moves is We move the n-1 stack twice T (1) = 1 T ( n) = 2T ( n 1) + 1 = 2 n 1 Exponential algorithm 31

Towers of Hanoi If you play HanoiTowers with... it takes... 1 disk 1 move 2 disks 3 moves 3 disks 7 moves 4 disks 15 moves 5 disks 31 moves... 20 disks... 1,048,575 moves 32 disks... 4,294,967,295 moves 32

Sorting A very common problem is to arrange data into either ascending or descending order Viewing, printing Faster to search, find min/max, compute median/mode, etc. Lots of sorting algorithms From the simple to very complex Some optimized for certain situations (lots of duplicates, almost sorted, etc.) 33

Selection Sort Find the smallest element and swap it with the first: Find the next smallest element and swap it with the second: 27 12 3 18 11 7 3 12 27 18 11 7 Do the same for the third element: 3 7 27 18 11 12 In-place sort And the fourth: 3 7 11 18 27 12 Finally, the fifth: 3 7 11 12 27 18 Completely sorted: 3 7 11 12 18 27 34

Selection sort def selectionsortrecursive(a,first,last): if (first < last): index = indexofmin(a,first,last) temp = a[index] a[index] = a[first] a[first] = temp a = selectionsortrecursive(a,first+1,last) return a (n -1) swaps Quadratic in time n( n 1) 2 1 comparisons def indexofmin(arr,first,last): index = first for k in xrange(index+1,last): if (arr[k] < arr[index]): index = k return index 35

Year 1202: Leonardo Fibonacci He asked the following question: How many pairs of rabbits are produced from a single pair in n months if every month each pair of rabbits more than 1 month old produces a new pair? Here we assume that each pair born has one male and one female and breeds indefinitely The initial pair at month 0 are newborns Let f(n) be the number of rabbit pairs present at the beginning of month n 36

Fibonacci Number month 0 (newborn) rabbit pairs 1 1 1 2 2 3 3 4 5 37

Clearly, we have: Fibonacci Number f(0) = 1 (the original pair, as newborns) f(1) = 1 (still the original pair because newborns need to mature a month before they reproduce) f(n) = f(n-1) + f(n-2) in month n we have the f(n-1) rabbit pairs present in the previous month, and newborns from the f(n-2) rabbit pairs present 2 months earlier f: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, The solution for this recurrence is (n > 0): f ( n) = 1 5 1+ 2 n 5 1 2 38 n 5

Fibonacci Number Recursive Algorithm Exponential time! n def fibonaccirecursive(n): if (n <= 1): return 1 else: a = fibonaccirecursive(n-1) b = fibonaccirecursive(n-2) return a+b n - 1 n - 2 n - 2 n - 3 n - 3 n - 4 n - 3 n - 4 n - 4 n - 5 n - 4 n - 5 n - 5 n - 6 39

Fibonacci Number Iterative Algorithm Linear time! def fibonacciiterative(n): f0, f1 = 1, 1 for i in xrange(0,n): f0, f1 = f1, f0 + f1 return f0 n n - 1 n - 2 n - 2 n - 3 n - 3 n - 4 n - 3 n - 4 n - 4 n - 5 n - 4 n - 5 n - 5 n - 6 40

Orders of magnitude 10^1 10^2 Number of students in computer science department 10^3 Number of students in the college of art and science 10^4 Number of students enrolled at UNC 10^10 Number of stars in the galaxy 10^20 Total number of all stars in the universe 10^80 Total number of particles in the universe 10^100 << Number of moves needed for 400 disks in the Towers of Hanoi puzzle Towers of Hanoi puzzle is computable but it is NOT feasible. 41

Is there a real difference? Growth of functions 42

Asymptotic Notation Order of growth is the interesting measure: Highest-order term is what counts As the input size grows larger it is the high order term that dominates Θ notation: Θ(n 2 ) = this function grows similarly to n 2. Big-O notation: O (n 2 ) = this function grows no faster than n 2. Describes an upper bound. 43

f ( n) = O( g( n) ) What does it mean? 0 If f(n) = O(n 2 ), then: Big-O Notation : there exist positive constants c f ( n) cg( n) for all n n0 f(n) can be larger than n 2 sometimes, but and We can choose some constant c and some value n 0 such that for every value of n larger than n 0 : f(n) < cn 2 That is, for values larger than n 0, f(n) is never more than a constant multiplier greater than n 2 Or, in other words, f(n) does not grow more than a constant factor faster than n 2. n 0 such that 44

Visualization of O(g(n)) cg(n) f(n) n 0 45

8/26/2014 46 Big-O Notation Comp 555 Bioalgorithms (Fall 2014) ) ( ) ( 2 2 ) ( 4 3 ) ( 20 1,000,000 ) ( 150,000 1,000,000 ) ( 2 2 2.1 2 3 2 2 2 2 2 2 2 n O n n O n n O n n O n n n O n n O n + = + = + + = + =

Big-O Notation Prove that: Let c = 21 and n 0 = 4 ( 2 ) 2 20n + 2n + 5 = O n 21n 2 > 20n 2 + 2n + 5 for all n > 4 n 2 > 2n + 5 for all n > 4 TRUE 47

f Θ-Notation Big-O is not a tight upper bound. In other words n = O(n 2 ) Θ provides a tight bound ( n) = Θ( g( n) ) : there exist positive constants c 0 c 1 g n = O(n 2 ) Θ(n 2 ) 200n 2 = O(n 2 ) = Θ(n 2 ) n 2.5 O(n 2 ) Θ(n 2 ) ( n) f ( n) c2g( n) for all n n0 1, c 2, and n 0 such that 48

Visualization of Θ(g(n)) c 2 g(n) f(n) c 1 g(n) n 0 49

Some Other Asymptotic Functions Little o A non-tight asymptotic upper bound n = o(n 2 ), n = O(n 2 ) 3n 2 o(n 2 ), 3n 2 = O(n 2 ) Ω A lower bound f ( n) = Ω( g( n) ) n 2 = Ω(n) : there exist positive constants c f The difference between big-o and little-o is subtle. For f(n) = O(g(n)) the bound 0 f(n) c g(n), n > n 0 holds for any c. For f(n) = o(g(n)) the bound 0 f(n) < c g(n), n > n 0 holds for all c. ( n) cg( n) for all n n0 and ω A non-tight asymptotic lower bound f(n) = Θ(n) f(n) = O(n) and f(n) = Ω(n) n 0 such that 50

Visualization of Asymptotic Growth o(f(n)) O(f(n)) Θ(f(n)) f(n) Ω(f(n)) ω(f(n)) n 8/26/2014 0 Comp 555 Bioalgorithms (Fall 2014) 51

Analogy to Arithmetic Operators f( n)= O g n f( n)= Ω g n f( n)= Θ g n f( n)= o g n f( n)= ω g n ( ( ) f g ( ( ) f g ( ( ) f = g ( ( ) f < g ( ( ) f > g 52

Measures of complexity Best case Super-fast in some limited situation is not very valuable information Worst case Good upper-bound on behavior Never gets worse than this Average case Averaged over all possible inputs Most useful information about overall performance Can be hard to compute precisely 53

Complexity Space Complexity Sp(n) : how much memory an algorithm needs (as a function of n) Space complexity Sp(n) is not necessarily the same as the time complexity T(n) T(n) Sp(n) 54

Next Time Our first bio algorithm Read book 4.1 4.3 55