Daa Module4 Slides
Daa Module4 Slides
DYNAMIC PROGRAMMING
Warshall’s Algorithm
Sumangala Biradar
ISE Department, BLDEACET
Sumangala Biradar
ISE Department, BLDEACET
Initial condition
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
Kruskal's Algorithm
Dijkstra's Algorithm
6 c 6 c c
a a a
4 1 1 1
4
2 2
d d d
b 3 b b 3
Run-time
O(|E| log |V|)
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
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
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.
Step 2 :
Step 3 :
Step 4 :
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