MODULE-4
DYNAMIC PROGRAMMING
Warshall’s Algorithm
Sumangala Biradar
ISE Department, BLDEACET
Created by Sumangala Biradar
Dynamic Programming
Dynamic Programming is a general design technique for solving problems
defined by or formulated as recurrences with overlapping subinstances.
❖ “Programming” here means “planning”
❖ Main idea:
• Set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
• Solve smaller instances once
• Record solutions in a table
• Extract solution to the initial instance from that table
Created by Sumangala Biradar
Warshall’s Algorithm: Transitive Closure
• Computes the transitive closure of a relation
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 of transitive closure:
3
Intermediate node 3
1
0 0 1 0 1
0 0 1 0
1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 2
4
1 1 1 1
4
2 Intermediate Intermediate
node node
Created by Sumangala Biradar
• R(0) is just the adjacency matrix
• Boxed row and column are used for
getting R(1).
• R(1) is intermediate vertices numbered not
higher than 1, i.e., just vertex a
• A new path from d to b : d-a-b
• R(2) intermediate vertices numbered not
higher than 2, i.e., a and b
• A two new paths a to d : a-b-d
d to d : d-a-b-d
• R(3) intermediate vertices numbered not
higher than 3, i.e., a, b, and c
• No new paths
• R(4) intermediate vertices numbered not higher
than 4, i.e., a, b, c, and d
• A five new paths a to a : a-b-d-a
a to c : a-b-d-c
b to a : b-d-a
Created by Sumangala Biradar
b to b : b-d-a-b
b to c : b-d-c
• R(0) is just the adjacency matrix
• Boxed row and column are used for
getting R(1).
• R(1) is intermediate vertices numbered not
higher than 1, i.e., just vertex a
• A new path from d to b : d-a-b
• R(2) intermediate vertices numbered not
higher than 2, i.e., a and b
• A two new paths a to d : a-b-d
d to d : d-a-b-d
• R(3) intermediate vertices numbered not
higher than 3, i.e., a, b, and c
• No new paths
• R(4) intermediate vertices numbered not higher
than 4, i.e., a, b, c, and d
• A five new paths a to a : a-b-d-a
a to c : a-b-d-c
b to a : b-d-a
Created by Sumangala Biradar
b to b : b-d-a-b
b to c : b-d-c
Warshall’s Algorithm (pseudocode and analysis)
K=1; i=1; j=1
R1[1,1]= R0[1,1] or R0[1,1] and
R0[1,1]
= 0 || 0 && 0 =0
K=1; i=1; j=2
R1[1,2]= R0[1,2] or R0[1,1] and
R0[1,2]
Analysis = 1 || 0 && 1 =1
K=1; i=1; j=3
Time efficiency: Θ(n3) R1[1,3]= R0[1,3] or R0[1,1] and
R0[1,3]
Space efficiency: Matrices can be written over their = 0 || 0 && 0 =0
K=1; i=1; j=4
predecessors (with some care), so it’s R1[1,4]= R0[1,4] or R0[1,1] and
R0[1,4]
Θ(n2).
Created by Sumangala Biradar = 0 || 0 && 1 =0
K=1; i=2; j=1
MODULE-4
DYNAMIC PROGRAMMING
Floyd’s Algorithm
Sumangala Biradar
ISE Department, BLDEACET
Created by Sumangala Biradar
Created by Sumangala Biradar
Created by Sumangala Biradar
Floyd’s Algorithm: All pairs shortest paths
❖ 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.
❖ The lengths of shortest paths in an n × n matrix D called the distance matrix: the element dij 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.
❖ Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through
a series of n × n matrices: D(0), . . . , D(k−1), D(k), . . . , D(n)
Created by Sumangala Biradar
• D(0) is the weighted matrix
• Length of shortest paths with no
intermediate nodes.
• Boxed row and column are used for
getting R(1).
• D(1) is the length of shortest paths with
intermediate nodes not higher than 1 i.e.
just a.
• Two new shortest paths from
b to c : b – a –c
2+ 3 =5
d to c : d – a –c
6 + 3 =9
Created by Sumangala Biradar
• D(2) is the length of shortest paths with
intermediate nodes not higher than 2 i.e.
just a ,b.
• One new shortest paths from
c to a : c – b –a
7 + 2 =9
• D(3) is the length of shortest paths with
intermediate nodes not higher than 3 i.e.
just a ,b & c.
• Four new shortest paths from
a to b : a – c –b
3 + 7 =10
a to d : a – c – d
3+1=4
b to d : b – a – c - d
2 + 3+1 = 6
d to b : d – a – c - b
6 + 3 + 7 =16
Created by Sumangala Biradar
• D(4) is the length of shortest paths with
intermediate nodes not higher than 4 i.e.
just a ,b , c & d.
• One new shortest path from
c to a : c– d –a
1+6=7
ALGORITHM Floyd(W[1..n, 1..n])
//Implements Floyd’s algorithm for the all-pairs shortest-paths
problem
//Input: The weight matrix W of a graph with no negative-length
cycle
//Output: The distance matrix of the shortest paths’ lengths
D ←W //is not necessary if W can be overwritten
for k←1 to n do
for i ←1 to n do
for j ←1 to n do K=4 i=3 j=1
D[i, j ]←min{D[i, j ], D[i, k]+ D[k, j]} D[3,1] = min{ D[3,1], D[3,4] +D[4,1]
return D = min { 9, 1+6}
Analysis = 7
Floyd’s Algorithm’s : Time efficiency is only Θ(n3).
Space
Created efficiency
by Sumangala is Θ(n2).
Biradar
i.e matrix size.
Created by Sumangala Biradar
Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
Items are indivisible; you either take an item or not. Some special
instances can be solved with dynamic programming
2. “Fractional knapsack problem”
Items are divisible: you can take any fraction of an item
Created by Sumangala Biradar
0-1 Knapsack problem
• Problem, in other words, is to find
max bi subject to wi W
iT iT
• The problem is called a “0-1” problem, because each item must be
entirely accepted or rejected.
Created by Sumangala Biradar
0-1 Knapsack problem
Initial condition
Created by Sumangala Biradar
Created by Sumangala Biradar
0-1 Knapsack problem
ALGORITHM DPKnapsack(w[1..n], v[1..n], W)
//var V[0..n,0..W], P[1..n,1..W]
for j := 0 to W do
V[0,j] := 0
for i := 0 to n do i=1, j=1
w[1]<=1 and v[1]+V[0,1-2] > V[0,1]
V[i,0] := 0 2<= 1 and 12+0 >0 (True)
V[1,1] = V[0,1]=0; P[1,1]=1
for i := 1 to n do i=1, j=2
w[1]<=1 and 12+V[0,2-1]> V[0,2]
for j := 1 to W do 1<=1 and 12+0>0 (True)
V[1,2]= 12+0=12
if (w[i] ≤ j and v[i] + V[i-1,j-w[i]] >i=1,
V[i-1,j]
j=3
1<=3 (false) )
V[1,3]=V[1,2]=12 ; P[1,3]=3
then …….……….
i=4, j=5
V[i,j] := v[i] + V[i-1,j-w[i]]; 2<=5 and 15+V[3,5-2]> V[3,5]
True and 15+V[3,3]>V[3,5]
P[i,j] := j-w[i]
Created by Sumangala Biradar
True and 15+22>32 (True)
V[4,5] = 15+22=37 ; P[4,5] = 5-2 = 3
else
Recursive Formula
Recursive formula for subproblems:
V [k − 1, w] if wk w
V [ k , w] =
max{V [k − 1, w],V [k − 1, w − wk ] + bk } else
It means, that the best subset of Sk that has
total weight w is:
1) the best subset of Sk-1 that has total weight w,
or
2) the best subset of Sk-1 that has total weight w-
wk plus the item k
Created by Sumangala Biradar
0-1 Knapsack Memory Function Algorithm
ALGORITHM MFKnapsack(i, j )
//Implements the memory function method for the knapsack problem
//Input: A nonnegative integer i indicating the number of the first
// items being considered and a nonnegative integer j indicating
// the knapsack capacity
//Output: The value of an optimal feasible subset of the first i items
//Note: Uses as global variables input arrays Weights[1..n], V alues[1..n],
//and table F[0..n, 0..W ] whose entries are initialized with −1’s except for
//row 0 and column 0 initialized with 0’s
if F[i, j ]< 0
if j <Weights[i]
value←MFKnapsack(i − 1, j)
else
value←max(MFKnapsack(i − 1, j),
Values[i]+ MFKnapsack(i − 1, j −Weights[i]))
F[i, j ]←value
return F[i, j ]
Created by Sumangala Biradar
22
MODULE- 3
GREEDY METHOD
SUMANGALA BIRADAR
23 ISE DEPARTMENT, BLDEACET
24 Contents
Prim’s algorithm
Kruskal's Algorithm
Dijkstra's Algorithm
Huffman Trees and Codes
25 Introduction
Feasible : Concentrate on constraints.
Optimal : Among all possible solutions finding only one solution
which is optimal always.
Irrevocable : Each iteration solutions are fixed.
Created by Sumangala Biradar Module-4 BCS401
26
Created by Sumangala Biradar Module-3 BCS401
27
Minimum Spanning Tree
Spanning tree : Connected acyclic graph G= { V, E}.
Minimum spanning tree : A weighted connected graph G with minimum total weight.
Example
6 c 6 c c
a a a
4 1 1 1
4
2 2
d d d
b 3 b b 3
Created by Sumangala Biradar Module-4 BCS401
28 Minimum Spanning Tree
Two Algorithms to find minimum spanning tree under
greedy method
❖ Prim’s Algorithm
❖ Kruskal’s Algorithm
Created by Sumangala Biradar Module-3 BCS401
Prim’s Algorithm
29
Algorithm: Prims-algorithm(G)
//Input: A weighted, connected, undirected graph G=(V,E).
//Output: ET the set of edges composing a minimum spanning tree.
VT<- {v0 }
ET<- { }
for i<-1 to |V|-1 do
find a minimum weight edge e*=(v*,u*) among all the
edges (v, u) such that v is in VT and u is in v-VT
VT<- VT U { u*}
ET <- ET U { e*}
Return ET
Created by Sumangala Biradar Module-3 BCS401
30 Prim’s Algorithm example
Prim’s Algorithm example
31
32 Prim’s Algorithm example
33
Prim’s Algorithm example
34 Analysis: Prim's Algorithm
Run-time
O(|E| log |V|) using a priority queue
Created by Sumangala Biradar Module-4 BCS401
Kruskal’s Algorithm
35 Algorithm:Kruskal’salgorithm(G)
// Input:A weighted, connected, undirected graphG=(V,E).
//Output: ET the set of edges composing a minimum spanning tree.
Sort E in increasing order of the edge weights
ET<-
encounter <-0
k<-0
While encounter < |V|-1 do
k<-k+1
if ET U {eik } is acyclic
ET <- ET U {eik}
encounter <- encounter + 1
Return ET
Created by Sumangala Biradar Module-4 BCS401
Kruskal’s Algorithm
36
Created by Sumangala Biradar Module-3 BCS401
37
Analysis: Kruskal's Algorithm
Run-time
O(|E| log |V|)
Created by Sumangala Biradar Module-4 BCS401
Single Source Shortest paths – Dijkstra’s algorithm
38
Single Source Shortest Paths Problem: Given a weighted connected (directed) graph G, find shortest
paths from source vertex s to each of the other vertices
From a to b : a − b of length 3
From a to d : a − b − d of length 5
3+2 =5
From a to c : a − b − c of length 7
3+4=7
From a to e : a − b − d − e of length 9
3+2+4=9
Created by Sumangala Biradar Module-4 BCS401
ALGORITHM Dijkstra(G, s)
39 //Dijkstra’s algorithm for single-source shortest paths
//Input: A weighted connected graph G = (V, E) with nonnegative weights and its vertex s
//Output: The length dv of a shortest path from s to v and its penultimate vertex pv for every
// vertex v in V
Initialize(Q) //initialize priority queue to empty
for every vertex v in V
dv ← ∞; pv ← null
Insert (Q, v, dv) //initialize vertex priority in the priority queue
Ds ← 0; Decrease(Q, s, ds) //update priority of s with ds
VT ← Φ
for i ←0 to |V| − 1 do
u* ← DeleteMin(Q) //delete the minimum priority element
VT ←VT ∪ {u* }
for every vertex u in V − VT that is adjacent to u* do
if du* + w(u*, u) < du
du ← du* + w(u *, u);
pu ← u *
Created by Sumangala Biradar u, d )
Decrease(Q, Module-3 BCS401
u
40
Created by Sumangala Biradar Module-4 BCS401
Notes on Dijkstra’s algorithm
41
Correctness can be proven by induction on the number of vertices.
We prove the invariants:
(i) when a vertex is added to the tree, its correct distance is calculated and
(ii) The distance is at least those of the previously added vertices.
Doesn’t work for graphs with negative weights Applicable to both undirected and directed graphs
Efficiency
O(|V|2) for graphs represented by weight matrix and array implementation of priority queue
O(|E|log|V|) for graphs represented by adj. lists and min-heap implementation of priority
queue
Created by Sumangala Biradar Module-4 BCS401
42 Huffman trees and codes
➢ To encode a text that comprises symbols from some n-
symbol alphabet by assigning to each of the text’s symbols
some sequence of bits called the codeword.
▪ Fixed-length encoding : ASCII code 8-bit for each character.
▪ Variable-length encoding : Prefix-free (or simply prefix) codes
of varying bits.
➢ Idea: Assign shorter code words to more frequent symbols
and longer code words to less frequent symbols.
➢ Huffman tree: is a binary tree with all the left edges are
labeled by 0 and all the right edges are labeled by 1.
Created by Sumangala Biradar Module-3 BCS401
43
Huffman’s algorithm
Step 1 : Initialize n one-node trees and label them with the symbols of the
alphabet given. Record the frequency of each symbol in its tree’s root
to indicate the tree’s weight. (More generally, the weight of a tree will
be equal to the sum of the frequencies in the tree’s leaves.)
Step 2 : Repeat the following operation until a single tree is obtained. Find
two trees with the smallest weight (ties can be broken arbitrarily). Make
them the left and right subtree of a new tree and record the sum of their
weights in the root of the new tree as its weight.
Created by Sumangala Biradar Module-4 BCS401
44 Example
Consider the five-symbol alphabet {A, B, C, D, - } with the
following occurrence frequencies in a text made up of these
symbols:
Created by Sumangala Biradar Module-4 BCS401
45
Step 1 :
Step 2 :
Step 3 :
Step 4 :
Created by Sumangala Biradar Module-4 BCS401
46
Created by Sumangala Biradar Module-4 BCS401
The resulting codewords are as follows:
47
Symbol A B C D _
Frequency 0.35 0.1 0.2 0.2 0.15
Codeword 11 100 00 01 101
Hence,
DAD is encoded as 011101
10011011011101 is decoded as BAD_AD
Average number of bits per symbol in this code
2 × 0.35 + 3 × 0.1+ 2 × 0.2 + 2 × 0.2 + 3 × 0.15 = 2.25
Created by Sumangala Biradar Module-4 BCS401