Professional Documents
Culture Documents
Algorithms Assignment 4: July 9, 2020
Algorithms Assignment 4: July 9, 2020
Algorithms Assignment 4: July 9, 2020
July 9, 2020
1 Question 1
Background: The optimal pairing order will minimize the loss occurring by
pairing a bad programmer with a good one while maximizing the gain by pairing
a good programmer with one similar in skill to ensure that the better one is
not wasted. This is done by ensuring that the worst programmers are paired
with each other in order to ensure that they do not ”drag down” the better
programmers
Algorithm 1: Pair up programmers
1 Function pairProgrammers(P[] ):
2 Sort P with mergeSort
3 Print P in groups of 2
Algorithm 1: This algorithm simply pairs each programmer with the next
most skilled one. The worst will be paired with the second worst and so on.
Mergesort is O(n log n) (see powerpoint from unit 1) and printing the list is
O(n) so the overall complexity will be O(n log n)
1
2 Question 2
Background: Imagining a tree with only three nodes (one root and two chil-
dren) If there was any skew, it would be fixed by adding the difference in lengths
to the shorter path. If it is known that the tree lacks skew elsewhere, that is the
correct action to take for a subtree of only three nodes. For some vertex in the
tree, the optimal solution for the subtree rooted at that node will neither add a
nonzero value to both edges ”out” of that vertex, nor increase the path length
to every leaf. The first property of the optimal solution is true because if adding
nonzero values to each edge would fix the skew, so would adding the difference
of those two values while it would also keep maintain a lower length, making
it optimal. The second property is true because if every path was increased by
a nonzero length, normalizing all corrections to zero (subtracting the shortest
correction) would fix the skew while also being more optimal.
Algorithm 2: find maximum distance from vertex to any leaf
1 Function dist(T ∈ V ):
2 if T = null then
3 return 0
4 Dl = dist(Tl ) + El
5 Dr = dist(Tr ) + Er
6 return max(Dl , Dr )
Algorithm 2: This algorithm recursively moves through the tree and fixes the
skew at each level. For every vertex T, it will compute the maximum distance
from each child node to all leaves from that child node. If these are not equal,
the difference will be added to the one which is lacking. If these are equal, the
difference will be 0 so both edges will correctly stay the same. The algorithm is
O(n) because constant work is done at each level before recursing on half of the
2
data twice. The master theorem therefore states that the overall complexity is
O(n)
Proof: Considering any other solution which fixes the skew but is unique from
the one produced in the algorithm above, there is some edge El and its partner
Er which come from vertex V. El is an edge where the solution given differs
from the one produced by the above algorithm. Cl , the corrective value added
to El is either greater or less than would be produced by the above algorithm.
However, if Cl is greater than produced by the algorithm, it must also be the
case that Cr is greater than produced by the algorithm because the difference
between the two maximum path lengths must still be 0 and the above algorithm
adds the minimum corrective value. This violates the principle established in
the background that no nonzero values should be added to both edges. If Cl
is less than the one produced by the above algorithm, the cost will need to be
”made up” lower in the tree. This will require adding nonzero values to all
children of those nodes which will again violate the principles laid out in the
background. Therefore this algorithm produces an optimal solution as deviation
from it will be the same or worse in cost.