A deterministic heuristic for NUMBER PARTITION is the Karmarkar-Karp algorithm. This KK algorithm uses differencing: repeatedly take the two largest elements from A, call them ai and aj , and replace the larger by |ai − aj | while replacing the smaller by zero, until there is only one non-zero element left. This final non-zero element corresponds to an achievable residue, but it may not be the best possible residue, that is why we refer to this as a heuristic. (Each difference operation corresponds to putting ai and aj in different sets: if they were weight 75 and 71, then it is as if we had just one element of weight 4 around.) For example, if A is initially (10, 8, 7, 6, 5), then the KK algorithm proceeds as follows:
(10, 8, 7, 6, 5) → (2, 0, 7, 6, 5) → (2, 0, 1, 0, 5) → (0, 0, 1, 0, 3) → (0, 0, 0, 0, 2).
Prove that there is a partition that achieves the residue computed by the KK algorithm. Then implement the KK algorithm (you need not compute the partition, just the residue), and justify your algorithm’s running time. You should strive to obtain an O(n log n) implementation.
Proof
This section gives the proof that there exists a partition that achieves the residue computed by the KK algorithm.
Consider each number as a node ( nodes can be used to construct trees). Whenever differencing operation happens between two numbers, an edge is added between two numbers. Let us look at the example given in the question and see if we can construct a tree. The algorithm repeatedly takes the difference between the largest two elements. In the example (10,8,7,6,5), first, the difference of 10 and 8 is taken and (10,8) is replaced by (2,0). So we add an edge between 10 and 8. Next, the difference of 7 and 6 is taken and (7,6) is replaced by (1,0). So we add an edge between 7 and 6. Next, the difference between 5 and 2 is taken and (2,5) is replaced by (0,3). Remember 2 was the difference between 10 and 8 which is at the position of 10. Hence we add an edge between 10 and 5. Next, we take the difference between 1 and 3. Remember 1 was the difference between 7 and 6 which is at the position of 7 and 3 was the difference between 2 and 5 which is at the position of 5. This adds an edge between 7 and 5. This forms a spanning tree as represented below:
8----10----5-----7----6
Each differencing operation adds an edge to the tree. If n is the number of elements or nodes, there are n-1 differencing operations resulting in n-1 edges. Repeated selection of highest numbers will eventually select all numbers and hence the tree is a spanning tree. Now we color the nodes such that no two connected nodes have the same color. Hence we get a two-color tree. We choose the nodes/elements of the same color to form the partition. For instance, in the above example, the partition is (8,5,6) and (10,7). This proves that there exists a partition which computes residue by KK algorithm
Implementation
Getmax function:
max=0, next_max =0
Loops over all elements of the array:
if any array element greater than max, max= array element, next_max= array element
max position = position of the element
else if any array element greater than next_max, next_max = array element
next_max position = position of the element
This function loops over all elements of the array and finds the two maximum elements and hence take O(n) time.
KK function:
while{
find max and next_max using Getmax function
if next_max =0:
then residue = max and break the loop
else residue = difference between max and next_max
element at next_max position is updated to 0
element at max psition is updated to residue
}
This function implements KK algorithm. The function will go back to Getmax funtion n-1 times. And hence the above implementation takes O(n^2) time. An alternative and more efficient approach is to :
Sort the numbers using merge sort -----> O(nlogn) time
Take the difference and each difference is added to the sorted order using a heap ----> O(nlogn) time
Look through the heap and select alternative elements to get the partition and residue ----> O(n) time
The total time complexity of this implementation is O(nlogn).
A deterministic heuristic for NUMBER PARTITION is the Karmarkar-Karp algorithm. This KK algorithm uses differencing: repeatedly...