SlideShare a Scribd company logo
Analysis and Design of Algorithms
(2150703)
Presented by :
Jay Patel (130110107036)
Gujarat Technological University
G.H Patel College of Engineering and Technology
Department of Computer Engineering
Greedy Algorithms
Guided by:
Namrta Dave
Greedy Algorithms:
• Many real-world problems are optimization problems in that they attempt to find an
optimal solution among many possible candidate solutions.
• An optimization problem is one in which you want to find, not just a solution, but the best
solution
• A “greedy algorithm” sometimes works well for optimization problems
• A greedy algorithm works in phases. At each phase: You take the best you can get right
now, without regard for future consequences.You hope that by choosing a local optimum at
each step, you will end up at a global optimum
• A familiar scenario is the change-making problem that we often encounter at a cash
register: receiving the fewest numbers of coins to make change after paying the bill for a
purchase.
• Constructs a solution to an optimization problem piece by
• piece through a sequence of choices that are:
1.feasible, i.e. satisfying the constraints
2.locally optimal (with respect to some neighborhood definition)
3.greedy (in terms of some measure), and irrevocable
• For some problems, it yields a globally optimal solution for every instance. For most, does
not but can be useful for fast approximations. We are mostly interested in the former case
in this class.
Greedy Technique:
Greedy Techniques:
• Optimal solutions:
• change making for “normal” coin denominations
• minimum spanning tree (MST)
• Prim’s MST
• Kruskal’s MST
• simple scheduling problems
• Dijkstra’s algo
• Huffman codes
• Approximations/heuristics:
• traveling salesman problem (TSP)
• knapsack problem
• other combinatorial optimization problems
Greedy Scenario:
• Feasible
• Has to satisfy the problem’s constraints
• Locally Optimal
• Has to make the best local choice among all feasible choices available on that step
• If this local choice results in a global optimum then the problem has optimal
substructure
• Irrevocable
• Once a choice is made it can’t be un-done on subsequent steps of the algorithm
• Simple examples:
• Playing chess by making best move without look-ahead
• Giving fewest number of coins as change
• Simple and appealing, but don’t always give the best solution
Change-Making Problem:
Given unlimited amounts of coins of denominations , give change for amount n with the least
number of coins
Example: d1 = 25 INR, d2 =10 INR, d3 = 5 INR, d4 = 1 INR and n = 48 INR
Greedy solution: <1, 2, 0, 3>
So one 25 INR coin
Two 10 INR coin
Zero 5 INR coin
Three 1 INR coin
But it doesn’t give optimal solution everytime.
Failure of Greedy algorithm
Example:
• In some (fictional) monetary system, “Coin” come in 1 INR, 7 INR, and 10 INR coins
Using a greedy algorithm to count out 15 INR, you would get
A 10 INR coin
Five 1 INR coin, for a total of 15 INR
This requires six coins
A better solution would be to use two 7 INR coin and one 1 INR coin
This only requires three coins
The greedy algorithm results in a solution, but not in an optimal solution
Knapsack Problem:
• Given n objects each have a weight wi and a value vi , and given a knapsack of total
capacity W. The problem is to pack the knapsack with these objects in order to maximize
the total value of those objects packed without exceeding the knapsack’s capacity.
• More formally, let xi denote the fraction of the object i to be included in the knapsack, 0 
xi  1, for 1  i  n. The problem is to find values for the xi such that
• Note that we may assume because otherwise, we would choose xi = 1 for each i
which would be an obvious optimal solution.
 

n
i
ii
n
i
ii vxWwx
11
maximized.isand
 

n
i
i Ww
1
The optimal Knapsack Algorithm:
This algorithm is for time complexity O(n lgn))
(1) Sort the n objects from large to small based on the ratios vi/wi . We assume the
arrays w[1..n] and v[1..n] store the respective weights and values after sorting.
(2) initialize array x[1..n] to zeros.
(3) weight = 0; i = 1
(4) while (i  n and weight < W) do
(I) if weight + w[i]  W then x[i] = 1
(II) else x[i] = (W – weight) / w[i]
(III) weight = weight + x[i] * w[i]
(IV) i++
There seem to be 3 obvious greedy strategies:
(Max value) Sort the objects from the highest value to the lowest, then pick them in that order.
(Min weight) Sort the objects from the lowest weight to the highest, then pick them in that
order.
(Max value/weight ratio) Sort the objects based on the value to weight ratios, from the highest
to the lowest, then select.
Example: Given n = 5 objects and a knapsack capacity W = 100 as in Table I. The three
solutions are given in Table II.
Knapsack Problem:
W
V
V/W
10 20 30 40 50
20 30 66 40 60
2.0 1.5 2.2 1.0 1.2
Max Vi
Min Wi
Max Vi/Wi
SELECT Xi
0 0 1 0.5 1
1 1 1 1 0
1 1 1 0 0.8
Value
146
156
164
Minimum Spanning Tree (MST):
16 states of Spanning tree can happened
A cable company want to connect five villages to their network which currently
extends to the market town of Avonford.
What is the minimum length of cable needed?
A F
B C
D
E
2
7
4
5
8 6
4
5
3
8
Example
Solution for MST:
Kruskal’s Algorithm:
A F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
List the edges in order of size:
ED 2 AB 3
AE 4 CD 4
BC 5 EF 5
CF 6 AF 7
BF 8 CF 8
MST-KRUSKAL(G, w)
1. A ← Ø
2. for each vertex v V[G]
3. do MAKE-SET(v)
4. sort the edges of E into nondecreasing order
by weight w
5. for each edge (u, v) E, taken in
nondecreasing
order by weight
6. do if FIND-SET(u) ≠ FIND-SET(v)
7. then A ← A {(u, v)}
8. UNION(u, v)
9. return A
Select the shortest
edge in the network
ED 2
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
1
43
2
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4 (or AE 4)
A F
B
C
D
E
2
7
4 5
8 6 4
5
3
8
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
BC 5 – forms a cycle
EF 5
A F
B
C
D
E
2
7
4
5
8 6 4
5
3
8
All vertices have been conn
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
5
6
Total weight of tree: 18
Kruskal’s Algorithm:
Prim’s Algorithm:
MST-PRIM(G, w, r)
1. for each u V [G]
2. do key[u] ← ∞
3. π[u] ← NIL
4. key[r] ← 0
5. Q ← V [G]
6. while Q ≠ Ø
7. do u ← EXTRACT-MIN(Q)
8. for each v Adj[u]
9. do if v Q and w(u, v) < key[v]
10. then π[v] ← u
11. key[v] ← w(u, v)
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
Select any vertex
A
Select the shortest edge connected to that vertex
AB 3
Prim’s Algorithm:
A F
B C
D
E
2
7
4
5
8 6 4
5
3
8
Select the shortest
edge connected to
any vertex already
connected.
AE 4
1
43
2
Select the shortest
edge connected to
any vertex already
connected.
ED 2
A F
B
C
D
E
2
7
4
5
8 6 4
5
3
8
Select the shortest
edge connected to
any vertex already
connected.
DC 4
A F
B
C
D
E
2
7
4
5
8 6 4
5
3
8
Select the shortest
edge connected to
any vertex already
connected.
EF 5
A F
B
C
D
E
2
7
4
5
8 6 4
5
3
8
Prim’s Algorithm:
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
All vertices have been connected.
The solution is
AB 3
AE 4
ED 2
DC 4
EF 5
Total weight of tree: 18
There are some methods left:
• Dijkstra’s algorithm
• Huffman’s Algorithm
• Task scheduling
• Travelling salesman Problem etc.
• Dynamic Greedy Problems
Greedy Algorithms:
We can find the optimized solution with Greedy method which may be optimal sometime.
THANK YOU

More Related Content

What's hot

Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Dr Shashikant Athawale
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
Amit Kumar Rathi
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
Learning Courses Online
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
Greedy method
Greedy method Greedy method
Greedy method
Dr Shashikant Athawale
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
Waqar Akram
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
sana younas
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
V.V.Vanniaperumal College for Women
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
N queen problem
N queen problemN queen problem
N queen problem
Ridhima Chowdhury
 
Randomized Algorithm
Randomized AlgorithmRandomized Algorithm
Randomized Algorithm
Kanishka Khandelwal
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
Adnan Malak
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Branch and bound technique
Branch and bound techniqueBranch and bound technique
Branch and bound technique
ishmecse13
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
Jyotsna Suryadevara
 
Greedy algorithm
Greedy algorithmGreedy algorithm
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 

What's hot (20)

Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Greedy method
Greedy method Greedy method
Greedy method
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
N queen problem
N queen problemN queen problem
N queen problem
 
Randomized Algorithm
Randomized AlgorithmRandomized Algorithm
Randomized Algorithm
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Branch and bound technique
Branch and bound techniqueBranch and bound technique
Branch and bound technique
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 

Similar to Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's

Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
muthukrishnavinayaga
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
Muthu Vinayagam
 
Mba i qt unit-1.2_transportation, assignment and transshipment problems
Mba i qt unit-1.2_transportation, assignment and transshipment problemsMba i qt unit-1.2_transportation, assignment and transshipment problems
Mba i qt unit-1.2_transportation, assignment and transshipment problems
Rai University
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
TayyabSattar5
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
Ruchika Sinha
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
Single source Shortest path algorithm with example
Single source Shortest path algorithm with exampleSingle source Shortest path algorithm with example
Single source Shortest path algorithm with example
VINITACHAUHAN21
 
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
HarshitSingh334328
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
BinayakMukherjee4
 
Mini project
Mini projectMini project
Mini project
VIKAS TIWARI
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 
Chapter 6-INTEGER PROGRAMMING note.pdf
Chapter 6-INTEGER PROGRAMMING  note.pdfChapter 6-INTEGER PROGRAMMING  note.pdf
Chapter 6-INTEGER PROGRAMMING note.pdf
Tsegay Berhe
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
LINEAR PROGRAMMING
LINEAR PROGRAMMINGLINEAR PROGRAMMING
LINEAR PROGRAMMING
rashi9
 
Linear Programming- Leacture-16-lp1.pptx
Linear Programming- Leacture-16-lp1.pptxLinear Programming- Leacture-16-lp1.pptx
Linear Programming- Leacture-16-lp1.pptx
SarahKoech1
 

Similar to Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's (20)

Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
36 greedy
36 greedy36 greedy
36 greedy
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
Mba i qt unit-1.2_transportation, assignment and transshipment problems
Mba i qt unit-1.2_transportation, assignment and transshipment problemsMba i qt unit-1.2_transportation, assignment and transshipment problems
Mba i qt unit-1.2_transportation, assignment and transshipment problems
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Single source Shortest path algorithm with example
Single source Shortest path algorithm with exampleSingle source Shortest path algorithm with example
Single source Shortest path algorithm with example
 
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
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Mini project
Mini projectMini project
Mini project
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Chapter 6-INTEGER PROGRAMMING note.pdf
Chapter 6-INTEGER PROGRAMMING  note.pdfChapter 6-INTEGER PROGRAMMING  note.pdf
Chapter 6-INTEGER PROGRAMMING note.pdf
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
LINEAR PROGRAMMING
LINEAR PROGRAMMINGLINEAR PROGRAMMING
LINEAR PROGRAMMING
 
Linear Programming- Leacture-16-lp1.pptx
Linear Programming- Leacture-16-lp1.pptxLinear Programming- Leacture-16-lp1.pptx
Linear Programming- Leacture-16-lp1.pptx
 

More from Jay Patel

Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2
Jay Patel
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
Jay Patel
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
Jay Patel
 
Cpd- Contribution and Personality Development
Cpd- Contribution and Personality DevelopmentCpd- Contribution and Personality Development
Cpd- Contribution and Personality Development
Jay Patel
 
Coa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COACoa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COA
Jay Patel
 
Ch7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COACh7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COA
Jay Patel
 
15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA
Jay Patel
 
9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA
Jay Patel
 
3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization
Jay Patel
 
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtuCn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Jay Patel
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
Jay Patel
 

More from Jay Patel (12)

Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2Motivational hindi cpd ppt sem 2
Motivational hindi cpd ppt sem 2
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
 
Cpd- Contributor and Personality Development
Cpd- Contributor and Personality DevelopmentCpd- Contributor and Personality Development
Cpd- Contributor and Personality Development
 
Cpd- Contribution and Personality Development
Cpd- Contribution and Personality DevelopmentCpd- Contribution and Personality Development
Cpd- Contribution and Personality Development
 
Coa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COACoa 09-36-computer organization and archietecture-CO-COA
Coa 09-36-computer organization and archietecture-CO-COA
 
Ch7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COACh7 official=computer organization and archietectur- CO-COA
Ch7 official=computer organization and archietectur- CO-COA
 
15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA15 control-computer organization and archietecture-CO-COA
15 control-computer organization and archietecture-CO-COA
 
9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA9 36-computer organization and archietecture- CO- COA
9 36-computer organization and archietecture- CO- COA
 
3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization3 4- computer organization and archietecture- COA- CO- Computer organization
3 4- computer organization and archietecture- COA- CO- Computer organization
 
Cn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtuCn 04,32,36-Cn all chapters1- computer networks- gtu
Cn 04,32,36-Cn all chapters1- computer networks- gtu
 
Greedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algoGreedy Algorithm-Dijkstra's algo
Greedy Algorithm-Dijkstra's algo
 

Recently uploaded

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 

Recently uploaded (20)

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 

Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's

  • 1. Analysis and Design of Algorithms (2150703) Presented by : Jay Patel (130110107036) Gujarat Technological University G.H Patel College of Engineering and Technology Department of Computer Engineering Greedy Algorithms Guided by: Namrta Dave
  • 2. Greedy Algorithms: • Many real-world problems are optimization problems in that they attempt to find an optimal solution among many possible candidate solutions. • An optimization problem is one in which you want to find, not just a solution, but the best solution • A “greedy algorithm” sometimes works well for optimization problems • A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences.You hope that by choosing a local optimum at each step, you will end up at a global optimum • A familiar scenario is the change-making problem that we often encounter at a cash register: receiving the fewest numbers of coins to make change after paying the bill for a purchase.
  • 3. • Constructs a solution to an optimization problem piece by • piece through a sequence of choices that are: 1.feasible, i.e. satisfying the constraints 2.locally optimal (with respect to some neighborhood definition) 3.greedy (in terms of some measure), and irrevocable • For some problems, it yields a globally optimal solution for every instance. For most, does not but can be useful for fast approximations. We are mostly interested in the former case in this class. Greedy Technique:
  • 4. Greedy Techniques: • Optimal solutions: • change making for “normal” coin denominations • minimum spanning tree (MST) • Prim’s MST • Kruskal’s MST • simple scheduling problems • Dijkstra’s algo • Huffman codes • Approximations/heuristics: • traveling salesman problem (TSP) • knapsack problem • other combinatorial optimization problems
  • 5. Greedy Scenario: • Feasible • Has to satisfy the problem’s constraints • Locally Optimal • Has to make the best local choice among all feasible choices available on that step • If this local choice results in a global optimum then the problem has optimal substructure • Irrevocable • Once a choice is made it can’t be un-done on subsequent steps of the algorithm • Simple examples: • Playing chess by making best move without look-ahead • Giving fewest number of coins as change • Simple and appealing, but don’t always give the best solution
  • 6. Change-Making Problem: Given unlimited amounts of coins of denominations , give change for amount n with the least number of coins Example: d1 = 25 INR, d2 =10 INR, d3 = 5 INR, d4 = 1 INR and n = 48 INR Greedy solution: <1, 2, 0, 3> So one 25 INR coin Two 10 INR coin Zero 5 INR coin Three 1 INR coin But it doesn’t give optimal solution everytime.
  • 7. Failure of Greedy algorithm Example: • In some (fictional) monetary system, “Coin” come in 1 INR, 7 INR, and 10 INR coins Using a greedy algorithm to count out 15 INR, you would get A 10 INR coin Five 1 INR coin, for a total of 15 INR This requires six coins A better solution would be to use two 7 INR coin and one 1 INR coin This only requires three coins The greedy algorithm results in a solution, but not in an optimal solution
  • 8. Knapsack Problem: • Given n objects each have a weight wi and a value vi , and given a knapsack of total capacity W. The problem is to pack the knapsack with these objects in order to maximize the total value of those objects packed without exceeding the knapsack’s capacity. • More formally, let xi denote the fraction of the object i to be included in the knapsack, 0  xi  1, for 1  i  n. The problem is to find values for the xi such that • Note that we may assume because otherwise, we would choose xi = 1 for each i which would be an obvious optimal solution.    n i ii n i ii vxWwx 11 maximized.isand    n i i Ww 1
  • 9. The optimal Knapsack Algorithm: This algorithm is for time complexity O(n lgn)) (1) Sort the n objects from large to small based on the ratios vi/wi . We assume the arrays w[1..n] and v[1..n] store the respective weights and values after sorting. (2) initialize array x[1..n] to zeros. (3) weight = 0; i = 1 (4) while (i  n and weight < W) do (I) if weight + w[i]  W then x[i] = 1 (II) else x[i] = (W – weight) / w[i] (III) weight = weight + x[i] * w[i] (IV) i++
  • 10. There seem to be 3 obvious greedy strategies: (Max value) Sort the objects from the highest value to the lowest, then pick them in that order. (Min weight) Sort the objects from the lowest weight to the highest, then pick them in that order. (Max value/weight ratio) Sort the objects based on the value to weight ratios, from the highest to the lowest, then select. Example: Given n = 5 objects and a knapsack capacity W = 100 as in Table I. The three solutions are given in Table II. Knapsack Problem: W V V/W 10 20 30 40 50 20 30 66 40 60 2.0 1.5 2.2 1.0 1.2 Max Vi Min Wi Max Vi/Wi SELECT Xi 0 0 1 0.5 1 1 1 1 1 0 1 1 1 0 0.8 Value 146 156 164
  • 11. Minimum Spanning Tree (MST): 16 states of Spanning tree can happened
  • 12. A cable company want to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum length of cable needed? A F B C D E 2 7 4 5 8 6 4 5 3 8 Example Solution for MST:
  • 13. Kruskal’s Algorithm: A F B C D E 2 7 4 5 8 6 4 5 3 8 List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 MST-KRUSKAL(G, w) 1. A ← Ø 2. for each vertex v V[G] 3. do MAKE-SET(v) 4. sort the edges of E into nondecreasing order by weight w 5. for each edge (u, v) E, taken in nondecreasing order by weight 6. do if FIND-SET(u) ≠ FIND-SET(v) 7. then A ← A {(u, v)} 8. UNION(u, v) 9. return A
  • 14. Select the shortest edge in the network ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the next shortest edge which does not create a cycle ED 2 AB 3 A F B C D E 2 7 4 5 8 6 4 5 3 8 1 43 2 Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 A F B C D E 2 7 4 5 8 6 4 5 3 8
  • 15. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 All vertices have been conn The solution is ED 2 AB 3 CD 4 AE 4 EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 5 6 Total weight of tree: 18 Kruskal’s Algorithm:
  • 16. Prim’s Algorithm: MST-PRIM(G, w, r) 1. for each u V [G] 2. do key[u] ← ∞ 3. π[u] ← NIL 4. key[r] ← 0 5. Q ← V [G] 6. while Q ≠ Ø 7. do u ← EXTRACT-MIN(Q) 8. for each v Adj[u] 9. do if v Q and w(u, v) < key[v] 10. then π[v] ← u 11. key[v] ← w(u, v)
  • 17. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select any vertex A Select the shortest edge connected to that vertex AB 3 Prim’s Algorithm:
  • 18. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. AE 4 1 43 2 Select the shortest edge connected to any vertex already connected. ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. DC 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8
  • 19. Prim’s Algorithm: A F B C D E 2 7 4 5 8 6 4 5 3 8 All vertices have been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18
  • 20. There are some methods left: • Dijkstra’s algorithm • Huffman’s Algorithm • Task scheduling • Travelling salesman Problem etc. • Dynamic Greedy Problems Greedy Algorithms: We can find the optimized solution with Greedy method which may be optimal sometime.