Greedy Algorithms-Complexity of Algorithms-Assignment Solution, Exercises of Advanced Algorithms

This assignment was submitted to Prof. Madhulata Nirmal at Chhattisgarh Swami Vivekanand Technical University for Theory of Complexity and Algorithms course. It includes: Greedy, Algorithms, Value, Index, Table, Fractional, Knapsack, Problem, Interval, Scheduling, Finish, Times

Typology: Exercises

2011/2012

Uploaded on 07/17/2012

toshi-sabri
toshi-sabri 🇮🇳

4.4

(20)

44 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Greedy algorithms
1. Consider the set of items S={a, b, c, d, e, f, g, h}where the benefits
and weights are given in the table below. Note that I have computed
the value index of each item (benefit/weight) and that is the last col-
umn in the table.
item benefit weight vi
a 14 3 42
3
b 5 1 5
c 10 6 1 2
3
d 12 4 3
e 8 2 4
f 10 4 21
2
g 16 8 2
h 9 9 1
Solve the Fractional Knapsack Problem for this set of items, where the
maximum total allowed weight is Wmax = 15.
Using the value indices, we select the items in order b,a, e, d, f , g, c, h
until the knapsack is full.
Then we
Pick item b. Total benefit (so far) = 5. Total weight (so far) = 1.
Pick item a. total benefit = 19. Total weight = 4.
Pick item e. Total benefit = 27. Total weight = 6.
Pick item d. Total benefit = 39. Total weight = 10.
Pick item f. Total benefit = 49. Total weight = 14.
Pick 1
8of item g. Total benefit = 49 + 1
8(16) = 51. Total weight =
14 + 1
8(8) = 15.
Therefore, our solution is to take items b, a, e, d, f, 1
8gfor a total benefit
of 51.
2. Let T={(1,3),(2,4),(3,5),(2,7),(4,6),(5,6),(3,7),(5,8),(6,10),(7,9),(8,10)}
denote the (start,finish) times for a collection of 11 tasks.
(a) Solve the Interval Scheduling Problem for this collection of tasks
(i.e. find the maximum number of tasks that can be scheduled on
a single machine, and give a set of compatible tasks that achieves
this maximum).
Solution: For the Interval Scheduling Problem, we first sort the
intervals by their finish times.
Note that I have numbered the tasks in order of their finish time.
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Greedy Algorithms-Complexity of Algorithms-Assignment Solution and more Exercises Advanced Algorithms in PDF only on Docsity!

Greedy algorithms

  1. Consider the set of items S = {a, b, c, d, e, f, g, h} where the benefits and weights are given in the table below. Note that I have computed the value index of each item (benefit/weight) and that is the last col- umn in the table. item benefit weight vi a (^14 3 4 ) b 5 1 5 c (^10 6 1 ) d 12 4 3 e 8 2 4 f (^10 4 2 ) g 16 8 2 h 9 9 1

Solve the Fractional Knapsack Problem for this set of items, where the maximum total allowed weight is Wmax = 15.

Using the value indices, we select the items in order b, a, e, d, f, g, c, h until the knapsack is full. Then we Pick item b. Total benefit (so far) = 5. Total weight (so far) = 1. Pick item a. total benefit = 19. Total weight = 4. Pick item e. Total benefit = 27. Total weight = 6. Pick item d. Total benefit = 39. Total weight = 10. Pick item f. Total benefit = 49. Total weight = 14. Pick 18 of item g. Total benefit = 49 + 18 (16) = 51. Total weight = 14 + 18 (8) = 15. Therefore, our solution is to take items b, a, e, d, f, 18 g for a total benefit of 51.

  1. Let T = {(1, 3), (2, 4), (3, 5), (2, 7), (4, 6), (5, 6), (3, 7), (5, 8), (6, 10), (7, 9), (8, 10)} denote the (start, finish) times for a collection of 11 tasks. (a) Solve the Interval Scheduling Problem for this collection of tasks (i.e. find the maximum number of tasks that can be scheduled on a single machine, and give a set of compatible tasks that achieves this maximum). Solution: For the Interval Scheduling Problem, we first sort the intervals by their finish times. Note that I have numbered the tasks in order of their finish time.

j sj fj 1 1 3 2 2 4 3 3 5 4 4 6 5 5 6 6 2 7 7 3 7 8 5 8 9 7 9 10 6 10 11 8 10 Having done this sorting, then we schedule the tasks in order, omitting those that conflict with already scheduled tasks. So we schedule (1, 3), which means we can’t schedule (2, 4). Then we schedule (3, 5), which means we can’t schedule (4, 6), etc. In the end, the tasks we will schedule using this procedure will be (1, 3), (3, 5), (5, 6), (7, 9), for a total of four tasks we can schedule (which is the most we could schedule on a single machine). Note that (in this case) there is more than one way we could schedule four tasks, but we won’t be able to schedule five tasks on a single machine in any fashion.

(b) Solve the Task Scheduling Problem for this collection of tasks (i.e. find the minimum number of machines required to complete all tasks, and give a schedule for doing so). Solution: This is similar to the above problem, but this time we’ll sort the tasks in order by their start times. j sj fj 1 1 3 2 2 4 3 2 7 4 3 5 5 3 7 6 4 6 7 5 6 8 5 8 9 6 10 10 7 9 11 8 10

Using these observations above, the argument can be made more pre- cise and mathematically formal to show that this greedy algorithm will minimize the number of coins used.

  1. Give an example set of coins (i.e. specify their values) so that a greedy change making algorithm will not always use the minimum number of coins. Illustrate this by showing a value for which a greedy algorithm like that in the above question doesn’t give the minimum number of coins. (Assume that the smallest denomination of coins in your set has value 1, so that a solution always exists.)

Solution: Well, I’m not going to give any particular example here, but will leave it up to you to find your own. You can certainly give examples that consists of only three coins, namely values 1, x, y for some x and y. (The value 1 is included because of the condition I imposed.) Think about the observations made for an optimal solution in the previous problem, and see where they might go wrong if you have other values of coins.

  1. In the art gallery guarding problem we are given a line L that represents a long (straight) hallway in an art gallery. We are also given a set X = {x 1 , x 2 ,... , xn} of real numbers that represent locations where paintings are hung in the hallway. Suppose that a single guard can protect all the paintings within distance at most 1 of his or her position (on both sides). Design an algorithm for finding a placement of the guards that uses the minimum number of guards to guard all the paintings with positions in X.

Here the solution (I hope) is straightforward. First of all, without loss of generality (by sorting and renumbering if necessary) we will assume that x 1 < x 2 < x 3 < · · · < xn. If it’s not clear from the problem description, we’re assuming that we can place a guard at any real number (and they need not be placed at a particular painting, but can be placed in between paintings). With this in mind, we will place the first guard at the real number x 1 + 1. This guard will cover the painting at position x 1 , and possibly others. So we can find the smallest index j such that xj− 1 ≤ x 1 + 1 but x 1 + 1 < xj. (In other words, this first guard can watch paintings at positions x 1 ,... , xj− 1 , but not the one at position xj .)

This leaves us with a subproblem consisting of paintings xj ,... , xn for some j ≥ 2. Then we repeat the argument above. This will give us the minimum number of guards necessary to guard all the paintings.

(Note that if the guards can only be placed at the same positions of the paintings, then a similar argument can be used to find the minimum number of guards. How would this procedure be modified to do so?)