Wednesday, July 18, 2012

Finding Second Minimum element in an Array


How would you find minimum element in an array (non-repeating random numbers) ? Well, the solution is obvious, you pick first element and compare with the next and keep the lower of the two. Do this again by comparing result of first comparison with the third element. Repeat this process for the complete array and you got the answer. So, in all for n elements you would need to do n-1 comparisons to find the minimum element (Figure 1).

Ok, the solution for finding smallest element was obvious. What if we need to find the next smallest element in an array? Well, one simple solution would be to compare the first two to select minimum and second minimum. Then pick the next element, compare with the minimum (selected in previous step) and replace it if the next element if lower. If not, compare with the second min and replace if necessary. Repeat this over the array and we have the second minimum. This algorithm would require 2n -3 comparisons (one comparison for first two elements + 2 comparisons for remaining elements of array. So 1 + 2 (n -2) = 2n -3.
What about finding 3rd minimum? The above logic would require 3 + 3(n-3) or 3n -6 comparisons.
The next question is can we do better than 2n -3 for finding second element (likewise for 3rd element and so forth). Can we not just sort the array and then just pick the 2nd value off the sorted array? Well, unfortunately that would not help either as best sorting algorithm will take nlog(n) time which is definitely more than what it would take for finding first log(n) elements using above method anyway. Well, the good news is that, yes there is a way to do better than 2n comparisons, and that is what this article is about - finding 2nd minimum in an array in efficient manner.
To solve problem of this nature we can take clue from a specialized form of problem set that can be best solved by technique called Dynamic Programming. My previous post about finding Critical Path is another example where dynamic programming technique was used.

Tournament Algorithm 

For solving problem of finding minimum element in an array, we will use what can be termed tournament method. Imagine a tennis tournament with 'n' players. Each player is paired with another player and the winner advance to next round and loser goes home. So, after first round the field is reduced to half. So, to find the winner of the tournament a total log(n) rounds will be played, where n is the field size (Figure 2).

Finding Min Using Tournament Method
For our problem, in the first iteration we compare adjacent elements of the array and the lower of two values are selected to form another array half the size (half + 1 for odd number of elements). The process is repeated till we find the minimum value. The number of comparisons needed to find the minimum is still the same, as calculated below:
Total comparisons: n (1/2 + 1/4 + … ) = n
(This above is convergent geometric series which has generalized solution of form (a/1 – r), or for our case it would be ½ (1 – ½); which equates to value 1)
During the process of finding minimum value, the generated arrays (during successive) iterations are saved to form a two-dimensional array (recursion tree) with first row being the input array and subsequent rows as generated from above iterations to form reverse tree (top row with input array and the minimum element at the bottom – root element).
The reason why we went the tournament way (as opposed to serial comparison) is that we can leverage the reverse tree to find the second minimum value with log(n) comparisons (asymptotic), hence the solution represents marked improvements over 2n (ignoring constant) comparisons as required by trivial method.
Here is the logic to find second minimum value.
The logic is that at some point the minimum element (root element) must have knocked out the second minimum element. So, by backtracking from the root element (minimum) all the way to the top and checking all the adjacent elements to the root element (for each row) we can find the second minimum. The key point to note is that at any level (above root level), the adjacent elements can be obtained by the index of root element at this level. Therefore, you don't need to scan complete sub-array (of recursion tree at any level). The adjacent elements for n-1 level is obtained as follows:
Adjacent Left (n-1 level) : 2 \* (root index for nth level)
Adjacent Right(n-1 level): 2 \* (root index for nth level) + 1

Therefore, for each row of the recursion tree, you just need to perform two comparisons to find the root element index to obtain the adjacent elements of row above. Refer to Figure 3 below. One of the elements marked in green box must be second minimum.

Finding 2nd Minimum using optimized back-tracking
So, how many comparisons we make using this method. Let’s calculate (ignoring constants): 
Comparisons for finding minimum: n
Comparisons for finding 2nd minimum: log(n)
Total: n + log(n)
Thus this method offers marked improvement over crude method.

Code 

Here is complete program:

 

/**
 * Copyright (c) 2010-2020 Malkit S. Bhasin. All rights reserved.
 * 
 * All source code and material on this Blog site is the copyright of Malkit S.
 * Bhasin, 2010 and is protected under copyright laws of the United States. This
 * source code may not be hosted on any other site without my express, prior,
 * written permission. Application to host any of the material elsewhere can be
 * made by contacting me at mbhasin at gmail dot com
 * 
 * I have made every effort and taken great care in making sure that the source
 * code and other content included on my web site is technically accurate, but I
 * disclaim any and all responsibility for any loss, damage or destruction of
 * data or any other property which may arise from relying on it. I will in no
 * case be liable for any monetary damages arising from such loss, damage or
 * destruction.
 * 
 * I further grant you ("Licensee") a non-exclusive, royalty free, license to
 * use, modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software;
 * 
 * As with any code, ensure to test this code in a development environment
 * before attempting to run it in production.
 * 
 * @author Malkit S. Bhasin
 * 
 */
public class SecondMinimum {

 public static int getSecondMinimumNonOptimized(int[] values) {
  int min = -1, secondMin = -1;
  int firstValue = values[0];
  int secondValue = values[1];
  if (firstValue < secondValue) {
   min = firstValue;
   secondMin = secondValue;
  } else {
   min = secondValue;
   secondMin = firstValue;
  }
  int nextElement = -1;
  for (int i = 2; i < values.length; i++) {
   nextElement = values[i];
   if (nextElement < min) {
    secondMin = min;
    min = nextElement;
   } else if (nextElement < secondMin) {
    secondMin = nextElement;
   }
  }
  return secondMin;
 }

 /**
  * Takes an input array and generated a two-dimensional array whose rows are
  * generated by comparing adjacent elements and selecting minimum of two
  * elements. Thus the output is inverse triangle (root at bottom)
  * 
  * @param values
  * @return
  */
 public static int[][] getOutputTree(int[] values) {
  Integer size = new Integer(values.length);
  double treeDepth = Math.log(size.doubleValue()) / Math.log(2);
  int intTreeDepth = getIntValue(Math.ceil(treeDepth)) + 1;
  int[][] outputTree = new int[intTreeDepth][];

  // first row is the input
  outputTree[0] = values;
  printRow(outputTree[0]);

  int[] currentRow = values;
  int[] nextRow = null;
  for (int i = 1; i < intTreeDepth; i++) {
   nextRow = getNextRow(currentRow);
   outputTree[i] = nextRow;
   currentRow = nextRow;
   printRow(outputTree[i]);
  }
  return outputTree;
 }

 /**
  * Compares adjacent elements (starting from index 0), and construct a new
  * array with elements that are smaller of the adjacent elements.
  * 
  * For even sized input, the resulting array is half the size, for odd size
  * array, it is half + 1.
  * 
  * @param values
  * @return
  */
 private static int[] getNextRow(int[] values) {
  int rowSize = getNextRowSize(values);
  int[] row = new int[rowSize];
  int i = 0;
  for (int j = 0; j < values.length; j++) {
   if (j == (values.length - 1)) {
    // this is the case where there are odd
    // number of elements
    // in the array. Hence the last loop will
    // have only one element.
    row[i++] = values[j];
   } else {
    row[i++] = getMin(values[j], values[++j]);
   }
  }
  return row;
 }

 /**
  * The logic for finding second minimum is as follows:
  * 
  * Starting from root element (which is minimum element), find the lower of
  * two adjacent element one row above. One of the two element must be root
  * element. If the root element is left adjacent, the root index (for one
  * row above) is two times the root index of any row. For right-adjacent, it
  * is two times plus one. Select the other element (of two adjacent
  * elements) as second minimum.
  * 
  * Then move to one row further up and find elements adjacent to lowest
  * element, again, one of the element must be root element (again, depending
  * upon the fact that it is left or right adjacent, you can derive the root
  * index for this row). Compare the other element with the second least
  * selected in previous step, select the lower of the two and update the
  * second lowest with this value.
  * 
  * Continue this till you exhaust all the rows of the tree.
  * 
  * @param values
  * @return
  */
 public static int getSecondMinimum(int[][] tree, int rootElement) {
  int adjacentleft = -1, adjacentRight = -1;
  int secondLeast = Integer.MAX_VALUE;
  int rootIndex = 0;
  int[] rowAbove = null;
  // we have to scan in reverse order
  for (int i = tree.length - 1; i > 0; i--) {
   // one row above
   rowAbove = tree[i - 1];
   adjacentleft = rowAbove[rootIndex * 2];

   // the root element could be the last element
   // carried from row above because of odd number
   // of elements in array,
   // you need to do following check. if you don't,
   // this case will blow {8,
   // 4, 5, 6, 1, 2}
   if (rowAbove.length >= ((rootIndex * 2 + 1) + 1)) {
    adjacentRight = rowAbove[rootIndex * 2 + 1];
   } else {
    adjacentRight = -1;
   }

   // if there is no right adjacent value, then
   // adjacent left must be root
   // continue the loop.
   if (adjacentRight == -1) {
    // just checking for error condition
    if (adjacentleft != rootElement) {
     throw new RuntimeException(
       "This is error condition. Since there "
         + " is only one adjacent element (last element), "
         + " it must be root element");
    } else {
     rootIndex = rootIndex * 2;
     continue;
    }
   }

   // one of the adjacent number must be root (min
   // value). Get the other number and compared
   // with second min so far
   if (adjacentleft == rootElement && adjacentRight != rootElement) {
    secondLeast = getMin(secondLeast, adjacentRight);
    rootIndex = rootIndex * 2;
   } else if (adjacentleft != rootElement
     && adjacentRight == rootElement) {
    secondLeast = getMin(secondLeast, adjacentleft);
    rootIndex = rootIndex * 2 + 1;
   } else {
    throw new RuntimeException(
      "This is error condition. One of the adjacent "
        + "elements must be root element");
   }
  }

  return secondLeast;
 }

 /**
  * Returns minimum of two passed in values.
  * 
  * @param num1
  * @param num2
  * @return
  */
 private static int getMin(int num1, int num2) {
  return Math.min(num1, num2);
 }

 /**
  * following uses Math.ceil(double) to round to upper integer value..since
  * this function takes double value, diving an int by double results in
  * double.
  * 
  * Another way of achieving this is for number x divided by n would be -
  * (x+n-1)/n
  * 
  * @param values
  * @return
  */
 private static int getNextRowSize(int[] values) {
  double size = Math.ceil(values.length / 2.0);
  return getIntValue(size);
 }

 private static int getIntValue(double value) {
  return new Double(value).intValue();
 }

 /**
  * Returns the root element of the two-dimensional array.
  * 
  * @param tree
  * @return
  */
 public static int getRootElement(int[][] tree) {
  int depth = tree.length;
  return tree[depth - 1][0];
 }

 private static void printRow(int[] values) {
  for (int i : values) {
   // System.out.print(i + " ");
  }
  // System.out.println(" ");
 }

 public static void main(String args[]) {
  int[] values = { 2, 4, 5, 3, 1, 8, 7, 10 };
  // Get Second Minimum (Non-Optimized)
  System.out.println("Second Minimum (using unoptimized algorithm): "
    + getSecondMinimumNonOptimized(values));

  // Get Tree and obtain the Minimum Element in the
  // array
  int[][] outputTree = getOutputTree(values);
  int min = getRootElement(outputTree);

  // Get Second Minimum (Optimized)
  System.out.println("Second Minimum (Using optimized algorithm): "
    + getSecondMinimum(outputTree, min));
 }
}

No comments: