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

Lab08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Code: TT/P.

KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

SUBJECT: DESIGN AND ANALYSIS OF ALGORITHMS

CODE: 503040

Duration: 150 minutes

Allowed to use materials.

LAB 08: Greedy Technique

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.

1. An example of a Greedy algorithm

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.

The implementation in Python is presented as follows


1 def make_change(n, d):
2 """
3 gives change for a specific amount n with the least number of coins
4 of the denominations d1 > d2 > . . . > dm
5 input:
6 n(int) - amount of money to be taken
7 d(array of int) - array of coin denominations
8 output:

Thien Nguyen PAGE 1


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

9 coins(array of int) - array of coin numbers corresponding to d


10 """
11 m = len(d)
12 coins = [0 for _ in range(m)]
13 for i in range(m):
14 coins[i] = n // d[i]
15 n = n % d[i]
16 return coins
17
18 #testcase
19 d = [25, 10, 5, 1]
20 n = 48
21 print(make_change(n, d))
22 #output: [1, 2, 0, 3]

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:

(1, 4), (5, 7), (8, 11), (12, 14)

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.

2. Job Sequencing Problem with Deadlines

Thien Nguyen PAGE 2


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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

Thien Nguyen PAGE 3


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

Prim’s algorithm constructs a minimum spanning tree through a sequence of expanding


subtrees. The initial subtree in such a sequence consists of a single vertex selected
arbitrarily from the set V of the graph’s vertices. On each iteration, the algorithm expands
the current tree in the greedy manner by simply attaching to it the nearest vertex not in
that tree. (By the nearest vertex, we mean a vertex not in the tree connected to a vertex in
the tree by an edge of the smallest weight. Ties can be broken arbitrarily.) The algorithm
stops after all the graph’s vertices have been included in the tree being constructed. Since
the algorithm expands a tree by exactly one vertex on each of its iterations, the total
number of such iterations is n − 1, where n is the number of vertices in the graph. The
tree generated by the algorithm is obtained as the set of edges used for the tree
expansions.

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.

Thien Nguyen PAGE 4


Code: TT/P.KT&KĐCL/11/BM02E
FACULTY OF INFORMATION TECHNOLOGY
Issue time: 01
ACADEMIC YEAR: 20..... - 20......

Effect on date: 07/01/2019

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/

Thien Nguyen PAGE 5

You might also like