Mod-4 Greedy Dynamic-24
Mod-4 Greedy Dynamic-24
• Design method that can be used when the solution to a problem can be
viewed as the result of a sequence of decisions.
• Greedy strategy
– Explores only one path (decision sequence), which seems to be
optimal.
– In dynamic programming all the paths are explored.
• Brute Force
– Try all possible sequences and pick the best.
– Time and space requirements will be very large.
• It is similar to divide and conquer in that, an instance of the problem
is divided into smaller instances.
• In this approach, smaller instances are solved first and stored, instead
of recomputing it when it is required.
•
Dynamic programming Principle
• Drastically reduces the amount of enumeration by avoiding enumeration
of some decision sequences that cannot possibly be optimal.
• Uses principle of optimality to obtain an optimal decision sequence.
• Principle Of Optimality
V [i 1, j ] if j-wi 0
V [i, j ]
max{V [i 1, j ], V [i 1, j wi ] pi } if j-wi >=0
• Transitive Closure:
Rules for warshall’s Algorithm
Algorithm
• First, it finds the shortest path from the source to a vertex nearest to it, then to
a second nearest, and so on.
Algorithm
• N: set of nodes for which shortest path already found
• Initialization: (Start with source node s)
• N = {s}, Ds = 0, “s is distance zero from itself”
• Dj=Csj for all j s, distances of directly-connected neighbors
• Step A: (Find next closest node i)
• Find i N such that
• Di = min Dj for j N
• Add i to N
• If N contains all the nodes, stop
• Step B: (update minimum costs)
• For each node j N
• Dj = min (Dj, Di+Cij)
• Go to Step A
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
4 1 4 2
1 2
3 3
2 2
5 4 5
4
2 3 1
1
6
5 2
3
1 4 2
3
2
4 5
Huffman Trees
• to encode a text that comprises characters from some n-character alphabet by assigning to
each of the text's characters some sequence of bits called the codeword.
• we can use a fixed-length encoding that assigns to each character a bit string of the same
length m.
• To avoid this complication, we can limit ourselves to prefix-free (or simply prefix) codes.
In a prefix code, no codeword is a prefix of a codeword of another character
Hence, DAD is encoded as 011101, and 10011011011101 is
decoded as BAD_AD.