0% found this document useful (0 votes)
18 views36 pages

Dynamic Programming and Algorithm Techniques

Uploaded by

Kartik Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views36 pages

Dynamic Programming and Algorithm Techniques

Uploaded by

Kartik Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DYNAMIC PROGRAMMING:

 Dynamic programming is a technique for solving problems with overlapping


subproblems.
 These subproblems arise from a recurrence relating a given problem’s solution to
solutions of its smaller subproblems.
 Rather than solving overlapping subproblems again and again, solve each of the smaller
subproblems only once and record the results in a table from which a solution to the
original problem can then be obtained.

Three Basic Examples:


Knapsack problem and memory functions:
Memory Functions:

The classic dynamic programming approach, on the other hand, works bottom up: it fills a table
with solutions to all smaller subproblems, but each of them is solved only once. An unsatisfying
aspect of this approach is that solutions to some of these smaller subproblems are often not
necessary for getting a solution to the problem given. Since this drawback is not present in the
top-down approach, it is natural to try to combine the strengths of the top-down and bottom-up
approaches. The goal is to get a method that solves only subproblems that are necessary and
does so only once. Such a method exists; it is based on using memory functions.
Warshall’s and Floyd’s Algorithms:

Used for computing the transitive closure of a directed graph. The transitive closure of a directed
graph with ‘n’ vertices can be defined as the n × n Boolean matrix T = {tij}, in which the element in
the ith row and the jth column is 1 if there exists a nontrivial path (i.e.,directed path of a positive
length) from the ith vertex to the jth vertex; otherwise, tij is 0.

Applications of transitive closure

• When a value in a spreadsheet cell is changed, the spreadsheet software must know all the
other cells affected by the change. If the spreadsheet is modelled by a digraph whose vertices
represent the spreadsheet cells and edges indicate cell dependencies, the transitive closure
will provide such information.
• In software engineering, transitive closure can be used for investigating data flow and control
flow dependencies as well as for inheritance testing of object-oriented software.
• In electronic engineering, it is used for redundancy identification and test generation for digital
circuits.
Rules for changing zeros to one.

 If an element rij is 1 in R(k−1), it remains 1 in R(k).


 If an element rij is 0 in R(k−1), it has to be changed to 1 in R(k) if and only if the element in its
row i and column k and the element in its column j and row k are both 1’s in R(k−1).
Time complexity for warshall O(n3)

Floyd’s Algorithm for the All-Pairs Shortest-Paths Problem

Given a weighted connected graph (undirected or directed), the all-pairs shortestpaths problem asks
to find the distances—i.e., the lengths of the shortest paths— from each vertex to all other vertices.

Floyd’s is applicable to both undirected and directed weighted graphs provided that they do not
contain a cycle of a negative length.
Time complexity of warshall O(n3)

GREEDY METHOD: Dijkstra’s Algorithm:

The algorithm maintains a set of visited vertices and a set of unvisited vertices. It
starts at the source vertex and iteratively selects the unvisited vertex with the
smallest tentative distance from the source. It then visits the neighbours of this
vertex and updates their tentative distances if a shorter path is found. This process
continues until the destination vertex is reached, or all reachable vertices have
been visited.
The need for Dijkstra’s algorithm arises in many applications where finding the
shortest path between two points is crucial.
For example, It can be used in the routing protocols for computer networks and
also used by map systems to find the shortest path between starting point and the
Destination.
in for graphs represented by their weight matrix and the priority queue
implemented as an unordered array. For graphs represented by their adjacency
lists and the priority queue implemented as a min-heap, it is in O(|E| log |V |).

Huffman Trees and Codes:


This scheme makes it necessary, however, to include the coding table into the encoded text to
make its decoding possible. This drawback can be overcome by using dynamic Huffman
encoding, in which the coding tree is updated each time a new symbol is read from the source
text. Further, modern alternatives such as Lempel-Ziv algorithms (e.g., [Say05]) assign
codewords not to individual symbols but to strings of symbols, allowing them to achieve better
and more robust compressions in many applications.
MODULE- 5

LIMITATIONS OF ALGORITHMIC POWER:

Some problems cannot be solved by an algorithm


Some problems can be solved algorithmically but not in polynomial time
Even though solution exists for a problem in polynomial time, there are lower bounds on the
efficiency of algorithms i.e, efficiency of those algorithms cannot be improved further.

Polynomial time:
The amount of time it takes for an algorithm to solve a polymomial function, which is a
mathematical expression that does not contain fractions or negative numbers (non-negative
integers). The time is proportional to the input and very efficient.

Classification of computational theory:


Computational theory:

P -Class NP-Class
NP-Complete NP-Hard

Class P is a class of decision problems that can be solved in polynomial time by


deterministic algorithms. This class of problems is called polynomial.
A nondeterministic algorithm is a two-stage procedure that takes as its input an instance I of
a decision problem and does the following.
Nondeterministic (“guessing”) stage: An arbitrary string S is generated that can be thought of
as a candidate solution to the given instance I
Deterministic (“verification”) stage: A deterministic algorithm takes both I and S as its input
and outputs, It produces yes if S represents a solution to instance I.
Class NP is the class of decision problems that can be solved by nondeterministic algorithms.
This class of problems is called nondeterministic.

Decision tree:
P,NP,NP Complete Problems:
COPING WITH LIMITATIONS OF ALGORITHMIC POWER:

 CONTENTS:
 Backtracking:

➢ n - Queens problem
➢ Hamiltonian Circuit Problem
➢ Subset – Sum Problem

 Branch-and-Bound

➢ Assignment Problem
➢ Knapsack Problem
➢ Traveling Salesperson Problem

 Approximation Algorithms for NP-Hard Problems

➢ Traveling Salesperson Problem


➢ Knapsack Problem
Backtracking:
Systematic method of searching for the solution to a combinatorial problem by
means of algorithm.
Some problems that have exponential time complexity can be best solved using
backtracking.
Principle idea of backtracking:
 The principal idea is to construct solutions one component at a time and
evaluate such partially constructed candidates as follows.
 If a partially constructed solution can be developed further without
violating the problem’s constraints, it is done by taking the first remaining
legitimate option for the next component.
 If there is no legitimate option for the next component, no alternatives for
any remaining component need to be considered. In this case, the
algorithm backtracks to replace the last component of the partially
constructed solution with its next option.
The problems that can be solved using backtracking are:

N-Queens problem

Subset-Sum problem

Hamiltonian Circuit problem

The output of a backtracking algorithm can be expressed as n-tuple:

State: (x1, x2, x3, …xn)

The n-tuple (x1, x2, x3, …xn) is called as state/solution of the problem.

State Space: The set of all possible states is called state space (solution space). It is denoted by S.

State Space Tree: The tree corresponding to the state space is called state space tree which is normally
constructed using depth first search method.

• Its root represents an initial state before the search for a solution begins.
• The nodes of the first level in the tree represent the choices made for the first component of
solution; the nodes of the second level represent the choices for the second component and
so on.
• A node in a state-space tree is said to be promising if it corresponds to a partially constructed
solution that may still lead to a complete solution otherwise it is called non-promising node.
• Leaves represent either non promising dead ends or complete solutions found by the
algorithm.
The following pseudo code gives the general form of a backtracking algorithm

Algorithm Backtrack(X[1…..i])

// Gives a template of a generic backtracking algorithm

// input: X[1…i] specifies first i promising components of a solution

// output: All the tuples representing the problems solutions

if X[1…i] is a solution write X[1…i]

else

for each element x ϵ Si+1 consistent with X[1….i] and the constraints do

X[i+1] x

Backtrack(X[1….i+1])

N-Queens problem:
Definition: The problem is to place n queens on an n × n chessboard so that no two queens attack
each other by being in the same row or in the same column or on the same diagonal.

For n = 1, the problem has a trivial solution.

For n=2 and n=3, no solution.

For n=4, there are 2 solutions as follows:

Soln 1: (x1, x2, x3, x4)=(2, 4, 3, 1) Soln 2: (x1, x2, x3, x4)=(3, 1, 4, 2)

Solution: The four queen’s problem can be solved by starting with the empty board and then place
queen 1 in the first possible position of its row, which is in column 1 of row 1. (1, 1)

To place queen 2 in 2nd row, the first acceptable position is column 3. Queen 2 cannot be placed in
column 1 (because of straight column attack from queen 1) and in column 2 (because of diagonal
attack). (2, 3)

This proves to be a dead end because there is no acceptable position for queen 3. So, the algorithm
backtracks and puts queen 2 in the next possible position at (2, 4). Then queen 3 is placed at (3, 2),
which proves to be another dead end. The algorithm then backtracks all the way to queen 1 and moves
it to (1, 2). Queen 2 then goes to (2, 4), queen 3 to (3, 1), and queen 4 to (4, 3), which is a solution to
the problem. The state-space tree of this search is shown below:
Sum of subsets problem
Definition: Given a set of n positive integers S = {s1, . . . , sn}, we have to find a subset of S whose sum
is equal to a given positive integer ‘d’. Some instances of this problem may have no solutions.

For example: Solve the given subset problem. A = {1, 2, 5, 6, 8} and d = 9. There are two solutions: {1,
2, 6} and {1, 8}.

Solution: The set’s elements have to be sorted in increasing order.

S1 ≤ S2 ≤ S3……. ≤ Sn

The state-space tree can be constructed as a binary tree.

Problem 1: S= {3, 5, 6, 7} and d=15

The following gives the state space tree


The root of the tree represents the starting point, with no decisions about the given elements made
as yet. Its left and right children represent, respectively, inclusion and exclusion of s1 in a set being
sought. Similarly, going to the left from a node of the first level corresponds to inclusion of s2 while
going to the right corresponds to its exclusion, and so on. Thus, a path from the root to a node on the
ith level of the tree indicates which of the first I numbers have been included in the subsets represented
by that node.

We record the value of s’, the sum of these numbers, in the node. If s’ is equal to d, we have a solution
to the problem. If s’ is not equal to d, we can terminate the node as non-promising if either of the
following two inequalities holds:

Hamiltonian circuit Problem:


Hamiltonian Circuit: is a cycle in an undirected graph that visits each vertex exactly once and returns
to the starting vertex. Determining whether such cycle exists in a graph is the Hamiltonian circuit
problem.

Using the alphabet order to break the three-way tie among the vertices adjacent to a, we select vertex
b. From b, the algorithm proceeds to c, then to d, then to e, and finally to f, which proves to be a dead
end. So the algorithm backtracks from f to e, then to d, and then to c, which provides the first
alternative for the algorithm to pursue. Going from c to e eventually proves useless, and the algorithm
has to backtrack from e to c and then to b. From there, it goes to the vertices f , e, c, and d, from which
it can legitimately return to a, yielding the Hamiltonian circuit a, b, f , e, c, d, a.

Graph Coloring

Let G be a graph and m be a given positive integer. Graph coloring is a problem of coloring each vertex
in graph in such a way that no two adjacent vertices have same color and yet m-colors are used.

This problem is also called m-coloring problem. If the degree of given graph is ‘d’ then we can color it
with ‘d+1’ colors. The least number of colors needed to color the graph is called its chromatic number.
Example:

We require 3 colors to color the graph. Hence the chromatic number of given graph is 3.

Graph coloring problem can be solved by using backtracking approach in the following way:

Problem: Show that the given graph can be colored using 3 colors by backtracking method.

Start with node A, color it with color 1


1 State-space tree

A 1
2 1

B 1 1
1
1 1

D 1
1

Next node is F, color 1 cannot be assigned to node F, So next possibility is color 2, Node F is assigned
with color 2.
State-space tree

1
A 1

2
1 1
B

1 D 1

F 1 1
2

Next node to be colored is E. To color E, we cannot use color 1, because D has already colored with 1
and D is E’s neighbor. We cannot use color 2, because F has already colored with 2 and F is E’s neighbor.

So use third color 3, to color node E.

To color C: cannot use 1, because C is a neighbor of A

cannot use 2, because C is a neighbor of B

cannot use 2, because C is a neighbor of E.


Given that we have to use 3 colors, we are struck here that we cannot use any of the three colors to
color node C. (Refer above figure)

So the algorithm backtracks to node E, no other option to node E other than color 3, So again the
algorithm backtracks one more step backward and comes to node F. Since we cannot use color 1 to
node F, color 2 leads to dead end. So we assign color 3 to node F.

Next color 2 is assigned to node E and color 3 is assigned to node C.

So we can show that the given graph can be colored using 3 colors successfully using backtracking
method. So the chromatic number of the given graph is 3.

Branch and Bound algorithm:


Branch and Bound is a method of systematically searching for a solution in state-space tree. It is used
to find the optimal solution. It consists of generating the partial solution by adding an item and
discarding the ones that cannot possibly lead to a feasible solution or optimal solution based on

• Lower bounds for minimization problems


• Upper bound for maximization problems.
The various problems that can be solved using branch and bound are:

• Knapsack problem
• Assignment problem
• Travelling Sales Person problem
• Assignment Problem
• Given n jobs <j1, j2, …, jn> and n persons <p1, p2, …, pn>, it is required to assign all ‘n’ jobs to
all ‘n’ persons with the constraint that one job has to be assigned to one person and the cost
involved in completing all the jobs should be minimum.

Example: Find the optimal solution for the given assignment problem using branch and bound
method

To calculate the lower-bound on the cost of an optimal selection, select the smallest entry in each row
in the given matrix.

lb=2+3+1+4 = 10

The state space tree for the given problem is constructed as follows

The nodes on the first level of the tree correspond to selections of an element in the first row of the
matrix. i.e, a job for person ‘a’.

So we have four live leaves (node 1, node 2, node 3 and node 4) that may contain an optimal solution.
The most promising of them is node 2 because it has the smallest lower-bound value. Following best-
first-branch and bound, we branch out from node 2, considering three different jobs that can be
assigned to person b.
Of the 6 live nodes (1, 3, 4, 5, 6, 7) that may contain an optimal solution, we again branch out from the
node, i.e, having lowest lower-bound i.e, 13 of node 5, considering 2 different ways of assigning jobs
to person c.

If we assign job 3 to person ‘c’, then job 4 has to be assigned to person ‘d’. Cost = 13

If job 4 is assigned to person ‘c’, then job 3 has to be assigned to person ‘d’. Cost=25

So node 8, with cost=13 is the optimal solution.

Solution:

Job 1 is assigned to person b

Job 2 is assigned to person a

Job 3 is assigned to person c


Job 4 is assigned to person d

Total cost= 13.

Travelling Salesman Problem


TSP is an example for minimization problem. So we need to calculate lower bound by the formula

lb(lower bound)= s/2 where ‘s’ is sum of two smallest distances from each city.

Problem 1: solve the following TSP by using Branch and Bound

Solution: The following points are considered to solve the problem:

• Tour starts at ‘a’


• Node ‘b’ is visited before node ‘c’.

Lower bound(lb) =

(a,b) lb=14

(a, d) lb= [ (1+5) + (3+6) + (1+2) + (5+3) + (2 +3) ] / 2 = 16

(a, e) lb = [ (1+8) + (3+6) + (1 + 2) + (3 + 4) + (2 + 8)] / 2 = 19

a, b, c lb = [(1+3) + (3+6) + (1 + 6) + (3 + 4) + (2 + 3)] / 2 = 16

a, b, d lb = [(1+3) + (3+7) + (1 + 2) + (3 + 7) + (2 + 3)] / 2 = 16

a, b, e lb = [(1+3) + (3+9) + (1 + 2) + (3 + 4) + (2 + 9)] / 2 = 19

State space tree:


So tour = a → b→ d → e → c → a with the cost 3 + 7 + 3 + 2 + 1 = 16.

0/1 Knapsack Problem:

LC Branch and Bound solution

LC stands for Least Cost

The steps to be followed are:


1. Draw the state space tree
2. Compute c^(.) and u(.) for each node
3. If c^(x) > upper kill node x.
4. Otherwise the minimum – u(.) becomes E-node. Generate children for E-node.
5. Repeat step 3 and 4 until all the nodes gets covered.
Soln: node 8 gives the answer (node with minimum –u(.) value). We get maximum profit with item 1,
2, and 4.

So the answer is (1, 1, 0, 1) with the maximum profit 38.


NP-Complete and NP-Hard problems:
Basic concepts

Depending on computing times, algorithms are clustered into two groups:

1. P –class Problems: Problems whose solution times are bounded by polynomials of small
degree.
Example: Ordered search O(n log n), sorting O(n log n), polynomial evaluation O(n) , string editing
O(mn) etc. [P –class Problems are the set of problems that can be solved by deterministic algorithms
in polynomial time]

2. NP-class Problems: Problems whose best- known algorithm are non-polynomial.


Example: TSP O(n22n) , Knapsack O(2n/2) [NP –class Problems are the set of decision problems that can
be solved by nondeterministic algorithms]. These problems requires vast amount of time to execute
even for moderate values of n. Such problems cannot be solved for large values of ‘n’.

Problems which don’t have polynomial time algorithm are computationally related and can be
classified as follows:

NP-complete Problems: A problem that is NP-complete has the property that it can be solved in
polynomial time if and only if all other NP-complete problems can also be solved in polynomial time.

NP-Hard Problems: If an NP-hard problem can be solved in polynomial time, then all NP-complete
problems can be solved in polynomial time.

Non-deterministic algorithms

Non deterministic algorithm contains operations whose outcomes are limited to specified sets of
possibilities. The following three functions are used:

1. Choice (S) arbitrarily chooses one of the elements of set S.


2. Failure () signals an unsuccessful completion.
3. Success () signals a successful completion.
Approximation algorithms for NP-Hard problems (Knapsack problem):

You might also like