DAA NOTES Unit 2
DAA NOTES Unit 2
Greedy Method
Greedy Method: The general method – Optimal Storage on Tapes – Knapsack
Problem – Job Sequencing with deadlines – Optimal Merge Patterns – Minimum
Spanning Trees – Single Source Shortest Paths
A greedy algorithm, as the name suggests, always makes the choice that
seems to be the best at that moment. This means that it makes a locally-
optimal choice in the hope that this choice will lead to a globally-optimal
solution.
We can make whatever choice seems best at the moment and then solve
the subproblems that arise later. The choice made by a greedy algorithm may
depend on choices made so far but not on future choices or all the solutions to
the subproblem. It iteratively makes one greedy choice after another, reducing
each given problem into a smaller one. Hence, we can say that Greedy
algorithm is an algorithmic paradigm based on heuristic that follows local
optimal choice at each step with the hope of finding global optimal solution.
• The function Select selects an input from a[] and removes it. The selected
input’s value is assigned to x.
• The function Union combines x with the solution and updates the objective
function.
Knapsack Problem
I2 I3 I1
Step- 03
Start filling the knapsack by putting the items into it one by one.
Knapsack Items in
Cost
Capacity Knapsack
20 0 0
5 I2 24
Now,
• Since in fractional knapsack problem, even the fraction of any item can
be taken.
< I2,(5/10)I3>
= 24+(5/10)15
=31.5 units.
• We get profit for a job only when the particular job is completed
within the deadline.
Analysis
In this algorithm, we are using two loops, one is within another. Hence,
the complexity of this algorithm is O(n2).
Example
n= 4, (p1,p2,p3,p4)=(100,10,15,27), (d1,d2,d3,d4)=(2,1,2,1)
Solution
Different feasible solution with the sequencing of jobs and total profit are given
below:
Feasible solution Processing Sequence Value
1. (1,2) 2,1 110
5. (3,4) 4,3 42
Step - 01
Sort all the given jobs in decreasing order of the profit
Jobs J1 J4 J3 J2
Deadline 2 1 2 1
Profit 100 27 15 10
Step - 02
0 1 2
Gantt Chart
Now,
• We take each job one by one in the order they appear in step-01.
• We place the job on Gantt chart as far as possible from 0.
Step – 03
• we take job J1
• since its deadline is 2, so we place it in the first empty cell before
deadline 2 as
0 1 2
J1
• we take job J4
• since its deadline is 1, so we place it in the first empty cell before
deadline 1 as
0 1 2
J4 J1
If all the vertices are connected in a graph, then there exists at least one
spanning tree. In a graph, there may exist more than one spanning tree.
Properties
Prim’s Algorithm
• Prim’s Algorithm is a famous greedy algorithm.
• It is used for finding the Minimum Spanning Tree (MST) of a given
graph.
• To apply Prim’s algorithm, the given graph must be weighted, connected
and undirected.
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 is included and Minimum
Spanning Tree (MST) is obtained.
Prim’s Algorithm Time Complexity-
Worst case time complexity of Prim’s Algorithm is-
Solution-
The above discussed steps are followed to find the minimum cost spanning tree
using Prim’s Algorithm-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Since all the vertices have been included in the MST, so we stop.
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Kruskal’s Algorithm
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.
Solution-
To construct MST using Kruskal’s Algorithm,
• Simply draw all the vertices on the paper.
• Connect these vertices using edges with minimum weights such that no
cycle gets formed.
Step-01
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Since all the vertices have been connected / included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
If the number of sorted files are given, there are many ways to merge
them into a single sorted file. This merge can be performed pair wise. Hence,
this type of merging is called as 2-way merge patterns.
At the end of this algorithm, the weight of the root node represents the optimal
cost.
Example
Let us consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30
number of elements respectively.
50 + 60 + 65 + 95 = 270
15 + 35 + 65 + 95 = 210
In this context, we are now going to solve the problem using this algorithm.
Initial Set
Step-1
Step-2
Step-3
Step-4
Huffman Codes
• The binary bits code word for a message determine the branching needed
at each level of the decode tree to reach the correct external node.
0 1
M4
M1
0 1
M3
0 1
M1 M2
• We interpret a zero as a left branch and a one as a right branch, then the
decode tree corresponds to codes 000,001,01 and 1 for messages M1, M2,
M3 and M4 respectively. These codes are called Huffman codes.
Example:
Input : n = 3 L[] = { 5, 3, 10 }
Output : Order should be { 3, 5, 10 } with MRT = 29/3
1. Here, n=3 and (L1, L2, L3) = (5, 10, 3). We can store these 3 programs
on the tape in any order but we want that order which will minimize the
MRT.
2. Suppose we store the programs in order (L1, L2, L3).
3. Then MRT is given as (5+(5+10)+(5+10+3))/3=38/3
4. To retrieve L1 we need 5 units of time. Because a tape is a sequential
device we will have to first pass through entire L1 even if we want to
retrieve L2.
5. Hence, retrieval time (RT) is 5 for program 1 and (5+10) for program 2.
6. Similarly, if program 3 is also considered then the total RT becomes 5+
(5+10) + (5+10+3) where (5+10+3) is the RT for program 3.
7. Since we want to find the mean retrieval time we add all the RT and then
divide the sum by n.
8. The aim over here is to find the minimum MRT. To do this we consider
all the possible orderings of these 3 programs. Since there 3 programs we
can have at the most 6(3!) combinations.
9. Consider the below table:
Ordering MRT
L1,L2,L3 5+(5+10)+(5+10+3)/3=38/3
L1,L3,L2 5+(5+3)+(5+10+3)/3=31/3
L2,L1,L3 10+(5+10)+(5+10+3)/3=43/3
Ordering MRT
L2,L3,L1 10+(3+10)+(5+10+3)/3=41/3
L3,L1,L2 3+(5+3)+(5+10+3)/3=29/3
L3,L2,L1 3+(3+10)+(5+10+3)/3=34/3
➢ The length of a path is now defined to be the sum of the weights of the
edges on that path.
➢ The starting vertex of the path is referred to as the source, and the last
vertex the destination.
➢ We are given a directed graph G=(V,E), a weighting function cost for the
edges of G, and a source vertex vo.
Example
Find the shortest distance from source vertex ‘S’ to remaining vertices in the
following graph-
S a b c d e
S 0 1 5
A 0 2 2 1
B 0 2
C 0 3 1
D 0 2
E 0
Solution-
Step-01:
• Unvisited set : {S , a , b , c , d , e}
• Visited set :{}
Step-02:
The two variables Π and d are created for each vertex and initialized as-
• d[S] + 1 = 0 + 1 = 1 < ∞
∴ d[a] = 1 and Π[a] = S
• d[S] + 5 = 0 + 5 = 5 < ∞
∴ d[b] = 5 and Π[b] = S
Now,
• d[a] + 2 = 1 + 2 = 3 < ∞
∴ d[c] = 3 and Π[c] = a
• d[a] + 1 = 1 + 1 = 2 < ∞
∴ d[d] = 2 and Π[d] = a
• d[b] + 2 = 1 + 2 = 3 < 5
∴ d[b] = 3 and Π[b] = a
• Unvisited set : {b , c , d , e}
• Visited set : {S , a}
Step-05:
Vertex ‘d’ is chosen.
• d[d] + 2 = 2 + 2 = 4 < ∞
∴ d[e] = 4 and Π[e] = d
• Unvisited set : {b , c , e}
• Visited set : {S , a , d}
Step-06:
Vertex ‘b’ is chosen.
Now,
• d[b] + 2 = 3 + 2 = 5 > 2
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
• Unvisited set : {c , e}
• Visited set : {S , a , d , b}
Step-07:
• d[c] + 1 = 3 + 1 = 4 = 4
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Now,
S , a , d , b , c , e.