CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

20
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Transcript of CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Page 1: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852Data StructuresLECTURE 12B

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

Page 2: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Agenda• Recursion– Definition– Examples• Math Functions• Algorithms• Data

– Computability

Page 3: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

What Is Recursion?

• Recursion, n. see Recursion• Some functional programming

languages don’t have loops (equivalent to imperative looping constructs)

• A function “calls itself” – directly or not

Recursion in computer science is a method where the solution to a problem depends on solutions

to smaller instances of the same problem.

DEFINITION

Page 4: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Visual Recursion

Source: Wikipedia

Page 5: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursion in Mathematics

• By this base case and recursive rule, one can generate the set of all natural numbers

1 is a natural number, and each natural number has a successor, which is also a

natural number.

SET THEORY DEFINITION FOR NATURAL NUMBERS

Page 6: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursive Function

• fib(0) = 0• fib(1) = 1• fib(n) = fib(n-1) + fib(n-2)

Fibonacci Series0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987…

Leonardo Pisano Bigolloc. 1170 – c. 1250

Liber Abaci(The Book of Calculation)

(the growth of a population of rabbitsunder idealized assumptions)

Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, Fibonacci

1.618 – Golden Ratio

Page 7: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Fibonacci Numbers BINARY REPRESENTATION

http://mathworld.wolfram.comThe first 511 terms of the Fibonacci sequence represented in binary.

Page 8: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Fibonacci Function

1: int fib(int n) {2: if (n==0)3: return 0;4: if (n==1) 5: return 1;6: return fib(n-1) + fib(n-2); 7: }

DEMO!

Page 9: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Fibonacci Numbers - Plotted

1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940414243444546474849500

1000000000

2000000000

3000000000

4000000000

5000000000

6000000000

7000000000

8000000000

9000000000

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 250

5000

10000

15000

20000

25000

30000

35000

40000

45000

50000

Page 10: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Factorial Function !

1: int fact(int n) {2: if (n==0)3: return 0;6: return n*fact(n-1); 7: }

0! = 1n! = n * (n-1)

Page 11: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Greatest Common Divisor

long gcd(long a, long b) { // a > b, b != 0 if (b==0) return a; return gcd(b, a % b);}

EUCLID’S ALGORITHM

Page 12: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursively Count From n Downto 0

void countDown(int n) { for (int i = n; i >= 0; i--) System.out.print(i);}

void countDown(int n) { System.out.print(i); countDown(n-1);}

void countDown(int n) { System.out.print(n); if (n == 0) return; countDown(n-1);}

9876543210

Page 13: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Other Examples of Recursive Algorithms

• Linear search through an array• Binary search in an array

Page 14: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursive Algorithm Design

• Identify the base case – the small problem that can be solved

directly• Split the big problem into smaller

subproblems– Divide-and-Conquer

• Combine the solutions to the smaller subproblems

Page 15: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Tail Recursion

• Occurs when the recursive call is at the “end” of the recursive function.

• The algorithm can usually be rewritten to use (pure) iteration instead.

• Computational Equivalence:– f1() { while(C) { S; }; return Q; }– f2() { if (C) then { S; return f(); } else { return Q; } }

Page 16: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursion versus Iteration• Both allow to repeat an operation• You can always write a recursive

solution to a problem solvable by iteration.

• The reverse is not necessarily true:– Some recursion problems (non-tail

recursion) cannot be solved by iteration alone.

Page 17: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

“Proving” Correctness• Verify that the base case is recognized

and solved correctly.• Verify that each recursive case makes

progress towards the base case.• Verify that if all smaller problems are

solved correctly, then the original problem is also solved correctly.

Page 18: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Recursive Data Structures

• Linked-List:– Node– Node, Linked-List

• Tree– Node– Left-(Sub) Tree– Right-(Sub)Tree

Page 19: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

CS-2852 Data Structures, Andrew J. Wozniewicz

Summary• Recursion– Recursive Functions• Fibonacci• Factorial• Greatest Common Divisor

– Recursive Algorithms• Designing Recursive Algorithms• Recursion versus Iteration• Proving Correctness – by Induction• Tail Recursion

– Recursive Data Structures

Page 20: CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Questions?

Image copyright © 2010 andyjphoto.com