1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
MATH 224 – Discrete Mathematics
HST 952 Computing for Biomedical Scientists Lecture 10.
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Complexity Analysis (Part I)
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Complexity Analysis (Part I)
RECURSION Self referential functions are called recursive (i.e. functions calling themselves) Recursive functions are very useful for many mathematical.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
CSE 1342 Programming Concepts Recursion. Overview of Recursion nRecursion is present when a function is defined in terms of itself. nThe factorial of.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Searching Topics Sequential Search Binary Search.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Algorithm Analysis 1.
Complexity Analysis (Part I)
Analysis of Algorithms
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Analysis of Algorithms
Recursion Chapter 12.
Towers of Hanoi Move n (4) disks from pole A to pole C
Analysis of Algorithms
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Algorithm Efficiency Chapter 10.
Recursion Data Structures.
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Algorithmic complexity
Complexity Analysis (Part I)
Analysis of Algorithms
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
Presentation transcript:

1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends on many factors: size of input speed of machine quality of source code quality of compiler These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure computing time as T(n)= computing time of an algorithm for input of size n = number of times the instructions are executed. Read §7.4

2 Example: Calculating the Mean / * Algorithm to find the mean of n real numbers. Receive:integer n  1 and an array x[0],..., x[n–1] of real numbers Return:The mean of x[0],..., x[n–1] */ 1. Initialize sum to Initialize index variable i to While i < n do the following: 4.a. Add x[i] to sum. 5.b. Increment i by Calculate and return mean = sum / n. T(n) = ___________

3 Big Oh Notation The computing time of an algorithm on input of size n, T(n), is said to have order of magnitude f(n), written T(n) is O(f(n)) if there is some ____________________ such that _____________ for all sufficiently ____________________. Another way of saying this: The complexity of the algorithm is O(f(n)). Example: For the Mean-Calculation Algorithm: T(n) is _________ Note that constants and multiplicative factors are ignored. f(n) is usually simple: n, n 2, n 3,... 2 n 1, log 2 n n log 2 n log 2 log 2 n

4 Worst-case Analysis The arrangement of the input items may affect the computing time. How then to measure performance? best case – not very informative average - too difficult to calculate worst case - usual measure / * Linear search of the list a[0],..., a[n – 1]. Receive:An integer n an array of n elements and item Return:found = true and loc = position of item if the search is successful; otherwise, found is false. */ 1. found = false. 2. loc = While (loc < n && !found ) 4.If item = a[loc] found = true// item found 5. Elseincrement loc by 1// keep searching Worst case: Item not in the list: T L (n) is _______ Average case (assume equal distribution of values) is ______

5 Binary Search /* Binary search of the list a[0],..., a[n – 1] in which the items are in ascending order. Receive: integer n and an array of n elements and item. Return:found = true and loc = position of item if the search successful otherwise, found is false. */ 1. found = false. 2. first = last = n – While (first < last && !found ) 5.Calculate loc = (first + last) / 2. 6.If item a[loc] then 9.first = loc + 1.// search last half 10.Else found = true.// item found Worst case: Item not in the list: T B (n) = ____________ Makes sense: each pass cuts search space in half!

6 Recursion A very old idea, with its roots in mathematical induction. It always has:  An anchor (or base or trivial) case  An inductive case So, a function is said to be defined recursively if its definition consists of  An ___________________________ in which the function’s value is defined for one or more values of the parameters  An ____________________________________in which the function’s value (or action) for the current parameter values is defined in terms of previously defined function values (or actions) and/or parameter values. Example: Factorials  Base case: 0! = 1  Inductive case: n! = n*(n-1)! Read §7.1, 7.3 Skim §7.2 int Factorial(int n) { if (n < 2) return 1; else return n * Factorial(n - 1); }

7 Computing times of recursive functions Have to solve a recurrence relation. // Towers of Hanoi void Move(int n, char source, char destination, char spare) { if (n <= 1) // anchor (base) case cout << "Move the top disk from " << source << " to " << destination << endl; else { // inductive case Move(n-1, source, spare, destination); Move(1, source, destination, spare); Move(n-1, spare, destination, source); } T(n) = ____________

8 There are several common applications of recursion where a corresponding iterative solution may not be obvious or easy to develop. Classic examples of such include Towers of Hanoi, path generation, multidimensional searching and backtracking. However, many common textbook examples of recursion are tail-recursive, i.e. the last statement in the recursive function is a recursive invocation. Tail-recursive functions can be (much) more efficiently written using a loop. Comments on Recursion

9 // Counting the number of digits in a positive integer int F(unsigned n, int count) {// recursive, expensive! if (n < 10) return 1 + count; else return F(n/10, ++count); } int F(unsigned n) {// iterative, cheaper int count = 0; while (n >= 10) { count++; n /= 10; } return count; }

10 // Fibonacci numbers int F(unsigned n) {// recursive, expensive! if (n < 3) return 1; else return F(n –1) + F(n - 2); } int F(unsigned n) {// iterative, cheaper int fib1 = 1, fib2 = 1; for (int i = 3; i < n; i++) { int fib3 = fib1 + fib2; fib1 = fib2; fib2 = fib3; } return fib2; } More complicated recursive functions are sometimes replaced by iterative functions that use a stack to store the recursive calls. (See Section 7.3) Iterative: O(_____) Recursive: O(_______)

11 Recursive Examples

12 Recursive Blobs const int maxRow = 15; const int maxCol = 15; typedef char Grid[maxRow][maxCol]; void initGrid(Grid g, int & rows, int & cols); int EatAllBlobs(Grid g, int rows, int cols); void EatOneBlob(Grid g, int x, int y, int rows, int cols); int main() { Grid grid; int rows, cols; initGrid(grid, rows, cols); cout << "\nThere are " << EatAllBlobs(grid, rows, cols) << " blobs in there\n"; }

13 void initGrid(Grid g, int & rows, int & cols) { cout << "# rows, # cols: "; cin >> rows >> cols; cout << "Enter grid of " << rows << " x " << cols << " grid of *'s and.'s:\n"; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) cin >> (g[i][j]); } int EatAllBlobs(Grid g, int rows, int cols) { int count = 0; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) if (g[i][j] == '*') { EatOneBlob(g, rows, cols, i, j); count++; } return count; }

14 void EatOneBlob(Grid g, int rows, int cols, int x, int y) { if (x = rows || y >= cols) return; if (g[x][y] != '*') return; // else g[x][y] = '.';// mark as visited EatOneBlob(g, rows, cols, x - 1, y); EatOneBlob(g, rows, cols, x + 1, y); EatOneBlob(g, rows, cols, x, y - 1); EatOneBlob(g, rows, cols, x, y + 1); } # rows, # cols: 3 12 Enter grid of 3 x 12 grid of *'s and.'s:..***.*** *.*.*... **..***...** There are 3 blobs in there

15 Recursive Permutations

16 Recursive Permutations

17 Recursive Permutations

18 Recursive Permutations