Dynamic programming

SKAhsan 501 views 21 slides May 28, 2019
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Its Al About Data Structure and Algorithm Analysis


Slide Content

Sunawar Khan
Islamia University of Bahawalpur
Bahawalnagar Campus
Analysis of Algorithm

ناخ رونس Algorithm Analysis
Counting Sort
Linear Time Sorting
Bucket Sort
Radix Sort
Contents

ناخ رونس 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.

ناخ رونس 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.

ناخ رونس Algorithm Analysis
Greedy V/S Dynamic
Greedy TechniqueDynamic 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

ناخ رونس 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 MemoizationProblem

ناخ رونس Algorithm Analysis
Fibonacci Series

0 ??????????????????=0
1 ??????????????????=1
????????????????????????−2+????????????????????????−1??????????????????>1
????????????????????????????????????(????????????????????????){
if(n <= 1)
return 1
return fib(n-2)+fib(n-1);
}

ناخ رونس Algorithm Analysis
Running Example
•If we wannafind 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)

ناخ رونس 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.

ناخ رونس 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

ناخ رونس 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

ناخ رونس Algorithm Analysis
Applications
•Matrix Chain Multiplication
•Optimal Binary Search Tree
•All Pairs Shortest Path Problems
•Travelling Salesperson Problem
•0/1 Knapsack Problem
•Reliability Design

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
13x1505x8989x0303x34
1234
1
0
2
0
3
0
4
0
m[1,1] m[2, 2]m[3, 3]m[4, 4]
m
A CB D

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
1234
1
0 5785
2
0 1335
3
0 9078
4
0
m[1, 2]m[2, 3]m[3, 4]
m
m[A, B]m[B, C]m[C, D]
m
A CB D
13x0505x8989x0303x34
13x0505x89 05x8989x03 89x0303x34
13x05x89 05x89x3 89x3x4
5785 1335 9078

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
1234
1
0 5785 1530
2
0 1335
3
0 9078
4
0
m[1, 3]
m
A.(B.C) (A.B).C
m
A CB D
13x0505x8989x0303x34
2 Possibilities
13x0505x8989x03
1530
m[1, 1]m[2,3]13 x 5 x 3
Cost ACost B.CCost A.B.C = 13x5x3
0 + 1335+ 195
13x0505x8989x03
9256
m[1, 2]m[3,3]13 x 89x 3
5785 +
0 + 3471

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
1234
1
0 5785 1530
2
0 1335 1845
3
0 9078
4
0
m[1, 3]
m
B.(C.D) (B.C).D
m
A CB D
13x0505x8989x0303x34
2 Possibilities
05x8989x0303x34
24208
m[2, 2]m[3, 4]05 x 89 x 34
Cost BCost C.DCost B.C.D = 05x89x34
0 + 9078+ 15130
05x8989x0303x34
1845
m[2, 3]m[4, 4]5 x 3 x 34
1330 +
0 + 510

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
•m[1, 4]
m[1, 4] = min{m[1,1] + m[2,4], 13 x 5 x 34,
m[1, 2] + m[3, 4] + 13 x 89 x 34,
m[1, 3] + m[4, 4] + 13 x 3 x 34 }

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
•m[1, 4]
m[1, 4] = min{A. (B. C. D), (A.B).(C.D),(A.B.C).D}

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
•m[1, 4]
m[1, 4] = min{0 + 1845 + 2210,
5789 + 78 + 39338,
1530 + 0 + 1326}

ناخ رونس Algorithm Analysis
Matrix Chain Multiplication
•m[1, 4]
m[1, 4] = min{4055, 54201, 2856}
WHAT IS MIN? 2856
1234
1
0578515302856
2
013351845
3
09078
4
0
mm

ناخ رونس Algorithm Analysis
Formula
•m[i, j] = min{m(I, k) + m(k+1, j) + d
i-1xd
kxd
j}
Tags