Algorithms An algorithm is a set of instructions














- Slides: 14

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

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(); }

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

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

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

Recursive Procedure – Illustrated (cont)

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! …. }

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

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

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

Loops are Usually Easy

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

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)

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