Week 5 and 6 - Greedy Strategy Technique
Week 5 and 6 - Greedy Strategy Technique
Week 5 and 6 - Greedy Strategy Technique
Code: CS-14101
Branch: B.Tech -IT 4th Semester
Week 4 &5 : Greedy Strategy Technique
• Greed works!!!!
We want to verify whether is it really so ?
Huffman Codes
• For compressing data (sequence of
characters)
• Widely used
• Very efficient (saving 20-90%)
• Use a table to keep frequencies of
occurrence of characters.
• Output binary string.
Huffman Codes Frequency Fixed-length Variable-length
Example:
codeword codeword
‘a’ 45000 000 0
‘b’ 13000 001 101
A file of 100,000 characters. ‘c’ 12000 010 100
Containing only ‘a’ to ‘e’
‘d’ 16000 011 111
‘e’ 9000 100 1101
‘f’ 5000 101 1100
0 100 100
1 0 1
86 14 a:45 55
0 1 0 0 1
58 28 14 25 30
0 1 0 1 0 1 0 1 0 1
a:45 b:13 c:12 d:16 e:9 f:5 b:13 c:12 14 d:16
0 1
e:9 f:5
Huffman Codes
How do we find the optimal Huffman code (1952) was invented to solve it.
prefix code? A Greedy Approach.
….
• electrical grids.
Thus, we may refer to Kruskal's algorithm as an O(n2log n) algorithm, where n is the number of
vertices. However, note that if |E| is similar to |V|, then the complexity is O(n2).
Time Complexity
• The edges are maintained as min heap.
• The next edge can be obtained in O(logE) time if graph has E edges.
• Reconstruction of heap takes O(E) time.
• So, Kruskal’s Algorithm takes O(ElogE) time.
• The value of E can be at most O(V2).
• So, O(logV) and O(logE) are same.
• If the edges are already sorted, then there is no need to construct
min heap.
• So, deletion from min heap time is saved.
• In this case, time complexity of Kruskal’s Algorithm = O(E + V)
Prim’s Algorithm
Prim’s Algorithm
Prim’s Algorithm
Prim’s Algorithm
Prim’s Algorithm
Prim’s Algorithm
Time Complexity
• If adjacency list is used to represent the graph, then using breadth first
search, all the vertices can be traversed in O(V + E) time.
• We traverse all the vertices of graph using breadth first search and use a
min heap for storing the vertices not yet included in the MST.
• To get the minimum weight edge, we use min heap as a priority queue.
• Min heap operations like extracting minimum element and decreasing key
value takes O(logV) time.
• So, overall time complexity
= O(E + V) x O(logV)
= O((E + V)logV)
= O(ElogV)
Single-source shortest-paths problem
• Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Example of Dijkstra’s property fails with
negative edge weights
Dijkstra’s algorithm would visit b then a and leave b with a distance of 2 instead of
the correct distance 1.
The problem is that when Dijkstra visits b, it fails to consider the possibility of
there being a shorter path from a to b (which is impossible with nonnegative edge
weights).
Container Loading problem
• A large ship is to be loaded with containers of cargos. Different
containers, although of equal size, will have different weights.
• Let wi be the weight of the ith container, 1 ≤ i ≤ n, and the capacity of
the ship is c.
• Find out how could we load the ship with the maximum number of
containers.
• Let xi ∈ {0, 1}.
• If xi= 1, we will load the ith container, otherwise, we will not load it.
• We wish to assign values to x‘is such that c, and xi ∈ {0, 1}.
• The optimization function is
Example
• Given that ,
• n = 8,
• i = [1,2,3,4,5,6,7,8],
• wi = [100,200, 50, 90, 150, 50, 20, 80]
• C= 400
• What is the order of loading ?
• [x1…x8]=[]
• [x1….x8] = [1,0,1,1,0,1,1,1]
The Container Loading Problem
• Let xi be a boolean variable (1=container loaded ,
0=container not loaded).
• The problem is to assign values xi such that with
• Wi - the weight of the container i and
• C - the maximum cargo carrying capacity of a ship
• is maximized with
𝑛
respect to the constraints that
𝑊𝑖𝑋𝑖 ≤ 𝐶
𝑖=1
Algorithm Container Loading
1. for i=1 to n
2. do x[i] = 0;
3. t <- Allocate the memory (n);
4. Indirect-Sort(w, t, n);
5. while (i <=n) and w[t[i]] ≤ C)
6. do
7. x[t[i]] = 1;
8. C = C – w[t[i]];
9. i=i+1;
10. delete t
Knapsack problem
• The problem of knapsack is to fill the available items into the knapsack so
that the knapsack gets filled up and yields a maximum profit.
• Profit= 47 units
• Profit= 54 units
4 -53
Optimal Storage on Tapes
• If all programs are retrieved equally often, then the mean
retrieval time
1 n
(MRT) = n j1
tj
d(L) = L
j1 k 1
ik
4 -54
Example
• Let n = 3, (L1,L2,L3) = (5,10,3). 6 possible orderings.
The optimal is 3,1,2
Ordering I d(I)
1,2,3 5+5+10+5+10+3 = 38
1,3,2 5+5+3+5+3+10 = 31
2,1,3 10+10+5+10+5+3 = 43
2,3,1 10+10+3+10+3+5 = 41
3,1,2 3+3+5+3+5+10 = 29
3,2,1, 3+3+10+3+10+5 = 34
4 -56
Optimal Storage on Tapes
• From the above discussion one can observe that the MRT can be
minimized if the programs are stored in an increasing order i.e., l1 <=
l2 <= l3, … ln.
• The solution set obtained need not be a subset of data but may be
the data set itself in a different sequence.
Pseudo code : Optimal Storage on Tapes
tapes(n, m) // here n is the numbers of programs and m is the numbers of tapes
{
j=0;
Sort (n);
for(i=1 to n do)
{
write ("append", i "to the permutation of the tape", j)
j=(j+1) mod m
}
}