CS243: Algorithms and Data Structures - Of the Clux
CS243: Algorithms and Data Structures - Of the Clux
CS243: Algorithms and Data Structures - Of the Clux
Transform your PDFs into Flipbooks and boost your revenue!
Leverage SEO-optimized Flipbooks, powerful backlinks, and multimedia content to professionally showcase your products and significantly increase your reach.
<strong>CS243</strong>: <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong><br />
Problem sheet 2<br />
Due on Monday 2 February 2009 by 13:05<br />
Solve THREE of <strong>the</strong> following four problems. State clearly which three problems you have solved.<br />
1. Imagine that you have n music files (songs) F 1 , F 2 , . . . , F n which you want to store on a memory<br />
stick. For every i = 1, 2, . . . , n, <strong>the</strong> song F i has size s i , or in o<strong>the</strong>r words, it requires s i kilobytes<br />
of space. The capacity of <strong>the</strong> memory stick is C kilobytes. Unfortunately, not all of your songs<br />
can fit on <strong>the</strong> memory stick because ∑ n<br />
i=1 s i > C.<br />
(a) Suppose that you want to maximize <strong>the</strong> number of songs held on <strong>the</strong> memory stick.<br />
Consider <strong>the</strong> greedy algorithm which selects songs in order of increasing size. Ei<strong>the</strong>r<br />
prove that this algorithm is correct or provide a counter-example.<br />
(b) Suppose that you want to use as much of <strong>the</strong> capacity of <strong>the</strong> memory stick as possible.<br />
Consider <strong>the</strong> greedy algorithm which selects songs in order of decreasing size. Ei<strong>the</strong>r<br />
prove that this algorithm is correct or provide a counter-example.<br />
2. Recall that an input to <strong>the</strong> interval scheduling problem is a sequence s 1 , s 2 , . . . , s n of starting<br />
times, <strong>and</strong> a sequence f 1 , f 2 , . . . , f n of finishing times of jobs 1, 2, . . . , n, respectively. Consider<br />
<strong>the</strong> following greedy algorithm for <strong>the</strong> interval scheduling problem.<br />
Sort <strong>the</strong> jobs according to <strong>the</strong>ir starting times; after this step we have that:<br />
s 1 ≤ s 2 ≤ · · · ≤ s n .<br />
Start with an empty set of selected jobs. For every i = n, n − 1, . . . , 1 (in this order),<br />
add job i to <strong>the</strong> set of selected jobs unless it is incompatible (that is, it overlaps)<br />
with ano<strong>the</strong>r job which has been selected before.<br />
Prove that this algorithm is correct, i.e., that it always computes a set of compatible jobs of<br />
maximum size.<br />
3. Suppose that you are consulting for a company which wants to organize an annual party for<br />
its employees. You have agreed with <strong>the</strong> management that a good party is such that:<br />
• for every participant, <strong>the</strong>re are at least two o<strong>the</strong>r participants whom (s)he knows; <strong>and</strong><br />
• for every participant, <strong>the</strong>re are at least two o<strong>the</strong>r participants whom (s)he doesn’t know.<br />
We assume throughout, that <strong>the</strong> acquaintance relation is symmetric, that is, any two employees<br />
ei<strong>the</strong>r both know each o<strong>the</strong>r, or <strong>the</strong>y both don’t know each o<strong>the</strong>r.<br />
(a) Consider two groups of employees:<br />
• employees 1, 2, <strong>and</strong> 3 who all know each o<strong>the</strong>r; <strong>and</strong><br />
• employees 4, 5, <strong>and</strong> 6 who all know each o<strong>the</strong>r;<br />
1
<strong>and</strong> nobody in <strong>the</strong> first group knows anybody in <strong>the</strong> o<strong>the</strong>r group (<strong>and</strong> vice versa).<br />
Draw <strong>the</strong> (undirected) graph in which <strong>the</strong> vertices correspond to <strong>the</strong> six employees, <strong>and</strong><br />
<strong>the</strong>re is an edge between two of <strong>the</strong>m if <strong>and</strong> only if <strong>the</strong>y know each o<strong>the</strong>r.<br />
Argue that if you invite all six employees to <strong>the</strong> party <strong>the</strong>n it will be a good party.<br />
(b) Consider <strong>the</strong> six employees from problem (3a), as well as employees 7 <strong>and</strong> 8, such that:<br />
• employee 7 knows only employee 1; <strong>and</strong><br />
• <strong>the</strong> only people that employee 8 does not know are employees 6 <strong>and</strong> 7.<br />
Explain why inviting employee 7 to <strong>the</strong> party would prevent it from being a good party.<br />
Can a party to which employee 8 is invited be a good party Justify your answer.<br />
What is <strong>the</strong> size of <strong>the</strong> biggest possible good party Who should be invited to it<br />
(c) Design an efficient algorithm that—given an (undirected) graph G = (V, E) in which vertices<br />
correspond to employees, <strong>and</strong> edges join employees who know each o<strong>the</strong>r—computes<br />
<strong>the</strong> size of <strong>the</strong> largest possible good party, <strong>and</strong> lists <strong>the</strong> employees who should be invited<br />
to it.<br />
Argue that your algorithm is correct.<br />
What is <strong>the</strong> running time of your algorithm<br />
(d) Illustrate <strong>the</strong> behaviour of your algorithm on <strong>the</strong> example from problem (3b), that is,<br />
explain how your algorithm computes <strong>the</strong> list of employees who should be invited to <strong>the</strong><br />
biggest possible good party.<br />
4. Solve ONE of <strong>the</strong> following two problems (4a) or (4b). State clearly whe<strong>the</strong>r you have solved<br />
problem (4a) or (4b).<br />
(a) You are consulting for a trucking company that does a large amount of business shipping<br />
packages between Edinburgh <strong>and</strong> London. The volume is high enough that <strong>the</strong>y have<br />
to send a number of trucks each day between <strong>the</strong> two locations. Trucks have a fixed<br />
limit W on <strong>the</strong> maximum amount of weight <strong>the</strong>y are allowed to carry. Boxes arrive at <strong>the</strong><br />
Edinburgh station one by one, <strong>and</strong> each package i has a weight w i . The trucking station<br />
is quite small, so at most one truck can be at <strong>the</strong> station at any time. Company policy<br />
requires that boxes are shipped in <strong>the</strong> order <strong>the</strong>y arrive; o<strong>the</strong>rwise, a customer might get<br />
upset upon seeing a box that arrived after his make it to London faster. At <strong>the</strong> moment,<br />
<strong>the</strong> company is using a simple greedy algorithm for packing: <strong>the</strong>y pack boxes in <strong>the</strong> order<br />
<strong>the</strong>y arrive, <strong>and</strong> whenever <strong>the</strong> next box does not fit, <strong>the</strong>y send <strong>the</strong> truck on its way.<br />
But <strong>the</strong>y wonder if <strong>the</strong>y might be using too many trucks, <strong>and</strong> <strong>the</strong>y want your opinion on<br />
whe<strong>the</strong>r <strong>the</strong> situation can be improved. Here is how <strong>the</strong>y are thinking. Maybe one could<br />
decrease <strong>the</strong> number of trucks needed by sometimes sending off a truck that was less full,<br />
<strong>and</strong> in this way allow <strong>the</strong> next few trucks to be better packed.<br />
Prove that, for a given set of boxes with specified weights, <strong>the</strong> greedy algorithm currently<br />
in use actually minimizes <strong>the</strong> number of trucks that are needed. Your proof could follow<br />
<strong>the</strong> type of analysis that is used for <strong>the</strong> Interval Scheduling Problem in <strong>the</strong> Kleinberg<br />
<strong>and</strong> Tardos book: it should establish <strong>the</strong> optimality of this greedy packing algorithm by<br />
identifying a measure under which it “stays ahead” of all o<strong>the</strong>r solutions.<br />
(b) Let’s consider a long, quiet country road with houses scattered very sparsely along it.<br />
(We can picture <strong>the</strong> road as a long line segment, with an eastern endpoint <strong>and</strong> a western<br />
2
endpoint.) Fur<strong>the</strong>r, let’s suppose that <strong>the</strong> residents of all <strong>the</strong>se houses are avid mobile<br />
phone users. You want to place mobile phone base stations at certain points along <strong>the</strong><br />
road, so that every house is within four miles of one of <strong>the</strong> base stations.<br />
Give an efficient algorithm that achieves this goal using as few base stations as possible.<br />
3