03chapter Three Greedy-Method
03chapter Three Greedy-Method
Greedy–Method
Basic Topics of Chapter-Three
• Kruskal’s algorithm
– Fractional Knapsack
2
Introduction
• Greedy algorithms are simple and the strategies for solving the
problem.
• Just like divide –conquer and other strategies greed algorithm is one of
the approach solving the problem, but the difference is that solutions to
the sub problems do not have to be known at each stage.
• They are short-sighted in their approach
6
…continue
• Select:
7
Greedy Algorithm: Counting Coins
• This problem is to count to a desired value by choosing the least possible
coins and the greedy approach forces the algorithm to pick the largest
possible coin.
E.g. Greedy algorithm to find minimum number of coins:
Q. Given a set of coins and a value we have to find the minimum number of coins
which satisfies the value.
8
Most networking algorithms use the greedy approach.
• Here is a list of few of them −
– Job Scheduling Problem
• There are lots of similar problems that uses the greedy approach to find an optimum
solution. 9
Job sequencing with deadlines
• That is; we have N Jobs, Profits Pi and Deadlines di for each job.
10
Job S. with deadlines: Problem Statement
– 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.
• “How can the total profit be maximized if only one job can be completed
at a time?”
11
Approach to 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.
• A feasible solution would be a subset of jobs where each job of the
subset gets completed within its deadline.
• Value of the feasible solution would be the sum of profit of all the jobs
contained in the subset.
• An optimal solution of the problem would be a feasible solution which
gives the maximum profit. 12
Greedy Approach
• Greedy Algorithm is adopted to determine how the next job is
selected for an optimal solution.
• Feasibility of a Set of Jobs: A set of jobs is feasible if there exists
at least one sequence that allows all the jobs to be executed no
later than their deadlines.
• Construct the schedule step by step.
• Add the job with highest value of Pi among those not considered,
provided that
– The chosen set of jobs remains feasible.
• Assumption every job takes single unit of time 13
Steps to solve Job S. with deadlines
Step1:
Sort all the given jobs in decreasing order of their profit.
Initially, these jobs are ordered according to profit, i.e. p1 ⩾p2 ⩾p3 ⩾... ⩾pn
if two or more jobs having the same profit then sort them as per their entry
in the job.
Step2:
Check the value of maximum deadline.
Draw a Gantt chart where maximum time on Gantt chart is the value of maximum
deadline
Step3: Pick up the jobs one by one.
– Put the job on Gantt chart as far as possible from 0 ensuring that the job gets
14
PRACTICE PROBLEM BASED ON JOB SEQUENCING WITH
DEADLINES-
• Problem-
a. Write the optimal schedule that gives maximum profit.(i.e. find a sequence of jobs, which
• 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.
16
Solution….
• Step-03:
• We take job J4.
• Since its deadline is 2, so we place it in the first empty cell before deadline 2 as-
• Step-04:
• We take job J1.
• Since its deadline is 5, so we place it in the first empty cell before deadline 5 as-
17
Solution….
• Step-06:
• We take job J2.
• Since its deadline is 3, so we place it in the first empty cell before deadline 3.
• Since the second and third cells are already filled, so we place job J2 in the first cell
as-
• Step-07:
• Now, we take job J5.
• Since its deadline is 4, so we place it in the first empty cell before deadline 4 as-
• Now,
• The only job left is job J6 whose deadline is 2.
• All the slots before deadline 2 are already occupied.
• Thus, job J6 can not be completed. 18
Solution….
• Now, the given questions may be answered as-
a
– The optimal schedule is-
– J2 , J4 , J3 , J5 , J1
– This is the required order in which the jobs must be completed in order to
obtain the maximum profit.
b:
– All the jobs are not completed in optimal schedule.
– This is because job J6 could not be completed within its deadline.
c:
– Maximum earned profit = Sum of profit of all the jobs in optimal schedule
= Profit of job J2 + Profit of job J4 + Profit of job J3 + Profit of job J5 +
Profit of job J1
= 180 + 300 + 190 + 120 + 200
= 990 units
19
Job sequencing with deadlines: Exercise #1
• Let us consider a set of given jobs as shown in the following table.
Steps:
1) Sort all jobs in decreasing order of profit.
2) Initialize the result sequence as first job in sorted jobs.
3) Do following for remaining n-1 jobs
If the current job can fit in the current result …….sequence without
missing the deadline,
add …..current job to the result.
Else
ignore the current ……….job
21
Job Sequencing Algorithm
Algorithm greedy job(d,j,n)
{
for i = 1 to n do
Set k = min(dmax, DEADLINE(i)) //where DEADLINE(i) denotes deadline of ith
job
while k >= 1 do
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
endif
Set k = k - 1
end while
End for
• Analysis: In this algorithm, we are using two loops, one is within another. Hence, the
complexity of this algorithm is O(n2)
22
Optimal Merge Pattern
• Merge a set of sorted files of different length into a single sorted file.
• We need to find an optimal solution, where the resultant file will be
generated in minimum time.
• 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.
• As, different pairings require different amounts of time, in this strategy
we want to determine an optimal way of merging many files together.
• At each step, two shortest sequences are merged.
23
Optimal Merge Pattern
• To merge a p-record file and a q-record file requires possibly p +
q record moves, the obvious choice being, merge the two smallest files
together at each step.
• When two or more sorted files are to be merged all together to form a
single file, the minimum computations done to reach this file are known
as Optimal Merge Pattern.
• Two-way merge patterns can be represented by binary merge trees. Let
us consider a set of n sorted files {f1, f2, f3, …, fn}.
• Let us consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30
number of elements respectively.
• If merge operations are performed according to the provided sequence,
then
– M1 = merge f1 and f2 => 20 + 30 = 50
• f4 , f 3 , f 1 , f 2 , f 5
• 15 + 35 + 65 + 95 = 210
29
Spanning Tree
30
Spanning Tree: Example #1
31
Minimum Spanning Trees
• A Minimum Spanning Tree (MST) is a subset of edges of a
connected weighted undirected graph that connects all the vertices
together with the minimum cost for that graph.
• 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.
• 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.
• To derive an MST, Prim’s algorithm or Kruskal’s algorithm can be used.
32
MST : Example #2
• Both work with weighted and unweighted graphs but are more
34
Prim's Algorithm
graph, each time selecting the node whose connecting edge has the
37
Kruskal's Algorithm
• At each step, we add one edge (the cheapest one) so that it joins
• If it were to form a cycle, it would simply link two nodes that were
already part of a single connected tree, so that this edge would not
be needed.
38
Kruskal's Algorithm: The Steps
• The steps are:
40
Solution
41
Solution…
42
Solution…
• 5th-Iteration • 6th-iteration
43
Solution…
• 7th-Iteration • 8th-Iteration
44
Final solution…
45
Knapsack-Problem
The Greedy algorithm could be understood very well with a well-
known problem referred to as Knapsack problem.
The Idea of Knapsack Problem:
– list of items is given, each item has its own value and weight. Items can be
placed in a knapsack whose maximum weight limit is W. The problem is to
find the weight that is less than or equal to W, and value is maximized.
46
Knapsack-Problem Cont’d
• Problem Scenario
• A thief is robbing a store and can carry a maximal weight of W into his
knapsack. There are n items available in the store and weight of ith item
is wi and its profit is pi.
3rd . Find out the ratio of profit by weight and then select the item having
maximum profit by ratio 48
Fractional Knapsack: Example #1
• Let us consider that the capacity of the knapsack W = 60 and the list of provided
items are shown in the following table −
• As the provided items are not sorted based on pi/wi. After sorting,
the items are as shown in the following table.
49
Solution
After sorting all the items according to pi/wi.
First all of B is chosen as weight of B is less than the capacity of
the knapsack.
Next, item A is chosen, as the available capacity of the knapsack is
greater than the weight of A.
Now, C is chosen as the next item.
However, the whole item cannot be chosen as the remaining
capacity of the knapsack is less than the weight of C.
50
Solution…
51
Example #2:Fractional Knapsack
• Number of item n=7, Bag capacity W=15 & the weights and profits
are given below.
Item A B C D E F G
Profit(P) 5 10 15 7 8 9 4
Weight(w) 1 3 5 4 1 3 2
52
Question
53