2. خان سنور Algorithm Analysis
Counting Sort
Linear Time Sorting
Bucket Sort
Radix Sort
Contents
3. خان سنور Algorithm Analysis
Greedy Method v/s Dynamic Programing
• Both are used for Optimization problem. It required minimum or
maximum results.
• In Greedy Method:- Always follow a function until you find the optimal
result whether it is maximum or minimum.
• Prims algorithm for MST
• Dijkstra Algorithm for finding shortest path.
4. خان سنور Algorithm Analysis
Greedy Method v/s Dynamic Programing
• In Dynamic programing we will try to find all possible solution and
then we’ll pick the best solution which is optimal.
• It is time consuming as compared to greedy method.
• It use recursive formulas for solving problem.
• It follows the principal of optimality.
5. خان سنور Algorithm Analysis
Greedy V/S Dynamic
Greedy Technique Dynamic Technique
• It gives local “Optimal Solution”
• Print in time makes “local optimization”
• More efficient as compared to dynamic programing
• Example:- Fractional Knapsack
• It gives “Global Optimal Solution”
• It makes decision “ smart recursion ”
• Less efficient as compared too greedy technique
• Example:- 0/1 Knapsack
6. خان سنور Algorithm Analysis
Principle of Optimality
• Principle of optimality says that a problem can be solved by sequence
of decisions to get the optimal solution.
• It follows Memoization Problem
8. خان سنور Algorithm Analysis
Running Example
• If we wanna find out the fib(5)
fib (5)
fib(3)
fib(1) fib(2)
fib(0) fib(1)
fib(4)
fib(2)
fib(0) fib(1)
fib(3)
fib(1) fib(2)
fib(0) fib(1)
9. خان سنور Algorithm Analysis
What?
• It is a strategy in designing of algorithm which is used when problem
breaks down into recurring small problems.
• It is typically applicable to optimization problems
• In such problems there can be many solution, each solution has a
value and we wish to find a solution with the optimal value.
10. خان سنور Algorithm Analysis
Elements of Dynamic Programming
• Simple Subproblems
• We should be able to break the original problem to small subproblems that
have the same structure
• Optimal substructure of problem
• The optimal solution to the problem contains within solution to its
subproblems.
• Overlapping Subproblems
• There exist some places where we solve the same subproblem more than once
11. خان سنور Algorithm Analysis
Steps to design Dynamic Programming Algo
• Characterize optimal substructure
• Recursively define the value of an optimal solution
• Compute the vale bottomUp
• (if needed) constraints an optimal solution
12. خان سنور Algorithm Analysis
Applications
• Matrix Chain Multiplication
• Optimal Binary Search Tree
• All Pairs Shortest Path Problems
• Travelling Salesperson Problem
• 0/1 Knapsack Problem
• Reliability Design