0% found this document useful (0 votes)
2 views44 pages

Module 2 Greedy Method

The document explains the greedy algorithm, which makes locally optimal choices at each stage to find a global optimum, and discusses its advantages and drawbacks. It covers specific applications such as the Knapsack Problem, Job Sequencing with Deadlines, and Minimum Spanning Trees using algorithms like Prim's and Kruskal's. Additionally, it provides examples and pseudo code for implementing these algorithms.

Uploaded by

blameunknown89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

Module 2 Greedy Method

The document explains the greedy algorithm, which makes locally optimal choices at each stage to find a global optimum, and discusses its advantages and drawbacks. It covers specific applications such as the Knapsack Problem, Job Sequencing with Deadlines, and Minimum Spanning Trees using algorithms like Prim's and Kruskal's. Additionally, it provides examples and pseudo code for implementing these algorithms.

Uploaded by

blameunknown89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Greedy

Method
General Method
A greedy algorithm, as the name suggests, always makes the
choice that seems to be the best at that moment.

A greedy algorithm is an algorithm that follows the problem-


solving heuristics of making the locally optimal choice at each
stage with the hope of finding a global optimum.

Choose the best choice that ‘reachable’ at current state


Greedy is easy to be
implemented.
Just search the best
Advantag choice from the current
e of state that ‘reachable’
Greedy (has any paths or any
connections).
In simple case, greedy
often give you the
solution.
Drawbac • In large and complex case,
greedy doesn’t always give you

k of the best solution, because it’s


just search and take the best
choice that you can reach from
Greedy the current state.
Huffman coding
Prim's Minimal
Spanning Tree
Kruskal's
Algorithm Minimal
Examples Spanning Tree
One-to-all
Algorithm. shortest
paths
Fractional knapsack
Job Scheduling Problem
Knapsack Problem
• Given n objects each have a weight and a profit and given a knapsack of total capacity .
The problem is to pack the knapsack with these objects in order to maximize the total
value of those objects packed without exceeding the knapsack’s capacity.
• More formally, let denote the fraction of the object to be included in the knapsack,
for .
• The problem is to find values for the such that,
Solution steps
Find Find the value/weight ratio for each given item.

Sort Sort the items based on the ratio of value/weight.

Start Then start picking up the item with the highest ratio until that item is completely taken up. After
that we will move to the next highest.
Algorithm
1. Calculate the ratio for all the n objects.
2. Sort the n objects from large to small based on the ratios
3. Initialize share array
4. Set weight = and i=1
5. While (i ≤ n and weight < )
a. If weight + ≤ then
b. else
c. weight = weight +
d. i++
6. Display share array.
Example
Question
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Capacity = m = 60

Solution
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Ratio 7 10 6 5
Example
Product B A C D
Profit 100 280 120 120
Weight 10 40 20 24
Ratio 10 7 6 5
Remaining Capacity 60-10=50 50-40=10 10-10=0 -
Share 1 1 -
Max Profit 0+100=100 100+280=380 380+120*0.5=440 440+0=440
Status Selected Selected Selected Rejected
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24

𝑥 [ 1 … 4 ] =[ 1 ,1 , 0.5 , 0 ] 𝑎𝑛𝑑 𝑃𝑟𝑜𝑓𝑖𝑡=440


Ratio 7 10 6 5
Example
Product A B C D E

𝑥 [ 1 … 5 ] =[ 1 , 0 ,1 , 0 , 0.875 ] 𝑎𝑛𝑑 𝑃𝑟𝑜𝑓𝑖𝑡=270


Weight
Profit
5
30
10
20
20
100
30
90
40
160
Capacity = m = 60
Product A C E D B
Weight 5 20 40 30 10
Profit 30 100 160 90 20
Ratio 6 5 4 3 2
Remaining Capacity 60-5=55 55-20=35 35-35=0 - -
Share 1 1 - -
Max Profit 0+30=30 30+100=130 130+160*0.875=270 270+0=270 270+0=270
Status Selected Selected Selected Rejected Rejected
Example
Product A B C D

𝑥 [ 1 … 4 ] =[ 1 ,1 , 1 ,0 ] 𝑎𝑛𝑑 𝑃𝑟𝑜𝑓𝑖𝑡=280
Weight 10 20 30 30
Profit 60 100 120 90
Capacity = m = 60
Product A B C D
Weight 10 20 30 30
Profit 60 100 120 90
Ratio 6 5 4 3
Remaining Capacity 60-10=50 50-20=30 30-30=0 -
Share 1 1 -
Max Profit 0+60=60 60+100=160 160+120=280 280+0=280
Status Selected Selected Selected Rejected
Time Complexity
Time Complexity =
Time complexity of the sorting + Time complexity of the loop to maximize profit
Example - Question
A thief enters a house for robbing it. He can carry a maximal weight of
60 kg into his bag. There are 5 items in the house with the following
weights and values. What items should thief take if he can even take the
fraction of any item with him?
Product A B C D E
Weight 5 10 15 22 25
Profit 30 40 45 77 90
Capacity = m = 60
Example - Solution
Product A B E D C
Weight 5 10 25 22 15
Profit 30 40 90 77 45
Ratio 6 4 3.6 3.5 3
Remaining Capacity 60-5=55 55-10=45 45-25=20 20-20=0 -
Share 1 1 1 -
160+77*0.9090
Max Profit 0+30=30 30+40=70 70+90=160 230+0=230
=230
Status Selected Selected Selected Selected Rejected

𝑥 [ 1 … 5 ] =[ 1 ,1 , 0 , 0.9090 , 1 ] 𝑎𝑛𝑑 𝑃𝑟𝑜𝑓𝑖𝑡=230


Example
A 2kg grapes cost Rs.60/-. A 3kg oranges cost Rs.72 /- whereas the cost of 4kg apple is Rs.97/-. If we
purchase a watermelon of weight 1.5kg then cost is Rs.75/-. At the same time mango of 0.5kg cost Rs.200/-.
Calculate the minimum amount require to purchase exactly 10kg fruits.
Fruits Orange Apple Grape Watermelon Mango

Weight 3 4 2 1.5 0.5

Cost 72 97 60 75 200

Ratio 24 24.25 30 50 400

Remaining Capacity 10-3=7 7-4=3 3-2=1 1-1=0 -

Share 1 1 1 -
229+75*0.667=
Min Cost 0+72=72 72+97=169 169+60=229 279.025
279.025
Status Selected Selected Selected Selected Rejected

𝑥 [ 1 … 5 ] =[ 1 ,1 , 1 , 0.667 , 0 ] 𝑎𝑛𝑑 𝐶𝑜𝑠𝑡=279.025


CCE Quiz
-1
Link for the Form:
https://forms.office.com
/r/b28EEaZame
Job Sequencing with Deadlines
• The sequence of jobs on a single processor with deadline constraints is called as
Job Sequencing with Deadlines.
• Here,
• We 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 withing its deadline.
• Only one processor is available for processing all the jobs.
• Processor takes one unit of time to complete a job.
• The Problem states-
“How can the total profit be maximized if only one job can be completed at a time?”
Approach to Solution
• 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.
Greedy Algorithm
• Greedy Algorithm is adopted to determine how the next job is selected for an optimal solution.
• The greedy algorithm described below always gives an optimal solution to the job sequencing
problem-
• Step-01:
• Sort all the given jobs in decreasing order of their profit.
• Step-02:
• Check the value of maximum deadline.
• Draw a Gantt chart where maximum time on Gantt chart is the value of maximum deadline.
• Step-03:
• Pick up the jobs one by one.
• Put the job on Gantt chart as far as possible from 0 ensuring that the job gets completed before its deadline.
Pseudo Code
1. Start 7. Display Total Profit
2. Read the number of jobs with profit & deadline. 8. Stop
3. Arrange all the jobs in decreasing order of profit. Function Check(p)
4. Initialize slot array to 0 value. 9. Start
5. For i=0 to n 10. Initialize ptr to 0
1. For j=d[i] to 1 //d: Deadline array 11. For i=0 to n
1. If check(j) ==1
1. If slot[i] ==p
1. slot[i] = j
1. Then ptr ++
2. break;

6. For i=0 to n 12. If ptr == 0


1. If slot [i] > 0 1. return 1
1. Display Job No, Profit, Deadline & Slot allocation as 2. Else return 0
slot [i]-1 to slot [i]
13. Stop
2. Else Job No, Profit, Deadline & Status as Rejected.
Example
Job Profit Deadline

Question: 1
2
40
15
2
1
3 10 3
4 100 2

Solution: Job Profit Deadline Job Profit Deadline Slot


4 100 2 4 100 2 1-2
1 40 2 1 40 2 0-1
2 15 1 2 15 1 Rejected
3 10 3 3 10 2 2-3

Total Profit = 100+40+10 = 150.


Example
Job Profit Deadline

Question: 1
2
70
55
2
1
3 80 2

Solution: Job Profit Deadline Job Profit Deadline Slot


3 80 2 3 80 2 1-2
1 70 2 1 70 2 0-1
2 55 1 2 55 1 Rejected

Total Profit = 80+70 = 150.


Graph Operations – Spanning
Tree
• A spanning tree of a connected graph G(V,E) is a sub graph that is also
a tree and connects all vertices in V.
• For a disconnected graph the spanning tree would be the spanning tree
of each component respectively.
• There is an interesting set of problems related to finding the minimum
spanning tree.
• There are many algorithms available to solve this problem, for e.g.:
Kruskal’s, Prim’s etc.
Graph Operations – Spanning
Tree
• Note that the concept of minimum spanning tree mostly makes sense
in case of weighted graphs. If the graph is not weighted, we consider
all the weights to be one and any spanning tree becomes the
minimum spanning tree.
Minimum Cost Spanning Tree
• Two Different Approach
• Prims Algorithm
• Works on the neighborhood relationship
• Kruskal’s Algorithm
• Connects smallest edges together
Prims Algorithm
• Create a set mstSet that keeps track of vertices already included in MST.
• Assign a key value to all vertices in the input graph. Initialize all key values as
INFINITE. Assign key value as 0 for the first vertex so that it is picked first.
• While mstSet doesn’t include all vertices
• Pick a vertex u which is not there in mstSet and has minimum key value.
• Include u to mstSet.
• Update key value of all adjacent vertices of u. To update the key values, iterate
through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v
is less than the previous key value of v, update the key value as weight of u-v
Example

3
Start b d
2 3
2
a f
3
c e

Min-cost=13
Exercise

7
Start 1 2 3
4 9
2
4
0 8 4
8
7 1
6 2
5

Min-cost=37
Kruskal’s Algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else, discard
it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Example

3
b d
2 3
2
a e
3
c e

Min-cost=13
Exercise

7
1 2 3
4 9
2
4
0 8 4
8
7 1
6 2
5

Min-cost=37
Greedy Method
Single Source Shortest Path
• Problem Statement:
Let G is the graph, and we want to find out the shortest path from source
vertex “s” to all other vertices. It can be directed or undirected graph.
Pseudocode
function dijekstras (source,cost,dist,n)
{
// Source is source vertex, cost is cost matrix of given graph G, dist is array to store shortest path from source to vertex, n is number of vertices in graph
for i=1 to n do
S[i] = false //to check if vertex i is visited or not
dist [i] = cost [ v, i ] //initial distance between source vertex v and vertex I from cost matrix
for num =2 to n do
choose a vertex u such that it has minimum path among all vertices which is not visited
S[u]= true
for w = 1 to n do
if dist [w] > dist [u] + cost [u] [w] then if dist [w] = dist [u] + cost [u] [w]
}
Example
1500 5 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
800 1200
2 3 4 250
300 ∞ ∞ ∞ ∞ ∞ ∞ ∞

1000 800 ∞ ∞ ∞ ∞ ∞ ∞
1000
300 1000 6 ∞ ∞ 1200 ∞ ∞ ∞ ∞ ∞
1400
900 ∞ ∞ ∞ 1500 250 ∞ ∞ ∞

1 1700 8 1000
7 ∞ ∞ ∞ ∞ ∞ ∞ 900 1400

∞ ∞ ∞ ∞ ∞ ∞ ∞ 1000

1700 ∞ ∞ ∞ ∞ ∞ ∞ ∞
Solution
Vertex
Iteration S 1 2 3 4 5 6 7 8
Selected
Initial {} ∞ ∞ ∞ 1500 0 250 ∞ ∞ 5

1 {5} ∞ ∞ ∞ 1500 0 250 ∞ ∞ 6

2 {5, 6} ∞ ∞ ∞ 1250 0 250 1150 1650 7

3 {5, 6, 7} ∞ ∞ ∞ 1250 0 250 1150 1650 4

4 {5, 6, 7, 4} ∞ ∞ 2450 1250 0 250 1150 1650 8

5 {5, 6, 7, 4, 8} 3350 ∞ 2450 1250 0 250 1150 1650 3

6 {5, 6, 7, 4, 8, 3} 3350 3250 2450 1250 0 250 1150 1650 2

7 {5, 6, 7, 4, 8, 3, 2} 3350 3250 2450 1250 0 250 1150 1650 1

8 {5, 6, 7, 4, 8, 3, 2, 1} 3350 3250 2450 1250 0 250 1150 1650 -


Example
0 6 ∞ ∞ ∞ ∞ ∞ 8 ∞

6 0 9 ∞ ∞ ∞ ∞ 18 ∞

∞ 9 0 11 ∞ 1 ∞ ∞ 2

∞ ∞ 11 0 14 10 ∞ ∞ ∞

∞ ∞ ∞ 14 0 12 ∞ ∞ ∞

∞ ∞ 1 10 12 0 9 ∞ ∞

∞ ∞ ∞ ∞ ∞ 9 0 4 7

8 18 ∞ ∞ ∞ ∞ 4 0 5

∞ ∞ 2 ∞ ∞ ∞ 7 5 0
Distance
Iteration S Vertex
Selected
1 2 3 4 5 6 7 8 9

Initial {} - ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

Solutio
1 {1} 2 0 6 ∞ ∞ ∞ ∞ ∞ 8 ∞

2 {1,2} 8 0 6 15 ∞ ∞ ∞ ∞ 8 ∞

n
3 {1,2,8} 7 0 6 15 ∞ ∞ ∞ 12 8 13

4 {1,2,8,7} 9 0 6 15 ∞ ∞ 2 12 8 13
1

5 {1,2,8,7,9} 3 0 6 15 ∞ ∞ 2 12 8 13
1

6 {1,2,8,7,9,3} 6 0 6 15 26 ∞ 1 12 8 13
6

7 {1,2,8,7,9,3,6} 4 0 6 15 26 28 1 12 8 13
6

8 {1,2,8,7,9,3,6,4} 5 0 6 15 26 28 1 12 8 13
6

9 {1,2,8,7,9,3,6,4,5} - 0 6 15 26 28 1 12 8 13
Example
Today, we will learn a very common problem which can be solved using the greedy algorithm. If you
are not very familiar with a greedy algorithm, here is the gist: At every step of the algorithm, you take
the best available option and hope that everything turns optimal at the end which usually does. The
problem at hand is coin change problem, which goes like given coins of denominations 1,5,10,25,100;
find out a way to give a customer an amount with the fewest number of coins. For example, if I ask
you
Cointo returnproblem
change me change for 30.
: 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:
3.1 Ck is largest coin such that amount > Ck
3.1.1 If there is no such coin return “no viable
solution”
3.1.2 Else include the coin in the solution S.
3.1.3 Decrease the remaining amount = amount –
Ck

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy