Algorithms Assignment 4: July 9, 2020

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Algorithms Assignment 4

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)

Proof: If there are 2k programmers where k is an integer, If k = 1, the best


solution, and the one presented by the algorithm, is to pair the two programmers
together. As k increases, the optimal solution will be to pair two programmers
who are worse and two programmers are better. Assuming that the members
are i ≤ j ≤ k ≤ m, pairing i,j and k,m gives a total value of i+k. If paired
otherwise, the maximum value could only be i+j which is less than i+k. Thus,
the productivity does not increase. This is extensible to any number of pro-
grammers following this same logic, but not shown because as the number of
pairs increases the possible pairings increases in a factorial manner. That is
to say, it is possible that 3+ pairs could be disrupted by the exchange, but
this principle would still hold true by the same logic as shown above. When
the worse programmers are not paired together, they ”drag down” the sum of
the overall productivity. For any number of pairs generated by this algorithm,
exchanging pair members does not increase the productivity.

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 )

Function 1: This function recursively calculates the maximum distance from


any vertex to the max length node. It adds the max distance from the children
to the length to get to that child and returns the maximum of those two values.
Algorithm 3: fix skew of tree by adding edge weight
1 Function fixTreeSkew(T ∈ V ):
2 Dr = dist(Tr ) + Er
3 Dl = dist(Tl ) + El
4 if Dr > Dl then
5 El + = Dr − Dl
6 else
7 Er + = Dl − Dr
8 fixTreeSkew(Tl )
9 fixTreeSkew(Tr )

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.

You might also like