SlideShare a Scribd company logo
Greedy Algorithms:
An Introduction
Greedy algorithms are a powerful class of algorithms that make locally
optimal choices at each stage with the goal of finding a global optimum.
They are simple yet effective, and are widely used in solving various
optimization problems.
by Riannel Tecson
What are Greedy Algorithms?
1 Stepwise Approach
Greedy algorithms make the
choice that seems best at each
stage, without considering the
long-term consequences.
2 Global Optimality
The goal is to find a global
optimal solution, even though
the algorithm may not
explore all possible options.
3 Efficiency
Greedy algorithms are often efficient and easy to implement,
making them a popular choice for many problems.
Characteristics of Greedy Algorithms
Simplicity
Greedy algorithms are
straightforward and easy to
understand, making them a popular
choice for many problems.
Locally Optimal
At each step, the algorithm makes the
choice that seems best at the
moment, without considering the
long-term consequences.
Efficiency
Greedy algorithms are often efficient
and can be implemented in a timely
manner, making them useful for real-
world applications.
Advantages and
Disadvantages
Advantages
Greedy algorithms are simple,
efficient, and often produce
good results, making them a
popular choice for many
problems.
Disadvantages
Greedy algorithms may not
always find the global optimal
solution, and can be prone to
getting stuck in local optima.
Coin Change Problem
1 Step 1
Define the available coin denominations, such as ₱1, ₱5,
₱10, and ₱20.
2 Step 2
Repeatedly choose the largest coin denomination that is
less than or equal to the remaining amount.
3 Step 3
Continue this process until the entire amount has been
covered, using the minimum number of coins.
Sample Problem 1.
Determine the minimum number of coins needed
to make 47 units of currency using denominations
of ₱1, ₱5, ₱10, and ₱20.
Sample Problem 1.
Determine the minimum number of coins needed to make 47 units
of currency using denominations of ₱1, ₱5, ₱10, and ₱20.
Solution:
Steps:
₱20: 2 coins (40 used, 7 remaining)
₱5: 1 coin (5 used, 2 remaining)
₱1: 2 coins (2 used, 0 remaining)
Answer: 5 coins (2 of ₱20, 1 of ₱5, 2 of ₱1).
Sample Problem 2.
Find the minimum number of coins
required to make ₱33.
Sample Problem 2.
Find the minimum number of coins required to
make ₱33.
•Solution:
• Steps:
₱20: 1 coin (20 used, 13 remaining)
₱10: 1 coin (10 used, 3 remaining)
₱1: 3 coins (3 used, 0 remaining)
• Answer: 5 coins (1 of ₱20, 1 of ₱10, 3 of ₱ 1).
Optimal solution
• In the coin change problem, an optimal solution means finding the
minimum number of coins needed to make a specified amount, given a
set of denominations.
• This solution may involve a combination of coins that doesn’t always
align with the choices a greedy algorithm would make, particularly
when the greedy approach does not yield the least number of coins.
To define it more precisely:
• Optimal Solution:
The smallest possible number of coins required to make up the
target amount.
Optimal solution
For example,
• if you want to make 30 units with denominations of 1, 5, 10, and 20, the greedy
algorithm would choose a 20 and a 10, totaling 2 coins.
• However, an alternative configuration of three 10 coins is also possible.
• Thus, when considering constraints or minimizing specific denominations, an
optimal solution would consider all combinations to ensure the absolute
minimum coin count or a setup that meets all constraints.
Sample Problem 3.
Given denominations of coins as 1, 3, and 4, find
the minimum number of coins needed to make 6
units.
How many coins needed to give 6 units using
Greedy algorithm?
Sample Problem 3.
Given denominations of coins as 1, 3, and 4, find the
minimum number of coins needed to make 6 units.
How many coins needed to give 6 units using Greedy
algorithm?
Solution Using Greedy Algorithm:
The greedy algorithm selects the largest denomination that doesn’t
exceed the remaining amount at each step.
1. Start with the largest denomination, 4:
1.4: 1 coin (4 used, 2 remaining)
2. Next, use the largest coin that is less than or equal to 2:
1.1: 2 coins (1 + 1 used, 0 remaining)
•Total Coins (Greedy Solution): 3 coins (1 of 4, 2 of 1).
Sample Problem 3.
Given denominations of coins as 1, 3, and 4, find the
minimum number of coins needed to make 6 units.
How many coins needed to give 6 units using Greedy
algorithm?
Optimal Solution:
By exploring all possible combinations, we can achieve the
target amount using 2 coins:
1.Use two coins of 3 (3 + 3 = 6).
• Total Coins (Optimal Solution): 2 coins (2 of 3).
Knapsack Problem
Define Items
Assign a value and weight to each item that can be placed
in the knapsack.
Maximize Value
Choose the items that maximize the total value while
staying within the weight limit of the knapsack.
Greedy Approach
A greedy algorithm would select the items with the
highest value-to-weight ratio, until the knapsack is full.
Problem 1: 0/1 Knapsack Problem where fractions of items are not allowed.
The goal is to maximize the total value of items in the knapsack.
• You cannot take fractions of items; each item must be taken in its entirety
(0/1 knapsack).
You have a knapsack that can hold up to 50 kg. You are given the following
items, each with a weight and a value:
Item Weight (Kg) Value (₱)
A 10 60
B 20 100
C 30 120
D 40 150
Solution Using Greedy Algorithm:
A common greedy approach for the knapsack problem is to select items based on
the highest value-to-weight ratio until the knapsack reaches capacity.
1.Calculate Value-to-Weight Ratios for each item:
1.Item A: 60/10 = 6
2.Item B: 100/20 = 5
3.Item C: 120/30 = 4
4.Item D: 150/40 = 3.75
2.Sort Items by Value-to-Weight Ratio (from highest to lowest):
Item Weight (Kg) Value (₱) Value-to-Weight Ratio
A 10 60 6
B 20 100 5
C 30 120 4
D 40 150 3.75
3. Select Items Based on Greedy Algorithm:
Step 1: Add Item A completely (10 kg, value ₱60).
1.Remaining Capacity: 50−10 = 40 kg
2.Total Value: ₱60
Step 2: Add Item B completely (20 kg, value ₱100).
3.Remaining Capacity: 40−20 = 20 kg
4.Total Value: ₱60 + ₱100 = ₱160
Step 3: Add Item C completely (30 kg, value ₱120).
5.Item C cannot be added since only 20 kg remain.
Step 4: Stop, as no other item can fit in the remaining capacity.
•Total Value (Greedy Solution): ₱160 (Items A and B).
Item Weight (Kg) Value (₱) Value-to-Weight Ratio
A 10 60 6
B 20 100 5
C 30 120 4
D 40 150 3.75
Is it OPTIMAL solution?
Optimal Solution:
By exploring all combinations, we find a better solution by selecting
Item D and Item A:
1. Add Item D completely (40 kg, value ₱150).
2. Add Item A completely (10 kg, value ₱60).
Total Weight: 40 + 10= 50 kg
Total Value (Optimal Solution): ₱150 + ₱60= ₱210
Item Weight (Kg) Value (₱) Value-to-Weight Ratio
A 10 60 6
B 20 100 5
C 30 120 4
D 40 150 3.75
Problem 2:
The goal is to maximize the total value of items in the knapsack.
• You can take fractions of items if needed (i.e., this is the fractional
knapsack problem).
You have a knapsack that can hold up to 50 kg. You are given the
following items with their weights and values:
Item Weight (Kg) Value (₱)
A 10 60
B 20 100
C 30 120
Solution Using Greedy Algorithm:
In the fractional knapsack problem, the greedy approach
selects items based on the highest value-to-weight ratio.
1.Calculate Value-to-Weight Ratios for each item:
1. Item A: 60/10 = 6
2. Item B: 100/20 = 5
3. Item C: 120/30 = 4
2.Sort Items by Value-to-Weight Ratio (from highest to lowest):
1.Item A (6)
2.Item B (5)
3.Item C (4)
Solution Using Greedy Algorithm:
3. Fill the Knapsack by adding items in order of the highest value-to-weight ratio until
the capacity is reached:
1.Step 1: Add Item A completely (10 kg, value ₱60).
1.Remaining Capacity: 50−10 = 40 kg
2.Total Value: ₱60
2.Step 2: Add Item B completely (20 kg, value ₱100).
1.Remaining Capacity: 40−20 = 20 kg
2.Total Value: ₱60 + ₱100 = ₱160
3.Step 3: Add Item C partially, taking only 20 kg out of 30 kg.
1.Value from Item C: (120/30) × 20 = 80
2.Total Value: ₱160 + ₱80 = ₱240
Item Weight (Kg) Value (₱)
A 10 60
B 20 100
C 30 120
Solution Using Greedy Algorithm:
Final Answer:
Maximum Value in Knapsack: ₱240
Items Selected:
Item A: Entire (10 kg, value ₱60)
Item B: Entire (20 kg, value ₱100)
Item C: Partial (20 kg, value ₱80)
Using the greedy approach based on the highest value-to-weight
ratio, we achieve a maximum total value of ₱240 within the 50 kg
weight limit of the knapsack.
Item Weight (Kg) Value (₱)
A 10 60
B 20 100
C 30 120
Kruskal's Algorithm
Sort Edges
Sort the edges of the graph in ascending order by weight.
Union-Find
Use a Union-Find data structure to keep track of connected components.
Add Edges
Iteratively add the lightest edge that doesn't create a cycle, until all vertices are
connected.
Conclusion and Recap
1 Simplicity
Greedy algorithms are
straightforward and easy to
implement, making them a
popular choice for many
problems.
2 Efficiency
Greedy algorithms are
often efficient and can
produce good results in a
timely manner.
3 Limitations
Greedy algorithms may not always find the global optimal
solution, and can be prone to getting stuck in local optima.

More Related Content

PPT
Greedy with Task Scheduling Algorithm.ppt
PPT
Greedy with Task Scheduling Algorithm.ppt
PDF
12 Greeddy Method
PPT
Greedy
PDF
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
PPTX
June 4, 2015
DOCX
DIMENSIONAL ANALYSIS final
PPT
Greedy method1
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
12 Greeddy Method
Greedy
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
June 4, 2015
DIMENSIONAL ANALYSIS final
Greedy method1

Similar to Greedy-Algorithms with Applications.pptx (20)

PPTX
Fractional Knapsack Problem
PPT
LP1 as EEM Electrial Engineering Department.ppt
PPT
PDF
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
PDF
greedy method.pdf
PPSX
Design and Analysis of Algorithms (Greedy Algorithm)
DOCX
Word+problems+le1
PPTX
Operation research - Chapter 01
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
PPT
Greedy algorithms
PPT
376951072-3-Greedy-Method-new-ppt.ppt
PPTX
Fractional Knapsack Problem
PPTX
Greedy algorithms
PPTX
Moles, Calculations, Dimensional Analysis!!!
PPTX
Math module 2 lesson 8
PPTX
Greedy Method unit-2(Design and analysis of algorithms).pptx
PDF
Management Science Class Exercise in year 2024
Fractional Knapsack Problem
LP1 as EEM Electrial Engineering Department.ppt
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
greedy method.pdf
Design and Analysis of Algorithms (Greedy Algorithm)
Word+problems+le1
Operation research - Chapter 01
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
Greedy algorithms
376951072-3-Greedy-Method-new-ppt.ppt
Fractional Knapsack Problem
Greedy algorithms
Moles, Calculations, Dimensional Analysis!!!
Math module 2 lesson 8
Greedy Method unit-2(Design and analysis of algorithms).pptx
Management Science Class Exercise in year 2024
Ad

More from Riannel Tecson (20)

PPT
Introduction to C# Language and Applications.ppt
PPTX
PF - 102 Terminal Project Guidelines.pptx
PPTX
Who is the most Influential Person in Your Life.pptx
PPTX
Week 6 MIL Evaluating Information and Importance.pptx
PPTX
Java Programs Debugging: Identifying Errors.pptx
PPT
Linked list-stack-queue Data Structure .ppt
PPT
List Data Structure, Linked List, Stacks.ppt
PPTX
Computer Systems Servicing Network Cabling.pptx
PPTX
introduction-to-functions-grade-11general-math.pptx
PPTX
Week-9-UCSP-How-Society-is-Organized.pptx
PPT
PC Components_Hardware_Software_CSS11.ppt
PPT
C# Control Statements, For loop, Do While.ppt
PPTX
Writing the Design and Methodology in Research
PPTX
capstone101 Requirements Methodology and
PPTX
Writing the Review of Related Literature.pptx
PPTX
Week 1 and 2 Getting started with DBMS.pptx
PPTX
Network Tools, Materials and Equipment.pptx
PPTX
Week 5 Update Anomalies.pptx
PPT
Binary Search Tree Traversal.ppt
PPT
Does technology shaed soceity.ppt
Introduction to C# Language and Applications.ppt
PF - 102 Terminal Project Guidelines.pptx
Who is the most Influential Person in Your Life.pptx
Week 6 MIL Evaluating Information and Importance.pptx
Java Programs Debugging: Identifying Errors.pptx
Linked list-stack-queue Data Structure .ppt
List Data Structure, Linked List, Stacks.ppt
Computer Systems Servicing Network Cabling.pptx
introduction-to-functions-grade-11general-math.pptx
Week-9-UCSP-How-Society-is-Organized.pptx
PC Components_Hardware_Software_CSS11.ppt
C# Control Statements, For loop, Do While.ppt
Writing the Design and Methodology in Research
capstone101 Requirements Methodology and
Writing the Review of Related Literature.pptx
Week 1 and 2 Getting started with DBMS.pptx
Network Tools, Materials and Equipment.pptx
Week 5 Update Anomalies.pptx
Binary Search Tree Traversal.ppt
Does technology shaed soceity.ppt
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Institutional Correction lecture only . . .
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Computing-Curriculum for Schools in Ghana
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Institutional Correction lecture only . . .
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pre independence Education in Inndia.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
Classroom Observation Tools for Teachers
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Computing-Curriculum for Schools in Ghana
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
GDM (1) (1).pptx small presentation for students

Greedy-Algorithms with Applications.pptx

  • 1. Greedy Algorithms: An Introduction Greedy algorithms are a powerful class of algorithms that make locally optimal choices at each stage with the goal of finding a global optimum. They are simple yet effective, and are widely used in solving various optimization problems. by Riannel Tecson
  • 2. What are Greedy Algorithms? 1 Stepwise Approach Greedy algorithms make the choice that seems best at each stage, without considering the long-term consequences. 2 Global Optimality The goal is to find a global optimal solution, even though the algorithm may not explore all possible options. 3 Efficiency Greedy algorithms are often efficient and easy to implement, making them a popular choice for many problems.
  • 3. Characteristics of Greedy Algorithms Simplicity Greedy algorithms are straightforward and easy to understand, making them a popular choice for many problems. Locally Optimal At each step, the algorithm makes the choice that seems best at the moment, without considering the long-term consequences. Efficiency Greedy algorithms are often efficient and can be implemented in a timely manner, making them useful for real- world applications.
  • 4. Advantages and Disadvantages Advantages Greedy algorithms are simple, efficient, and often produce good results, making them a popular choice for many problems. Disadvantages Greedy algorithms may not always find the global optimal solution, and can be prone to getting stuck in local optima.
  • 5. Coin Change Problem 1 Step 1 Define the available coin denominations, such as ₱1, ₱5, ₱10, and ₱20. 2 Step 2 Repeatedly choose the largest coin denomination that is less than or equal to the remaining amount. 3 Step 3 Continue this process until the entire amount has been covered, using the minimum number of coins.
  • 6. Sample Problem 1. Determine the minimum number of coins needed to make 47 units of currency using denominations of ₱1, ₱5, ₱10, and ₱20.
  • 7. Sample Problem 1. Determine the minimum number of coins needed to make 47 units of currency using denominations of ₱1, ₱5, ₱10, and ₱20. Solution: Steps: ₱20: 2 coins (40 used, 7 remaining) ₱5: 1 coin (5 used, 2 remaining) ₱1: 2 coins (2 used, 0 remaining) Answer: 5 coins (2 of ₱20, 1 of ₱5, 2 of ₱1).
  • 8. Sample Problem 2. Find the minimum number of coins required to make ₱33.
  • 9. Sample Problem 2. Find the minimum number of coins required to make ₱33. •Solution: • Steps: ₱20: 1 coin (20 used, 13 remaining) ₱10: 1 coin (10 used, 3 remaining) ₱1: 3 coins (3 used, 0 remaining) • Answer: 5 coins (1 of ₱20, 1 of ₱10, 3 of ₱ 1).
  • 10. Optimal solution • In the coin change problem, an optimal solution means finding the minimum number of coins needed to make a specified amount, given a set of denominations. • This solution may involve a combination of coins that doesn’t always align with the choices a greedy algorithm would make, particularly when the greedy approach does not yield the least number of coins. To define it more precisely: • Optimal Solution: The smallest possible number of coins required to make up the target amount.
  • 11. Optimal solution For example, • if you want to make 30 units with denominations of 1, 5, 10, and 20, the greedy algorithm would choose a 20 and a 10, totaling 2 coins. • However, an alternative configuration of three 10 coins is also possible. • Thus, when considering constraints or minimizing specific denominations, an optimal solution would consider all combinations to ensure the absolute minimum coin count or a setup that meets all constraints.
  • 12. Sample Problem 3. Given denominations of coins as 1, 3, and 4, find the minimum number of coins needed to make 6 units. How many coins needed to give 6 units using Greedy algorithm?
  • 13. Sample Problem 3. Given denominations of coins as 1, 3, and 4, find the minimum number of coins needed to make 6 units. How many coins needed to give 6 units using Greedy algorithm? Solution Using Greedy Algorithm: The greedy algorithm selects the largest denomination that doesn’t exceed the remaining amount at each step. 1. Start with the largest denomination, 4: 1.4: 1 coin (4 used, 2 remaining) 2. Next, use the largest coin that is less than or equal to 2: 1.1: 2 coins (1 + 1 used, 0 remaining) •Total Coins (Greedy Solution): 3 coins (1 of 4, 2 of 1).
  • 14. Sample Problem 3. Given denominations of coins as 1, 3, and 4, find the minimum number of coins needed to make 6 units. How many coins needed to give 6 units using Greedy algorithm? Optimal Solution: By exploring all possible combinations, we can achieve the target amount using 2 coins: 1.Use two coins of 3 (3 + 3 = 6). • Total Coins (Optimal Solution): 2 coins (2 of 3).
  • 15. Knapsack Problem Define Items Assign a value and weight to each item that can be placed in the knapsack. Maximize Value Choose the items that maximize the total value while staying within the weight limit of the knapsack. Greedy Approach A greedy algorithm would select the items with the highest value-to-weight ratio, until the knapsack is full.
  • 16. Problem 1: 0/1 Knapsack Problem where fractions of items are not allowed. The goal is to maximize the total value of items in the knapsack. • You cannot take fractions of items; each item must be taken in its entirety (0/1 knapsack). You have a knapsack that can hold up to 50 kg. You are given the following items, each with a weight and a value: Item Weight (Kg) Value (₱) A 10 60 B 20 100 C 30 120 D 40 150
  • 17. Solution Using Greedy Algorithm: A common greedy approach for the knapsack problem is to select items based on the highest value-to-weight ratio until the knapsack reaches capacity. 1.Calculate Value-to-Weight Ratios for each item: 1.Item A: 60/10 = 6 2.Item B: 100/20 = 5 3.Item C: 120/30 = 4 4.Item D: 150/40 = 3.75 2.Sort Items by Value-to-Weight Ratio (from highest to lowest): Item Weight (Kg) Value (₱) Value-to-Weight Ratio A 10 60 6 B 20 100 5 C 30 120 4 D 40 150 3.75
  • 18. 3. Select Items Based on Greedy Algorithm: Step 1: Add Item A completely (10 kg, value ₱60). 1.Remaining Capacity: 50−10 = 40 kg 2.Total Value: ₱60 Step 2: Add Item B completely (20 kg, value ₱100). 3.Remaining Capacity: 40−20 = 20 kg 4.Total Value: ₱60 + ₱100 = ₱160 Step 3: Add Item C completely (30 kg, value ₱120). 5.Item C cannot be added since only 20 kg remain. Step 4: Stop, as no other item can fit in the remaining capacity. •Total Value (Greedy Solution): ₱160 (Items A and B). Item Weight (Kg) Value (₱) Value-to-Weight Ratio A 10 60 6 B 20 100 5 C 30 120 4 D 40 150 3.75 Is it OPTIMAL solution?
  • 19. Optimal Solution: By exploring all combinations, we find a better solution by selecting Item D and Item A: 1. Add Item D completely (40 kg, value ₱150). 2. Add Item A completely (10 kg, value ₱60). Total Weight: 40 + 10= 50 kg Total Value (Optimal Solution): ₱150 + ₱60= ₱210 Item Weight (Kg) Value (₱) Value-to-Weight Ratio A 10 60 6 B 20 100 5 C 30 120 4 D 40 150 3.75
  • 20. Problem 2: The goal is to maximize the total value of items in the knapsack. • You can take fractions of items if needed (i.e., this is the fractional knapsack problem). You have a knapsack that can hold up to 50 kg. You are given the following items with their weights and values: Item Weight (Kg) Value (₱) A 10 60 B 20 100 C 30 120
  • 21. Solution Using Greedy Algorithm: In the fractional knapsack problem, the greedy approach selects items based on the highest value-to-weight ratio. 1.Calculate Value-to-Weight Ratios for each item: 1. Item A: 60/10 = 6 2. Item B: 100/20 = 5 3. Item C: 120/30 = 4 2.Sort Items by Value-to-Weight Ratio (from highest to lowest): 1.Item A (6) 2.Item B (5) 3.Item C (4)
  • 22. Solution Using Greedy Algorithm: 3. Fill the Knapsack by adding items in order of the highest value-to-weight ratio until the capacity is reached: 1.Step 1: Add Item A completely (10 kg, value ₱60). 1.Remaining Capacity: 50−10 = 40 kg 2.Total Value: ₱60 2.Step 2: Add Item B completely (20 kg, value ₱100). 1.Remaining Capacity: 40−20 = 20 kg 2.Total Value: ₱60 + ₱100 = ₱160 3.Step 3: Add Item C partially, taking only 20 kg out of 30 kg. 1.Value from Item C: (120/30) × 20 = 80 2.Total Value: ₱160 + ₱80 = ₱240 Item Weight (Kg) Value (₱) A 10 60 B 20 100 C 30 120
  • 23. Solution Using Greedy Algorithm: Final Answer: Maximum Value in Knapsack: ₱240 Items Selected: Item A: Entire (10 kg, value ₱60) Item B: Entire (20 kg, value ₱100) Item C: Partial (20 kg, value ₱80) Using the greedy approach based on the highest value-to-weight ratio, we achieve a maximum total value of ₱240 within the 50 kg weight limit of the knapsack. Item Weight (Kg) Value (₱) A 10 60 B 20 100 C 30 120
  • 24. Kruskal's Algorithm Sort Edges Sort the edges of the graph in ascending order by weight. Union-Find Use a Union-Find data structure to keep track of connected components. Add Edges Iteratively add the lightest edge that doesn't create a cycle, until all vertices are connected.
  • 25. Conclusion and Recap 1 Simplicity Greedy algorithms are straightforward and easy to implement, making them a popular choice for many problems. 2 Efficiency Greedy algorithms are often efficient and can produce good results in a timely manner. 3 Limitations Greedy algorithms may not always find the global optimal solution, and can be prone to getting stuck in local optima.