0% found this document useful (0 votes)
8 views

Lecture # 03 - New

Uploaded by

usmanqasimcs
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)
8 views

Lecture # 03 - New

Uploaded by

usmanqasimcs
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/ 50

Design and Analysis of Algorithm

Tanveer Ahmed Siddiqui

Department of Computer Science


COMSATS University, Islamabad
Recap

Lecture No 2

Department of Computer Science 2


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 Problem Newly decomposed 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 is Recursion?
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 is Recursion?
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


RecursiveRecursion
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
RecursiveAlgorithm
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 15


Steps for Designing
designing Recursive
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 16


Steps for Designing
designing Recursive
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 17


Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


 Solution: Iterative an = a × a × … … × a
 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?
 an = a×an-1 Top down: recursive
 What is base condition? 1 if n = 0
 For n = 0, return 1 f(n) =
a×f(n-1) if n > 0

Department of Computer Science 18


Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


Iterative 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 19
Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


 Solution:
Decrease by a constant 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
at each step
Decrease by a constant factor
 What is base case?
 For n =0, return 1

Department of Computer Science 20


Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


 Solution:
Decrease by a constant factor

 Suppose we want to compute 95 Effectively, when


power is not divisible
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. 21
Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


 Solution:
Decrease by a constant factor

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

Department of Computer Science 22


Example-1: Computing
Brute Force Example a n

 Design a recursive algorithm that computes an


 Solution:
Decrease by a constant factor
Recursive Versions

Department of Computer Science 23


Example-1: Computing
Brute Force Example a n

 Conversion of recursive algorithm that computes


an into iterative
Decrease by a constant factor
 Solution: Iterative Versions

Department of Computer Science 24


Example-2:
Brute Maximum Number
Force Example
 Design an algorithm that find maximum in an
array A[0..n-1] Iterative
 Solution:
 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 25


Example-2:
Brute Maximum Number
Force Example
 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 26
Example-2:
Brute Maximum Number
Force Example
 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 27


Example-2:
Brute Maximum Number
Force Example
 Design an algorithm that find maximum in an
array A[0..n-1]
 Solution: Recursive
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 28


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

Department of Computer Science 29


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

Department of Computer Science 30


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

How we decompose given problem into smaller version:

Department of Computer Science 31


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

How we decompose given problem into smaller version:

Department of Computer Science 32


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

Recursive

Department of Computer Science 33


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

Recursive 1

Department of Computer Science 34


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


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

Department of Computer Science 36


Example-3: Recursive Algorithm
Recursive callsfor Factorial
- example

Problem: Compute factorial of a positive number n.

Convert iterative version into recursive

Department of Computer Science 37


Example-3: Recursive Algorithm
Recursive callsfor Factorial
- example
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 38


Example 4:Recursive Algorithm
Recursivefor calls
Fibonacci series
- example

Problem: Compute Fibonacci series of a positive number


n.

Convert iterative version into recursive


Department of Computer Science 39
Top-down vs Bottom up

S.
No Recursion Iteration
.

Terminates when the


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

2 Used with functions. Used with loops.

Every recursive call needs Every iteration does not


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

4 Smaller code size. Larger code size.


Department of Computer Science
Top-down vs Bottom up

Top down Bottom up


Largest to smallest problem Smallest to largest
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 Example
Brute Force to use 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.
 Recursion is generally slower than iteration.
Department of Computer Science 44
Advantage and disadvantages
Brute 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 45


Advantage and disadvantages
Brute 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 46


How we do it?

CONCLUSION

Department of Computer Science


Brute Force ExampleUPSHOT
 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 48


What is Recursion? UPSHOT
Recursion
 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


Tips for Designing
designing Recursive
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 50

You might also like