Asst2 Students
Asst2 Students
1
COMP3121/9101 23T2 — Assignment 2 (UNSW Sydney)
Question 1
1.1 [12 marks] You are preparing to cross the Grand Canyon on a tightrope, and are building
your balance pole out of steel rods. Naturally, you want to do this as cheaply as possible.
You have made a trip to Bunnings, and picked up n rods, the ith of which has length ℓi . You need
to weld these all together to create your balance pole. Welding rods x and y costs ℓx + ℓy dollars,
and results in a new rod with length ℓx + ℓy .
For example, if we have rods with lengths [2, 1, 3]:
• After welding rods 2 and 3, costing $4, we have rods with lengths [2, 4]
• We weld the remaining two rods, costing $6
• The total cost to create the pole is $11
Note that this is not necessarily the optimal sequence for these lengths.
Design an O(n log n) algorithm which finds the order you should weld the rods to minimize the
total cost of welding.
1.2 [8 marks] Unfortunately, half way across the canyon, you dropped your pole. Whoops!
As it tumbles into the abyss, you remember that you have brought spare rods and a welding device.
As in part 1, you have n rods, the ith of which has length ℓi . To ensure that you don’t fall down
while you create a new balance pole, you need to keep the left and right halves of the pole as close
in length as possible while you make it.
Initially, the left and right halves are empty, and at each step, you must:
• Choose a remaining rod to add to your pole, and
• Decide whether you will weld it to the left or the right half.
After adding the ith rod to your pole, the left and right halves have lengths
Pn Li and Ri respectively.
We wish to minimise the sum of the imbalance after each step, i.e. i=1 |Li − Ri |.
Question 2
Sam has recently acquired Friendbook, a large social media conglomerate, under dubious circum-
stances. Since their position as the company’s CEO is only probational, they wish to curry favour
with the employees by providing raises. However, they don’t want to give raises to those who
aren’t well-known in the company (that would be a waste), and they can’t give raises to those
who are too well-known (that would raise suspicions). As such, they want to figure out the largest
2
COMP3121/9101 23T2 — Assignment 2 (UNSW Sydney)
number of people in the company they can give a raise to, without wasting money, and without
being caught.
Thankfully for Sam, the company keeps an absurd amount of data on its employees, so they
have access to an undirected graph G of all n company employees and who they know within the
company. In particular, this graph is provided as an adjacency matrix M such that M [i, j] = 1
if and only if employee i and j know each other. An induced subgraph H of G is a graph
formed from a subset of the vertices of G, where pairs of vertices in H are adjacent if and only if
the corresponding vertices are adjacent in G. In other words, an induced subgraph is formed by
deleting some (or maybe none) of the vertices of G, and all adjacent edges.
Sam wishes to find the largest possible induced subgraph H of G such that every vertex in H is
adjacent to at least k other vertices of H, but is also not adjacent to at least k other vertices of
H. Such a subgraph is called “maximally nepotistic”.
h b
j a
f d
g e
i c
With k = 3, explain why any maximally nepotistic subgraph of G cannot include the vertex
labelled x.
2.2 [2 marks] Using the graph G defined in 2.1, and again with k = 3, find a maximally nepotistic
subgraph H of G, and explicitly list the vertices of H.
2.3 [16 marks] Given a graph G with n vertices and adjacency matrix M , and integer k ≥ 1,
design an algorithm which finds a maximally nepotistic subgraph of G, if it exists, and lists its
vertices. If no such subgraph exists, the algorithm should not output anything. Your algorithm
must run in time O(n3 ).
Question 3
3.1 [10 marks] Santa’s assistant has been laid off and it is now your job to ensure that as many
wrapped presents get delivered to shopping centres as possible.
Santa has an infinite supply of unwrapped presents at the North Pole, and can distribute them
to k workshops across the globe. At each workshop i, a maximum of ai presents can be wrapped,
and the North Pole can distribute to it at most di presents.
The workshops wrap the presents, and then deliver these to m shopping centres. Each shopping
centre j can hold up to bj packages, however, due to some legacy contractual agreements, each
shopping centre can only receive presents from certain workshops. You are given the contractual
agreements as a two dimensional array C, where C[i][j] denotes that workshop i has an agreement
with shopping centre j.
Design an algorithm that runs in O((k + m)k 2 m2 ) time which finds the maximum number of
wrapped presents that can be delivered to shopping centres.
3
COMP3121/9101 23T2 — Assignment 2 (UNSW Sydney)
3.2 [6 marks] On Christmas Eve, you realise that you should ensure that each of the n children
on Santa’s nice list receive at least one present. Children are willing to travel at most D distance
to a shopping centre to receive a present. You are given the location of all children and shopping
centres.
Design an algorithm that runs in O(nm(k + m)) time which finds the maximum number of children
on the nice list who can receive at least one present.
3.3 [4 marks] Let the farthest distance from any child to a shopping centre be F .
Design an algorithm that runs in O(nm(k + m) log F ) time which finds the minimum value of D
such that all n children on the nice list receive at least one present, or returns that no such D is
possible.