cop 3503 fall 2012 shayan javed lecture 17

22
1/ 22 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 17 Programming Fundamentals using Java

Upload: shilah

Post on 22-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 17. Programming Fundamentals using Java. Recursion. Definition. Method where: Solution to a problem depends on solutions of smaller instances of the same problem. Example: Merge Sort. Split Now Sort and Merge. Recursive Function. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP  3503  FALL 2012 Shayan Javed Lecture 17

1/ 22

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 17

Programming Fundamentals using Java

Page 2: COP  3503  FALL 2012 Shayan Javed Lecture 17

2/ 22

Recursion

Page 3: COP  3503  FALL 2012 Shayan Javed Lecture 17

3/ 22

Definition

Method where:

Solution to a problem depends on solutions of smaller instances of the same problem.

Page 4: COP  3503  FALL 2012 Shayan Javed Lecture 17

4/ 22

Example: Merge Sort

Split

Now Sort and Merge

i 0 1 2 3 4 5

A 55 19 100 45 87 33

0 1 2

55 19 100

3 4 5

45 87 33

0

55

1 2

19 100

3

45

4 5

87 33

Page 5: COP  3503  FALL 2012 Shayan Javed Lecture 17

5/ 22

Recursive Function

A function which calls itself to solve a problem.

Alternative to iterative solutions

Most programming languages support recursion Some only support recursion (Functional languages)

Page 6: COP  3503  FALL 2012 Shayan Javed Lecture 17

6/ 22

Problems solved by Recursion

Mathematical problems (Factorial, Fibonacci sequence, etc.)

Searching and sorting algorithms (binary search, merge sort, etc.)

Traversing file systems

Traversing data structures (linked lists, trees)

Etc…

Page 7: COP  3503  FALL 2012 Shayan Javed Lecture 17

7/ 22

Defining a Recursive Function

1. When does the recursion stop? Have to stop at some point otherwise you’ll run into

problems Also known as the “base case”

2. Repeat the process by calling the function again

Page 8: COP  3503  FALL 2012 Shayan Javed Lecture 17

8/ 22

Defining a Recursive Function

Example:

int recursiveMethod(parameters) {if (baseCase)

return someValue;else

return recursiveMethod(modifiedParameters);

}

Page 9: COP  3503  FALL 2012 Shayan Javed Lecture 17

9/ 22

Fibonacci sequence

A sequence of numbers:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

How would you implement this recursively?

Page 10: COP  3503  FALL 2012 Shayan Javed Lecture 17

10/ 22

Fibonacci sequence

int fibonacci(int n) {if (n == 0)

return 0;else if (n == 1)

return 1;else

return fibonacci(n-1) + fibonacci(n-2);}

Page 11: COP  3503  FALL 2012 Shayan Javed Lecture 17

11/ 22

Fibonacci sequenceHow would you implement this iteratively? (Using loops)

int fibonacci(int n) {int fn1 = 0, fn2 = 1;int prev;

for(int i = 0; i < num; i++) {prev = fn1;fn1 = fn2;fn2 = fn2 + prev;}

return fn1;}

Let’s run both

Page 12: COP  3503  FALL 2012 Shayan Javed Lecture 17

12/ 22

Fibonacci sequence

Recursive version seems to be much slower.

Why?

What happens when a function call is made?

Page 13: COP  3503  FALL 2012 Shayan Javed Lecture 17

13/ 22

Function calls

When a method is called:

The method reference and arguments/parameters are pushed onto the calling method’s operand stack

A new stack frame is created for the method which is called. Contains variables, operand stack, etc. for it.

Stack frame is pushed onto the Java Stack.

When method is done, it is popped from the Java Stack.

Page 14: COP  3503  FALL 2012 Shayan Javed Lecture 17

14/ 22

Function calls

Java Stack Approximation after calling fibonacci(2):

1 is returned to Main

MainMain

fib(2)

Main

1

Main

fib(2)

fib(1)

Main

1

fib(0)

fib(2)

Main

1

0

fib(2)

Page 15: COP  3503  FALL 2012 Shayan Javed Lecture 17

15/ 22

Function calls

Program counters have to be updated, local variables, stacks, method references, etc.

So a lot of work is done when methods are called.

Imagine calling fibonacci(1000). Results in “stack overflow” (no available memory on

the call stack)

Page 16: COP  3503  FALL 2012 Shayan Javed Lecture 17

16/ 22

Recursion

Advantages: Very simple to write Programs are short Sometimes recursion is the only option

Disadvantages: Extra storage required Slow

Page 17: COP  3503  FALL 2012 Shayan Javed Lecture 17

17/ 22

More Examples

Iterative version of Binary Search:

int binarySearch(int[] array, int key, int left, int right){while (left <= right) {int middle = (left + right)/2; // Compute mid pointif (key < array[mid]) {right = mid-1; // repeat search in bottom half} else if (key > array[mid]) {left = mid + 1; // Repeat search in top half} else {return mid; // found!}}return -1; // Not found

}How would you implement recursively?

Page 18: COP  3503  FALL 2012 Shayan Javed Lecture 17

18/ 22

Binary Search

Identify base case first When do we stop?

When do you repeat?

Page 19: COP  3503  FALL 2012 Shayan Javed Lecture 17

19/ 22

Binary Search

int binarySearch(int[] array, int key, int left, int right) {if (left > right) // base case 1: not foundreturn -1;

int mid = (left + right)/2; // Compute mid point

if (key == array[mid]) // base case 2: found!return middle; else if (key < array[mid]) // repeat search in upper halfreturn binarySearch(array, key, left, mid-1);else // lower halfreturn binarySearch(array, key, mid, right);

}

Page 20: COP  3503  FALL 2012 Shayan Javed Lecture 17

20/ 22

File Systems

What happens when you run this command in Linux?

rm –r *

Recursively (“-r”) goes through every file in the directory and sub-directories and deletes it.

Has to use recursion

Page 21: COP  3503  FALL 2012 Shayan Javed Lecture 17

21/ 22

File Systems

How do you think the program “rm” is implemented?

Probably something like this (pseudocode):

function rm(directory):File[] files = directory.getAllFiles();for each file in files:if (file is directory)rm(file);elsedelete file;

Page 22: COP  3503  FALL 2012 Shayan Javed Lecture 17

22/ 22

Summary

Recursion is useful for writing simple programs.

Alternative to iterative solutions, but slower and requires more space.

Some solutions require recursion (file directory traversal)