Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
102 views

Design and Analysis of Algorithm

1) The document discusses the topic of dynamic programming and provides examples. It describes dynamic programming as a technique for solving complex problems by breaking them into simpler subproblems. 2) It provides an example of using dynamic programming to calculate binomial coefficients more efficiently than recursion by storing results of subproblems. The time complexity of the dynamic programming approach is O(n*k) compared to exponential time for recursion. 3) Another example provided is the 0/1 knapsack problem, which is about optimally filling a knapsack without exceeding weight capacity.

Uploaded by

Abhijit Bodhe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
102 views

Design and Analysis of Algorithm

1) The document discusses the topic of dynamic programming and provides examples. It describes dynamic programming as a technique for solving complex problems by breaking them into simpler subproblems. 2) It provides an example of using dynamic programming to calculate binomial coefficients more efficiently than recursion by storing results of subproblems. The time complexity of the dynamic programming approach is O(n*k) compared to exponential time for recursion. 3) Another example provided is the 0/1 knapsack problem, which is about optimally filling a knapsack without exceeding weight capacity.

Uploaded by

Abhijit Bodhe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Sanjivani Rural Education Society’s

Sanjivani College of Engineering, Kopargaon-423 603


(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified

Department of Computer Engineering


(NBA Accredited)

Subject- Design and Analysis of Algorithms (DAA) [CO 301)]


Unit 3:- Dynamic Programing

Prof. Abhijit S. Bodhe


Assistant Professor
Department of Computer Engineering
E-mail : bodheabhijitcomp@sanjivani.org.in
Contact No: 7709 340 570
Unit 3:- Dynamic Programming
• Dynamic Programming: Principle,
• Control Abstraction,
• Time Complexity Analysis,
• Binomial Coefficients,
• 0/1 Knapsack,
• Case study: Optimal Binary Search Tree (OBST)
• Application of DP: Path Finder GPS Application-Uber.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 2


Dynamic Programing-Principle
• Dynamic Programming is mainly an optimization over plain recursion.
Wherever we see a recursive solution that has repeated calls for same
inputs, we can optimize it using Dynamic Programming.
• Basic idea is to simply store the results of sub problems, so that we do
not have to re-compute them when needed later.
• This simple optimization reduces time complexities from exponential
to polynomial.
• if we write simple recursive solution for Fibonacci Numbers, we get
exponential time complexity and if we optimize it by storing solutions
of subproblems, time complexity reduces to linear.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 3


Dynamic Programing

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 4


Recursion Vs DP
• What is the difference between recursion and Dynamic Programming?
• Recursion is when a function can be called and executed by itself,
while dynamic programming is the process of solving problems by
breaking them down into sub-problems to resolve the complex one.
• Dynamic programming differs from Greedy method, because Greedy
method makes only one decision sequence but dynamic programming
makes more than one decision.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 5


DAC Vs. Greedy Vs. DP

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 6


Dynamic Programing Characteristics
1. overlapping sub problems and optimal substructure.
2. Dynamic Programming is not recursive in nature.
3. DP solves the sub problems only once and then stores it in the table.
4. In DP the sub-problems are not independent.
5. DP follows a bottom-up technique, by which it start with smaller and
hence simplest sub instances..
6. many decision sequences may be generated but choose optimal.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 7


Principle of Dynamic Programing
• Dynamic programming is both a mathematical optimization method and a
computer programming method.
• Dynamic programming can defined as, “a technique for solving a complex
problem by first breaking into a collection of simpler subproblems, solving
each subproblem just once, and then storing their solutions to avoid
repetitive computations”. OR
• Dynamic programming is a “method of solving problems exhibiting the
properties of overlapping sub problems and optimal substructure”
• Dynamic programming is heavily used in computer networks, routing,
graph problems, computer vision, artificial intelligence, machine learning.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 8


Control Abstraction for Dynamic Programing
1. Principle of Optimality :-An optimal sequence of decisions has the
property that whatever the initial state and decisions are, the remaining
decisions must constitute an optimal decision sequence with regard to the
state resulting from the first decision,
2. Optimal substructure: For the optimal solution of the problem, a solution
of sub problem also must be optimal. Dynamic programming builds the
optimal solution of the bigger problem using the solution of smaller sub
problems. Hence we should consider only those sub problems which have
an optimal solution.
3. Overlapping subproblems: When the big problem is divided into small
problems, it may create exponential subproblems.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 9


Control abstraction-Flow for DP
• Dynamic Programming algorithm is designed using the following four
steps:-
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a bottom-up
fashion.
4. Construct an optimal solution from the computed information.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 10


Control abstraction for DP
1. Dynamic programming (DP) splits the large problem at every possible
point. When the problem becomes sufficiently small, DP solves it.
2. Dynamic programming is bottom up approach, it finds the solution of
the smallest problem and constructs the solution of the larger problem
from already solved smaller problems.
3. To avoid recomputation of the same problem, DP saves the result of sub
problems into the table. When next time same problem encounters, the
answer is retrieved from the table by lookup procedure.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 11


Control abstraction-programing for DP

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 12


Time analysis of control abstraction
• Dynamic programming problems, Time Complexity is the number of
unique states/subproblems * time taken per state.
• In such problem, for a given n, there are n unique states/subproblems.
• For convenience, each state is said to be solved in a constant time.
• Hence the time complexity is O(n * 1).
• Hence Linear Time complexity

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 13


Application 1:- Binomial Coefficients
• Binomial coefficient C(n, k) defines as,
• coefficient of the term xn in the expansion of (1 + x)n.
• C(n, k) also defines the number of ways to select any k items out of n
items. Mathematically it is defined as,

C(n, k) can be found in different ways:-


1. DandC
2. DP
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 14
Binomial Coefficient using Divide & Conquer

• D&C divides the problem of size C(n, k), in two sub problems,
each of size C(n – 1, k – 1) and C(n – 1, k) respectively.
• Solution of larger problem is build by adding solution of these two sub
problems.
• Structure of binomial coefficient problem using divide and conquer approach
is described as :

Where n>k>0

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 15


Divide and conquer approach for BC-Solution

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 16


Algorithm of Divide and conquer approach for BC

• Recursive algorithm of solving binomial coefficient problem using divide and


conquer approach.
Algorithm BINOMIAL_DC (n, k)
// n is total number of items
// k is the number of items to be selected from n
if k == 0 or k == n then
return 1
else
return DC_BINOMIAL(n – 1, k – 1) + DC_BINOMIAL(n – 1, k)
end
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 17
Binomial Coefficient using Dynamic Programming

• Many sub problems are called again and again, since they have an
overlapping sub problems property.
• Re-computations of the same sub problems is avoided by storing their
results in the temporary array C[i, j] in a bottom up manner.
• The optimal substructure for using dynamic programming is stated as,

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 18


Binomial Coefficient using Dynamic Programming

• In Table, index i indicates row and index j indicates column.


• Tabular representation of binomial coefficients is known as Pascal’s triangle.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 19


Algorithm for BC using Recursion.(Not
prefered)
Algorithm BINOMIAL_DC (n, k)
// n is total number of items
// k is the number of items to be selected from n
if k == 0 or k == n then
return 1
else
return DC_BINOMIAL(n – 1, k – 1) + DC_BINOMIAL(n – 1,
k)
end
• Refer Pdf of Algorithm:- Refer a link shared
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 20
Complexity Analysis
• The cost of the algorithm is cost of filing out the table.
• Addition is the basic operation.
• Because k ≤ n, the sum needs to be split into two parts, only the half of
the table needs to be filled out for i < k and the remaining part of the
table is filled out across the entire row.
• Hence TC becomes
• T(n, k) = sum for upper triangle + sum for the lower rectangle
T(n,k)= O(n*K)

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 21


BC: DP: Example: C(5,3)

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 22


BC: Example: C(5,3)

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 23


Example: C(5,3)

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 24


Application 2:- 0/1 knapsack
• The knapsack problem is a problem in combinatorial optimization:
• Given a set of items, each with a weight and a value, determine the
number of each item to include in a collection so that the total weight
is less than or equal to a given limit and the total value is as large as
possible.
• The problem often arises in resource allocation, where the decision-
makers have to choose from a set of non-divisible projects or tasks
under a fixed budget or time constraint,
• The 0/1 knapsack problem means that the items are either
completely or no items are filled in a knapsack.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 25


Technical Problem Statement
• We are given a knapsack of capacity c and a set of n objects numbered
1,2,…,n. Each object i has weight wi and profit pi.
• Let v = [v1, v2,…, vn] be a solution vector in which vi = 0 if object i is not in
the knapsack, and vi = 1 if it is in the knapsack.
• The goal is to find a subset of objects to put into the knapsack so that,

(that is, the objects fit into the knapsack) and


• is maximized (that is, the profit is maximized).

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 26


Solving Method

1. Using Tabular Method (Mostly used in dynamic programing)


2. Using Set Theory (Mostly for BackTracking)

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 27


Example1
• Let n = 4 w = 5 kg
• (w1, w2, w3, w4) = (2, 3, 4, 5) (p1, p2, p3, p4) = (3, 4, 5, 6)

• So, maximum possible value that can be put into the knapsack = 7. With v={1,1,0,0}
• https://www.gatevidyalay.com/0-1-knapsack-problem-using-dynamic-
programming-approach/
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 28
Example2

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 29


Example 3-
• Consider the problem having weights and profits are:
• Weights: {3, 4, 6, 5}
• Profits: {2, 3, 1, 4}
• The weight of the knapsack is 8 kg
• The number of items is 4

• Refer PDF 1
• https://www.javatpoint.com/0-1-knapsack-problem.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 30


0/1 Knapsack Algorithm

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 31


0/1 Knapsack Algorithm Time Complexity

• The time complexity for the 0/1 Knapsack problem solved using DP is
• O(N*W) (Linear) where
• N denotes the number of items available and
• W denotes the capacity of the knapsack.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 32


Case Study Application 3:- OBST
• Optimal Binary Search Tree extends the concept of Binary search tree.
• In BST, left child is smaller than root and right child is greater than root.
• Optimal Binary Search Tree (OBST) is very useful in dictionary search.
• OBST has great application in translation. If we translate the book from
English to German, equivalent words are searched from English to
German dictionary and replaced in translation.
• Storing words according to their probabilities, facilitates few searches for
frequent words as they would be near the root. Such tree is called Optimal
Binary Search Tree.
• https://codecrucks.com/optimal-binary-search-tree-how-to-solve-using-
dynamic-programming/
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 33
OBST
• The goal is to construct a tree which minimizes the total search cost.
Such tree is called optimal binary search tree.
• OBST does not claim minimum height, also not necessary that parent
of sub tree has higher priority than its child.
• Dynamic programming can help us to find such optimal tree.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 34


BST Example

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 35


Possible BST’s

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 36


Possible BST’s with Cost

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 37


OBST Control Abstraction

Time complexity:- O(n log(n))

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 38


OBST Example

• Construct OBST for n=4, (a1,a2,a3,a4)=(do,if,int,while)


p(1:4)=(3,3,1,1) and q(0:4)=(2,3,1,1,1).
• Possible Trees :- 14.
• OBST is :-

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 39


Formula for OBST using DP Refer PDF

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 40


Solution of OBST

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 41


Solution of OBST

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 42


OBST Solved

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 43


OBST Solved

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 44


Unit 3:- Dynamic Programming
• Dynamic Programming: Principle,
• Control Abstraction,
• Time Complexity Analysis,
• Binomial Coefficients,
• 0/1 Knapsack,
• Case study: Optimal Binary Search Tree (OBST)
• Application of DP: Path Finder GPS Application-Uber.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 45


Application of DP: Path Finder GPS Application-Uber.

• https://scholarworks.uark.edu/cgi/viewcontent.cgi?article=1516&con
text=jaas
• https://www.spatialpost.com/applications-of-gps/
• https://psl.noaa.gov/psd3/multi/remote/
• https://www.uber.com/en-IN/blog/rethinking-gps/

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 46


Unit 4:-Backtracking and Branch -and-Bound

• Backtracking: Principle, control abstraction,


• Application 1 :- 8-queen problem
• Application 2:- sum of subsets problem.
• Branch-and-Bound: Principle, control abstraction,
• Application 1:- TSP,
• Application 2:- knapsack problem.
• Case Study :-graph coloring problem

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 47

You might also like