SlideShare a Scribd company logo
Data Structures and Algorithm
Prepared by
Mrs.P.Anusha M.Sc(IT).,M.Phil.,D.P.T.T.,(Ph.D).,
Assistant professor,
Department of Information Technology,
Bon secours college for women,
Thanjavur.
GREEDY METHOD
Greedy Method
• General Approach
• Given a set of n inputs
• Find a subset called feasible solution of the n inputs
subject to some constraints and satisfying a given
objective function
• If the objective function is maximized or minimized the
feasible solution is optimal
• It is locally optimal method
Components of Greedy Algorithm
Greedy algorithms have the following five components −
• A candidate set − A solution is created from this set.
• A selection function − Used to choose the best candidate
to be added to the solution.
• A feasibility function − Used to determine whether a
candidate can be used to contribute to the solution.
• An objective function − Used to assign a value to a
solution or a partial solution.
• A solution function − Used to indicate whether a
complete solution has been reached.
Greedy method control Abstraction for the subset paradigm
Algorithm
• Step 1: Choose an input from the input set, based on some
criterion. If no more input exit.
• Step 2 : Check whether the chosen input yields to a
feasible solution. If no, discard the input and go to step 1
• Step 3 : Include the input into the solution vector and
update the objective function. Go to step 1.
Optimal Storage on Tapes
• There are n programs that are to be stored on a computer
tape of length L. Associated with each program i is length Li
• Assume the tape is initially positioned at the front. If the
programs are stored in the order I = i1,i2…in the time tj needed
to retrieve program ij
• If all programs are retrieved equally often, then the
Mean Retrieval Time (MRT) =
• This problem fits the ordering paradigm. Minimizing the MRT
is equivalent to minimizing
• d(I) =
Example:
• Let n = 3, (L1, L2, L3) = (5, 10, 3) 6 possible orderings. The
optimal is 3,1,2.
Assigning programs to tapes
Job Sequencing with Deadlines
Problem Statement
• In job sequencing problem, the objective is to find a sequence of
jobs, which is completed within their deadlines and gives
maximum profit.
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.
• Assume, deadline of ith job Ji is di and the profit received from
this job is pi.
• Hence, the optimal solution of this algorithm is a feasible
solution with maximum profit.
• Thus, D(i) > 0 for 1⩽i⩽n.
• Initially, these jobs are ordered according to profit,
• i.e. p1⩾ p2⩾ p3⩾...⩾ pn
•
Analysis
• In this algorithm, we are using two loops, one is within another.
Hence, the complexity of this algorithm is O(n2).
Example
• Let us consider a set of given jobs as shown in the following table.
• We have to find a sequence of jobs, which will be completed within
their deadlines and will give maximum profit.
• Each job is associated with a deadline and profit.
Job J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
Solution
• To solve this problem, the given jobs are sorted according to
their profit in a descending order.
• Hence, after sorting, the jobs are ordered as shown in the
following table.
Job J2 J1 J4 J3 J5
Deadline 1 2 2 3 1
Profit 100 60 40 20 20
• From this set of jobs, first we select J2, as it can be completed
within its deadline and contributes maximum profit.
• Next, J1 is selected as it gives more profit compared to J4.
• In the next clock, J4 cannot be selected as its deadline is over,
hence J3 is selected as it executes within its deadline.
• The job J5 is discarded as it cannot be executed within its deadline.
• Thus, the solution is the sequence of jobs (J2, J1, J3), which are
being executed within their deadline and gives maximum profit.
• Total profit of this sequence is 100 + 60 + 20 = 180.
Optimal Merge Pattern
Problem Statement
• 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.
• 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.
• Two-way merge patterns can be represented by binary merge
trees. Let us consider a set of n sorted files {f1, f2, f3, …, fn}.
• Initially, each element of this is considered as a single node
binary tree.
• To find this optimal solution, the following algorithm is used.
• At the end of this algorithm, the weight of the root node
represents the optimal cost.
Example
• 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
• M2 = merge M1 and f3 => 50 + 10 = 60
• M3 = merge M2 and f4 => 60 + 5 = 65
• M4 = merge M3 and f5 => 65 + 30 = 95
• Hence, the total number of operations is
50 + 60 + 65 + 95 = 270
• Now, the question arises is there any better solution?
• Sorting the numbers according to their size in an
ascending order, we get the following sequence −
• f4, f3, f1, f2, f5
• Hence, merge operations can be performed on this
sequence
• M1 = merge f4 and f3 => 5 + 10 = 15
• M2 = merge M1 and f1 => 15 + 20 = 35
• M3 = merge M2 and f2 => 35 + 30 = 65
• M4 = merge M3 and f5 => 65 + 30 = 95
• Therefore, the total number of operations is
15 + 35 + 65 + 95 = 210
• Obviously, this is better than the previous one.
• In this context, we are now going to solve the problem
using this algorithm.
Initial Set
Step-1
Step-2
Step-3
Step-4
• Hence, the solution takes 15 + 35 + 60 + 95 = 205 number
of comparisons.
Greedy method

More Related Content

What's hot

Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
V.V.Vanniaperumal College for Women
 
N queen problem
N queen problemN queen problem
N queen problem
Ridhima Chowdhury
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
 
Np hard
Np hardNp hard
Np hard
jesal_joshi
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
Animesh Chaturvedi
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
Amit Kundu
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
shashidharPapishetty
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
Arafat Hossan
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
Mahdi Esmailoghli
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
Saravanan Natarajan
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 

What's hot (20)

Np complete
Np completeNp complete
Np complete
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
N queen problem
N queen problemN queen problem
N queen problem
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Np hard
Np hardNp hard
Np hard
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Big o notation
Big o notationBig o notation
Big o notation
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 

Similar to Greedy method

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
iqbalphy1
 
Linear Programing.pptx
Linear Programing.pptxLinear Programing.pptx
Linear Programing.pptx
AdnanHaleem
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
Akhil Kaushik
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
deepakjoshi29912
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
infanciaj
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
Dabbal Singh Mahara
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptx
ssuser1a5f25
 
12-greedy.ppt
12-greedy.ppt12-greedy.ppt
12-greedy.ppt
ASVKVinayak
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
VinayNassa3
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
AnkitaVerma776806
 

Similar to Greedy method (20)

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Linear Programing.pptx
Linear Programing.pptxLinear Programing.pptx
Linear Programing.pptx
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Computational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptxComputational Thinking 11- ActivitySelection.pptx
Computational Thinking 11- ActivitySelection.pptx
 
12-greedy.ppt
12-greedy.ppt12-greedy.ppt
12-greedy.ppt
 
Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 

More from Anusha sivakumar

deadline.pptx
deadline.pptxdeadline.pptx
deadline.pptx
Anusha sivakumar
 
Evolution of Computers ppt.pptx
Evolution of Computers ppt.pptxEvolution of Computers ppt.pptx
Evolution of Computers ppt.pptx
Anusha sivakumar
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
Anusha sivakumar
 
PURPOSE OF DATABASE final.pptx
PURPOSE OF DATABASE final.pptxPURPOSE OF DATABASE final.pptx
PURPOSE OF DATABASE final.pptx
Anusha sivakumar
 
NESTED SUBQUERY.pptx
NESTED SUBQUERY.pptxNESTED SUBQUERY.pptx
NESTED SUBQUERY.pptx
Anusha sivakumar
 
join relation.pptx
join relation.pptxjoin relation.pptx
join relation.pptx
Anusha sivakumar
 
fundamental relation algebra.pptx
fundamental relation algebra.pptxfundamental relation algebra.pptx
fundamental relation algebra.pptx
Anusha sivakumar
 
DBMS ARCHITECTURE.pptx
DBMS ARCHITECTURE.pptxDBMS ARCHITECTURE.pptx
DBMS ARCHITECTURE.pptx
Anusha sivakumar
 
Database user and administrator.pptx
Database user and administrator.pptxDatabase user and administrator.pptx
Database user and administrator.pptx
Anusha sivakumar
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
Anusha sivakumar
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
Anusha sivakumar
 
AGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptxAGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptx
Anusha sivakumar
 
CSS
CSSCSS
Heap sort
Heap sortHeap sort
Heap sort
Anusha sivakumar
 

More from Anusha sivakumar (14)

deadline.pptx
deadline.pptxdeadline.pptx
deadline.pptx
 
Evolution of Computers ppt.pptx
Evolution of Computers ppt.pptxEvolution of Computers ppt.pptx
Evolution of Computers ppt.pptx
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
PURPOSE OF DATABASE final.pptx
PURPOSE OF DATABASE final.pptxPURPOSE OF DATABASE final.pptx
PURPOSE OF DATABASE final.pptx
 
NESTED SUBQUERY.pptx
NESTED SUBQUERY.pptxNESTED SUBQUERY.pptx
NESTED SUBQUERY.pptx
 
join relation.pptx
join relation.pptxjoin relation.pptx
join relation.pptx
 
fundamental relation algebra.pptx
fundamental relation algebra.pptxfundamental relation algebra.pptx
fundamental relation algebra.pptx
 
DBMS ARCHITECTURE.pptx
DBMS ARCHITECTURE.pptxDBMS ARCHITECTURE.pptx
DBMS ARCHITECTURE.pptx
 
Database user and administrator.pptx
Database user and administrator.pptxDatabase user and administrator.pptx
Database user and administrator.pptx
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
 
AGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptxAGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptx
 
CSS
CSSCSS
CSS
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

Greedy method

  • 2. Prepared by Mrs.P.Anusha M.Sc(IT).,M.Phil.,D.P.T.T.,(Ph.D)., Assistant professor, Department of Information Technology, Bon secours college for women, Thanjavur.
  • 4. Greedy Method • General Approach • Given a set of n inputs • Find a subset called feasible solution of the n inputs subject to some constraints and satisfying a given objective function • If the objective function is maximized or minimized the feasible solution is optimal • It is locally optimal method
  • 5. Components of Greedy Algorithm Greedy algorithms have the following five components − • A candidate set − A solution is created from this set. • A selection function − Used to choose the best candidate to be added to the solution. • A feasibility function − Used to determine whether a candidate can be used to contribute to the solution. • An objective function − Used to assign a value to a solution or a partial solution. • A solution function − Used to indicate whether a complete solution has been reached.
  • 6. Greedy method control Abstraction for the subset paradigm
  • 7. Algorithm • Step 1: Choose an input from the input set, based on some criterion. If no more input exit. • Step 2 : Check whether the chosen input yields to a feasible solution. If no, discard the input and go to step 1 • Step 3 : Include the input into the solution vector and update the objective function. Go to step 1.
  • 8. Optimal Storage on Tapes • There are n programs that are to be stored on a computer tape of length L. Associated with each program i is length Li • Assume the tape is initially positioned at the front. If the programs are stored in the order I = i1,i2…in the time tj needed to retrieve program ij • If all programs are retrieved equally often, then the Mean Retrieval Time (MRT) = • This problem fits the ordering paradigm. Minimizing the MRT is equivalent to minimizing • d(I) =
  • 9. Example: • Let n = 3, (L1, L2, L3) = (5, 10, 3) 6 possible orderings. The optimal is 3,1,2.
  • 11. Job Sequencing with Deadlines Problem Statement • In job sequencing problem, the objective is to find a sequence of jobs, which is completed within their deadlines and gives maximum profit. 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.
  • 12. • It may happen that all of the given jobs may not be completed within their deadlines. • Assume, deadline of ith job Ji is di and the profit received from this job is pi. • Hence, the optimal solution of this algorithm is a feasible solution with maximum profit. • Thus, D(i) > 0 for 1⩽i⩽n. • Initially, these jobs are ordered according to profit, • i.e. p1⩾ p2⩾ p3⩾...⩾ pn •
  • 13.
  • 14. Analysis • In this algorithm, we are using two loops, one is within another. Hence, the complexity of this algorithm is O(n2). Example • Let us consider a set of given jobs as shown in the following table. • We have to find a sequence of jobs, which will be completed within their deadlines and will give maximum profit. • Each job is associated with a deadline and profit. Job J1 J2 J3 J4 J5 Deadline 2 1 3 2 1 Profit 60 100 20 40 20
  • 15. Solution • To solve this problem, the given jobs are sorted according to their profit in a descending order. • Hence, after sorting, the jobs are ordered as shown in the following table. Job J2 J1 J4 J3 J5 Deadline 1 2 2 3 1 Profit 100 60 40 20 20
  • 16. • From this set of jobs, first we select J2, as it can be completed within its deadline and contributes maximum profit. • Next, J1 is selected as it gives more profit compared to J4. • In the next clock, J4 cannot be selected as its deadline is over, hence J3 is selected as it executes within its deadline. • The job J5 is discarded as it cannot be executed within its deadline. • Thus, the solution is the sequence of jobs (J2, J1, J3), which are being executed within their deadline and gives maximum profit. • Total profit of this sequence is 100 + 60 + 20 = 180.
  • 17. Optimal Merge Pattern Problem Statement • 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.
  • 18. • 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. • 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. • Two-way merge patterns can be represented by binary merge trees. Let us consider a set of n sorted files {f1, f2, f3, …, fn}. • Initially, each element of this is considered as a single node binary tree. • To find this optimal solution, the following algorithm is used.
  • 19. • At the end of this algorithm, the weight of the root node represents the optimal cost.
  • 20. Example • 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 • M2 = merge M1 and f3 => 50 + 10 = 60 • M3 = merge M2 and f4 => 60 + 5 = 65 • M4 = merge M3 and f5 => 65 + 30 = 95 • Hence, the total number of operations is 50 + 60 + 65 + 95 = 270
  • 21. • Now, the question arises is there any better solution? • Sorting the numbers according to their size in an ascending order, we get the following sequence − • f4, f3, f1, f2, f5 • Hence, merge operations can be performed on this sequence • M1 = merge f4 and f3 => 5 + 10 = 15 • M2 = merge M1 and f1 => 15 + 20 = 35 • M3 = merge M2 and f2 => 35 + 30 = 65 • M4 = merge M3 and f5 => 65 + 30 = 95 • Therefore, the total number of operations is 15 + 35 + 65 + 95 = 210 • Obviously, this is better than the previous one.
  • 22. • In this context, we are now going to solve the problem using this algorithm. Initial Set Step-1
  • 24. Step-4 • Hence, the solution takes 15 + 35 + 60 + 95 = 205 number of comparisons.