SlideShare a Scribd company logo
1 of 28
 GREEDY METHOD:THE GENERAL METHO
 OPTIMAL STORAGE ON TAPES
 KNAPSACK PROBLEM
 JOB SEQUENCING WITH DEADLINES
 OPTIMAL MERGE PATTERNS
 The greedy method suggests that one can devise an
algorithm that works in stages, considering one input
at a time.
 At each stage, a decision is made regarding whether a
particular input is in an optimal solution.
 This is done by considering the inputs in an order
determined by some selection procedure.
 The greedy method is perhaps the most straight
forward design technique we consider in this text, and
what’s more it can be applied to a wide variety of
problems.
1.Algorithm Greedy(a,n)
2.//a[1:n]contains the n inputs.
3.{
4. solution:=0;//Intialize the solution.
5. for i:=1 to n do
6. {
7. x:=select(a);
8. if Feasible(solution,x)then
9. solution:=union(solution,x);
10. }
11. return solution;
12.}
 A large ship is to be loaded with cargo. The cargo is
containerized, and all containers are the same size. Different
containers may have different weights.
 The sip may be loaded in stages; one container per stage. At
each stage we need to select a container to load.
 For this decision we may use the greedy criterion: from the
remaining containers, select the one with least weight.
 This order of selection will keep the total weight of the selected
containers minimum and hence leave maximum capacity for
loading more containers.
 Using the greedy algorithm just outlined, we first select the
container that has least weight, then the one with the next
smallest weight, and so on until either all containers have been
loaded or there isn’t enough capacity for the next one.
{
//sort into increasing order
sort (c,numberO f Containers);
n:=numberO f Containers;
//initialize x
for i:=1 to n do
x[i]:=0;
//select containers in order of weight
i:=1;
While(i<n&&c[i]weight<capacity)
{
//enough capacity for container c[i].id
x[c[i].id]:=1;
Capacity--=c[i].weight;
//remaining capacity
i++
}
}
1.Suppose you have 6 containers whose weights are
50,10,30,20,60,and 5 and a ship whose capacity is 100.
use algorithm 4.2 to find an optimal solution to this
instance of the container loading problem.
2.Extend the greedy solution for the container loading
problem to the case when there are two ships. Does
your algorithm always generate optimal solutions?
3.We are given n tasks to perform is sequence. Suppose
that task needs it units of time. If the tasks are done in
the order 1,2,….,n, then task I completes at time
chi=∑j
i=1 tj. The average completion time (ACT) is
1/n∑n
i=1ci.
 `Optimal merge pattern is a pattern that relates to the
merging of two or more sorted files in a single sorted file.
This type of merging can be done by the two-way merging
method.
 If we have two sorted files containing n and m records
respectively then they could be merged together, to obtain
one sorted file in time O (n+m).
 There are many ways in which pairwise merge can be done
to get a single sorted file. Different pairings require a
different amount of computing time.
 The formula of external merging cost is:
 Where, f (i) represents the number of records in each
file and d (i) represents the depth.
The main thing is to pairwise merge the n
sorted files so that the number of comparisons
will be less.
Tree(n)
//list is a global list of n single node
{
For i=1 to i= n-1 do
{
// get a new tree node
Pt: new treenode;
// merge two trees with smallest length
(Pt = lchild) = least(list);
(Pt = rchild) = least(list);
(Pt =weight) = ((Pt = lchild) = weight) = ((Pt = rchild)
= weight);
Insert (list , Pt);
}
// tree left in list
Return least(list);
}
 Given a set of unsorted files: 5, 3, 2, 7, 9, 13
 Now, arrange these elements in ascending order: 2, 3,
5, 7, 9, 13
 After this, pick two smallest numbers and repeat this
until we left with only one number.
 Step 1: Insert 2, 3
 Step 2:
Step 3: Insert 5
Step 4: Insert 13
Step 5: Insert 7 and 9
Step 6:
So, The merging cost = 5 + 10 + 16 + 23 + 39 = 93
 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.
 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 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.
Problem-
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
Answer the following questions-
1.Write the optimal schedule that gives maximum profit.
2.Are all the jobs completed in the optimal schedule?
3.What is the maximum earned profit?
Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
Profits 300 200 190 180 120 100
Solution-
Step-01:
Sort all the given jobs in decreasing order of their profit-
Step-02:
Value of maximum deadline = 5.
So, draw a Gantt chart with maximum time on Gantt chart = 5 units as shown-
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.
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-
Step-05:
•We take job J3.
•Since its deadline is 3, so we place it in the first empty cell
before deadline 3 as-
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.
Now,
 The given questions may be answered as-
Part-01:
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.
Part-02:
•All the jobs are not completed in optimal schedule.
•This is because job J6 could not be completed
within its deadline.
Part-03:
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

More Related Content

What's hot

Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Shiang-Yun Yang
 
LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfzupsezekno
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...Daniel Hutama
 
Algorithm1 course 1st slide
Algorithm1 course 1st slideAlgorithm1 course 1st slide
Algorithm1 course 1st slideHani Ghazi
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
On the Configuration-LP of the Restricted Assignment Problem
On the Configuration-LP of the Restricted Assignment ProblemOn the Configuration-LP of the Restricted Assignment Problem
On the Configuration-LP of the Restricted Assignment ProblemArash Pourdamghani
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm CaseyHaaland
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 

What's hot (20)

Logistics Management Homework Help
Logistics Management Homework HelpLogistics Management Homework Help
Logistics Management Homework Help
 
Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)Aaex7 group2(中英夾雜)
Aaex7 group2(中英夾雜)
 
LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdf
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...
Cryptanalysis with a Quantum Computer - An Exposition on Shor's Factoring Alg...
 
Algorithm1 course 1st slide
Algorithm1 course 1st slideAlgorithm1 course 1st slide
Algorithm1 course 1st slide
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
On the Configuration-LP of the Restricted Assignment Problem
On the Configuration-LP of the Restricted Assignment ProblemOn the Configuration-LP of the Restricted Assignment Problem
On the Configuration-LP of the Restricted Assignment Problem
 
Business Logistics Assignment Help
Business Logistics Assignment HelpBusiness Logistics Assignment Help
Business Logistics Assignment Help
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
36 greedy
36 greedy36 greedy
36 greedy
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 
Exam110121
Exam110121Exam110121
Exam110121
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Mergesort
MergesortMergesort
Mergesort
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 

Similar to data structures and algorithms Unit 4

Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with DeadlinesYashiUpadhyay3
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdfJasmineSayyed3
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxssuser1a5f25
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsMAHERMOHAMED27
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 

Similar to data structures and algorithms Unit 4 (20)

Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
Greedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
 
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
 
04greedy 2x2
04greedy 2x204greedy 2x2
04greedy 2x2
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
12-greedy.ppt
12-greedy.ppt12-greedy.ppt
12-greedy.ppt
 
Flowshop scheduling
Flowshop schedulingFlowshop scheduling
Flowshop scheduling
 
Greedy method
Greedy methodGreedy method
Greedy method
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptx
 
Greedy
GreedyGreedy
Greedy
 
Exam100412
Exam100412Exam100412
Exam100412
 
Greedy method
Greedy method Greedy method
Greedy method
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elements
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Algorithm
AlgorithmAlgorithm
Algorithm
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

data structures and algorithms Unit 4

  • 1.  GREEDY METHOD:THE GENERAL METHO  OPTIMAL STORAGE ON TAPES  KNAPSACK PROBLEM  JOB SEQUENCING WITH DEADLINES  OPTIMAL MERGE PATTERNS
  • 2.  The greedy method suggests that one can devise an algorithm that works in stages, considering one input at a time.  At each stage, a decision is made regarding whether a particular input is in an optimal solution.  This is done by considering the inputs in an order determined by some selection procedure.  The greedy method is perhaps the most straight forward design technique we consider in this text, and what’s more it can be applied to a wide variety of problems.
  • 3. 1.Algorithm Greedy(a,n) 2.//a[1:n]contains the n inputs. 3.{ 4. solution:=0;//Intialize the solution. 5. for i:=1 to n do 6. { 7. x:=select(a); 8. if Feasible(solution,x)then 9. solution:=union(solution,x); 10. } 11. return solution; 12.}
  • 4.  A large ship is to be loaded with cargo. The cargo is containerized, and all containers are the same size. Different containers may have different weights.  The sip may be loaded in stages; one container per stage. At each stage we need to select a container to load.  For this decision we may use the greedy criterion: from the remaining containers, select the one with least weight.  This order of selection will keep the total weight of the selected containers minimum and hence leave maximum capacity for loading more containers.  Using the greedy algorithm just outlined, we first select the container that has least weight, then the one with the next smallest weight, and so on until either all containers have been loaded or there isn’t enough capacity for the next one.
  • 5. { //sort into increasing order sort (c,numberO f Containers); n:=numberO f Containers; //initialize x for i:=1 to n do x[i]:=0; //select containers in order of weight i:=1; While(i<n&&c[i]weight<capacity) { //enough capacity for container c[i].id x[c[i].id]:=1; Capacity--=c[i].weight; //remaining capacity i++ } }
  • 6. 1.Suppose you have 6 containers whose weights are 50,10,30,20,60,and 5 and a ship whose capacity is 100. use algorithm 4.2 to find an optimal solution to this instance of the container loading problem. 2.Extend the greedy solution for the container loading problem to the case when there are two ships. Does your algorithm always generate optimal solutions? 3.We are given n tasks to perform is sequence. Suppose that task needs it units of time. If the tasks are done in the order 1,2,….,n, then task I completes at time chi=∑j i=1 tj. The average completion time (ACT) is 1/n∑n i=1ci.
  • 7.  `Optimal merge pattern is a pattern that relates to the merging of two or more sorted files in a single sorted file. This type of merging can be done by the two-way merging method.  If we have two sorted files containing n and m records respectively then they could be merged together, to obtain one sorted file in time O (n+m).  There are many ways in which pairwise merge can be done to get a single sorted file. Different pairings require a different amount of computing time.
  • 8.  The formula of external merging cost is:  Where, f (i) represents the number of records in each file and d (i) represents the depth. The main thing is to pairwise merge the n sorted files so that the number of comparisons will be less.
  • 9. Tree(n) //list is a global list of n single node { For i=1 to i= n-1 do { // get a new tree node Pt: new treenode; // merge two trees with smallest length (Pt = lchild) = least(list); (Pt = rchild) = least(list); (Pt =weight) = ((Pt = lchild) = weight) = ((Pt = rchild) = weight); Insert (list , Pt); } // tree left in list Return least(list); }
  • 10.  Given a set of unsorted files: 5, 3, 2, 7, 9, 13  Now, arrange these elements in ascending order: 2, 3, 5, 7, 9, 13  After this, pick two smallest numbers and repeat this until we left with only one number.
  • 11.  Step 1: Insert 2, 3  Step 2:
  • 12. Step 3: Insert 5 Step 4: Insert 13
  • 13. Step 5: Insert 7 and 9
  • 14. Step 6: So, The merging cost = 5 + 10 + 16 + 23 + 39 = 93
  • 15.  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.
  • 16.  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.
  • 17.  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.
  • 18. Problem- 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
  • 19. Answer the following questions- 1.Write the optimal schedule that gives maximum profit. 2.Are all the jobs completed in the optimal schedule? 3.What is the maximum earned profit? Jobs J4 J1 J3 J2 J5 J6 Deadlines 2 5 3 3 4 2 Profits 300 200 190 180 120 100 Solution- Step-01: Sort all the given jobs in decreasing order of their profit-
  • 20. Step-02: Value of maximum deadline = 5. So, draw a Gantt chart with maximum time on Gantt chart = 5 units as shown- 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.
  • 21. Step-03: •We take job J4. •Since its deadline is 2, so we place it in the first empty cell before deadline 2 as-
  • 22. Step-04: •We take job J1. •Since its deadline is 5, so we place it in the first empty cell before deadline 5 as-
  • 23. Step-05: •We take job J3. •Since its deadline is 3, so we place it in the first empty cell before deadline 3 as-
  • 24. 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-
  • 25. 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-
  • 26. 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. Now,  The given questions may be answered as-
  • 27. Part-01: 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. Part-02: •All the jobs are not completed in optimal schedule. •This is because job J6 could not be completed within its deadline.
  • 28. Part-03: 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