Professional Documents
Culture Documents
Unit III
Unit III
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
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
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
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?
D E
1+4+10+3=18
B C A
D E
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 --
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