DAA ch3 Updated 2016
DAA ch3 Updated 2016
2016
GREEDY METHOD
A Greedy algorithm is an approach to solving a problem that selects the most appropriate option
based on the current situation. This algorithm ignores the fact that the current best result may not
bring about the overall optimal result. Even if the initial decision was incorrect, the algorithm never
reverses it.
This simple, intuitive algorithm can be applied to solve any optimization problem which requires
the maximum or minimum optimum result. The best thing about this algorithm is that it is easy to
understand and implement.
General method:
In this method, the decision of solution is taken based on the information available.
It is a straight forward method.
It is used for obtaining optimizing solution.
In greedy technique, the solution is constructed through a sequence of steps, each
expanding a partially constructed solution obtained, until a complete solution to the
problem is reached,
At each step the choice made should be
Algorithm:
Greedy(D,n)
//Initially assume
Solution=0;
For i=0 to n do
Page 1
Design and Analysis of Algorithm G3 CS 2016
2016
If( feasible(solution, s)) then // check if the selected solution is feasible or not
}
Return solution;
}
Some properties in greedy method:
Greedy choice property: This property says that the globally optimal solution can be
obtained by making a locally optimal solution (Greedy). The choice made by a Greedy
algorithm may depend on earlier choices but not on the future. It iteratively makes one
Greedy choice after another and reduces the given problem to a smaller one.
Note: Making locally optimal choices does not always work. Hence, Greedy algorithms will not
always give the best solutions.
Job Sequencing
Consider that there are n jobs that are to be executed. At any time t=1,2, 3,… only exactly
one job is to be executed. The profits pi are given. These profits are gained by
corresponding jobs. For obtaining feasible solution we should take care that the jobs get
completed within their given deadlines.
Let n=4
Page 2
Design and Analysis of Algorithm G3 CS 2016
2016
Goal is to schedule jobs to maximize the total profit.
Consider all possible schedules and compute the minimum total time in the system
Page 3
Design and Analysis of Algorithm G3 CS 2016
2016
Each job takes only one unit of time. Deadline of job means a time on which or before which
the job has to be executed. The sequence {2,4} is not allowed because both have deadline 1.
If job 2 is started at 0 it will be completed on 1 but we cannot start job 4 on 1 since deadline
Page 4
Design and Analysis of Algorithm G3 CS 2016
2016
of job 4 is 1.the feasible sequence is a sequence that allows all jobs in a sequence to be
executed within their deadlines and highest profit can be gained.
In above example sequence 3,2 is not considered as d3 > d2 but we have considered the
sequence 2,3 as feasible solution because d2 < d3
We have chosen job 1 first then we have chosen job 4. The solution 4, 1 is feasible as the
order of execution is 4 then 1. Also, d4 < d1. If we try {1,3,4} then it is not a feasible solution,
hence reject 3 from the set. Similarly,if we add job 2 in the sequence then the sequence
becomes {1,2,4}.This is also not a feasible solution hence reject it. Finally the feasible
sequence is 4,1 . This sequence is optimum solution as well.
Algorithm:
Algorithm Job_seq(D,J,n)
//Problem description: This algorithm is for job sequencing using Greedy method.
Count =1 ;
For 2 to n do
{t = count;
J[s+1] ← J[s];
J[t=1] ← I;
Count ← count +1 ;
Page 5
Design and Analysis of Algorithm G3 CS 2016
2016
} // end of while
Return count;
The sequence of J will be inserted if and only if D[J[t]] != t .This also means that the job J
will be processede if it is in within the deadline.
The computing time taken by above Job_seq algorithm is О(n2 ),because the basic operation
of computing sequence in array J is within two nested for loops.
For instance :if there are a, b,c and d files which are already sorted and has to be merged
then-
We may merge a,b to produce x1 then x1 can be merged with file c to produce X2 ,then
X2 can be merged with file d to produce X3.
We may merge a,b to produce x1,then merge c and d to produce X2 finally merge x1
and X2 to produce x3
Thus, different pairings can be possible to produce final merged file. Clearly these pairing
may require different amount of computing time. The pairing of files which require optimum
(minimum) computing time to get a single merged file denotes the optimal merge pattern.
For example: consider that there are 3 files a,b,c of lengths 11,12 and 13 respectively. Then
following are two sample merge patterns that can be produced.
Pattern 2 is faster than first pattern. A Greedy method is used to obtain optimal merge pattern
For example, 2,3,5,7,15 are six files with given lengths then we can build a merge pattern as
Page 6
Design and Analysis of Algorithm G3 CS 2016
2016
The leaf nodes denoted by squares called external nodes and remaining nodes are called
internal nodes. The path length from root to corresponding external node is called external
path length. An application of optimal merge pattern is Huffman code
Huffman Code:
Huffman coding is a data compression algorithm that is used to reduce the size of files or data
streams without losing any information. It is used for lossless data compression, and was
developed by David A. Huffman in 1952 while he was a graduate student at MIT.
Lossless compression is a method of data compression where the original data can be perfectly
reconstructed from the compressed data. The primary goal of lossless compression is to reduce
the size of the data without any loss of information. In lossless compression, the compressed data
retains all the original data's details and quality.
The basic idea behind Huffman coding is to assign variable-length codes to different symbols in a
given dataset, such as characters in a text document, based on their frequency of occurrence. The
more frequently a symbol appears, the shorter its corresponding code will be. This allows for
more efficient representation of commonly occurring symbols, resulting in overall compression of
the data.
In Huffman‟s coding method, the data is inputted as a sequence of characters.Then a table of
frequency of occurrence of each character in the data is built.
From the table of frequencies Huffman‟s tree is constructed using Greedy approach.
The Huffman‟s tree is further used for encoding each character, so that binary encoding is
obtained for given data.
In Huffman coding there is a specific method of representing each symbol. This method
produces a code in such a manner that no code word is prefix of some other code word. such
codes are called prefix code or prefix-free codes. thus, this method is useful for obtaining
optimal data compression.
Solution: Basically, there are two types of coding-1. Fixed length encoding and 2. Variable
length coding. If we use fixed length coding, we need 3 bits to represent any character from
a to h.
Page 7
Design and Analysis of Algorithm G3 CS 2016
2016
Page 8
Design and Analysis of Algorithm G3 CS 2016
2016
Step 7:
Hence if the string is “aeed” then we get 000100100011 as a code word. But this method is
not that much efficient.
Hence variable length encoding has come up to bring efficiency in Huffman‟s algorithm. By
this method, we assign short code word for frequently used characters and long code word for
infrequent characters.
Page 9
Design and Analysis of Algorithm G3 CS 2016
2016
Page 10
Design and Analysis of Algorithm G3 CS 2016
2016
Page 11
Design and Analysis of Algorithm G3 CS 2016
2016
Step 7:
The method of using variable length encoding is really efficient than fixed length encoding.
Consider,for each character number of bits required = frequency * number of bits used for
representation.
Clearly Variable length encoding requires less number of bits than fixed length encoding.
Huffman’s Algorithm: The Greedy method is used to construct optimal prefix code called
Huffman code. This algorithm builds a tree in bottom up manner. We can denote this tree byT.
Page 12
Design and Analysis of Algorithm G3 CS 2016
2016
a = left(temp)
b=right(temp)
f(temp) ← f(a) + f(b)
insert (Q,temp)
}
Return Get_min (Q)
Analysis:
The Priority queue Q can be implemented as binary Heap. The construction of binary heap
can be done in O(n) time. The for loop will be executed in O(log n) time. Hence for (n-1)
nodes O(n log n) time will be required. Therefore total running time of Huffman‟s
algorithmwill be O(log n)
Graphs:
Definition of Graph:
A graph is a collection of two sets V and E where V is a finite non empty set of vertices and
E is a finite non empty set of edges.
Vertices are nothing but eh nodes in the graph and the two adjacent vertices are joined by
edges. The graph is thus a set of two sets.
Type of Graphs:
Directed Graphs
Undirected Graphs
In the directed graph the directions are shown on the edges.The edges between the vertices
are ordered.in this type of graph,the edge E1 is in between the vertices V1 and V2.the V1 is
called head and the V2 is called the tail. similarly for V1 head , the tail is V3 and so on.
Page 13
Design and Analysis of Algorithm G3 CS 2016
2016
Spanning tree:
A spanning tree of a graph G is a subgraph which is basically a tree and it contains all the
vertices of G containing no circuit.
A minimum spanning tree of a weighted connected graph G is a spanning tree with minimum
or smallest weight.
A weight of the tree is defined as the sum of weights of all its edges.
For example: consider a weighted connected graph G because some weights are given along
every edge and the graph is a connected graph.
Page 14
Design and Analysis of Algorithm G3 CS 2016
2016
Application of Spanning trees:
2.Spanning trees have wide applications in many areas such as network design.
Now, we will consider all the vertices first. Then we will select an edge with minimum
weight. The algorithm proceeds by selecting adjacent edges with minimum weight. Care
should be taken for not forming circuit
Page 15
Design and Analysis of Algorithm G3 CS 2016
2016
Page 16
Design and Analysis of Algorithm G3 CS 2016
2016
Algorithm:
Prim (G[0,….size-1,0….size-1],nodes)
//Problem Description: This algorithm is for implementing Prim‟s algorithm for finding the
spanning tree
Total = 0;
For i ←o to nodes -1 do
Tree[i] ← 0
Page 17
Design and Analysis of Algorithm G3 CS 2016
2016
For K← 1 to nodes do
Min_dist ← ∞
For i← 0 to nodes-1 do
For j← 0 to nodes-1 do (
//select an edge such that one vertex is selected and the other is not and the edge has the least
weight
V1← i;
}}}}
Write (V1,V2,Min_dist);
Tree[V1] ← tree{V2] ← 1
Analysis: The algorithm spends most of the time in selecting the edge with minimum length.
Hence the basic operation of this algorithm is to find the edge with minimum path length.
This can be given by following formula
Page 18
Design and Analysis of Algorithm G3 CS 2016
2016
We take variable n for „nodes‟ for the sake of simplicity of solving the equation then
T(n) =
=2n
= 2n2 – 2n
T(n) = n2
T(n) = ϴ(n2 )
Kruskal’s Algorithm:
It is an another algorithm for obtaining minimum spanning tree.n this algorithm always the
minimum cost edge has to be selected.But it is not necessary that selected optimum edge is
adjacent.
This algorithm is for obtaining the minimum This algorithm is for obtaining the minimum
spanning tree by selecting the adjacent spanning tree but it is not necessary to choose
vertices of already selected vertices adjacent vertices of already selected vertices
Page 19
Design and Analysis of Algorithm G3 CS 2016
2016
Example:
First , we will select all the vertices .Then an edge with optimum weight is selected from
heap,even though it is not adjacent to previously seelcted edge.Care should be taken for not
forming circuit.
Page 20
Design and Analysis of Algorithm G3 CS 2016
2016
Page 21
Design and Analysis of Algorithm G3 CS 2016
2016
Page 22
Design and Analysis of Algorithm G3 CS 2016
2016
Sum ← 0
For i← 0 to tot_nodes do
parents[i] ← i
while ( count != tot_nodes -1 ) do
{
Post _Minimum (tot_edges); // finding the minimum cost edge
If(pos = -1 then //Perhaps no node in the graph
Break
V1 ← G[pos].V1
V2 ← G[pos].V2
i ←find(v1,parent)
j ←Find(v2,parent)
if( i !=j) then
{
Tree[k][0] ← V1 //Tree[] is an array in which the spanning tree edges are stored.
Tree[k] [1] ← v2
K++
Count ++;
Sum + ← G[pos].cost //computing the total cost of all the minimum distances
Union( I,j,parent);
}
G[pos].cost INFINITY
}
If(count = tot_nodes -1 ) then
{
For i ← 0 to tot_nodes -1
{
Write (tree[i][0],tree[i] [1] //for each node of I, the minimum distnace edges are collected in
the array tree[][].The spanning treeis printed here
}
Write (“cost of spanning tree is”,sum)
}
Analysis: The efficiency of Kruskal‟s algorithm is ϴ(ǀEǀ log ǀEǀ ) where E is the edges in
thegraph.
Dijkstra’s Algorithm:
It is a popular algorithm for finding the shortest path. This is also called the single source
shortest path algorithm. In this algorithm, for a given vertex called source the shortest path
to all other vertices is obtained and the main focus is not to find only one single path but to
find the shortest paths from any vertex to all other remaining vertices.
Page 23
Design and Analysis of Algorithm G3 CS 2016
2016
This algorithm finds a shortest path to graph‟s vertices in order of their distance from a given
source. In this process of finding shortest path, first it finds the shortest path from the source
toa vertex nearest to it, then second nearest and so on.
The shortest path from V0 is obtained. First, we find shortest path from V0-V1 then V1-V2
then from V2-V3 the shortest distance is obtained. Consider a weighted connected graph
below:
Now, we will consider each vertex as a source and will find the shortest distance from this
vertex to every other remaining vertex. Let us start with vertex A
Page 24
Design and Analysis of Algorithm G3 CS 2016
2016
But we have one shortest distance obtained from A to E and that is A-B-C-E with path length
=4+1+3 =8. Similarly other shortest paths can be obtained by choosing appropriate source
and destination.
Path[v2] ← v1
}}}}} // V1 is next selected destination vertex with shortest distance .All such vertices are
accumulated in array path []
Knapsack Algorithm
The weights (Wi) and profit values (Pi) of the items to be added in the knapsack are taken as an
input for the fractional knapsack algorithm and the subset of the items added in the knapsack
without exceeding the limit and with maximum profit is achieved as the output.
Algorithm
Consider all the items with their weights and profits mentioned respectively.
Calculate Pi/Wi of all the items and sort the items in descending order based on their
Pi/Wi values.
Without exceeding the limit, add the items into the knapsack.
If the knapsack can still store some weight, but the weights of other items exceed the limit,
the fractional part of the next time can be added.
Hence, giving it the name fractional knapsack problem.
Examples
For the given set of items and the knapsack capacity of 10 kg, find the subset of the items to
be added in the knapsack such that the profit is maximum.
Page 25
Design and Analysis of Algorithm G3 CS 2016
2016
Items 1 2 3 4 5
Profits 10 15 10 12 8
Solution
Step 1
Given, n = 5
Wi = {3, 3, 2, 5, 1}
Pi = {10, 15, 10, 12, 8}
Items 1 2 3 4 5
Profits 10 15 10 20 8
Pi/Wi 3.3 5 5 4 8
Step 2
Items 5 2 3 4 1
Profits 8 15 10 20 10
Pi/Wi 8 5 5 4 3.3
Step 3
Without exceeding the knapsack capacity, insert the items in the knapsack with maximum profit.
Page 26
Design and Analysis of Algorithm G3 CS 2016
2016
Knapsack = {5, 2, 3}
However, the knapsack can still hold 4 kg weight, but the next item having 5 kg weight will exceed
the capacity. Therefore, only 4 kg weight of the 5 kg will be added in the knapsack.
Items 5 2 3 4 1
Profits 8 15 10 20 10
Knapsack 1 1 1 4/5 0
Hence, the knapsack holds the weights = [(1 * 1) + (1 * 3) + (1 * 2) + (4/5 * 5)] = 10, with
maximum profit of [(1 * 8) + (1 * 15) + (1 * 10) + (4/5 * 20)] = 37.
Page 27