0% found this document useful (0 votes)
1 views47 pages

Lecture # 03 - New

The document outlines the steps for developing algorithms, emphasizing the Top-Down Design method and recursion. It explains how to break down complex problems into smaller subproblems, define base and recursive cases, and ensure termination of recursion. Additionally, it provides examples of recursive algorithms for computing powers and finding maximum values in arrays.

Uploaded by

Saif Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views47 pages

Lecture # 03 - New

The document outlines the steps for developing algorithms, emphasizing the Top-Down Design method and recursion. It explains how to break down complex problems into smaller subproblems, define base and recursive cases, and ensure termination of recursion. Additionally, it provides examples of recursive algorithms for computing powers and finding maximum values in arrays.

Uploaded by

Saif Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

Recap

Lecture No 2

Department of Computer Science 1


Steps of development of an
algorithm
 Understand the problem
/Writing the statement of the
problem
 Develop a model/choose
appropriate data structure
 Select appropriate designing
technique.
 Specify the algorithm
 Prove correctness of the
algorithm
 Analyze the complexity of the
algorithm
 Implement the algorithm/code
Department of Computer Science
Lecture No 03

Top-Down Design: Design using


Recursion
(Recursive Algorithm)

Department of Computer Science


Today
Covered
After completing this lecture you
understand
 The “Top Down Design Method”.
 The Importance of Algorithm Design by
Top Down Design Method.
 The order in which a Recursive Function
executes its statement.
 To convert an iterative algorithm into
recursive algorithm.

Department of Computer Science


Recursive Algorithm

Department of Computer Science


Solving Complex problem
 How to design algorithm of a complex
problem? i.e. How to solve a complex
problem?
 Break down the problem into segments
(smaller chunks/parts) and then target to
solve the segments, ultimately the problem
will be solved.
 Top down is a design method that involves
taking a complex problem and breaking
down into smaller sub problems.
 These subproblems can often be broken
down into even smaller sub-problems. This
process is known as decomposition
Department of Computer Science
Design by Top-down approach: General Idea
 A good way to approach top-down design
is to follow these steps:
 To solve a large problem(P), break the
problem into several smaller
tasks(P1,P2,P3,P4……Pn) and work on each
task separately.

Department of Computer Science


Design by Top-down approach: General Idea
 To solve each task, treat it as a new
problem that can be broken down into
smaller problems.
 Repeat this process with each new task
until each can be solved without the need
for any further decomposition.

Department of Computer Science


Design by Top-down approach: General Idea
 There are two key point in top-down approach
 Key Point 1: How to decompose bigger problem into smaller problem?
 Decrease the input size of the problem

 Divide the input size of the problem

Problem

Newly decomposed Problem

Problem

Newly decomposed Newly decomposed


Problem Problem
In both cases the subproblems are identical to original
problem
Department of Computer Science
Design by Top-down approach: General Idea
 There are two key point in top-down approach
 How to decompose bigger problem into smaller problem?
 Decreasing
 Dividing
 In both cases the subproblems are identical to original problem
 The decomposition process is repeated in term of itself. i.e., problem is reducing into smaller versions of itself.
 Key Point 2: When to stop decomposing problem.
 Stop decomposition when the solution is obtained directly.
 In algorithm design this approach is known as recursion.
 So, let us discuss recursion in detail.

Department of Computer Science


What isRecursion
Recursion?
 Dictionary definition:
 A problem-solving method of
“decomposing bigger problems into smaller sub-
problems that are identical to itself.”
 Recursion:
 Process of solving a problem by reducing it to
smaller versions of itself

Department of Computer Science


What isRecursion
Recursion?
 There are two key point in recursion
 Base case(Stopping Criteria):
 Case in recursive definition in which the solution is
obtained directly.
 Stops the recursion.
 General case(Decomposition):
 Case in recursive definition in which a smaller
version of itself is called.
 Must eventually be reduced to a base case.

Department of Computer Science


Recursive
Recursion
Method

Recursion (Size of Problem: n)


if( Stopping Criteria) // Base Case
return X
else
// General Case: decomposition strategy e.g., n-1, n/2
etc.
return Recursion (decomposition
strategy)
Department of Computer Science
Recursive
Recursive Algorithm
Algorithm
 Recursive algorithm:
 Algorithm that finds the solution of a given
problem by reducing the problem into smaller
versions of itself.
 Has one or more base cases.
 Implemented using recursive methods.
 Recursive method:
 Method that calls itself.

Department of Computer Science 14


Designing Recursive
Steps for designing Recursive
Algorithms
Algorithm
 Step 1: Define a Base Case:
 Identify the simplest case for which the
solution is known or trivial.
 This is the stopping condition for the
recursion, as it prevents the function from
infinitely calling itself.
 Step 2: Define a recursive case:
 Define the problem in terms of smaller
subproblems.
 Break the problem down into smaller
versions of itself and call the function
recursively to solve each subproblem.

Department of Computer Science 15


Designing Recursive
Steps for designing Recursive
Algorithms
Algorithm
 Step 3: Ensure the recursion
terminates:
 Make sure that the recursive function
eventually reaches the base case and
does not enter an infinite loop.
 Step 4: Combine the solutions:
 Combine the solutions of the
subproblems to solve the original
problem.

Department of Computer Science 16


Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes an Iterativ an = a × a × … … × a
e
 Solution:
 What is input?
 Two numbers say a and n.
 What should be the output?
 return the value of an
 How we decompose
Top down:
given problem into smaller version of itself?
recursive
 an = a×an-1 1 if n = 0
 What is base condition?f(n)
= a×f(n-1) if n > 0
 For n = 0, return 1
Department of Computer Science 17
Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes
Iterative a n
Recursive/Top Down
an = a × a × … … × a 1 if n = 0
f(n)
= a×f(n-1) if n > 0

Can you improve an


computation ?
Department of Computer Science 18
Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes an
Decrease by a constant
 Solution: factor
 What is input?
 Two numbers say a and n.
 What should be the output?
 Return the value of an
 How we decompose given problem into
smaller version of itself?
 Divide the power in half Decrease by a constant
at each step factor
 What is base case?
 For n =0, return 1
Department of Computer Science 19
Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes an
Decrease by a constant
 Solution: factor

Effectively, when
power is not divisible
 Suppose we want to compute 95
by 2, we make power
even by taking out
the extra 9. Then we
already know the
solution when power
is divisible by 2.
Divide the power by 2
and multiply the base
Department of Computer Science to itself. 20
Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes an
Decrease by a constant
 Solution: factor

 Now our problem is to find (812) * 9

Department of Computer Science 21


Example-1:
Brute Force Example
Computing an
 Design a recursive algorithm that
computes an
Decrease by a constant
 Solution: Recursive Versions factor

Department of Computer Science 22


Example-1:
Brute Force Example
Computing an
 Conversion of recursive algorithm that
computes an into iterative
Decrease by a constant
 Solution: Iterative factor
Versions

Department of Computer Science 23


Brute Example-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1] Iterativ
 Solution: e
 What is input
 An array of n items.
 What should be the output
 Maximum number
 How we decompose given problem into
smaller version of itself?

Department of Computer Science 24


Brute Example-2: Maximum
Force Example
Number
 How we decompose given problem into smaller
version of itself?

 Get the array for which the minimum is to be found


 Recursively find the minimum according to the
following:
 Recursively traverse the array from the end
 Base case: If the remaining array is of length 1, return
the only present element i.e., arr[0]
if n = 1
return A[0]
Department of Computer Science 25
Brute Example-2: Maximum
Force Example
Number
 General Case:
 Recursive call: If the base case is not met,
then call the function by passing the array of one
size less from the end, i.e., from A[0] to A[n-2].
 Return statement: At each recursive call
(except for the base case), return the maximum
of the last element of the current array (i.e., A[n-
1]) and the element returned from the previous
recursive call.
return min(A[n-1], recursive_function(A[0----n-
2])

Department of Computer Science 26


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution:Recursiv
e
ALGORITHM findMax(A,n)
// This algorithms determines the value of the largest
element in a given array A
// Input: An array A[0---n-1] of real numbers.
// Output: The value of the largest element in A
if n = 1
return A[0]
else
return max(A[n-1],findMax(A, n-1)

Department of Computer Science 27


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution:Recursiv
e
ALGORITHM findMax(A,n)
if n = 1
return A[0]
else
return max(A[n-1],findMax(A,
n-1)

Department of Computer Science 28


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution:Recursive(complete Detail abstract
version)

Department of Computer Science 29


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution: Iterativ
e

Recursiv
e

Department of Computer Science 30


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution: Iterativ
Recursive 2 e

Recursive 1

Department of Computer Science 31


Example-2: Dry Run
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 9 7 5

9 7

Department of Computer Science


BruteExample-2: Maximum
Force Example
Number
 Design an algorithm that find maximum
in an array A[0..n-1]
 Solution:

Department of Computer Science 33


Example-3: Recursive Algorithm
Recursive for
calls - example
Factorial
Problem: Compute factorial of a positive
number n.

Convert iterative version into


recursive

Department of Computer Science 34


Example-3: Recursive Algorithm
Recursive for
calls - example
Factorial
fact(4) 24 stack = []
fact(4): stack = [4]
4*fact(3) 4*6
fact(3): stack = [3,4] stack = [4]
fact(3) 6
3*fact(2) 3*2
fact(2): stack = [2,3,4] stack = [3,4]
fact(2) 2

2*fact(1) 2*1
fact(1): stack = [1,2,3,4]
fact(1) 1
fact(n)
If n<=1 then
return←1 Sequence of Back to
else recursive the calling function
return ←n*fact(n-1) calls

Department of Computer Science 35


Example 4:Recursive Algorithmcalls
Recursive for Fibonacci
- example
series

Problem: Compute Fibonacci series of a positive


number n.

Convert iterative version into


recursive
Department of Computer Science 36
Top-down vs Bottom
up
S.
N Recursion Iteration
o.

Terminates when
Terminates when the
1 the condition
base case becomes true.
becomes false.

2 Used with functions. Used with loops.

Every recursive call Every iteration does


3 needs extra space in the not require any
stack memory. extra space.

4 Smaller code size. Larger code size.


Department of Computer Science
Top-down vs Bottom
up
Top down Bottom up
Largest to smallest Smallest to largest
problem
Mainly Recursive Mainly Iterative

Department of Computer Science


Top-down vs Bottom
up

Department of Computer Science


Top-down vs Bottom
up

Department of Computer Science


When
Brute Force to use
Example
Recursion?
 Recursion is one of the best solutions for a
task that can be defined with its similar
subtask.
 Advantages: This is an easy approach to
solving problems.
 The biggest advantage of Recursion is that it uses
less number of code lines and hence the code looks
shorter and cleaner.
 Disadvantages:
 The code becomes hard to understand and analyze.
 A lot of memory is used to hold the copies of
recursive functions in the memory.
 Time and Space complexity is increased.

DepartmentRecursion is generally slower than iteration.
of Computer Science 41
Advantage
Bruteand disadvantages
Force Exampleof
Recursion.
 Advantages: This is an easy approach to solving
problems.
 The biggest advantage of Recursion is that it uses less
number of code lines and hence the code looks shorter
and cleaner.
 Performing the same operations multiple times with
different inputs.
 Disadvantages:
 The code becomes hard to understand and analyze.
 A lot of memory is used to hold the copies of recursive
functions in the memory.
 Recursion uses more memory, because the recursive
function adds to the stack (LAST IN FIRST OUT) with each
recursive call, and keeps the values there until the call is
finished.

Department of Computer Science 42


Advantage
Bruteand disadvantages
Force Exampleof
Recursion.
 Disadvantages:
 If the base case is not reached or not defined,
then the stack overflow problem may arise.
 Time and Space complexity is increased.
 Recursion is generally slower than iteration.

Department of Computer Science 43


How we do
it?

CONCLUSION

Department of Computer Science


UPSHOT
Brute Force Example
 Top-Down Design: A design process where an
overall task is broken down into smaller tasks.
 Decomposition: The process of breaking down a
large task into smaller tasks.
 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.

Department of Computer Science 45


What is Recursion?
Recursion
UPSHOT
 General Idea:
 Solve simplest (smallest) cases
DIRECTLY
 usually these are very easy to solve
 Solve bigger problems using smaller
sub-problems
 that are identical to itself (but smaller
and simpler)

Department of Computer Science


Designing Recursive
Tips for designing Recursive
Algorithms
Algorithm
 Understand problem requirements.
 Identify Base cases.
 Determine limiting conditions.
 Provide direct solution to each base
case.
 Identify General cases.
 Provide solutions to general cases in
terms of smaller versions of general
cases.

Department of Computer Science 47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy