Analysis of Algorithms Algorithm Input Output Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms1.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
The Seven Functions. Analysis of Algorithms. Simple Justification Techniques. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer Goodrich,
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
PSU CS 311 – Algorithm Design and Analysis Dr. Mohamed Tounsi 1 CS 311 Design and Algorithms Analysis Dr. Mohamed Tounsi
Analysis of Performance
CS2210 Data Structures and Algorithms Lecture 2:
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis Algorithm Analysis I Reading: Weiss, chap.2.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
1 Dr. J. Michael Moore Data Structures and Algorithms CSCE 221 Adapted from slides provided with the textbook, Nancy Amato, and Scott Schaefer.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Dr. Alagoz CHAPTER 2 Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of.
CSC401 – Analysis of Algorithms Chapter 1 Algorithm Analysis Objectives: Introduce algorithm and algorithm analysis Discuss algorithm analysis methodologies.
CSC401 – Analysis of Algorithms Lecture Notes 2 Asymptotic Analysis Objectives: Mathematics foundation for algorithm analysis Amortization analysis techniques.
Analysis of Algorithms Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
Introduction to Analysis of Algorithms CS342 S2004.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
CSC Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
1 Dr. Scott Schaefer Data Structures and Algorithms CSCE 221H.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Analysis of Algorithms
Analysis of Algorithms
Definition of Computer Science
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
03 Algorithm Analysis Hongfei Yan Mar. 9, 2016.
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Data Structures and Algorithms CSCE 221H
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Analysis of Algorithms Algorithm Input Output Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms1

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms2 Part 1: Running Time

Running Time Most algorithms transform input objects into output objects. The running time of an algorithm typically grows with the input size. Average case time is often difficult to determine. We focus on the worst case running time. o Easier to analyze o Crucial to applications such as games, finance and robotics Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms3

Experimental Studies Write a program implementing the algorithm Run the program with inputs of varying size and composition, noting the time needed Plot the results Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms4

Limitations of Experiments It is necessary to implement the algorithm, which may be difficult, time consuming or costly. Results may not be indicative of the running time on other inputs not included in the experiment. In order to compare two algorithms, the same hardware and software environments must be used. Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms5

Theoretical Analysis Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, n Takes into account all possible inputs Allows us to evaluate the speed of an algorithm independent of the hardware/software environment Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms6

Pseudocode High-level description of an algorithm More structured than English prose Less detailed than a program Preferred notation for describing algorithms Hides program design issues Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms7

Pseudocode Details Control flow – if … then … [else …] – while … do … – repeat … until … – for … do … – Indentation replaces braces Method declaration Algorithm method (arg [, arg…]) Input … Output … Control flow – if … then … [else …] – while … do … – repeat … until … – for … do … – Indentation replaces braces Method declaration Algorithm method (arg [, arg…]) Input … Output … Method call method (arg [, arg…]) Return value return expression Expressions:  Assignment  Equality testing n 2 Superscripts and other mathematical formatting allowed Method call method (arg [, arg…]) Return value return expression Expressions:  Assignment  Equality testing n 2 Superscripts and other mathematical formatting allowed Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms8

The Random Access Machine (RAM) Model A RAM consists of A CPU A potentially unbounded bank of memory cells, each of which can hold an arbitrary number or character Memory cells are numbered and accessing any cell in memory takes unit time Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms

Seven Important Functions  Seven functions that often appear in algorithm analysis: Constant  1 Logarithmic  log n Linear  n N-Log-N  n log n Quadratic  n 2 Cubic  n 3 Exponential  2 n  In a log-log chart, the slope of the line corresponds to the growth rate Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms10

Primitive Operations Basic computations performed by an algorithm Identifiable in pseudocode Largely independent from the programming language Exact definition not important (we will see why later) Assumed to take a constant amount of time in the RAM model Examples: – Evaluating an expression – Assigning a value to a variable – Indexing into an array – Calling a method – Returning from a method Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms11

Counting Primitive Operations By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms12 STEP345678TOTAL # ops221+2n2n0 to 2n14n+6 to 6n+6

Estimating Running Time Algorithm arrayMax executes 6n + 6 primitive operations in the worst case, 4n + 6 in the best case. Define: a = Time taken by the fastest primitive operation b = Time taken by the slowest primitive operation Let T(n) be worst-case time of arrayMax. Then a (4n + 6)  T(n)  b(6n + 6) Hence, the running time T(n) is bounded by two linear functions Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms13

Growth Rate of Running Time Changing the hardware/ software environment o Affects T(n) by a constant factor, but o Does not alter the growth rate of T(n) The linear growth rate of the running time T(n) is an intrinsic property of algorithm arrayMax Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms14

Why Growth Rate Matters Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms15 if runtime is...time for n + 1time for 2 ntime for 4 n c lg nc lg (n + 1)c (lg n + 1)c(lg n + 2) c nc (n + 1)2c n4c n c n lg n ~ c n lg n + c n 2c n lg n + 2cn 4c n lg n + 4cn c n 2 ~ c n 2 + 2c n4c n 2 16c n 2 c n 3 ~ c n 3 + 3c n 2 8c n 3 64c n 3 c 2 n c 2 n+1 c 2 2n c 2 4n runtime quadruples when problem size doubles

Comparison of Two Algorithms Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms16 insertion sort is n 2 / 4 merge sort is2 n lg n sort a million items? insertion sort takes roughly 70 hours while merge sort takes roughly 40 seconds This is a slow machine, but if 100 x as fast, then it’s 40 minutes versus less than 0.5 seconds

Constant Factors The growth rate is not affected by o constant factors or o lower-order terms Examples o 10 2 n is a linear function o 10 5 n n is a quadratic function Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms17

Big-Oh Notation Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n 0 such that f(n)  cg(n) for n  n 0 Example: 2n + 10 is O(n) o 2n + 10  cn o (c  2) n  10 o n  10/(c  2) o Pick c = 3 and n 0 = 10 Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms18

Big-Oh Example Example: the function n 2 is not O(n) o n 2  cn o n  c o The above inequality cannot be satisfied for all sufficiently large n, since c must be a constant Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms19

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms20 More Big-Oh Examples  7n - 2 7n-2 is O(n) need c > 0 and n 0  1 such that 7 n - 2  c n for n  n 0 this is true for c = 7 and n 0 = 1 7n-2 is O(n) need c > 0 and n 0  1 such that 7 n - 2  c n for n  n 0 this is true for c = 7 and n 0 = 1  3 n n n n is O(n 3 ) need c > 0 and n 0  1 such that 3 n n  c n 3 for n  n 0 this is true for c = 4 and n 0 = 21 3 n n is O(n 3 ) need c > 0 and n 0  1 such that 3 n n  c n 3 for n  n 0 this is true for c = 4 and n 0 = 21  3 log n log n + 5 is O(log n) need c > 0 and n 0  1 such that 3 log n + 5  c log n for n  n 0 this is true for c = 8 and n 0 = 2 3 log n + 5 is O(log n) need c > 0 and n 0  1 such that 3 log n + 5  c log n for n  n 0 this is true for c = 8 and n 0 = 2

Big-Oh and Growth Rate The big-Oh notation gives an upper bound on the growth rate of a function The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the growth rate of g(n) We can use the big-Oh notation to rank functions according to their growth rate Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms21

Big-Oh Rules If f(n) is a polynomial of degree d, then f(n) is O(n d ), i.e., o drop lower-order terms o drop constant factors Use the smallest possible class of functions o Say “2n is O(n)” instead of “2n is O(n 2 )” Use the simplest expression of the class o Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)” Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms22

Asymptotic Algorithm Analysis The asymptotic analysis of an algorithm determines the running time in big-Oh notation To perform the asymptotic analysis o We find the worst-case number of primitive operations executed as a function of the input size o We express this function with big-Oh notation Example: o We say that algorithm arrayMax “runs in O(n) time” Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitive operations Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms23

Computing Prefix Averages We further illustrate asymptotic analysis with two algorithms for prefix averages The i-th prefix average of an array X is average of the first (i + 1) elements of X: A[i] = (X[0] + X[1] + … + X[i])/(i+1) Computing the array A of prefix averages of another array X has applications to financial analysis Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms24

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms25 Prefix Averages (Quadratic) The following algorithm computes prefix averages in quadratic time by applying the definition

Arithmetic Progression The running time of prefixAverage1 is O( …+ n) The sum of the first n integers is n(n + 1) / 2 – There is a simple visual proof of this fact Thus, algorithm prefixAverage1 runs in O(n 2 ) time Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms26

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms27 Prefix Averages 2 (Linear) The following algorithm uses a running sum to improve efficiency Algorithm prefixAverage2 runs in O(n) time!

Math you need to Review Summations Powers Logarithms Proof techniques – Induction –... Basic probability Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms28

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms29 Relatives of Big-Oh big-Omega f(n) is  (g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n)  c g(n) for n  n 0 big-Theta f(n) is  (g(n)) if there are constants c’ > 0 and c’’ > 0 and an integer constant n 0  1 such that c’ g(n)  f(n)  c’’ g(n) for n  n 0

Intuition for Asymptotic Notation Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms30  big-Oh f(n) is O(g(n)) if f(n) is asymptotically less than or equal to g(n)  big-Omega f(n) is  (g(n)) if f(n) is asymptotically greater than or equal to g(n)  big-Theta f(n) is  (g(n)) if f(n) is asymptotically equal to g(n)

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms31 Example Uses of the Relatives of Big-Oh f(n) is  (g(n)) if it is  (n 2 ) and O(n 2 ). We have already seen the former, for the latter recall that f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n) < c g(n) for n  n 0. Let c = 5 and n 0 = 1. f(n) is  (g(n)) if it is  (n 2 ) and O(n 2 ). We have already seen the former, for the latter recall that f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n) < c g(n) for n  n 0. Let c = 5 and n 0 = 1. 5n 2 is  (n 2 ) f(n) is  (g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n)  c g(n) for n  n 0. Let c = 1 and n 0 = 1. f(n) is  (g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n)  c g(n) for n  n 0. Let c = 1 and n 0 = 1. 5n 2 is  (n) f(n) is  (g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n)  c g(n) for n  n 0. Let c = 5 and n 0 = 1. f(n) is  (g(n)) if there is a constant c > 0 and an integer constant n 0  1 such that f(n)  c g(n) for n  n 0. Let c = 5 and n 0 = 1. 5n 2 is  (n 2 )

Part 1: Summary Analyzing running time of algorithms o Experimentation & its limitations o Theoretical analysis Pseudo-code RAM: Random Access Machine 7 important functions Asymptotic notations: O(),  (),  () Asymptotic running time analysis of algorithms Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms 32

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms 33

Part 2: Correctness Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 34

Outline Iterative Algorithms: Assertions and Proofs of Correctness Binary Search: A Case Study Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 35

Assertions An assertion is a statement about the state of the data at a specified point in your algorithm. An assertion is not a task for the algorithm to perform. You may think of it as a comment that is added for the benefit of the reader. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 36

Loop Invariants Binary search can be implemented as an iterative algorithm (it could also be done recursively). Loop Invariant: An assertion about the current state useful for designing, analyzing and proving the correctness of iterative algorithms. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 37

Other Examples of Assertions Pre-conditions: Any assumptions that must be true about the input instance. Post-conditions: The statement of what must be true when the algorithm/program returns. Exit-condition: The statement of what must be true to exit a loop. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 38

Iterative Algorithms Take one step at a time towards the final destination loop { if (done) { exit loop } take step } loop { if (done) { exit loop } take step } Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 39

From Pre-Condition to Post-Condition Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 40 Loop Invariant Pre-Condition Post-Condition Pre-Loop Loop Post-Loop

Establishing Loop Invariant Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 41 1.from the pre-condition on the input instance, establish the loop invariant. 1

Maintain Loop Invariant Suppose that 1)We start in a safe location (loop invariant just established from pre-condition) 2)If we are in a safe location (loop invariant), we always step to another safe location (loop invariant) Can we be assured that the computation will always keep us in a safe location? By what principle? Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 42 2

Maintain Loop Invariant Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms

Ending The Algorithm Define Exit Condition Termination: With sufficient progress, the exit condition will be met. When we exit, we know o loop invariant is true o exit condition is true 3)from these we must establish the post-condition. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 44 3

45 Iterative Algorithm Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms Pre-loop Loop-Invariant Loop Body Post-Condition Pre-Condition YES NO Exit-Condition Post-loop

Definition of Correctness &  If the input meets the pre-conditions, then the output must meet the post-conditions. If the input does not meet the preconditions, then nothing is required. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 46

Binary Search: a case study Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 47

Define Problem: Binary Search PreConditions – Key 25 – Sorted indexed List A PostConditions – Find key in list (if there) Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 48

Define Loop Invariant Maintain a sub-list. LI: If the key is contained in the original list, then the key is contained in the sub-list. key Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 49

Define Step Cut sub-list in half. Determine which half the key would be in. Keep that half. key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. mid Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 50

Define Step It is faster not to check if the middle element is the key. Simply continue. key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 51

Make Progress The size of the list becomes smaller Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 52

Exit Condition LI: If the key is contained in the original list, then the key is contained in the sub-list. Sub-list contains one element If element = key, return associated entry. Otherwise return false. key 25 Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 53

Running Time key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 54

Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 55

Simple, right? Although the concept is simple, binary search is notoriously easy to get wrong. Why is this? Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 56

Boundary Conditions The basic idea behind binary search is easy to grasp. It is then easy to write pseudo-code that works for a ‘typical’ case. Unfortunately, it is equally easy to write pseudo-code that fails on the boundary conditions. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 57

Boundary Conditions or What condition will break the loop invariant? Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 58 if key  A[mid] then q  mid else p  mid + 1 if key  A[mid] then q  mid else p  mid + 1 if key < A[mid] then q  mid else p  mid + 1 if key < A[mid] then q  mid else p  mid + 1

Boundary Conditions key mid Bug!! Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 59

Boundary Conditions OK Not OK !! Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 60 if key  A[mid] then q  mid else p  mid + 1 if key  A[mid] then q  mid else p  mid + 1 if key < A[mid] then q  mid - 1 else p  mid if key < A[mid] then q  mid - 1 else p  mid if key < A[mid] then q  mid else p  mid + 1 if key < A[mid] then q  mid else p  mid + 1

key Boundary Conditions or Shouldn’t matter, right? Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 61

674 Boundary Conditions key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. mid Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 62

Boundary Conditions key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. mid Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 63

Boundary Conditions key If key ≤ A[mid], then key is in left half. If key > A[mid], then key is in right half. Another bug! No progress toward goal: Loops Forever! mid Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 64

Boundary Conditions OK Not OK !! Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 65

Getting it Right How many possible algorithms? How many correct algorithms? Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 66 then q  mid - 1 else p  mid then q  mid - 1 else p  mid or ? if key < A[mid] or ?

Alternative Algorithm: Less Efficient but More Clear Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 67 Still O(log n), but with slightly larger constant. Still O(log n), but with slightly larger constant.

Part 2: Summary From Part 2, you should be able to:  Use the loop invariant method to think about iterative algorithms.  Prove that the loop invariant is established.  Prove that the loop invariant is maintained in the ‘typical’ case.  Prove that the loop invariant is maintained at all boundary conditions.  Prove that progress is made in the ‘typical’ case  Prove that progress is guaranteed even near termination, so that the exit condition is always reached.  Prove that the loop invariant, when combined with the exit condition, produces the post-condition. Last Update: Aug 21, 2014EECS2011: Analysis of Algorithms 68

Last Update: Aug 21, 2014 EECS2011: Analysis of Algorithms 69