ADA Unit 2 - 1711437399
ADA Unit 2 - 1711437399
INSTITUTIONS
Rupanshi Patidar
Assistant Professor
CI Department
Study of Greedy strategy
• A greedy strategy is an algorithmic paradigm that
follows the problem-solving heuristic of making the
locally optimal choice at each stage with the hope
of finding a global optimum.
• In other words, at each step of the algorithm, it
makes the best decision without considering the
consequences of that decision on future steps.
Greedy algorithms are known for their simplicity
and efficiency in many scenarios, although they
don't always guarantee an optimal solution.
Study of Greedy strategy
Applications of Greedy Approach:
(8) Dijkstra’s algorithm
(1) Make a change problem
(9) Greedy coloring
(2) Knapsack problem
(10) Minimum cost
(3) Minimum spanning tree spanning tree
(4) Single source shortest
(11) Job scheduling
path
(12) Interval scheduling
(5) Activity selection
problem (13) Greedy set cover
(6) Job sequencing problem (14) Knapsack with
(7) Huffman code generation. fractions
Characteristic components of greedy
algorithm:
Step 4:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Example
Step 1. Build a min heap that contains 6 nodes where
each node represents root of a tree with single node.
Step 2 Extract two minimum frequency nodes from min
heap. Add a new internal node with frequency
5 + 9 = 14.
Example
• Now min heap contains 5 nodes where 4 nodes are
roots of trees with single element each, and one
heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal node 14
e 16
f 45
Example
• Step 3: Extract two minimum frequency nodes from
heap. Add a new internal node with frequency 12 +
13 = 25
Example
• Now min heap contains 4 nodes where 2 nodes are
roots of trees with single element each, and two
heap nodes are root of tree with more than one
nodes
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Example
• Step 4: Extract two minimum frequency nodes. Add a
new internal node with frequency 14 + 16 = 30
Example
• Now min heap contains 3 nodes.
character Frequency
Internal Node 25
Internal Node 30
f 45
character Frequency
f 45
Internal node 55
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
• Traverse the tree formed starting from the root.
• Maintain an auxiliary array.
• While moving to the left child, write 0 to the array.
• While moving to the right child, write 1 to the array.
• Print the array when a leaf node is encountered.
Example
Example
• The codes are as follows:
character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111
Huffman Coding
• Time complexity: O(nlogn)
• Space complexity :- O(N)
Applications of Huffman Coding:
• They are used for transmitting fax and text.
• They are used by conventional compression formats
like PKZIP, GZIP, etc.
• Multimedia codes like JPEG, PNG, and MP3 use
Huffman encoding(to be more precise the prefix
codes).
Huffman Coding Example : 1
Que – 2. How many bits may be required for encoding
the message ‘mississippi’?
• Solution: Following is the frequency table of
characters in ‘mississippi’ in non-decreasing order of
frequency:
Huffman Coding Example : 1
Huffman Coding Example : 1
Huffman Coding Example : 1
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Kruskal’s Algorithm
Choose the edge with the least weight, if there are more than 1,
choose anyone
Kruskal’s Algorithm
Choose the next shortest edge that doesn't create a cycle and add it
Kruskal’s Algorithm
Choose the next shortest edge that doesn't create a cycle and add it
Choose a vertex
Choose the nearest edge not yet in the solution, if there are multiple
choices, choose one at random
Prims Algorithm
The final structure of the MST is as follows and the weight of the edges
of the MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
Prims Algorithm
Prim's Algorithm Complexity
• The time complexity of Prim's algorithm is O(E log V).
Items 5 2 3 4 1
Weights (in kg) 1 3 2 5 3
Profits 8 15 10 20 10
Knapsack 1 1 1 4/5 0