Beispiel #1
0
 public function testAuxiliaryFunctions()
 {
     $this->specify("Get the minimum distance between two data array", function () {
         $data = getDataWithMinimumDistance(array(1, 8, 9), array(3, 8, 11));
         expect($data[0])->equals(1);
         expect($data[1])->equals(8);
         expect($data[2])->equals(9);
     });
     $this->specify("Get the distance between two values", function () {
         $distance = getDistanceBetweenTwoValues(88, 98);
         expect($distance)->equals(10);
     });
     $this->specify("Get the closest values by brute force algorithm", function () {
         $data = getClosestPairBybruteForce(array(1, 8, 9), 3);
         expect($data[0])->equals(1);
         expect($data[1])->equals(8);
         expect($data[2])->equals(9);
     });
     $this->specify("Get the closest values in split array by brute force algorithm", function () {
         $data = getClosestPairInSplitArrayByBruteForce(array(3, 4, 10, 20, 40), 5, array(20, 10, 30));
         expect($data[0])->equals(1);
         expect($data[1])->equals(3);
         expect($data[2])->equals(4);
     });
 }
Beispiel #2
0
/**
 * Get the closest values in split array by divide and conquer algorithm with brute force algorithm
 * Data[distance, value 1, value 2]
 * @param  [Array] $arraySplit      [Array input]
 * @param  [Number] $arraySplitSize [Array input length]
 * @param  [Array] $minData         [Data with minimum distance for the moment]
 * @return [Array]                  [Data with minimum distance]
 */
function getClosestPairInSplitArrayByBruteForce($arraySplit, $arraySplitSize, $minData)
{
    $minDistance = $minData[0];
    $value1 = $minData[1];
    $value2 = $minData[2];
    for ($i = 0; $i < $arraySplitSize; ++$i) {
        for ($j = $i + 1; $j < $arraySplitSize && $arraySplit[$j] - $arraySplit[$i] < $minDistance; ++$j) {
            $distanceBetweenTwoValues = getDistanceBetweenTwoValues($arraySplit[$i], $arraySplit[$j]);
            if ($distanceBetweenTwoValues < $minDistance && $distanceBetweenTwoValues > 0) {
                $minDistance = $distanceBetweenTwoValues;
                $value1 = $arraySplit[$i];
                $value2 = $arraySplit[$j];
            }
        }
    }
    return [$minDistance, $value1, $value2];
}