0% found this document useful (0 votes)
1 views15 pages

Module 4 - Dynamic Programming

Module 4 covers various algorithms in Dynamic Programming and graph theory, including methods for solving the Knapsack problem, finding shortest paths using Bellman-Ford and Floyd's algorithms, and determining transitive closures with Warshall's algorithm. It also discusses multistage graphs and the Travelling Sales Person problem. The content is aimed at providing a comprehensive understanding of these algorithms and their applications in artificial intelligence and machine learning.

Uploaded by

Asha K
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)
1 views15 pages

Module 4 - Dynamic Programming

Module 4 covers various algorithms in Dynamic Programming and graph theory, including methods for solving the Knapsack problem, finding shortest paths using Bellman-Ford and Floyd's algorithms, and determining transitive closures with Warshall's algorithm. It also discusses multistage graphs and the Travelling Sales Person problem. The content is aimed at providing a comprehensive understanding of these algorithms and their applications in artificial intelligence and machine learning.

Uploaded by

Asha K
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
You are on page 1/ 15

Module 4- DAA (21CS42)

MODULE 4 – Syllabus Topics


Dynamic Programming: General method with Examples, Multistage Graphs.
Transitive Closure: Warshall’s Algorithm. All Pairs Shortest Paths: Floyd's Algorithm,
Knapsack problem, Bellman-Ford Algorithm, Travelling Sales Person problem.
Space-Time Tradeoffs: Introduction, Sorting by Counting, Input Enhancement in String Matching Harspool’s
algorithm.

Dynamic Programming: General method with Examples


Q. Explain Dynamic Programming with General method & Examples
Dynamic programming is a technique for solving problems with overlapping subproblems. Typically, 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, dynamic programming suggests solving each of
the smaller subproblems only once and recording the results in a table from which a solution to the original
problem can then be obtained.
The Dynamic programming can be used when the solution to a problem can be viewed as the result of sequence
of decisions.

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Multistage Graphs:
Q. Explain the multistage graph with an example. Write backward multistage graph algorithm
Q Define a multi stage graph. Give an example. Explain the technique for finding the minimum cost path
in a multi stage graph

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

A Multistage graph is a directed, weighted graph in which the nodes can be divided into a set of
stages such that all edges are from a stage to next stage only (In other words there is no edge between
vertices of same stage and from a vertex of current stage to previous stage). The vertices of a
multistage graph are divided into n number of disjoint subsets S = { S 1 , S2, S3 ……….. Sn }, where S1 is the
source and Sn is the sink ( destination ). The cardinality of S 1 and Sn are equal to 1. i.e., |S 1| = |Sn| = 1.
We are given a multistage graph, a source and a destination, we need to find shortest path from source
to destination. Following is an example graph.

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Problems on multistage graph

Transitive Closure: Warshall’s Algorithm


Q. Explain the transitive closure of a directed graph. Write Warshall’s Algorithm to find the transitive
closure.

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Warshall's algorithm is used to determine the transitive closure of a directed graph or all paths in a directed graph
by using the adjacency matrix. For this, it generates a sequence of n matrices. Where, n is used to describe the
number of vertices.
Definition: 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.
Example: An example of a digraph, its adjacency matrix, and its transitive closure is given below.

We can generate the transitive closure of a digraph with the help of depth first search or breadth-first search.
Performing either traversal starting at the ith vertex gives the information about the vertices reachable from it
and hence the columns that contain 1’s in the ith row of the transitive closure. Thus, doing such a traversal for
every vertex as a starting point yields the transitive closure in its entirety.
Warshall’s algorithm constructs the transitive closure through a series of n × n boolean matrices:

As an example, the application of Warshall’s algorithm to the digraph is shown below. New 1’s are in bold.

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Analysis
Its time efficiency is Θ(n3 ). We can make the algorithm to run faster by treating matrix rows as bit strings and

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

employ the bitwise or operation available in most modern computer languages.


Space efficiency: Although separate matrices for recording intermediate results of the algorithm are used, that
can be avoided.

Problems on Warshall’s algorithm

All Pairs Shortest Paths using Floyd's Algorithm:


Q. Design an algorithm to find all pairs of shortest paths given a weighted connected path using dynamic
programming technique.
Problem definition: Given a weighted connected graph (undirected or directed), the all-pairs shortest paths
problem asks to find the distances—i.e., the lengths of the shortest paths - from each vertex to all other vertices.
Applications: Solution to this problem finds applications in communications, transportation networks, and
operations research. Among recent applications of the all-pairs shortest-path problem is pre-computing distances
for motion planning in computer games.
We store the lengths of shortest paths in an n x n matrix D called the distance matrix: the element d ij in the ith
row and the jth column of this matrix indicates the length of the shortest path from the ith vertex to the jth vertex.

We can generate the distance matrix with an algorithm that is very similar to Warshall’s algorithm. It is called
Floyd’s algorithm. Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through
a series of n × n matrices:

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Analysis: Its time efficiency is Θ(n3 ), similar to the warshall’s algorithm.


Application of Floyd’s algorithm to the digraph is shown below. Updated elements are shown in bold.

Problems on Floyd’s algorithm

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Knapsack problem:
Q. Design an dynamic programming algorithm for the knapsack problem:
Given n items of known weights w1, . . . ,wn and values v1, . . . , vn and a knapsack of capacity W, find the most
valuable subset of the items that fit into the knapsack. To design a dynamic programming algorithm, we need to
derive a recurrence relation that expresses a solution to an instance of the knapsack problem in terms of solutions
to its smaller sub instances. Let us consider an instance defined by the first i items, 1≤ i ≤ n, with weights w1, . .
. ,wi, values v1, . . . , vi , and knapsack capacity j, 1 ≤ j ≤ W. Let F(i, j) be the value of an optimal solution to this
instance.
We can divide all the subsets of the first i items that fit the knapsack of capacity j into two categories: those that
do not include the ith item and those that do.
The algorithm for the knapsack problem can be stated as follows
Input: n – total items, W – capacity of the knapsack
wi– weight of the ith item, vi– value of the ith item,
Output: F(i, j) be the value of an optimal solution to this instance considering first i items with capacity j. F(n,W)
is the optimal solution
Method:

Problems on Knapsack

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Bellman-Ford Algorithm (Single source shortest path with – ve weights)


Problem definition: Given a graph and a source vertex s in graph, find shortest paths from s to all vertices in
the given graph. The graph may contain negative weight edges.
Note that we have discussed Dijkstra’s algorithm for single source shortest path problem. Dijksra’s algorithm is
a Greedy algorithm and time complexity is O(VlogV). But Dijkstra doesn’t work for graphs with negative weight
edges.
Bellman-Ford works for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed
systems. But time complexity of Bellman-Ford is O(VE), which is more than Dijkstra.
How it works? - Like other Dynamic Programming Problems, the algorithm calculates shortest paths in bottom-
up manner. It first calculates the shortest distances for the shortest paths which have at-most one edge in the
path. Then, it calculates shortest paths with at-most 2 edges, and so on.
Iteration i finds all shortest paths that use i edges. There can be maximum |V| – 1 edges in any simple path, that
is why the outer loop runs |v| – 1 times. The idea is, assuming that there is no negative weight cycle, if we have
calculated shortest paths with at most i edges, then an iteration over all edges guarantees to give shortest path
with at-most (i+1) edges.

Bellman-Ford algorithm to compute shortest path

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Problems on Bellman-Ford Algorithm

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere


Module 4- DAA (21CS42)

Travelling Sales Person problem. (Using backtracking)

Problems on Travelling Sales Person

Department of Artificial Intelligence & Machine Learning, GMIT, Davangere

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy