Greedy Algorithm
Greedy Algorithm
10
Huffman Coding Algorithm - Example
11
Huffman Coding Algorithm - Example
12
Huffman Coding Algorithm - Example
• Once the tree is built, each leaf node corresponds to a letter with a code.
• To determine the code for a particular node, traverse from the root to the leaf node.
• For each move to the left, append a 0 to the code, and for each move to the right, append a 1.
• As a result, for the above generated tree, we get the following codes:
•
Calculating Bits Saved
• let’s assume each character is stored with a three bit code. Since there are 133
such characters (multiply total frequencies by 3), the total number of bits used is
3 * 133 = 399.
• Using the Huffman coding frequencies we can calculate the new total number of
bits used:
• Thus, we saved 399 – 238 = 161 bits, or nearly 40% of the storage space
Problem
• Given an array F with size n. Assume the array content F[i] indicates the length of the ith file and
we want to merge all these files into one single file. Provide an algorithm with minimum merging
cost.
• Approach 1: Merge the files contiguously. That means select the first two files and merge them.
Then select the output of the previous merge and merge with the third file, and keep going.
• let us consider the following file sizes array. F = {10,5,100,50,20,15}
• As per the above algorithm, we need to merge the first two files (10 and 5 size files), and as a
result we get the following list of files. In the list below, 15 indicates the cost of merging two files
with sizes 10 and 5. {15,100,50,20,15}
• Similarly, merging 15 with the next file 100 produces: {115,50,20,15}. For the subsequent steps
the list becomes {165,20,15}, {185,15}
• Finally, {200}
• The total cost of merging = Cost of all merging operations = 15 + 115 + 165 + 185 + 200 = 680.
Problem - 1
• Given an array F with size n. Assume the array content F[i] indicates the length of the ith
file and we want to merge all these files into one single file. Provide an algorithm with
minimum merging cost. F = {10,5,100,50,20,15}
• Approach 2: Merge the files in pairs. That means after the first step, the algorithm
produces the n/2 intermediate files. For the next step, we need to consider these
intermediate files and merge them in pairs and keep going.
• Note: Sometimes this algorithm is called 2-way merging. Instead of two files at a time, if
we merge K files at a time then we call it K-way merging.
• we need to merge the first pair of files (10 and 5 size files), the second pair of files (100
and 50) and the third pair of files (20 and 15). As a result we get the following list of files.
{15,150,35}
• Similarly, merge the output in pairs and this step produces [below, the third element
does not have a pair element, so keep it the same]: {165,35} Finally, {185}
• The total cost of merging = Cost of all merging operations = 15 + 150 + 35 + 165 + 185 =
550.
Problem - 1
• Given an array F with size n. Assume the array content F[i] indicates the length of the ith
file and we want to merge all these files into one single file. Provide an algorithm with
minimum merging cost. F = {10,5,100,50,20,15}
• Approach 3: As per the above algorithm, after sorting the list it becomes:
{5,10,15,20,50,100}. We need to merge the two smallest files (5 and 10 size files) and as
a result we get the following list of files. In the list below, 15 indicates the cost of merging
two files with sizes 10 and 5. {15,15,20,50,100}
• Similarly, merging the two smallest elements (15 and 15) produces: {20,30,50,100}. For
the subsequent steps the list becomes {50,50,100} // merging 20 and 30
• {100,100} // merging 20 and 30
• Finally, {200}
• The total cost of merging = Cost of all merging operations = 15 + 30 + 50 + 100 + 200 =
395. So, this algorithm is producing the optimal solution for this merging problem.
Interval Scheduling Algorithm
• Given a set of n intervals S = {(starti, endj) for 1 ≤ i≤ n. Let us assume
that we want to find a maximum subset S′ of S such that no pair of
intervals in S′ overlaps. Provide an algorithm to schedule maximum
number of possible events.
Interval Scheduling Algorithm
• Approach 1: