Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:

Similar presentations


Presentation on theme: "Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:"— Presentation transcript:

1 Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties: Accurate specification of the input Definiteness of each instruction Termination Correctness Description of result or effect

2 Recursive Procedures A procedure that under certain circumstances invokes itself is called recursive Is the following a recursive procedure? void foo () { cout << “Hello World!” << endl; foo(); }

3 The Idea Behind a Recursive Procedure
A recursive procedure should involve a conditional test that divides execution into two or more cases: A trivial version of the problem (i.e. the base case) Solved without a recursive call A more complicated version of the problem (i.e. recursive case) Solved by: Making the problem a little bit simpler, and Calling the recursive procedure to solve the simpler problem

4 Recursive Procedure – An Example
void printUnsigned (unsigned int val) { // print the character representation of the unsigned argument if (val < 10) // base case: val is only one digit printChar(digitChar(val)); else { printUnsigned (val /10); // recursive call: // print high order part printChar(digitChar(val % 10)); // print last character }

5 Recursive Procedure – Illustrated
main() calls printUnsigned (456) Recursive case invoked – calls printUnsigned (45) Recursive case invoked – calls printUnsigned (4) Base case invoked – prints ‘4’ Prints ‘5’ Prints ‘6’

6 Recursive Procedure – Illustrated (cont)

7 Let’s Write Our Own Recursive Procedure!
The Problem: write a function, fac, that computes n! (n factorial) int fac (unsigned int n) { // computes n! …. }

8 The Recursive Factorial Procedure
What is the base case? Let’s write it. What is the recursive case? Does it: Make the problem a little bit simpler? Call the recursive procedure to solve the simpler problem?

9 The Recursive Factorial Procedure
Is this an algorithm? A set of instructions used to solve a specific problem Is it a useful algorithm? Accurate specification of the input Definiteness of each instruction Termination Correctness Description of result or effect

10 Proving Termination In order to prove termination, find a property or value that possesses all three following characteristics: Can be placed in 1-to-1 correspondence with the integers Is non-negative Decreases as algorithm executes

11 Loops are Usually Easy

12 Loops are Usually Easy double power (double base, unsigned int n) {
// return the value yielded by raising base to the exponent, n. double result = 1.0; for (unsigned int i=0;i<n; i++) result *= base; return result; } The quantity n-i: Can be placed in 1-to-1 correspondence with the integers Is non-negative Decreases as algorithm executes

13 The Value Need Not Be in the Algorithm
unsigned int gcd(unsigned int n unsigned int m) { // compute the gcd of two positive integers assert (n > 0 && m > 0); while (n != m) { if (n > m) n = n –m; else m = m –n; } return n; } // consider the quantity (n+m)

14 Other Important Considerations
Time to execute (efficiency) How long does an algorithm take to arrive at a result In the best case? In the average case? In the worst case? Are there other algorithms that solve the same problem more quickly? Space utilization


Download ppt "Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:"

Similar presentations


Ads by Google