Lab08
Lab08
KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......
CODE: 503040
I. Objectives
Understand the properties of Greedy Technique for algorithm design
Be able to design, implement, and analyze Greedy algorithms solving common problems.
II. Idea
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
feasible, i.e. satisfying the constraints
locally optimal (with respect to some neighborhood definition)
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.
Implement and analyze a Greedy algorithm to give change for a specific amount n
with the least number of coins of the denominations d1 > d2 > . . . > dm.
Analysis:
1/ Basic operation: assignment on line 14
2/ Worst case: as average case
3/Counting the number of basic operations in the worst case:
…
Time efficiency
T(m) = m Θ(m)
III. Exercises
For each problem in this part, implement (in Python) and analyze a greedy algorithm to solve the
problem.
Warm up
1. Activity Selection Problem
Given a set of activities, along with the starting and finishing time of each activity, find
the maximum number of activities performed by a single person assuming that a person
can only work on a single activity at a time.
For example,
Input: Following set of activities
(1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10), (8, 11), (8, 12), (2, 13), (12, 14)
Output:
Hint: The idea is to initially sort the activities in increasing order of their finish times and
create a set S to store the selected activities and initialize it with the first activity.
Given a list of tasks with deadlines and total profit earned on completing a task, find the
maximum profit earned by executing the tasks within the specified deadlines. Assume
that each task takes one unit of time (for example: from 0 to 1, from 1 to 2, from 2 to 3,
…) to complete, and a task can’t execute beyond its deadline. Also, only a single task
will be executed at a time.
For example, consider the following set of tasks with a deadline and the profit associated
with it. If we choose tasks 1, 3, 4, 5, 6, 7, 8, and 9, we can achieve a maximum profit of
109. Note that task 2 and task 10 are left out.
0 1 2 3 4 5 6 7 8 9
5(25) 6(20) 3(18) 1(15) 8(10)
Hint: The idea is simple – consider each task in decreasing order of their profits and
schedule it in the latest possible free slot that meets its deadline. If no such slot is there,
don’t schedule the task.
Intermediate exercises
The minimum spanning tree problem is the problem of finding a minimum spanning tree
for a given weighted connected graph.
3. Prim’s Algorithm
4. Kruskal’s Algorithm
The algorithm begins by sorting the graph’s edges in nondecreasing order of their
weights. Then, starting with the empty subgraph, it scans this sorted list, adding the next
edge on the list to the current subgraph if such an inclusion does not create a cycle and
simply skipping the edge otherwise.
References:
1) Introduction to The Design & Analysis of Algorithms, 3rd edition, ISBN 978-0-
13-231681-1 by Anany Levitin published by Pearson Education © 2012.
2) https://www.techiedelight.com/