Greedy Method
Greedy Method
Unit-2
Greedy Method
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. Let's
understand through some terms.
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.
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.
P:A→B
The problem is that we have to travel this journey from A to B. There are various
solutions to go from A to B. We can go from A to B by walk, car, bike, train, aeroplane,
etc. There is a constraint in the journey that we have to travel this journey within 12 hrs.
If I go by train or aeroplane then only, I can cover this distance within 12 hrs. There are
many solutions to this problem but there are only two solutions that satisfy the
constraint.
If we say that we have to cover the journey at the minimum cost. This means that we
have to travel this distance as minimum as possible, so this problem is known as a
minimization problem. Till now, we have two feasible solutions, i.e., one by train and
another one by air. Since travelling by train will lead to the minimum cost so it is an
optimal solution. An optimal solution is also the feasible solution, but providing the best
result so that solution is the optimal solution with the minimum cost. There would be
only one optimal solution.
The problem that requires either minimum or maximum result then that problem is
known as an optimization problem. Greedy method is one of the strategies used for
solving the optimization problems.
We have to travel from the source to the destination at the minimum cost. Since we
have three feasible solutions having cost paths as 10, 20, and 5. 5 is the minimum cost
path so it is the optimal solution. This is the local optimum, and in this way, we find the
local optimum at each stage in order to calculate the global optimal solution.
This problem can be solved with the help of using two techniques:
● Brute-force approach: The brute-force approach tries all the possible solutions
with all the different fractions but it is a time-consuming approach.
● Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and
accordingly, we will select the item. The item with the highest ratio would be
selected first.
● The first approach is to select the item based on the maximum profit.
● The second approach is to select the item based on the minimum weight.
● The third approach is to calculate the ratio of profit/weight.
Objects : 1 2 3 4 5 6 7
Profit (P) : 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
W (Weight of the knapsack): 15
n (no of items): 7
First approach:
The second approach is to select the item based on the maximum profit.
Object Profit Weight Remaining weight
3 15 5 15 - 5 = 10
2 10 3 10 - 3 = 7
6 9 3 7-3=4
5 8 1 4-1=3
7 7 * ¾ = 5.25 3 3-3=0
The total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = 47.25
Second approach:
The second approach is to select the item based on the minimum weight.
Object Profit Weight Remaining weight
1 5 1 15 - 1 = 14
5 7 1 14 - 1 = 13
7 4 2 13 - 2 = 11
2 10 3 11 - 3 = 8
6 9 3 8-3=5
4 7 4 5-4=1
3 15 * 1/5 = 3 1 1-1=0
Third approach:
In the third approach, we will calculate the ratio of profit/weight.
Objects: 1 2 3 4 5 6 7
Profit (P): 5 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
Object 1: 5/1 = 5
Object 2: 10/3 = 3. 33
Object 3: 15/5 = 3
Object 4: 7/4 = 1.7
Object 5: 8/1 = 8
Object 6: 9/3 = 3
Object 7: 4/2 = 2
In this approach, we will select the objects based on the maximum profit/weight ratio.
Since the P/W of object 5 is maximum so we select object 5.
5 8 1 15 - 8 = 7
After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select object
1 shown in the below table:
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select
object 2 having profit/weight ratio as 3.3.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select object
3 having profit/weight ratio as 3.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object
6 having profit/weight ratio as 3.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object
7 having profit/weight ratio as 2.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
7 4 2 2-2=0
As we can observe in the above table that the remaining weight is zero which means
that the knapsack is full. We cannot add more objects in the knapsack. Therefore, the
total profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51.
In the first approach, the maximum profit is 47.25. The maximum profit in the second
approach is 46. The maximum profit in the third approach is 51. Therefore, we can say
that the third approach, i.e., maximum profit/weight ratio is the best approach among all
the approaches
p1⩾p2⩾p3⩾...⩾pn
Analysis
In this algorithm, we are using two loops, one is within another. Hence, the complexity of
this algorithm is O(n2).
Example
Let us consider a set of given jobs as shown in the following table. We have to find a
sequence of jobs, which will be completed within their deadlines and will give maximum
profit. Each job is associated with a deadline and profit.
Job J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
To solve this problem, the given jobs are sorted according to their profit in a descending
order. Hence, after sorting, the jobs are ordered as shown in the following table.
Job J2 J1 J4 J3 J5
Deadline 1 2 2 3 1
Profit 100 60 40 20 20
From this set of jobs, first we select J2, as it can be completed within its deadline and
contributes maximum profit.
● Next, J1 is selected as it gives more profit compared to J4.
● In the next clock, J4 cannot be selected as its deadline is over, hence J3 is
selected as it executes within its deadline.
● The job J5 is discarded as it cannot be executed within its deadline.
Thus, the solution is the sequence of jobs (J2, J1, J3), which are being executed within
their deadline and gives maximum profit.
Total profit of this sequence is 100 + 60 + 20 = 180.
Spanning Tree
A spanning tree is a subset of an undirected Graph that has all the vertices connected
by minimum number of edges.
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.
Example
In the following graph, the highlighted edges form a spanning tree.
● If we remove any edge from the spanning tree, then it becomes disconnected.
Therefore, we cannot remove any edge from the spanning tree.
● If we add an edge to the spanning tree then it creates a loop. Therefore, we
cannot add any edge to the spanning tree.
● In a graph, each edge has a distinct weight, then there exists only a single and
unique minimum spanning tree. If the edge weight is not distinct, then there can
be more than one minimum spanning tree.
1. Kruskal's Algorithm
2. Prim's Algorithm
As we have discussed, one graph may have more than one spanning tree. If there are n
number of vertices, the spanning tree should have n - 1 number of edges. In this context,
if each edge of the graph is associated with a weight and there exists more than one
spanning tree, we need to find the minimum spanning tree of the graph.
Kruskal's Algorithm:
For Example: Find the Minimum Spanning Tree of the following graph using Kruskal's
algorithm.
First we initialize the set A to the empty set and create |v| trees, one containing each
vertex with MAKE-SET procedure. Then sort the edges in E into order by non-decreasing
weight.
Now, check for each edge (u, v) whether the endpoints u and v belong to the same tree.
If they do then the edge (u, v) cannot be supplementary. Otherwise, the two vertices
belong to different trees, and the edge (u, v) is added to A, and the vertices in two trees
are merged in by union procedure.
Step 3: then (a, b) and (i, g) edges are considered, and the forest becomes
Step 4: Now, edge (h, i). Both h and i vertices are in the same set. Thus it creates a
cycle. So this edge is discarded.
Then edge (c, d), (b, c), (a, h), (d, e), (e, f) are considered, and the forest becomes.
Step 5: In (e, f) edge both endpoints e and f exist in the same tree so discarded this
edge. Then (b, h) edge, it also creates a cycle.
Step 6: After that edge (d, f) and the final spanning tree is shown as in dark lines.
Step 7: This step will be required Minimum Spanning Tree because it contains all the 9
vertices and (9 - 1) = 8 edges
e → f, b → h, d → f [cycle will be formed]
Analysis:
Where E is the number of edges in the graph and V is the number of vertices, Kruskal's
Algorithm can be shown to run in O (E log E) time, or simply, O (E log V) time, all with
simple data structures. These running times are equivalent because:
Prim's Algorithm
Prim’s algorithm is a greedy approach to find the minimum spanning tree. In this
algorithm, to form a MST we can start from an arbitrary vertex.The idea is to maintain
two sets of vertices:
MST-PRIM (G, w, r)
1. for each u ∈ V [G]
2. do key [u] ← ∞
3. π [u] ← NIL
4. key [r] ← 0
5. Q ← V [G]
6. While Q ? ∅
7. do u ← EXTRACT - MIN (Q)
8. for each v ∈ Adj [u]
9. do if v ∈ Q and w (u, v) < key [v]
10. then π [v] ← u
11. key [v] ← w (u, v)
Example: Generate minimum cost spanning tree for the following graph using Prim's
algorithm.
Solution: In Prim's algorithm, first we initialize the priority Queue Q. to contain all the
vertices and the key of each vertex to ∞ except for the root, whose key is set to 0.
Suppose 0 vertex is the root, i.e., r. By EXTRACT - MIN (Q) procure, now u = r and Adj [u]
= {5, 1}.
Removing u from set Q and adds it to set V - Q of vertices in the tree. Now, update the
key and π fields of every vertex v adjacent to u but not in a tree.
1. π[3]= 4 π[6]= 4
Now in Q, key [2] = 12, key [6] = 18, key [1] = 28 and parent of 2 and 6 is 3.
1. π [2] = 3 π[6]=3
1. u = EXTRACT_MIN (2, 6)
2. u=2 [key [2] < key [6]]
3. 12 < 18
4. Now the root is 2
5. Adj [2] = {3, 1}
6. 3 is already in a heap
7. Taking 1, key [1] = 28
8. w (2,1) = 16
9. w (2,1) < key [1]
1. π[1]= 2
1. Π [6] = 1
Now all the vertices have been spanned, Using above the table we get Minimum
Spanning Tree.
1. 0 → 5 → 4 → 3 → 2 → 1 → 6
2. [Because Π [5] = 0, Π [4] = 5, Π [3] = 4, Π [2] = 3, Π [1] =2, Π [6] =1]
Total Cost = 10 + 25 + 22 + 12 + 16 + 14 = 99
In a shortest- paths problem, we are given a weighted, directed graphs G = (V, E), with
weight function w: E → R mapping edges to real-valued weights. The weight of path p =
(v0,v1,..... vk) is the total of the weights of its constituent edges:
We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if there is a
path from u to v, and δ(u,v)= ∞, otherwise.
The shortest path from vertex s to vertex t is then defined as any path p with weight w
(p) = δ(s,t).
The breadth-first- search algorithm is the shortest path algorithm that works on
unweighted graphs, that is, graphs in which each edge can be considered to have unit
weight.In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we
want to find the shortest path from a given source vertex s ∈ V to every vertex v ∈ V.
Variants:
● Single- destination shortest - paths problem: Find the shortest path to a given
destination vertex t from every vertex v. By shift the direction of each edge in the
graph, we can shorten this problem to a single - source problem.
● Single - pair shortest - path problem: Find the shortest path from u to v for given
vertices u and v. If we determine the single - source problem with source vertex u,
we clarify this problem also. Furthermore, no algorithms for this problem are
known that run asymptotically faster than the best single - source algorithms in
the worst case.
● All - pairs shortest - paths problem: Find the shortest path from u to v for every
pair of vertices u and v. Running a single - source algorithm once from each
vertex can clarify this problem; but it can generally be solved faster, and its
structure is of interest in the own right.
Shortest Path
Given a graph G = (V, E), we maintain for each vertex v ∈ V a predecessor π [v] that is
either another vertex or NIL. During the execution of shortest paths algorithms, however,
the π values need not indicate shortest paths. As in breadth-first search, we shall be
interested in the predecessor subgraph Gn= (Vn,En) induced by the value π. Here again,
we define the vertex set Vπ, to be the set of vertices of G with non - NIL predecessors,
plus the source s:
The directed edge set EΠ is the set of edges induced by the Π values for vertices in VΠ:
EΠ= {(Π[v], v) ∈ E: v ∈ VΠ - {s}}
A shortest - paths tree rooted at s is a directed subgraph G = (V' E'), where V'∈ V
andE'∈E, such that
1. V' is the set of vertices reachable from s in G
2. G' forms a rooted tree with root s, and
3. For all v ∈ V', the unique, simple path from s to v in G' is the shortest path from s
to v in G.
Shortest paths are not naturally unique, and neither is shortest - paths trees.
1. Optimal substructure property: All subpaths of shortest paths are shortest paths.
Let P1 be x - y sub path of shortest s - v path. Let P2 be any x - y path. Then cost of P1≤
cost of P2,otherwise P not shortest s - v path.
2. Triangle inequality: Let d (v, w) be the length of shortest path from v to w. Then,
d (v, w) ≤ d (v, x) + d (x, w)
3. Upper-bound property: We always have d[v] ≥ δ(s, v) for all vertices v ∈ V, and once
d[v] conclude the value δ(s, v), it never changes.
4. No-path property: If there is no path from s to v, then we regularly have d[v] = δ(s, v) =
∞.
5. Convergence property: If s->u->v is a shortest path in G for some u, v ∈ V, and if d[u]
= δ(s, u) at any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times thereafter.
Relaxation
The single - source shortest paths are based on a technique known as relaxation, a
method that repeatedly decreases an upper bound on the actual shortest path weight of
each vertex until the upper bound equivalent the shortest - path weight. For each vertex
v ∈ V, we maintain an attribute d [v], which is an upper bound on the weight of the
shortest path from source s to v. We call d [v] the shortest path estimate.
After initialization, π [v] = NIL for all v ∈ V, d [v] = 0 for v = s, and d [v] = ∞ for v ∈ V - {s}.
The development of relaxing an edge (u, v) consists of testing whether we can improve
the shortest path to v found so far by going through u and if so, updating d [v] and π [v].
A relaxation step may decrease the value of the shortest - path estimate d [v] and
updated v's predecessor field π [v].
Fig: Relaxing an edge (u, v) with weight w (u, v) = 2. The shortest-path estimate of each
vertex appears within the vertex.
(b) Here, v. d < u. d + w (u, v) before relaxing the edge, and so the relaxation step
leaves v. d unchanged.
RELAX (u, v, w)
1. If d [v] > d [u] + w (u, v)
2. then d [v] ← d [u] + w (u, v)
3. π [v] ← u
Dijkstra Algorithm
Let's understand the working of Dijkstra's algorithm. Consider the below graph.
First, we have to consider any vertex as a source vertex. Suppose we consider vertex 0
as a source vertex.
Here we assume that 0 as a source vertex, and distance to all the other vertices is
infinity. Initially, we do not know the distances. First, we will find out the vertices which
are directly connected to the vertex 0. As we can observe in the above graph that two
vertices are directly connected to vertex 0.63.5K
Let's assume that the vertex 0 is represented by 'x' and the vertex 1 is represented by 'y'.
The distance between the vertices can be calculated by using the below formula:
Therefore, we come to the conclusion that the formula for calculating the distance
between the vertices:
Therefore, the value of d(y) is 8. We replace the infinity value of vertices 1 and 4 with the
values 4 and 8 respectively. Now, we have found the shortest path from the vertex 0 to 1
and 0 to 4. Therefore, vertex 0 is selected. Now, we will compare all the vertices except
the vertex 0. Since vertex 1 has the lowest value, i.e., 4; therefore, vertex 1 is selected.
Since vertex 1 is selected, so we consider the path from 1 to 2, and 1 to 4. We will not
consider the path from 1 to 0 as the vertex 0 is already selected.
First, we calculate the distance between the vertex 1 and 2. Consider the vertex 1 as 'x',
and the vertex 2 as 'y'.
Now, we calculate the distance between the vertex 1 and vertex 4. Consider the vertex 1
as 'x' and the vertex 4 as 'y'.
= 15 < ∞
Since 15 is less than the infinity so we update d(8) from infinity to 15.
Now, we consider the vertex 5. First, we calculate the distance between the vertex 4 and
5. Consider the vertex 4 as 'x', and the vertex 5 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (8 + 1) < ∞
=9<∞
Since 5 is less than the infinity, we update d(5) from infinity to 9.
Till now, three nodes have been selected, i.e., 0, 1, and 4. Now we have to compare the
nodes except the nodes 0, 1 and 4. The node 5 has the minimum value, i.e., 9. Therefore,
vertex 5 is selected.
Since the vertex 5 is selected, so we will consider all the direct paths from vertex 5. The
direct paths from vertex 5 are 5 to 8, and 5 to 6.
First, we consider the vertex 8. First, we calculate the distance between the vertex 5 and
8. Consider the vertex 5 as 'x', and the vertex 8 as 'y'.
Since 15 is not less than 12, we will not update d(2) from 12 to 15
Now we consider the vertex 3. Consider the vertex 6 as 'x', and the vertex 3 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (11 + 14) < ∞
= 25 < ∞
Since 25 is less than ∞, so we will update d(3) from ∞ to 25.
Now we consider the vertex 7. Consider the vertex 6 as 'x', and the vertex 7 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (11 + 10) < ∞
= 22 < ∞
Till now, nodes 0, 1, 4, 5, and 6 have been selected. Now we have to compare all the
unvisited nodes, i.e., 2, 3, 7, and 8. Since node 2 has the minimum value, i.e., 12 among
all the other unvisited nodes. Therefore, node 2 is selected.
Since node 2 is selected, so we consider all the direct paths from node 2. The direct
paths from node 2 are 2 to 8, 2 to 6, and 2 to 3.
First, we consider the vertex 8. Consider the vertex 2 as 'x' and 8 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (12 + 2) < 15
= 14 < 15
Since 14 is less than 15, we will update d(8) from 15 to 14.
Now, we consider the vertex 6. Consider the vertex 2 as 'x' and 6 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (12 + 4) < 11
= 16 < 11
Since 16 is not less than 11 so we will not update d(6) from 11 to 16.
Now, we consider the vertex 3. Consider the vertex 2 as 'x' and 3 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (12 + 7) < 25
= 19 < 25
Since 19 is less than 25, we will update d(3) from 25 to 19.
Till now, nodes 0, 1, 2, 4, 5, and 6 have been selected. We compare all the unvisited
nodes, i.e., 3, 7, and 8. Among nodes 3, 7, and 8, node 8 has the minimum value. The
nodes which are directly connected to node 8 are 2, 4, and 5. Since all the directly
connected nodes are selected so we will not consider any node for the updation.
The unvisited nodes are 3 and 7. Among the nodes 3 and 7, node 3 has the minimum
value, i.e., 19. Therefore, the node 3 is selected. The nodes which are directly connected
to the node 3 are 2, 6, and 7. Since the nodes 2 and 6 have been selected so we will
consider these two nodes.Now, we consider the vertex 7. Consider the vertex 3 as 'x'
and 7 as 'y'.
d(x, y) = d(x) + c(x, y) < d(y)
= (19 + 9) < 21
= 28 < 21
Since 28 is not less than 21, so we will not update d(7) from 28 to 21.