Unit 2 - Analysis and Design of Algorithms - WWW - Rgpvnotes.in PDF
Unit 2 - Analysis and Design of Algorithms - WWW - Rgpvnotes.in PDF
Unit-2
Greedy Technique
1. Introduction:
Greedy is the most straight forward design technique. Most of the problems have n inputs and require
us to obtain a subset that satisfies some constraints. Any subset that satisfies these constraints is
called a feasible solution. We need to find a feasible solution that either maximizes or minimizes the
objective function. A feasible solution that does this is called an optimal solution.
The greedy method is a simple strategy of progressively building up a solution, one element at a
time, by choosing the best possible element at each stage. At each stage, a decision is made regarding
whether or not a particular input is in an optimal solution. This is done by considering the inputs in an
order determined by some selection procedure. If the inclusion of the next input, into the partially
constructed optimal solution will result in an infeasible solution then this input is not added to the
partial solution. The selection procedure itself is based on some optimization measure.
1.1 Elements of greedy algorithm
Greedy choice property
A globally optimal solution is derived from a locally optimal (greedy) choice.
When choices are considered, the choice that looks best in the current problem is
chosen, without considering results from sub problems.
Optimal substructures
A problem has optimal substructure if an optimal solution to the problem is composed
of optimal solutions to sub problems.
This property is important for both greedy algorithms and dynamic programming.
1.2 Problems based on Greedy Strategy:-
Fractional Knapsack
Optimal Merge Pattern
Job sequencing with Deadlines
Minimum Spanning Tree
Single Source Shortest path
1.3 Advantages & Disadvantages
Disadvantages:
They do not always work.
Short term choices may be disastrous on the long term.
Correctness is hard to prove
Advantages:
When they work, they work fast
Simple and easy to implement
Note:
Concepts
Choosing the best possible choice at each step.
This decision leads to the best overall solution.
Greedy algorithms do not always yield optimal solutions.
95
z4
z2 35 60 z
3
z1 15 20 30 30
x1 x5 x2
5 10
x4 x3
Time Complexity:
If list is kept in nondecreasing order: O(n2)
If list is represented as a minheap: O(n log n)
4. Huffman Codes
Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length codes to
input characters, lengths of the assigned codes are based on the frequencies of corresponding characters.
The most frequent character gets the smallest code and the least frequent character gets the largest code.
Analysis & Design of Algorithm (CS-4004) Page 17
The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences)
are assigned in such a way that the code assigned to one character is not prefix of code assigned to any
other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the
generated bit stream.
Steps to build Huffman code
Input is array of unique characters along with their frequency of occurrences and output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min Heap is used as
a priority queue. The value of frequency field is used to compare two nodes in min heap. Initially, the least
frequent character is at root)
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with frequency equal to the sum of the two nodes frequencies. Make the
first extracted node as its left child and the other extracted node as its right child. Add this node to the min
heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root node and
the tree is complete.
Example:
Letter A B C D E F
Frequency 10 20 30 40 50 60
V 210
0 1
Z 90 W 120
0 1 0 1
D 40 E 50 Y 60 F 60
0 1
X 30 C 30
0 1
A 10 B 20
Analysis of algorithm
Each priority queue operation (e.g. heap): O(log n)
In each iteration: one less subtree.
Initially: n subtrees.
Total: O(n log n) time.
5. Minimum Spanning Tree
A given graph can have many spanning trees. From these many spanning trees, we have to select a
cheapest one. This tree is called as minimal cost spanning tree.
Minimal cost spanning tree is a connected undirected graph G in which each edge is labeled with a
number (edge labels may signify lengths, weights other than costs). Minimal cost spanning tree is a
spanning tree for which the sum of the edge labels is as small as possible
In the spanning tree algorithm, any vertex not in the tree but connected to it by an edge can be added.
To find a Minimal cost spanning tree, we must be selective - we must always add a new vertex for which
the cost of the new edge is as small as possible.
If each edge of E has a weight, G is called a weighted graph.
Let G=(V,E) be an undirected connected graph.
T is a minimum spanning tree of G if T ⊆ E is an acyclic subset that connects all of the vertices and whose
total weight w(T ) = w(u, v) is minimized.
(u,v)∈T
Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep on adding the shortest (least
cost) edge, while avoiding the creation of cycles, until (n - 1) edges have been added. Sometimes two or
more edges may have the same cost. The order in which the edges are chosen, in this case, does not
matter. Different MSTs may result, but they will all have the same total cost, which will always be the
minimum cost.
Algorithm Kruskal (E, cost, n, t)
// E is the set of edges in G. G has n vertices. cost [u, v] is the
// ost of edge u, . t is the set of edges i the i i u -cost spanning tree.
// The final cost is returned.
{
Construct a heap out of the edge costs using heapify;
for i := 1 to n do parent [i] := -1;
i := 0; mincost := 0.0;
// Each vertex is in a different set.
while ((i < n -1) and (heap not empty)) do
{
Delete a minimum cost edge (u, v) from the heap and re-heapify using Adjust;
j := Find (u); k := Find (v);
if (j < k) then
{
i := i + 1;
t [i, 1] := u; t [i, 2] := v; mincost :=mincost + cost [u, v]; Union (j, k);
}
}
if (i >n-1) then write ("no spanning tree");
else return mincost;
}
Running time:
• The number of finds is at most 2e, and the number of unions at most n-1. Including the initialization
time for the trees, this part of the algorithm has a complexity that is just slightly more than O (n + e).
• We can add at most n-1 edges to tree T. So, the total time for operations on T is O(n).
Summing up the various components of the computing times, we get O (n + e log e) as asymptotic
complexity.
5.1.2 Pri ’s Algorith
P i s algorithm has the property that the edges in the set A always form a single tree.
The tree starts from an arbitrary root vertex r.
Grow the tree until it spans all the vertices in V.
At each step, a light edge is added to the tree A that connects A to an isolated vertex of GA = (V, A).
Algorithm Prim (E, cost, n, t)
Analysis & Design of Algorithm (CS-4004) Page 20
1 1
28
2 2
10
10 14 16
14 16
3 3
6 7 6 7
24
25 18 12 25 12
5 5
4 4
22 22
Analysis & Design of Algorithm (CS-4004) Page 21
1 1 1
10 10
10 2 2
2
3 6 7 3 7 3
6 7 6
25 25
5 5 5
4 4 4
22
(a) (b) (c)
1 1
1
10 10 10
2 2
2 16
16 14
3 7 3
7 3 6 7 6
6
25 25
25 12 12
12
5 5
5
4 22 4
22 4 22
Kruskal Algorithm:
1 1
1
10 10
2 2 2
3 7 3 3
6 7 6 7
6
12
5 5 5
4 4 4
1 1
1
10
10 10 2 2
2 14 16
14 14 16
3 6 3
6 3 6 7 7
7
12 12
12
5 5
5
4 22 4
4
6. KNAPSACK PROBLEM
In Knapsack problem there are n objects and each object i has a weight wi and a profit pi and Knapsack
(Capacity) is M. The objective is to obtain a filling of knapsack to maximizing the total profit.
There are two types of Knapsack problem:
Fractional Knapsack
O/1 Knapsack
0/1 Knapsack is based on Dynamic Programming and Fractional knapsack is based on Greedy Strategy,
Here we are discussing Fractional Knapsack.
Let us appl the g eed ethod to sol e the k apsa k p o le . We a e gi e o je ts a d a knapsack.
The o je t i has a eight i a d the k apsa k has a apa it . If a f a tio i, < i < of o je t i is
placed into the knapsack then a profit of pi xi is earned. The objective is to fill the knapsack that maximizes
the total profit earned.
maximize pi xi
“i e the k apsa k apa it is , e e ui e the total eight of all hose o je ts to e at ost .
w x
1 i n
and 0 xi 1, 1 i n
6.1 Algorithm
If the objects are already been sorted into non-increasing order of p[i] / w[i] then the algorithm given
below obtains solutions corresponding to this strategy. Here p[i] / w[i] is our selection function.
Greedy Fractional-Knapsack (P[1..n], W[1..n], X [1..n], M)
/* P[1..n] and W[1..n] contains the profit and weight of the n-objects ordered such that X[1..n] is
a solution set and M is the capacity of Knapsack*/
{
For i ← to n do
X[i] ←
profit ← //Total profit of item filled in Knapsack
weight ← // Total weight of items packed in Knapsack
i←
While (Weight < M) // M is the Knapsack Capacity
{
if (weight + W[i] M)
X[i] = 1
weight = weight + W[i]
else
X[i] = (M-weight)/w[i]
weight = M
Analysis & Design of Algorithm (CS-4004) Page 24
Running time:
The objects are to be sorted into non-decreasing order of pi / wi ratio. But if we disregard the time to
initially sort the objects, the algorithm requires O(nlogn) time.
Example:
n = 3 objects, m = 20
P = (25, 24, 15), W = (18, 15, 10),
V =P/W
V= (1.39, 1.6, 1.5)
Objects in decreasing order of V are {2 , 3 , 1}
Set X = {0, 0, 0} and Rem = m = 20
K = 1, Choose object i = 2:
w2 < Rem, Set x2 = 1, w2 x2 = 15 , Rem = 5
K = 2, Choose object i = 3:
w3 > Rem, break;
K < n, x3 = Rem / w3 = 0.5
Optimal solution is X = (0 , 1.0 , 0.5) ,
Total profit is 1 i n pi xi = 31.5
Total weight is 1 i n wi xi = m = 20
We still have to discuss the running time of the algorithm. The initial sorting can be done in time O(n log
n), and the rest loop takes time O(n). It is not hard to implement each body of the second loop in time
O(n), so the total loop takes time O(n2). So the total algorithm runs in time O(n2). Using a more
sophisticated data structure one can reduce this running time to O(n log n), but in any case it is a
polynomial-time algorithm.