5CS4-AOA-Unit-2_ppt @zammers
5CS4-AOA-Unit-2_ppt @zammers
Jaipur
1
Greedy Method
Def: A greedy algorithm 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.
"Greedy Method finds out of many options,
but you have to choose the best option."
In this method, we have to find out the best
method/option out of many present ways.
In this approach/method we focus on the
first stage and decide the output, don't think
about the future.
This method may or may not give the best output.
Greedy Algorithm solves problems by making the
best choice that seems best at the particular
moment.
Many optimization problems can be determined
using a greedy algorithm.
A greedy algorithm may provide a solution that is
close to optimal.
A greedy algorithm works if a problem exhibits the
following two properties:
Greedy Choice Property: A globally optimal solution can be reached at
by creating a locally optimal solution. In other words, an optimal solution
can be obtained by creating "greedy" choices.
Optimal substructure: Optimal solutions contain optimal sub solutions.
In other words, answers to sub problems of an optimal solution are
optimal.
Example:
machine scheduling
Fractional Knapsack Problem
Minimum Spanning Tree
Huffman Code
Job Sequencing
Activity Selection Problem
Steps for achieving a Greedy Algorithm
are:
Feasible: Here we check whether it
satisfies all possible constraints or not, to
obtain at least one solution to our
problems.
Local Optimal Choice: In this, the
choice should be the optimum which is
selected from the currently available
Unalterable: Once the decision is made,
at any subsequence step that option is
not altered.
Components of Greedy Algorithm
Here-
You are given a set of jobs.
Each job has a defined deadline and some profit associated with it.
The profit of a job is given only when that job is completed within
its deadline.
Only one processor is available for processing all the jobs.
Processor takes one unit of time to complete a job.
Greedy Algorithm is adopted to determine how the next job is selected for an optimal solution.
The greedy algorithm described below always gives an optimal solution to the job sequencing
problem-
Step-01:
Sort all the given jobs in decreasing order of their profit.
Step-02:
Check the value of maximum deadline.
Draw a Gantt chart where maximum time on Gantt chart is the value of maximum deadline.
Step-03:
Pick up the jobs one by one.
Put the job on Gantt chart as far as possible from 0 ensuring that the job gets completed before
its deadline..
Step-01:
Sort all the given jobs in decreasing order
of their profit-
Part-01:
The optimal schedule is-
J2 , J4 , J3 , J5 , J1
This is the required order in which the jobs must be completed in
order to obtain the maximum profit.
Part-02:
All the jobs are not completed in optimal schedule.
This is because job J6 could not be completed within its deadline.
Part-03:
Maximum earned profit
= Sum of profit of all the jobs in optimal schedule
= Profit of job J2 + Profit of job J4 + Profit of job J3 + Profit of job
J5 + Profit of job J1
= 180 + 300 + 190 + 120 + 200
Optimal Merge Pattern
Merge a set of sorted files of different length into
a single sorted file. We need to find an optimal
solution, where the resultant file will be generated
in minimum time.
Removing one edge from the spanning tree will make the
graph disconnected, i.e. the spanning tree is minimally
connected.
Step-01:
Randomly choose any vertex.
The vertex connecting to the edge having least weight is usually selected.
Step-02:
Find all the edges that connect the tree to new vertices.
Find the least weight edge among those edges and include it in the existing
tree.
If including that edge creates a cycle, then reject that edge and look for the
next least weight edge.
Step-03:
Keep repeating step-02 until all the vertices are included and Minimum
Spanning Tree (MST) is obtained.
Prim’s Algorithm Time Complexity-
Worst case time complexity of Prim’s Algorithm is-
O(ElogV) using binary heap
O(E + VlogV) using Fibonacci heap
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)
This time complexity can be improved and reduced to O(E + VlogV) using
Fibonacci heap.
Kruskal’s Algorithm Implementation-
Step-01:
Sort all the edges from low weight to high weight.
Step-02:
Take the edge with the lowest weight and use it to connect
the vertices of graph.
If adding an edge creates a cycle, then reject that edge and go
for the next least weight edge.
Step-03:
Keep adding edges until all the vertices are connected and a
Minimum Spanning Tree (MST) is obtained.
Kruskal’s Algorithm Time Complexity-
Analysis-
•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)
Dynamic Programming
Dynamic Programming is also used in
optimization problems.
Like divide-and-conquer method, Dynamic
Programming solves problems by combining the
solutions of subproblems.
Moreover, Dynamic Programming algorithm
solves each sub-problem just once and then saves
its answer in a table, thereby avoiding the work of
re-computing the answer every time.
Two main properties of a problem suggest that
the given problem can be solved using Dynamic
Programming. These properties are overlapping
sub-problems and optimal substructure.
Overlapping Sub-Problems
Similar to Divide-and-Conquer approach,
Dynamic Programming also combines solutions
to sub-problems.
It is mainly used where the solution of one sub-
problem is needed repeatedly.
The computed solutions are stored in a table,
so that these don’t have to be re-computed.
Hence, this technique is needed where
overlapping sub-problem exists.
For example, Binary Search does not have
overlapping sub-problem. Whereas recursive
program of Fibonacci numbers have many
overlapping sub-problems.
Optimal Sub-Structure
A given problem has Optimal Substructure
Property, if the optimal solution of the given
problem can be obtained using optimal solutions
of its sub-problems.
For example, the Shortest Path problem has the
following optimal substructure property −
If a node x lies in the shortest path from a source
node u to destination node v, then the shortest
path from u to v is the combination of the
shortest path from u to x, and the shortest path
from x to v.
The standard All Pair Shortest Path algorithms
like Floyd-Warshall and Bellman-Ford are typical
examples of Dynamic Programming.
Steps of Dynamic Programming Approach
Dynamic Programming algorithm is designed
using the following four steps −
In Dynamic Programming, initialization of every method done by '0'.So we initialize it by '0'.It will sort
out diagonally.
We have to sort out all the combination but the minimum output combination is taken into
consideration.
Calculation of Product of 2 matrices:
1. m (1,2) = m1 x m2
= 4 x 10 x 10 x 3
= 4 x 10 x 3 = 120
2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12
= 10 x 3 x 12 = 360
3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 = 720
4. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680
We initialize the diagonal element with equal i,j value
with '0'.
After that second diagonal is sorted out and we get
all the values corresponded to it
Now the third diagonal will be solved out in the same
way.
Now product of 3 matrices:
M [1, 3] = M1 M2 M3
There are two cases by which we can solve this
multiplication:
( M1 x M2) + M3,
M1+ (M2x M3)
After solving both cases we choose the case in which
minimum output is there.
M [1, 3] =264
As Comparing both output 264 is minimum in both
cases so we insert 264 in table and ( M1 x M2) + M3 this
combination is chosen for the output making.
M [2, 4] = M2 M3 M4
There are two cases by which we can solve
this multiplication: (M2x M3)+M4, M2+(M3 x
M4)
After solving both cases we choose the case
in which minimum output is there.
M [2, 4] = 1320
As Comparing both output 1320 is
minimum in both cases so we insert 1320 in
table and M2+(M3 x M4) this combination is
chosen for the output making.
M [3, 5] = M3 M4 M5
There are two cases by which we can solve this
multiplication:
( M3 x M4) + M5,
M3+ ( M4xM5)
After solving both cases we choose the case in
which minimum output is there.
M [3, 5] = 1140
As Comparing both output 1140 is minimum in
both cases so we insert 1140 in table and ( M3 x
M4) + M5this combination is chosen for the
output making.
Now Product of 4 matrices:
M [1, 4] = M1 M2 M3 M4
There are three cases by which we can solve this multiplication:
( M1 x M2 x M3) M4
M1 x(M2 x M3 x M4)
(M1 xM2) x ( M3 x M4)
Question: P [7, 1, 5, 4, 2}
Solution: Here, P is the array of a dimension of
matrices.
So here we will have 4 matrices:
A17x1 A21x5 A35x4 A44x2
i.e. First Matrix A1 have dimension 7 x 1
Second Matrix A2 have dimension 1 x 5
Third Matrix A3 have dimension 5 x 4
Fourth Matrix A4 have dimension 4 x 2
Let say, From P = {7, 1, 5, 4, 2} - (Given)
And P is the Position p0 = 7, p1 =1, p2 = 5, p3 = 4,
p4=2. Length of array P = number of elements in P
∴length (p)= 5
From step 3 Follow the steps in Algorithm
in Sequence According to Step 1 of
Algorithm Matrix-Chain-Order
Step 1:
n ← length [p]-1 Where n is the total
number of elements And length [p] = 5 ∴
n=5-1=4n=4
Now we construct two tables m and s.
Table m has dimension [1.....n, 1.......n]
Table s has dimension [1.....n-1, 2.......n]
Now we compare the value for both k=1
and k = 2.
The minimum of two will be placed in m
[i,j] or s [i,j] respectively.
Here, also find the minimum value of m [i,j] for two
values of k = 2 and k =3
But 28 < ∞
So m [i,j] ← q
And q ← 28
m [2, 4] ← 28
and s [2, 4] ← 3
e. It means in s table at s [2,4] insert 3 and at m [2,4]
insert 28.
So no change occurs. So the value of m [1, 4] remains 42. And value of s [1, 4] = 1
Longest Common Subsequence
The longest common subsequence problem is finding the longest sequence
which exists in both the given strings.
Subsequence
Let us consider a sequence S = <s1, s2, s3, s4, …,sn>.
A sequence Z = <z1, z2, z3, z4, …,zm> over S is called a subsequence of S, if
and only if it can be derived from S deletion of some elements.
Common Subsequence
Suppose, X and Y are two sequences over a finite set of elements. We can
say that Z is a common subsequence of X and Y, if Z is a subsequence of
both X and Y.
Longest Common Subsequence
If a set of sequences are given, the longest common subsequence problem
is to find a common subsequence of all the sequences that is of maximal
length.
The longest common subsequence problem is a classic computer science
problem, the basis of data comparison programs such as the diff-utility, and
has applications in bioinformatics. It is also widely used by revision control
systems, such as SVN and Git, for reconciling multiple changes made to a
revision-controlled collection of files.
Naïve Method
Let X be a sequence of length m and Y a sequence of length n.
Check for every subsequence of X whether it is a subsequence
of Y, and return the longest common subsequence found.
There are 2m subsequences of X. Testing sequences whether or not
it is a subsequence of Y takes O(n) time. Thus, the naïve algorithm
would take O(n2m) time.
Dynamic Programming
Let X = < x1, x2, x3,…, xm > and Y = < y1, y2, y3,…, yn > be the
sequences. To compute the length of an element the following
algorithm is used.
In this procedure, table C[m, n] is computed in row major order
and another table B[m,n] is computed to construct optimal
solution.
Algorithm: LCS-Length-Table-Formulation (X,Y)
m := length(X)
n := length(Y)
for i = 1 to m
do
C[i, 0] := 0
for j = 1 to n
do
C[0, j] := 0
for i = 1 to m
do
for j = 1 to n
do
if xi = yj
C[i, j] := C[i - 1, j - 1] + 1
B[i, j] := ‘D’
else if C[i -1, j] ≥ C[i, j -1]
C[i, j] := C[i - 1, j] + 1
B[i, j] := ‘U’
else
C[i, j] := C[i, j - 1]
B[i, j] := ‘L’
return C and B
Algorithm: Print-LCS (B, X, i, j)
if i = 0 and j = 0
return if B[i, j] = ‘D’
Print-LCS(B, X, i-1, j-1)
Print(xi)
else if B[i, j] = ‘U’
Print-LCS(B, X, i-1, j)
else
Print-LCS(B, X, i, j-1)
This algorithm will print the longest
common subsequence of X and Y.
Analysis
To populate the table, the outer for loop iterates m times
and the inner for loop iterates n times.
Hence, the complexity of the algorithm is O(mn),
where m and n are the length of two strings.
Example
In this example, we have two strings X = BACDB and Y =
BDCB to find the longest common subsequence.
Following the algorithm LCS-Length-Table-Formulation (as
stated above), we have calculated table C (shown on the left
hand side) and table B (shown on the right hand side).
In table B, instead of ‘D’, ‘L’ and ‘U’, we are using the diagonal
arrow, left arrow and up arrow, respectively. After generating
table B, the LCS is determined by function LCS-Print. The
result is BCB.
0/1 Knapsack Problem
Knapsack Problem:
Knapsack is basically means bag. A bag of
given capacity.
We want to pack n items in your luggage.
The ith item is worth vi dollars and weight
wi pounds.
Take as valuable a load as possible, but
cannot exceed W pounds.
vi wi W are integers.
W ≤ capacity
Value ← Max
Input:
Knapsack of capacity
List (Array) of weight and their corresponding value.
Output: To maximize profit and minimize weight in capacity.
The knapsack problem where we have to pack the knapsack with
maximum value in such a manner that the total weight of the items
should not be greater than the capacity of the knapsack.
Knapsack problem can be further divided into two
parts:
Finally output is: