SlideShare a Scribd company logo
1 of 24
Download to read offline
Unit-III
Abstract
Algorithms
Sub-DAA Class-TE Comp
Greedy Method
• Greedy Principal: are
typically used to solve
optimization problem.
• Most of these problems have
n inputs and require us to
obtain a subset that satisfies
some constraints.
• Any subset that satisfies these
constraints is called a feasible
solution.
• We are required to find a
feasible solution that either
minimizes or maximizes a
given objective function
Sub-DAA Class-TE Comp
• Subset Paradigm : Devise an algorithm that works in stages
• Consider the inputs in an order based on some selection procedure
• Use some optimization measure for selection procedure
• At every stage, examine an input to see whether it leads to an
optimal solution
• If the inclusion of input into partial solution yields an infeasible
solution, discard the input; otherwise, add it to the partial solution
Applications of Greedy Method:
 Knapsack Problem
 Minimum Spanning Tree
 Job Sequencing Problem
 Huffman Code Generation
 Activity Selection Problem
 Shortest Path
Sub-DAA Class-TE Comp
Control Abstraction or Algorithm of
Greedy Method
Sub-DAA Class-TE Comp
Difference Between Divide & Conquer and
Greedy Method
Sub-DAA Class-TE Comp
Knapsack Problem
Sub-DAA Class-TE Comp
 The knapsack problem is a problem of optimization: Given a set of
items n, each with a weight w and profit p, determine the number of
each item to include in a knapsack such that the total weight is less
than or equal to a given knapsack limit M and the total Profit is
maximum.
 There are two types of Knapsack Problem.
 The 0-1 Knapsack Problem: same as integer knapsack except that
the values of xi 's are restricted to 0 or 1.
 The Fractional Knapsack Problem : same as integer knapsack
except that the values of xi 's are between 0 and 1.
Knapsack Problem Formula
Sub-DAA Class-TE Comp
Knapsack Problem Algorithm and
Time Complexity
Sub-DAA Class-TE Comp
Algorithm GreedyKnapsack (m, n)
// P[1 : n] and w[1 : n] contain the profits and
weights respectively of
// Objects ordered so that p[i] / w[i] > p[i + 1] /
w[i + 1].
// m is the knapsack size and x[1: n] is the
solution vector.
{
for i := 1 to n do x[i] := 0.0 // initialize x U := m;
for i := 1 to n do
{
if (w(i) > U) then break;
x [i] := 1.0; U := U – w[i];
}
if (i < n) then x[i] := U / w[i];
}
Running time:
The objects are to be sorted
into non-increasing order of
pi / wi ratio.
But if we disregard the time to
initially sort the objects, the
algorithm requires only O(n)
time
Sub-DAA Class-TE Comp
Knapsack(0/1) Problem using Greedy
Method
Ex1) n = 3, M(size) = 20, (p1 , p2 , p3 ) = (25, 24, 15) (w1 , w2 , w3 ) = (18, 15, 10)
Solution:-Step 1)Find out Pi/Wi
Step 2) Arrange Pi/Wi (with P and W) in Decreasing Order.
P W Pi/Wi
25 18 1.38
24 15 1.6
15 10 1.5
P W Pi/Wi
24 15 1.6
15 10 1.5
25 18 1.38
Sub-DAA Class-TE Comp
• Step 3) Insert Weight into bucket upto size M
15(w2)
M=20
5(w3)
15(w2)
Step 4) Solution
W1 W2 W3
0 1 ½ (10/2=5)
Step 5) Calculate Total Profit
=P1*0+ P2*1 +P3* ½
=25*0+24*1+15*1/2
=0+24+7.5
Total Profit= 31.5
Sub-DAA Class-TE Comp
Ex 2) n = 3, M(size) = 20, (p1 , p2 , p3 ) = (30, 21, 18) (w1 , w2 , w3 ) = (18, 15, 10)
Solution: Step-1) Arrange Pi/Wi (with P and W) in Decreasing Order.
W1 W2 W3
1/2 1/15 1
P W Pi/Wi
18 10 1.8
30 18 1.66
21 15 1.4
Step 2) Insert Weight into bucket
upto size M M=20
1
9
10
Step 3) Solution
Step 4) Find the total Profit
Total Profit= 18+21*1/15+15
=18+1.4+15=34.4
Sub-DAA Class-TE Comp
Ex1) n = 5, M(size) = 100,
(p1 , p2 , p3,p4,p5 ) = (10,20,30,40,50)
(w1 , w2 , w3 ,w4 ,w5 ) = (20,30,66,40,60)
Solution:=
Weight=40+60=100
Profit=0+0+0+40+50
=90
Sub-DAA Class-TE Comp
Job sequencing using Greedy Method
The problem is stated as below.
 There are n jobs to be processed on a
machine.
 Each job i has a deadline di≥ 0 and profit
pi≥0 .
 Pi is earned if the job is completed by its
deadline.
 The job is completed if it is processed on a
machine for unit time.
 Only one machine is available for
processing jobs.
 Only one job is processed at a time on the
machine.
 A feasible solution is a subset of jobs J such
that each job is completed by its deadline.
 An optimal solution is a feasible solution
with maximum profit value.
Job Sequencing Algorithm:
Algorithm GreedyJob (d, J, n)
// J is a set of jobs that can be
completed by their deadlines.
{
J := {1};
for i := 2 to n do
{
if (all jobs in J U {i} can be
completed by their dead lines) then J
:= J U {i};
}
}
Sub-DAA Class-TE Comp
Job sequencing using Greedy Method
Formula:-
For slot assign:-
=[ Alpha(a)-1, Alpha(a)]
Optimal Solution:-
i.e ∑pi 1≤ i≤ n
Job sequencing using Greedy Method Example
Ex 1) Solve following using JSD Method
Where n=5
Sub-DAA Class-TE Comp
JOBs J1 J2 J3 J4 J5
Profit 20 15 10 5 1
Deadline 2 2 1 3 3
Job Consider Slot Assign Solution Profit
J1 [2-1,2]=[1,2] J1 20
J2 [1,2] [0,1] J1,J2 20+15
J3 [1,2] [0,1] J1,J2 20+15
J4 [1,2] [0,1]
[2,3]
J1,J2,J4 20+15+5
J5 [1,2] [0,1]
[2,3]
J1,J2,J4 20+15+5=40
Solution:-Step-1) Arrange all Job in Descending Order
Step-2) Job Sequence Path :- 0…J2…..1…J1….2…J4….3
Profit:- 20+15+5=40
Ex2)N=4 (p1,p2,p3,p4)=(100,10,15,27)
(d1,d2,d3,d4)=(2,1,2,1)
Ex3) N=5 (p1,p2,p3,p4,p5)=(25,15,12,5,1)
(d1,d2,d3,d4,d5)=(3,2,1,3,3)
Sub-DAA Class-TE Comp
Minimum Spanning Tree
Sub-DAA Class-TE Comp
Minimum Spanning Trees (MST):
 A spanning tree for a connected graph is a tree whose vertex set is the same
as the vertex set of the given graph, and whose edge set is a subset of the
edge set of the given graph. i.e., any connected graph will have a spanning
tree.
 Weight of a spanning tree w (T) is the sum of weights of all edges in T. The
Minimum spanning tree (MST) is a spanning tree with the smallest possible
weight.
Sub-DAA Class-TE Comp
Prims Algorithm
How Prim's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local
optimum in the hopes of finding a global optimum.
We start from one vertex and keep adding edges with the lowest weight until we
reach our goal.
The steps for implementing Prim's algorithm are as follows:
1.Initialize the minimum spanning tree with a vertex chosen at random.
2.Find all the edges that connect the tree to new vertices, find the minimum and
add it to the tree
3.Keep repeating step 2 until we get a minimum spanning tree
Sub-DAA Class-TE Comp
Kruskal's Algorithm
How Kruskal's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local
optimum in the hopes of finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we
reach our goal.
The steps for implementing Kruskal's algorithm are as follows:
1.Sort all the edges from low weight to high
2.Take the edge with the lowest weight and add it to the spanning tree. If
adding the edge created a cycle, then reject this edge.
3.Keep adding edges until we reach all vertices.
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf

More Related Content

Similar to final-ppts-daa-unit-iii-greedy-method.pdf

Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxMaryJacob24
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxHarshitSingh334328
 
Backtracking & branch and bound
Backtracking & branch and boundBacktracking & branch and bound
Backtracking & branch and boundVipul Chauhan
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhdanielgetachew0922
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy MethodMaryJacob24
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 

Similar to final-ppts-daa-unit-iii-greedy-method.pdf (20)

Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
Backtracking & branch and bound
Backtracking & branch and boundBacktracking & branch and bound
Backtracking & branch and bound
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy Method
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 

Recently uploaded

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 

Recently uploaded (20)

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 

final-ppts-daa-unit-iii-greedy-method.pdf

  • 2. Greedy Method • Greedy Principal: are typically used to solve optimization problem. • Most of these problems have n inputs and require us to obtain a subset that satisfies some constraints. • Any subset that satisfies these constraints is called a feasible solution. • We are required to find a feasible solution that either minimizes or maximizes a given objective function Sub-DAA Class-TE Comp
  • 3. • Subset Paradigm : Devise an algorithm that works in stages • Consider the inputs in an order based on some selection procedure • Use some optimization measure for selection procedure • At every stage, examine an input to see whether it leads to an optimal solution • If the inclusion of input into partial solution yields an infeasible solution, discard the input; otherwise, add it to the partial solution Applications of Greedy Method:  Knapsack Problem  Minimum Spanning Tree  Job Sequencing Problem  Huffman Code Generation  Activity Selection Problem  Shortest Path Sub-DAA Class-TE Comp
  • 4. Control Abstraction or Algorithm of Greedy Method Sub-DAA Class-TE Comp
  • 5. Difference Between Divide & Conquer and Greedy Method Sub-DAA Class-TE Comp
  • 6. Knapsack Problem Sub-DAA Class-TE Comp  The knapsack problem is a problem of optimization: Given a set of items n, each with a weight w and profit p, determine the number of each item to include in a knapsack such that the total weight is less than or equal to a given knapsack limit M and the total Profit is maximum.  There are two types of Knapsack Problem.  The 0-1 Knapsack Problem: same as integer knapsack except that the values of xi 's are restricted to 0 or 1.  The Fractional Knapsack Problem : same as integer knapsack except that the values of xi 's are between 0 and 1.
  • 8. Knapsack Problem Algorithm and Time Complexity Sub-DAA Class-TE Comp Algorithm GreedyKnapsack (m, n) // P[1 : n] and w[1 : n] contain the profits and weights respectively of // Objects ordered so that p[i] / w[i] > p[i + 1] / w[i + 1]. // m is the knapsack size and x[1: n] is the solution vector. { for i := 1 to n do x[i] := 0.0 // initialize x U := m; for i := 1 to n do { if (w(i) > U) then break; x [i] := 1.0; U := U – w[i]; } if (i < n) then x[i] := U / w[i]; } Running time: The objects are to be sorted into non-increasing order of pi / wi ratio. But if we disregard the time to initially sort the objects, the algorithm requires only O(n) time
  • 9. Sub-DAA Class-TE Comp Knapsack(0/1) Problem using Greedy Method Ex1) n = 3, M(size) = 20, (p1 , p2 , p3 ) = (25, 24, 15) (w1 , w2 , w3 ) = (18, 15, 10) Solution:-Step 1)Find out Pi/Wi Step 2) Arrange Pi/Wi (with P and W) in Decreasing Order. P W Pi/Wi 25 18 1.38 24 15 1.6 15 10 1.5 P W Pi/Wi 24 15 1.6 15 10 1.5 25 18 1.38
  • 10. Sub-DAA Class-TE Comp • Step 3) Insert Weight into bucket upto size M 15(w2) M=20 5(w3) 15(w2) Step 4) Solution W1 W2 W3 0 1 ½ (10/2=5) Step 5) Calculate Total Profit =P1*0+ P2*1 +P3* ½ =25*0+24*1+15*1/2 =0+24+7.5 Total Profit= 31.5
  • 11. Sub-DAA Class-TE Comp Ex 2) n = 3, M(size) = 20, (p1 , p2 , p3 ) = (30, 21, 18) (w1 , w2 , w3 ) = (18, 15, 10) Solution: Step-1) Arrange Pi/Wi (with P and W) in Decreasing Order. W1 W2 W3 1/2 1/15 1 P W Pi/Wi 18 10 1.8 30 18 1.66 21 15 1.4 Step 2) Insert Weight into bucket upto size M M=20 1 9 10 Step 3) Solution Step 4) Find the total Profit Total Profit= 18+21*1/15+15 =18+1.4+15=34.4
  • 12. Sub-DAA Class-TE Comp Ex1) n = 5, M(size) = 100, (p1 , p2 , p3,p4,p5 ) = (10,20,30,40,50) (w1 , w2 , w3 ,w4 ,w5 ) = (20,30,66,40,60) Solution:= Weight=40+60=100 Profit=0+0+0+40+50 =90
  • 13. Sub-DAA Class-TE Comp Job sequencing using Greedy Method The problem is stated as below.  There are n jobs to be processed on a machine.  Each job i has a deadline di≥ 0 and profit pi≥0 .  Pi is earned if the job is completed by its deadline.  The job is completed if it is processed on a machine for unit time.  Only one machine is available for processing jobs.  Only one job is processed at a time on the machine.  A feasible solution is a subset of jobs J such that each job is completed by its deadline.  An optimal solution is a feasible solution with maximum profit value. Job Sequencing Algorithm: Algorithm GreedyJob (d, J, n) // J is a set of jobs that can be completed by their deadlines. { J := {1}; for i := 2 to n do { if (all jobs in J U {i} can be completed by their dead lines) then J := J U {i}; } }
  • 14. Sub-DAA Class-TE Comp Job sequencing using Greedy Method Formula:- For slot assign:- =[ Alpha(a)-1, Alpha(a)] Optimal Solution:- i.e ∑pi 1≤ i≤ n
  • 15. Job sequencing using Greedy Method Example Ex 1) Solve following using JSD Method Where n=5 Sub-DAA Class-TE Comp JOBs J1 J2 J3 J4 J5 Profit 20 15 10 5 1 Deadline 2 2 1 3 3 Job Consider Slot Assign Solution Profit J1 [2-1,2]=[1,2] J1 20 J2 [1,2] [0,1] J1,J2 20+15 J3 [1,2] [0,1] J1,J2 20+15 J4 [1,2] [0,1] [2,3] J1,J2,J4 20+15+5 J5 [1,2] [0,1] [2,3] J1,J2,J4 20+15+5=40 Solution:-Step-1) Arrange all Job in Descending Order Step-2) Job Sequence Path :- 0…J2…..1…J1….2…J4….3 Profit:- 20+15+5=40
  • 16. Ex2)N=4 (p1,p2,p3,p4)=(100,10,15,27) (d1,d2,d3,d4)=(2,1,2,1) Ex3) N=5 (p1,p2,p3,p4,p5)=(25,15,12,5,1) (d1,d2,d3,d4,d5)=(3,2,1,3,3) Sub-DAA Class-TE Comp
  • 17. Minimum Spanning Tree Sub-DAA Class-TE Comp Minimum Spanning Trees (MST):  A spanning tree for a connected graph is a tree whose vertex set is the same as the vertex set of the given graph, and whose edge set is a subset of the edge set of the given graph. i.e., any connected graph will have a spanning tree.  Weight of a spanning tree w (T) is the sum of weights of all edges in T. The Minimum spanning tree (MST) is a spanning tree with the smallest possible weight.
  • 18. Sub-DAA Class-TE Comp Prims Algorithm How Prim's algorithm works It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global optimum. We start from one vertex and keep adding edges with the lowest weight until we reach our goal. The steps for implementing Prim's algorithm are as follows: 1.Initialize the minimum spanning tree with a vertex chosen at random. 2.Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree 3.Keep repeating step 2 until we get a minimum spanning tree
  • 19. Sub-DAA Class-TE Comp Kruskal's Algorithm How Kruskal's algorithm works It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global optimum. We start from the edges with the lowest weight and keep adding edges until we reach our goal. The steps for implementing Kruskal's algorithm are as follows: 1.Sort all the edges from low weight to high 2.Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then reject this edge. 3.Keep adding edges until we reach all vertices.