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

Lec-02-Types of Algorithm

The document discusses different types of algorithms including simple recursive, divide and conquer, dynamic programming, greedy, and brute force algorithms. Examples are provided for each type to illustrate their properties and how they solve problems.

Uploaded by

DJBRAVE131
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lec-02-Types of Algorithm

The document discusses different types of algorithms including simple recursive, divide and conquer, dynamic programming, greedy, and brute force algorithms. Examples are provided for each type to illustrate their properties and how they solve problems.

Uploaded by

DJBRAVE131
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE225: Data Structure and

Algorithms

Types of Algorithms

1
Algorithm
An Algorithm is any well-defined
computational procedure that takes some
values or set of values as input and produces
some values or set of values as output.

An Algorithm is a well defined list of steps to


solve a particular problem.

2
NOTION OF ALGORITHM
problem

algorith
m

input “computer” output

Algorithmic solution

3
SOME WELL-KNOWN COMPUTATIONAL PROBLEMS
 Sorting
 Searching
 Shortest paths in a graph
 Minimum spanning tree
 Primality testing
 Traveling salesman problem
 Knapsack problem
 Chess
 Towers of Hanoi

4
Algorithms
Properties of a good Algorithm:

• Input from a specified set,


• Output from a specified set (solution),
• Effectiveness of each calculation step and
• Finiteness of the number of calculation steps,
• Correctness of output for every possible input

5
Types of Algorithms

6
Algorithm classification
• Algorithms that use a similar problem-solving
approach can be grouped together
• This classification scheme is neither exhaustive
nor disjoint
• The purpose is not to be able to classify an
algorithm as one type or another, but to highlight
the various ways in which a problem can be
solved

7
A short list of categories
• Algorithm types we will consider include:
– Simple recursive algorithms
– Divide and conquer algorithms
– Dynamic programming algorithms
– Greedy algorithms
– Brute force algorithms

8
What is Recursion? 
The process in which a function calls itself directly or
indirectly is called recursion and the corresponding function is
called as recursive function. Using recursive algorithm, certain
problems can be solved quite easily. Examples of such
problems are Towers of Hanoi (TOH), Inorder/Preorder/
Postorder Tree Traversals, DFS of Graph, etc.

9
Simple recursive algorithms I
• A simple recursive algorithm:
– Solves the base cases directly
– Recurs with a simpler subproblem
– Does some extra work to convert the solution to
the simpler subproblem into a solution to the given
problem
• “simple” because several of the other algorithm
types are inherently recursive

10
Finding the factorial of 3

fact(1)
1
fact(2) fact(2) fact(2)
2
fact(3) fact(3) fact(3) fact(3) fact(3)
6
main() main() main() main() main() main()

Time 2: Time 3: Time 4: Time 5: Time 6: Time 7:


Push: fact(3) Push: fact(2) Push: fact(1) Pop: fact(1) Pop: fact(2) Pop: fact(3)
returns 1. returns 2. returns 6.

Inside findFactorial(3): Inside findFactorial(2): Inside findFactorial(1):


if (number <= 1) return 1; if (number <= 1) return 1; if (number <= 1) return 1;
else return (3 * factorial (2)); else return (2 * factorial (1)); else return (1 * factorial (0));
Divide and Conquer
• A divide and conquer algorithm consists of two
parts:
– Divide the problem into smaller subproblems of
the same type, and solve these subproblems
– Combine the solutions to the subproblems into a
solution to the original problem
• Traditionally, an algorithm is only called divide
and conquer if it contains two or more
recursive calls

12
Divide and conquer algorithm
Divide and conquer is an algorithm design paradigm.
 A divide-and-conquer algorithm recursively breaks down a
problem into two or more sub-problems of the same or related
type, until these become simple enough to be solved directly.
 The solutions to the sub-problems are then combined to give a
solution to the original problem.

The divide-and-conquer technique is the basis of efficient


algorithms for many problems, such as sorting (e.g.,
quicksort, merge sort), multiplying large numbers (e.g.,
the Karatsuba algorithm).

13
Examples
• Quicksort:
– Partition the array into two parts, and quicksort
each
of the parts
– No additional work is required to combine the two
sorted parts
• Mergesort:
– Cut the array in half, and mergesort each half
– Combine the two sorted arrays into a single sorted
array by merging them

14
Dynamic programming algorithms
• A dynamic programming algorithm remembers past results
and uses them to find new results
• Dynamic programming is generally used for optimization
problems
– Multiple solutions exist, need to find the “best” one
– Requires “optimal substructure” and “overlapping subproblems”
• Optimal substructure: Optimal solution contains
optimal solutions to subproblems
• Overlapping subproblems: Solutions to subproblems can
be stored and reused in a bottom-up fashion
• This differs from Divide and Conquer, where subproblems
generally need not overlap

15
if a problem can be solved optimally by breaking it
into sub-problems and then recursively finding the
optimal solutions to the sub-problems, then it is
said to have optimal substructure

16
Greedy algorithms
• An optimization problem is one in which you want
to find, not just a solution, but the best solution
• A “greedy algorithm” sometimes works well for
optimization problems
• A greedy algorithm works in phases: At each
phase:
– You take the best you can get right now, without regard
for future consequences
– You hope that by choosing a local optimum at each
step, you will end up at a global optimum

17
Greedy algorithm
A greedy algorithm is any algorithm that follows the
problem-solving heuristic of making the locally
optimal choice at each stage.
In many problems, a greedy strategy does not usually
produce an optimal solution, but nonetheless, a
greedy heuristic may yield locally optimal solutions
that approximate a globally optimal solution in a
reasonable amount of time.

Example: Huffman Coding, Travelling Sales Person


18
Brute force algorithm
• A brute force algorithm simply tries all
possibilities until a satisfactory solution is found
– Such an algorithm can be:
• Optimizing: Find the best solution. This may require
finding all solutions, or if a value for the best
solution is known, it may stop when any best
solution is found
– Example: Finding the best path for a travelling salesman
• Satisficing: Stop as soon as a solution is found that
is good enough
– Example: Finding a travelling salesman path that is
within 10% of optimal

19

You might also like