Module 3 - DAA Vtu
Module 3 - DAA Vtu
Greedy technique
Define greedy technique
• The greedy method is one of the strategies like Divide and conquer
used to solve the problems. This method is used for solving
optimization problems. An optimization problem is a problem that
demands either maximum or minimum results.
• The Greedy method is the simplest and straightforward approach. It is
not an algorithm, but it is a technique. The main function of this
approach is that the decision is taken on the basis of the currently
available information. Whatever the current information is present, the
decision is made without worrying about the effect of the current
decision in future.
continued
• This technique is basically used to determine the feasible solution that
may or may not be optimal. The feasible solution is a subset that
satisfies the given criteria. The optimal solution is the solution which
is the best and the most favorable solution in the subset. In the case of
feasible, if more than one solution satisfies the given criteria then
those solutions will be considered as the feasible, whereas the optimal
solution is the best solution among all the solutions.
Applications of Greedy Algorithm
For j ← 1 to n do
X[j]← 0
profit ← 0 // Total profit of item filled in the knapsack
weight ← 0 // Total weight of items packed in knapsacks
j←1
While (Weight < M) // M is the knapsack capacity
Fractional Knapsack Problem Using Greedy Method-
Fractional knapsack problem is solved using greedy method in the following steps-
Step-01:
For each item, compute its value / weight ratio.
Step-02:
Arrange all the items in decreasing order of their value / weight ratio
Step-03:
Start putting the items into the knapsack beginning from the item with the highest ratio.
Optimal solution using this method is (x1, x2, x3,x4,x5,x6,x7) = (1, 1,0.8,0,1,1,1) with profit = 54
Optimal solution is not guaranteed using method 1 and 2
Solution Vector = (1, 2/3,1,0, 1,1,1)= (1, 0.67,1,0, 1,1,1)
Optimal solution is (x1, x2, x3,x4,x5,x6,x7) = (1, 0.67,1,0, 1,1,1) with profit
[1*10+0.67*5+1*15+0*7+1*6+1*18+1*3]= 55.34 Weight=[1*2+0.67*3+1*5+0*7+1*1+1*4+1*1]=15
This greedy approach always results optimal solution
Problem Statement
In job sequencing problem, the objective is to find a sequence of jobs,
which is completed within their deadlines and gives maximum profit.
Solution
Let us consider, a set of n given jobs which are associated with deadlines and profit is
earned, if a job is completed by its deadline. These jobs need to be ordered in such a way
that there is maximum profit.
It may happen that all of the given jobs may not be completed within their deadlines.
Assume, deadline of ith job Ji is di and the profit received from this job is pi. Hence, the
optimal solution of this algorithm is a feasible solution with maximum profit.
Thus, D(i)>0
for 1⩽i⩽n
Initially, these jobs are ordered according to profit, i.e. p1⩾p2⩾p3⩾...⩾pn
Algorithm: Job-Sequencing-With-Deadline
(D, J, n, k)
D(0) := J(0) := 0
k := 1
J(1) := 1 // means first job is selected
for i = 2 … n do
r := k
while D(J(r)) > D(i) and D(J(r)) ≠ r do
r := r – 1
if D(J(r)) ≤ D(i) and D(i) > r then
for l = k … r + 1 by -1 do
J(l + 1) := J(l)
J(r + 1) := i
k := k + 1
Analysis
In this algorithm, we are using two loops, one is within another. Hence, the
complexity of this algorithm is O(n2)
.
Examples:
Input:
Total profit – 20 + 25 + 35 + 30 = 110
We are given the jobs, their deadlines and associated profits as shown-
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
Profits 201 181 191 301 121 101
Step-01:
Firstly, we need to sort all the given jobs in decreasing order of their profit as follows.
Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
Profits 300 200 190 180 120 100
Step-02:
For each step, we calculate the value of the maximum deadline.
Here, the value of the maximum deadline is 5.
So, we draw a Gantt chart as follows and assign it with a maximum time on the Gantt chart with 5 units as
shown below.
Now,
We will be considering each job one by one in the same order as they appear in the Step-
01.
We are then supposed to place the jobs on the Gantt chart as far as possible from 0.
Step-03:
We now consider job4.
Since the deadline for job4 is 2, we will be placing it in the first empty cell before deadline 2
as follows.
Step-04:
Now, we go with job1.
Since the deadline for job1 is 5, we will be placing it in the first empty cell before deadline 5 as
shown below.
Step-05:
We now consider job3.
Since the deadline for job3 is 3, we will be placing it in the first empty cell before
deadline 3 as shown in the following figure.
Step-07:
Now, we consider job5.
Since the deadline for job5 is 4, we will be placing it in the first empty cell before
deadline 4 as shown in the following figure.
Now,
We can observe that the only job left is job6 whose deadline is 2.
Since all the slots before deadline 2 are already occupied, job6 cannot be completed.
Now, the questions given above can be answered as follows:
Part-01:
The optimal schedule is-
In order to obtain the maximum profit this is the required order in which the jobs must be
completed.
Part-02:
As we can observe, all jobs are not completed on the optimal schedule.
This is because job6 was not completed within the given deadline.
Part-03:
Maximum earned profit = Sum of the profit of all the jobs from the optimal
schedule
= Profit of job2 + Profit of job4 + Profit of job3 + Profit of job5 + Profit of job1
= 995 units
Minimum Spanning Tree
What is a Spanning Tree?
• The cost of the spanning tree is the sum of the weights of all the edges in the tree. There
can be many spanning trees. Minimum spanning tree is the spanning tree where the cost is
minimum among all the spanning trees. There also can be many minimum spanning trees.
• Minimum spanning tree has direct application in the design of networks. It is used in
algorithms approximating the travelling salesman problem, multi-terminal minimum cut
problem and minimum-cost weighted perfect matching. Other practical applications are:
Cluster Analysis
Handwriting recognition
Image segmentation
Contains all the original graph’s vertices.
Reaches out to (spans) all vertices.
Is acyclic. In other words, the graph doesn’t have any nodes which loop back to itself.
Adjacency Matrix
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from
vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge BD
has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In this case, the
edges DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E and A. So, select the
edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it
would create a cycle to the graph. So, choose the edge CA and add it to the
MST.
So, the graph produced in step 5 is the minimum spanning tree of the
given graph. The cost of the MST is given below -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 1 - First, add the edge AB with weight 1 to the MST.
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating
the cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not
forming the cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will
create the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the
cycle, so discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create
the cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph
by using Kruskal's algorithm is -
Now, the number of edges in the above tree equals the number of vertices minus
1. So, the algorithm stops here.
Algorithm
Step 1: Create a forest F in such a way that every vertex of the
graph is a separate tree.
Step 2: Create a set E that contains all the edges of the graph.
Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not
spanning
Step 4: Remove an edge from E with minimum weight
Step 5: IF the edge obtained in Step 4 connects two different trees,
then add it to the forest F
(for combining two trees into one tree).
ELSE
Discard the edge
Step 6: END
Complexity of Kruskal's algorithm
Now, let's see the time complexity of Kruskal's algorithm.
Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V
logV), where E is the no. of edges, and V is the no. of vertices.
Shortest paths – Dijkstra’s algorithm
• The Dijkstra’s algorithm finds the shortest path from a given vertex to
all the remaining vertices in a diagraph.
• We have to find out the shortest path from a given source vertex ‘S’ to
each of the destinations (other vertices ) in the graph.
Dijkstra’s( s)
// Finds shortest path from source vertex to
all other vertices
//Input: Weighted connected graph G=<V,E>
with nonnegative weights and its vertices s
//Output: The length of distance of a shortest
path from s to v
{
1. for i = 1 to n do // Intialize
S[i] = 0;
d[i] = a[s][i];
Dijkstra's Algorithm
Dijkstra’s Algorithm Applications
• It follows the blind search, so wastes a lot of time to give the desired
output.
• It Negative edges value cannot be handled by it.
• We need to keep track of vertices that have been visited.
Optimal Tree : Huffman coding
• Huffman coding provides codes to characters such that the length of the code
depends on the relative frequency or weight of the corresponding character.
Huffman codes are of variable-length, and without any prefix (that means no
code is a prefix of any other). Any prefix-free binary code can be displayed
or visualized as a binary tree with the encoded characters stored at the leaves.
• Huffman tree or Huffman coding tree defines as a full binary tree in which
each leaf of the tree corresponds to a letter in the given alphabet.
• The Huffman tree is treated as the binary tree associated with minimum
external path weight that means, the one associated with the minimum sum
of weighted path lengths for the given set of leaves. So the goal is to
construct a tree with the minimum external path weight
Letter frequency table
Letter z k m c u d l e
Frequency 2 7 24 32 37 42 42 120
Huffman code
Letter Freq Code Bits
e 120 0 1
d 42 101 3
Huffman code
Letter Freq Code Bits
e 120 0 1
d 42 101 3
l 42 110 3
u 37 100 3
c 32 1110 4
m 24 11111 5
k 7 111101 6
z 2 111100 6
The Huffman tree (for the above example) is given below -
END OF MODULE 3