Algorithms and Complexity 1 Module 8
Algorithms and Complexity 1 Module 8
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
On this module we will first discuss the term optimization problem and the proceed
to Greedy Algorithm. For better understanding we will discuss the structure of
Greedy Algorithm and explore some applications it.
A. Greedy Algorithm
A greedy algorithm is a simple, intuitive algorithm that is used in optimization
problems. The algorithm makes the optimal choice at each step as it attempts to
find the overall optimal way to solve the entire problem. Greedy algorithms are quite
successful in some problems, such as Huffman encoding which is used to compress
data, or Dijkstra's algorithm, which is used to find the shortest path through a
graph.
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 1 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
The illustration demonstrates the nature of Greedy Algorithm, we begin on the top
node and from there it will select the node with a higher value (since it is greedy),
thus it will proceed to node 12. From node 12 there are two options, node 5 and
node 6, since node 6 is the larger than 5 the greedy algorithm will select it. After the
selection the greedy algorithm will ends with the following nodes whose values are 7,
12, and 6. Summing up these values we will get 25 which is wrong as compared to
the correct solution, because the actual largest sum or path is 109. You can get 109
in selecting 7 then 3 and then 99.
If both of the properties below are true, a greedy algorithm can be used to solve the
problem.
In other words, greedy algorithms work on problems for which it is true that, at
every step, there is a choice that is optimal for the problem up to that step, and
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 2 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
after the last step, the algorithm produces the optimal solution of the complete
problem.
To make a greedy algorithm, identify an optimal substructure or subproblem in the
problem. Then, determine what the solution will include (for example, the largest
sum, the shortest path, etc.). Create some sort of iterative way to go through all of
the subproblems and build a solution.
Other references consider that there are five components on a greedy algorithm;
• A candidate set, from which a solution is created
• A selection function, which chooses the best candidate to be added to the
solution
• A feasibility function, that is used to determine if a candidate can be used to
contribute to a solution
• An objective function, which assigns a value to a solution, or a partial
solution, and
• A solution function, which will indicate when we have discovered a complete
solution
If a greedy algorithm can be proven to yield the global optimum for a given problem
class, it typically becomes the method of choice because it is faster than other
optimization methods like dynamic programming. Examples of such greedy
algorithms are Kruskal's algorithm and Prim's algorithm for finding minimum
spanning trees, and the algorithm for finding optimum Huffman trees.
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 3 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
The notion of a node's location (and hence "closeness") may be determined by its
physical location, as in geographic routing used by ad hoc networks. Location may
also be an entirely artificial construct as in small world routing and distributed
hash table.
Most networking algorithms use the greedy approach, here is the list of a few of
them;
• Travelling Salesman Problem
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Minimal Spanning Tree Algorithm
• Graph - Map Coloring
• Graph - Vertex Cover
• Knapsack Problem
• Job Scheduling Problem
Scheduling Problem
Let us consider this kind of problem and apply the “greedy algorithm” approach to
solve it. This is an interesting problem that you can encounter in almost any
industry or any walk of life. Some instances of the problem are as follows:
• You are given a set of N schedules of lectures for a single day at a university.
The schedule for a specific lecture is of the form (s time, f time) where s time
represents the start time for that lecture and similarly the f time represents
the finishing time. Given a list of N lecture schedules, we need to select
maximum set of lectures to be held out during the day such that none of the
lectures overlap with one another i.e. if lecture Li and Lj are included in our
selection then the start time of j >= finish time of i or vice versa .
• The plan is to send the contestants out in a staggered fashion, via the
following rule: the contestants must use the pool one at a time. In other
words, first one contestant swims the 20 laps, gets out, and starts biking.
• As soon as this first person is out of the pool, a second contestant begins
swimming the 20 laps; as soon as he or she is out and starts biking, a third
contestant begins swimming, and so on.
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 4 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
the time projections are accurate. What is the best order for sending people
out, if one wants the whole competition to be over as soon as possible? More
precisely, give an efficient algorithm that produces a schedule whose
completion time is as small as possible.
• Earliest Start Time First i.e. select the interval that has the earliest
start time. Take a look at the following example that breaks this solution.
This solution failed because there could be an interval that starts very
early but that is very long. This means the next strategy that we could try
would be where we look at smaller intervals first.
• Smallest Interval First i.e. you end up selecting the lectures in order of
their overall interval which is nothing but their finish time - start time .
Again, this solution is not correct. Look at the following case;
You can clearly see that the shortest interval lecture is the one in the
middle, but that is not the optimal solution here. Let's look at yet
another solution for this problem deriving insights from this solution.
• Least Conflicting Interval First i.e. you should look at intervals that
cause the least number of conflicts. Yet again we have an example where
this approach fails to find an optimal solution.
The diagram shows us that the least confliciting interval is the one in the
middle with just 2 conflicts. After that we can only pick the two intervals
at the very ends with conflicts 3 each. But the optimal solution is to pick
the 4 intervals on the topmost level.
• Earliest Finishing time first. This is the approach that always gives us
the most optimal solution to this problem. We derived a lot of insights
from previous approaches and finally came upon this approach. We sort
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 5 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
The correct solution for the longest path through the graph is 7, 3, 1, 99. This is
clear to us because we can see that no other combination of nodes will come close to
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 6 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
a sum of 99, so whatever path we choose, we know it should have 99 in the path.
There is only one option that includes 99: 7, 3, 1, 99.
The greedy algorithm fails to solve this problem because it makes decisions purely
based on what the best answer at the time is: at each step it did choose the largest
number. However, since there could be some huge number that the algorithm
hasn't seen yet, it could end up selecting a path that does not include the huge
number. The solutions to the subproblems for finding the largest sum or longest
path do not necessarily appear in the solution to the total problem. The optimal
substructure and greedy choice properties don't hold in this type of problem.
G. Video Presentations
Attached to this module are four videos that discuss Greedy Algorithm and some of
its applications;
• Huffman Encoding
Link: https://www.youtube.com/watch?v=dM6us854Jk0
Author: CSBreakdown
• K Center Problem
Link: https://www.youtube.com/watch?v=dpYZojRuJEI
Author: CSBreakdown
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 7 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
V. LEARNING TASK
Holistic Rubric
Exceeding Meeting Approaching Below None
(10) (8) (6) (2) (0)
Substantial,
Content Clarity
specific, and/or
The presence of ideas Sufficient
illustrative
developed through developed Limited content Superficial
content No content
facts, examples, content with with inadequate and/or
demonstrating or answer
illustrations, details, adequate elaboration or minimal
strong provided.
opinions, statistics, elaboration or explanation content
development and
reasons, and/or explanation.
sophisticated
explanations.
ideas.
1. Given N white dots and N black dots, how would you connect them using
the least amount of wires with the implementation of Greedy Algorithm?
Assume that the wire connecting two adjacent dots is 1.
3. Give three (3) problems that cannot be solve by the Greedy Algorithm?
Explain why did you say that this approach is impossible for the said
problems.
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 8 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Score Sheet
Question 1 Question 2 Question 3 Total Score
VI. REFERENCES
E-Books
• Cormen, Thomas, etal. Introduction to Algorithms Third Edition,
Massachusetts Institute of Technology, 2009
Online Resources:
• https://en.wikipedia.org/wiki/Greedy_algorithm
• https://www.freecodecamp.org/news/what-is-a-greedy-algorithm/
• https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorit
hms.htm
• https://brilliant.org/wiki/greedy-algorithm/
• https://www.youtube.com/watch?v=ARvQcqJ_-NY
• https://www.youtube.com/watch?v=gdmfOwyQlcI
• https://www.youtube.com/watch?v=4ZlRH0eK-qQ
• https://www.youtube.com/watch?v=dpYZojRuJEI
Algorithm and Complexity I NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 9 of 9