Lec-02-Types of Algorithm
Lec-02-Types of Algorithm
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.
2
NOTION OF ALGORITHM
problem
algorith
m
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:
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()
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.
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.
19