Greedy Appraoch and Dynamic Programming
Greedy Appraoch and Dynamic Programming
D
B
A
Floyd’s Algorithms- All-pairs shortest-paths
problem
• It is convenient to record 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:
Floyd’s Algorithms
The element d(k)ij in the ith row and the jth column of
matrix D(k) (i, j = 1, 2, . . . , n, k = 0, 1, . . . , n) is equal to
the length of the shortest path among all paths from the
ith vertex to the jth vertex with each intermediate vertex,
if any, numbered not higher than k.
• D (0) is simply the weight matrix of the graph
• The last matrix in the series, D(n), contains the lengths of
the shortest paths among all paths that can use all n
vertices as intermediate
• We can Compute D(k) from D (k-1)
Floyd’s Algorithms
Greedy Technique
• A greedy algorithm obtains an optimal
solution to a problem by making a sequence
of choices. At each decision point, the
algorithm makes choice that seems best at
the moment. This heuristic strategy does not
always produce an optimal solution.
Greedy-choice property
• greedy-choice property: we can assemble a
globally optimal solution by making locally
optimal (greedy) choices. In other words, when
we are considering which choice to make, we
make the choice that looks best in the current
problem, without considering results from
subproblems.
Huffman Trees and Codes
• Encoding 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.
• For example, we can use a fixed-length
encoding that assigns to each symbol a bit
string of the same length m (m ≥ log2 n). This is
exactly what the standard ASCII code does.
• One way of getting a coding scheme that yields
a shorter bit string on the average is based on
the old idea of assigning shorter codewords to
more frequent symbols and longer codewords
to less frequent symbols.
Problem with Variable Length Encoding
• How can we tell how many bits of an encoded
text represent the first symbol
Solution : Use Prefix free or Prefix code
• In a prefix code, no codeword is a prefix of a codeword
of another symbol.
• Hence, with such an encoding,
– we can simply scan a bit string until we get the first
group of bits that is a codeword for some symbol,
– replace these bits by this symbol,
– and repeat this operation until the bit string’s end is
reached.
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.
• Step 2 : Repeat the following operation until a
single tree is obtained. Find two trees with the
smallest weight. 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 3: All the left edges are labeled by 0 and all the
right edges are labeled by 1. The codeword of a symbol
can then be obtained by recording the labels on the
simple path from the root to the symbol’s leaf.
Huffman Tree
Huffman Code
Encode : CAB
0011100
Decode : 011010011100
011010011100
D _ C A B
Prim’s Algorithm
• A spanning tree of an undirected connected
graph is its connected acyclic subgraph that
contains all the vertices of the graph.
• If such a graph has weights assigned to its
edges, a minimum spanning tree is its
spanning tree of the smallest weight, where the
weight of a tree is defined as the sum of the
weights on all its edges.
• The minimum spanning tree problem is the
problem of finding a minimum spanning tree for
a given weighted connected graph.
Algorithm
Analysis
• If a graph is represented by its adjacency lists and
the priority queue is implemented as a min-heap, the
running time of the algorithm is in O(|E| log |V |).
• This is because the algorithm performs |V| − 1
deletions of the smallest element and makes |E|
verifications and, possibly, changes of an element’s
priority in a min-heap of size not exceeding |V |.
Each of these operations, as noted earlier, is a O(log
|V |) operation. Hence, the running time of this
implementation of Prim’s algorithm is in
Kruskal’s algorithm
• Kruskal’s algorithm looks at a minimum
spanning tree of a weighted connected graph
G = V, E as an acyclic subgraph with |V| − 1
edges for which the sum of the edge weights
is the smallest.