0% found this document useful (0 votes)
11 views

Lec5, Algorithm Analysis & Design, Greedy Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lec5, Algorithm Analysis & Design, Greedy Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Greedy Algorithms

Presented by Dr/Hossam Hawash


Greedy Algorithms
◼ Greedy algorithms are a class of algorithms that follow
the "greedy heuristic" approach, making locally optimal
choices at each step with the hope of finding a global
optimum.
◼ A greedy algorithm makes the best possible choice at
each step without reconsidering previous/next choices.
◼ They are generally used for optimization problems where
a solution is searched to maximizes or minimizes a
certain objective function.

2
Principles of Greedy Algorithms
◼ Locally Optimal Choices
• Greedy algorithms make decisions that seem best at each step,
without considering the overall problem.
◼ Lack of Backtracking
• Decisions made are irreversible; the algorithm doesn't reconsider
previous choices.
◼ Greedy Choice Property
• Selecting the best local choice should contribute to the global
optimal solution.
◼ Trade-offs
• While often efficient, greedy algorithms may not always
guarantee the most optimal solution.

3
Minimum number of Coins
◼ Input: Infinite set of the denominations, value.

◼ Output: minimum number of coins and/or


notes needed to make the change

4
Minimum number of Coins
◼ Input:

denominations

Value =50

5
Minimum number of Coins

6
Minimum number of Coins

7
Minimum number of Coins

8
Minimum number of Coins

9
Minimum number of Coins

10
Minimum number of Coins

11
Minimum number of Coins

12
Minimum number of Coins
◼ Follow the steps below to implement the idea:
• Sort the array of coins in decreasing order.
• Initialize Out vector as empty.
• Find the largest denomination that is smaller than remaining
value and while it is smaller than the value:
• Add found denomination to Out.

• Subtract value of found denomination from value.

• If amount becomes 0, then print Out.

13
Minimum number of Coins
◼ Given value=11 and denominations= {9, 6, 5, 1}, what is
the output of greedy algorithm?
◼ Out= { 9, 1,1}
◼ Optimal Out= {5, 6}

14
Find maximum weighted path in tree.

15
Find maximum weighted path in tree.

16
Find maximum weighted path in tree.

17
Activity Selection Problem
◼ Inputs:
◼ A set of activities, each represented by a start time 𝑠𝑖 and a finish
time 𝑓𝑖 .
◼ The start and finish times can be represented as pairs
𝑠1 , 𝑓1 , 𝑠2 , 𝑓2 , … , 𝑠𝑛 , 𝑓𝑛 .
❑ The activities are sorted by their finish times, i.e.,
𝑓1 ≤ 𝑓2 ≤ ⋯ ≤ 𝑓𝑛 .
◼ Output: A maximum-size subset of mutually compatible
activities, i.e., a subset where no two activities overlap in
time.

18
Activity Selection Problem
◼ Algorithmic Steps:
1. Sort Activities: Sort the activities by their finish times in non-
decreasing order. This step ensures that the activity with the earliest
finish time comes first.
2. Select First Activity: Select the first activity from the sorted list.
3. Start iterating from the second activity in the sorted list. For each
activity:
I. If the start time of the current activity is greater than or equal to
the finish time of the previously selected activity, select the
current activity.
II. Otherwise, discard the current activity since it overlaps with the
previously selected one.
4. Repeat: Repeat step 3 until all activities are processed.

19
Activity Selection Problem

20
Activity Selection Problem

21
Activity Selection Problem

22
Activity Selection Problem

23
Activity Selection Problem

24
Activity Selection Problem

25
Activity Selection Problem

26
Activity Selection Problem

27
Lab content

◼ Fractional Knapsack Problem

◼ Huffman Code Problem

You might also like