data structures r e c u r s i o n azhar maqsood school of electrical engineering and computer...

30
Data Structures Data Structures R e c u r s i o n Azhar Maqsood Azhar Maqsood School of Electrical Engineering and School of Electrical Engineering and Computer Sciences (SEECS-NUST) Computer Sciences (SEECS-NUST)

Upload: daniella-patterson

Post on 12-Jan-2016

228 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Data StructuresData Structures

R e c u r s i o n

Azhar MaqsoodAzhar MaqsoodSchool of Electrical Engineering and Computer Sciences School of Electrical Engineering and Computer Sciences

(SEECS-NUST)(SEECS-NUST)

Page 2: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive ThinkingRecursive Thinking

Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways

Recursion splits a problem into one or more simpler versions of itself

Page 3: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

RecursionRecursion Typically problems are solved by dividing into sub-

problems These sub-problems may also be divided into

further smaller sub-sub-problems When the sub-problems are small enough to solve

directly the process stops. A recursive algorithm follows the same technique

by solving a given problem through solution of two or more easier to solve sub-problems

A recursive call is a function call in which the called function is the same as the one making the call.

Page 4: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive DefinitionsRecursive Definitions

Recursion– Process of solving a problem by reducing it to

smaller versions of itself

Recursive definition– Definition in which a problem is expressed in

terms of a smaller version of itself– Has one or more base cases

Page 5: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive DefinitionsRecursive DefinitionsRecursive algorithm

– Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself

– Has one or more base cases– Implemented using recursive functions

Recursive function– function that calls itself

Page 6: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive DefinitionsRecursive Definitions

Base case– Case in recursive definition in which the

solution is obtained directly – Stops the recursion

General case– Case in recursive definition in which a smaller

version of itself is called– Must eventually be reduced to a base case

Page 7: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

RecursionRecursion

Define an object in terms of itself

Consists of two parts– Recursive step– Base step

f(n) = f(n-1) * n;f(1) = 1;

Page 8: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Outline of a Recursive FunctionOutline of a Recursive Function

if (answer is known) provide the answer

else make a recursive call to solve a smaller version of the same problem

basecase

recursivecase -

Page 9: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Tracing a Recursive FunctionTracing a Recursive FunctionRecursive function

– Has unlimited copies of itself– Every recursive call has

its own codeown set of parametersown set of local variables

Page 10: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Tracing a Recursive FunctionTracing a Recursive FunctionAfter completing recursive call

Control goes back to calling environment

Recursive call must execute completely before control goes back to previous call

Execution in previous call begins from point immediately following recursive call

Page 11: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

How Recursion WorksHow Recursion Works

a recursive function call is handled like any other function call

each recursive call has an activation record on the stack

– stores values of parameters and local variables when base case is reached return is made to

previous call– the recursion “unwinds”

Page 12: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Finding a Recursive SolutionFinding a Recursive Solution A recursive solution to a problem must be written

carefully.

The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved.

This easily solved situation is called the base case.

Each recursive algorithm must have at least one base case, as well as the general case.

Page 13: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Designing Recursive FunctionsDesigning Recursive FunctionsUnderstand problem requirementsDetermine limiting conditionsIdentify base cases

Page 14: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Designing Recursive FunctionsDesigning Recursive FunctionsProvide direct solution to each base caseIdentify general case(s) Provide solutions to general cases in terms of

smaller versions of itself

Page 15: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Steps to Design a Recursive AlgorithmSteps to Design a Recursive Algorithm

There must be at least one case (the base case), for a small value of n, that can be solved directly

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Recognize the base case and provide a solution to it Devise a strategy to split the problem into smaller

versions of itself while making progress toward the base case

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 16: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

FactorialFactorial4! = 4 * 3 * 2 * 1n! = n * (n-1) * … * 14! = 4 * 3!n! = n * (n-1)!

factorial(n) = n * factorial(n-1)//recursive stepfactorial(1) = 1 //base step

Page 17: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive Factorial FunctionRecursive Factorial Functionint fact(int num)

{

if(num == 0)

return 1;

else

return num * fact(num – 1);

}

Page 18: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Execution Model for FactorialExecution Model for Factorial

Fact(2)

n

if( n <= 0) return 1;} else { return n * Fact(n - 1);

}

n

if( n <= 0) return 1;} else { return n * Fact(n - 1);

}

n

if( n <= 0) return 1;} else { return n * Fact(n - 1);

}

2 1 0

n! = 1, if n = 0n * (n-1)! if n > 0

Page 19: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive Factorial TraceRecursive Factorial Trace

Page 20: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Calling Stack Of Recursive CallCalling Stack Of Recursive Callint fact(int n){if(n==1)

return 1;else

return (fact(n-1)*n);}

Calling stack

fact(n)

fact(n-1)

Page 21: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Calling StackCalling Stack

int fact(int n){//base stepif (n == 1)

return 1;//recursive stepint temp = n * fact(n-1);return temp;

}fact(4) = 4! = 24

Calling stack

fact(4)

fact(3)

fact(2)

fact(1)

temp=?n=4

n=3

n=2

n=1

temp=?

temp=?

temp=1

temp=24

temp=6

temp=2

temp=1

Page 22: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Another Recursive DefinitionAnother Recursive Definition

Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, ….

1 for n == 1fib(n) = 1 for n == 2 fib(n-2) + fib(n-1) for n>2

Page 23: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Fibonacci NumbersFibonacci Numbers0, 1, 1, 2, 3, 5, 8, 13, …

Fib(n) = Fib(n-1) + Fib(n-2)Fib(0) = 0Fib(1) = 1

Base case

Recursive case

Page 24: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursive Fibonacci repeats Recursive Fibonacci repeats computationscomputations

fib (4) : 3

fib (3) : 2 fib (2) : 1

fib (2) : 1 fib (1) : 1

fib (1) : 1 fib (0) : 0

fib (1) : 1 fib (0) : 0

Page 25: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Evaluating Exponents RecursivelyEvaluating Exponents Recursively

int power(int k, int n) {

// raise k to the power n

if (n == 0)

return 1;

else

return k * power(k, n – 1);

}

Page 26: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursion or Iteration?Recursion or Iteration?Two ways to solve particular problem

– Iteration– Recursion

Iterative control structures: uses looping to repeat a set of statements

Tradeoffs between two options– Sometimes recursive solution is easier– Recursive solution is often slower

Page 27: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Recursion Versus IterationRecursion Versus IterationIn iteration, a loop repetition condition

determines whether to repeat the loop body or exit from the loop

In recursion, the condition usually tests for a base case

You can always write an iterative solution to a problem that is solvable by recursion

Recursive code may be simpler than an iterative algorithm and thus easier to write, read, and debug

Page 28: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

When to use recursion?When to use recursion?

if recursive and iterative algorithms are of similar efficiency --> iteration

if the problem is inherently recursive and a recursive algorithm is less complex to write than an iterative algorithm --> recursion

recursion very inefficient when values are recomputed

Page 29: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Efficiency of RecursionEfficiency of RecursionRecursive methods often have slower execution

times when compared to their iterative counterparts

The overhead for loop repetition is smaller than the overhead for a method call and return

If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method– The reduction in efficiency does not outweigh the

advantage of readable code that is easy to debug

Page 30: Data Structures R e c u r s i o n Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

Pitfalls of Recursion One pitfall of recursion is infinite regress, i.e. a

chain of recursive calls that never stops. E.g. if you forget the base case. Make sure you have enough base cases.

Recursion can be less efficient than an iterative implementation. This can happen because there is recalculation of intermediate results.

There can be extra memory use because recursive calls must be stored on the execution stack.