Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Greedy Method

Introduction
• A greedy algorithm, as the name
suggests, always makes the choice that seems
to be the best at that moment.
• Among all the algorithmic approaches, the
simplest and straightforward approach is the
Greedy method. This approach is mainly used
to solve optimization problems.
Greedy choice property
• This technique suggests constructing a
solution through a sequence of steps until
complete solution to the problem is reached
1. Satisfy the problem constraint and the
find the possible feasible solution
2. Best solution from the feasible solution
is optimal solution.
3. Many possible solutions and one
optimal solutions
Optimal Substructure
• An optimal solution to the problem contains
an optimal solution to sub problems.
• This property is used to determine the
usefulness of dynamic programming and
greedy algorithms for a problem.
• Most of the problems will have N inputs. And
require us to obtain subsets that satisfies the
constraint.
• Feasible Solution: Any Subset that satisfies
the constraint is called Feasible solution
• Optimal Solution: A feasible solution that
satisfies the objective of the function or
problem is called Optimal Solution.
Applications of Greedy Method
• It is used in finding the shortest path.
• It is used to find the minimum spanning tree
using the prim's algorithm or the Kruskal's
algorithm.
• It is used in a job sequencing with a deadline.
• This algorithm is also used to solve the
fractional knapsack problem.
Control Abstraction for Greedy
Method
Algorithm Greedy (a, n)
{
Solution : = 0;
for i = 0 to n do
{
x: = select(a);
if feasible(solution, x)
{
Solution: = union(solution , x)
}
return solution;
}}
Example
• Suppose there is a problem 'P'. I want to travel from A to B
shown as below:
P:A→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.
• 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.
• 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
Coin change problem
• The famous coin change problem is a classic
example of using greedy algorithms.
• According to the coin change problem, we are
given a set of coins of various denominations.
• Our task is to use these coins to form a sum of
money using the minimum (or optimal)
number of coins.
Algorithm
1. Sort n denomination coins in increasing order
of value.
2. Initialize set of coins as empty. S = {}
3. While amount is not zero:
Ck is largest coin such that amount > Ck
If there is no such coin return “no
viable solution”
Else include the coin in the solution S.
Decrease the remaining amount = amount – Ck
Knapsack Problem
• The fractional knapsack problem is also one of
the techniques which are used to solve the
knapsack problem. In fractional knapsack, the
items are broken in order to maximize the
profit. The problem in which we break the
item is known as a Fractional knapsack
problem.
• There are basically three approaches to solve
the problem:
• 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 total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = 47.25

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
Second Approach
• The second approach is to select the item
based on the minimum weight.
In this case, the total profit would be equal to (5 + 7 + 4 + 10 + 9 + 7 + 3) = 46

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
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
In this case, we first calculate the profit/weight ratio.
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
P:w: 5 3.3 3 1.7 8 3 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.
Job Sequencing with Dead line
• The sequencing of jobs on a single processor with deadline
constraints is called as Job Sequencing with Deadlines.
• 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.
• Job is Ji for ith job
• Dead line for Job Ji is di
• Deadline of ith job Ji is di and the profit received
from this job is pi. Hence, the optimal solution of
this algorithm is
• Initially, these jobs are ordered according to
profit, i.e. p1⩾p2⩾p3⩾...⩾pn
for i = 1 to n do
K=maxdeadline
while k >= 1 do
Job = dead line
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
End if
Set k = k - 1
End while
End for
Example
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 200 180 190 300 120 100


Job Sequencing with Deadlines

Jobs J1 J2 J3 J4 J5 J6

Deadlines 5 3 3 2 4 2

Profits 200 180 190 300 120 100

Jobs J4 J1 J3 J2 J5 J6

Deadlines 2 5 3 3 4 2

Profits 300 200 190 180 120 100

180+300+190+120+200=990
Another Example
Job J1 J2 J3 J4 J5

Deadline 2 1 3 2 1

Profit 60 100 20 40 20

Job J2 J1 J4 J3 J5

Deadline 1 2 2 3 1

Profit 100 60 40 20 20

J2 J1 J3
0 1 2 3

100+60+20=180
Spanning Tree
Tree:
• A tree is a graph with the following properties:
• The graph is connected (can go from
anywhere to anywhere)
• There are no cyclic (Acyclic)
• A Spanning tree is a connected undirected
graph,
G=(V,E)
• A Graph can have many spanning trees
• Example
What is a Minimum 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.
• There are two famous algorithms for finding the
Minimum Spanning Tree:
Kruskal’s Algorithm
Prim’s Algorithm
Properties of Spanning Tree:

• There may be several minimum spanning trees of the same


weight having the minimum number of edges.
• If all the edge weights of a given graph are the same, then
every spanning tree of that graph is minimum.
• If each edge has a distinct weight, then there will be only
one, unique minimum spanning tree.
• A connected graph G can have more than one spanning
trees.
• A disconnected graph can't have to span the tree, or it can't
span all the vertices.
• Spanning Tree doesn't contain cycles.
• Spanning Tree has (n-1) edges where n is the number of
vertices.
Prims algorithm
• Prims algorithm is mainly useful calculate
Minimum cost spanning tree
• A spanning tree is sub graph of given graph
• A spanning tree that produces the minimum
cost than the remaining spanning trees is
called minimum cost spanning tree
• Now we will see prims algorithm
• 1. start with any vertex of given graph
• 2.find the edges that associted with that
vertex
• 3.Add minimum cost edge to spanning tree if
it is not forming the cycle, if it is forming any
cycle then discard that step
• 4. we have to continue same procedure till all
the vertices are covered
• if a graph contain the n vertices
• Spanning tree should satisfy following
properties
• 1. spanning tree should contain all the vertices
that are there in graph
• 2. if a graph contains n vertices the a spanning
tree should contain n vertices and n-1 edges.
• 3. A spanning tree should contain no cycles
Minimum cost spanning tree using
prims algorithm

Cost of MST = 4 + 2 + 1 + 3 = 10 units.


B C
A

D E

1+4+10+3=18

B C A

D E

MST= 4+1+6+3=14 UNITS Cost of MST = 4 + 2 + 1 + 3 = 10 units


1+6+10+3=20

B C

D E
1 2 3 4 5 6 7
1 0 28 0 0 0 10 0
2 28 0 16 0 0 0 14
3 0 16 0 12 0 0 0
4 0 0 12 0 22 0 18
5 0 0 0 22 0 25 24
6 10 0 0 0 25 0 0
7 0 14 0 18 24 0 0
ste Visited Unvisited Minimum cost edge illustration
p vertices vertices

1 -- 1,2,3,4,5,6,7 --

2 1 2,3,4,5,6,7 {1,2}, {1,6}=


Min(28,10)=10

3 1,6 2,3,4,5,7 {6,5}=25

4 1,6,5 2,3,4,7 {5,7}{5,4}=


Min(24,22)=22

5 1,6,5,4 2,3,7 {4,7},{4,3}=


Min(18,12)=12

6 1,6,5,4,3 2,7 {3,2}=16

7 1,6,5,4,3,2 7 {2,7}=14
10+25+22+12+16+14=99

V=7
Spanning tree= E= v-1=7-1=6
Step 1 - Remove all loops and parallel edges

You might also like