



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to Assignment 3 [Divide and Conquer, Dynamic Programming, etc]
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




As a first step go to the last page and read the section: Advice on how to do the home work.
For each of the following problems indicate which algorithmic approach (Greedy, Divide-and-conquer, Dynamic programming, Max-flow) you would use to at- tempt to solve the problem. You do not have to give an algorithm.
Answer: 2 marks for answer given, or other answer with a reason- able justification. 1 mark for other answer with a plausible justifi- cation
(a) In an n×n array, a peak is an element that is greater than its four neighbours (left, right, top and bottom). Peaks can also lie on edges or corners, they are greater than their three or two (respectively) neighbours. There can be more than one peak in an array. Come up with an algorithm that efficiently finds a peak in an n × n array.
Answer: Divide and conquer.
(b) A computer network is a graph where the vertices are computers and the (undirected) edges indicate a physical network link between two computers. A computer sends a message to another computer on the network by passing it along the network links until it reaches its destination. Find an algorithm that can determine if an eavesdropper that has the ability to monitor mes- sages on up to k network links can intercept every message sent from s to t.
Answer: Max-flow.
(c) You are in charge of assigning planes to gates at an airport. After a plane lands, it needs to be assigned to a gate, where it must remain until it takes off. Given a list of plane arrival and departure times, come up with an algorithm that assigns planes to gates in an efficient manner.
Answer: Greedy.
(d) Your small town has grown large enough that it has to be divided into suburbs to efficiently allocate resources (e.g. electricity, water, school zones). Come up with an algorithm that assigns households into five “geographically close” suburbs – i.e. divide houses into five groups so that the minimum distance between houses in different suburbs is maximized.
Answer: Greedy.
(e) Come up with an algorithm for determining how many ways you can make a total t by summing k numbers (possibly repeated) from a set S.
Answer: Dynamic Programming.
Give a detailed algorithm for one of the problems in Question 1. Make sure to include:
Answer:
(c) Some slight ambiguity on what “efficient” means. The natu- ral assumption is that the aim is to minimize the number of allocated gates. Under that assumption, this is a direct appli- cation of the Interval Partitioning problem. Running time is O(n log n): the time it takes to sort the planes by arrival times.
(d) The states of a Dynamic Programming solution could be C[i, j, u] where 0 ≤ i ≤ |S|, 0 ≤ j ≤ k, and 0 ≤ u ≤ t. In- tuitively C[i, j, u] counts the number of ways of using j of the first i elements of S to form a sum of u. The idea is that we want to compute C[|S|, k, t]. The recurrence would be based on the observation that the number of ways of using j of the first i elements of S to sum to u can be counted by adding the number of ways of using j of the first i − 1 elements of S to sum to u, together with the number of ways of using j − 1 of the first i elements of S to sum to u − si. More succinctly:
C[i, j, u] = C[i − 1 , j, u] + C[i, j − 1 , u − si].
There are O(|S|kt) states, and, based on the above recurrence it takes O(1) time to compute each state, so the running time is O(|S|kt).
For each of the following problems, decide if the provided answer correctly solves the problem. If it does then analyse the running-time of the algorithm. If it does not, give an example demonstrating why not.
(a) Problem: In a weighted directed graph, find the longest simple (i.e. no repeated vertices) path between two vertices s and t. Solution: Multiply all edges by −1 and, using the Bellman-Ford algo- rithm, find the shortest path between s and t in the new graph. This will be the longest path in the original graph.
Answer: This is not correct. Consider the following graph:
The loop becomes a negative loop after the transformation, and so Bellman-Ford will terminate without finding a shortest path. In this case it can be resolved, but that is not guaranteed.
(b) Problem: In a connected weighted graph with distinct edge weights, find the spanning tree with maximum weight. Solution: Order the edges by weight from smallest to largest. Remove edges following this order, as long as removing an edge does not dis- connect the graph. When all edges have been processed, the remaining edges form the maximum spanning tree.
Answer: This is correct. The algorithm describes the reverse- delete algorithm for finding an MST on the graph when we multiply the edges by −1. Therefore the resulting graph will be a spanning tree of maximal weight under the original weight function. Running time: O(m log m) to sort the edges, O(m + n) time to check con- nectivity after each edge deletion, giving an O(m(m + n)) algorithm [better bounds may be possible].
(c) Problem: In a binary tree, find the size of the smallest subset of vertices V ′^ such that every edge has at least one endpoint in V ′. Solution: Using dynamic programming. Define M [v, i] for every vertex v of the tree and i ∈ { 0 , 1 } as follows:
Answer: This is correct. We are looking for a vertex cover of the binary tree. Intuitively M [v, 0] is the size of the smallest vertex cover of the subtree rooted at v where v is not in the cover; and M [v, 1] is the size of the smallest vertex cover when v is in the cover. There are O(2|V |) = O(n) states and it takes O(1) time to compute each state, yielding an O(n)-time algorithm for the problem.
(d) Problem: Given n people, n jobs, and a table of “rewards” for assigning people to jobs – i.e. R(i, j) is the reward for assigning person i to job j; find the maximum total reward that can be achieved by a matching of people to jobs (i.e. exactly one person per job). Solution: Use the table of rewards to set up a preference relation - e.g. Person i prefers job j 1 to j 2 if R(i, j 1 ) > R(i, j 2 ); and Job j prefers to be assigned to person i 1 over i 2 if R(i 1 , j) > R(i 2 , j). Run the Gale- Shapley algorithm to find a matching. Compute the reward for this matching. This will be the maximum reward.