Java Distance Calculate computeDistanceImpl(final String firstStringInAlgorithm, final String secondStringInAlgorithm)

Here you can find the source of computeDistanceImpl(final String firstStringInAlgorithm, final String secondStringInAlgorithm)

Description

Algorithm:
(terms: s = firstString.

License

BSD License

Exception

Parameter Description
RuntimeException illegal strings

Return

the distance

Declaration

private static long computeDistanceImpl(final String firstStringInAlgorithm,
        final String secondStringInAlgorithm) throws RuntimeException 

Method Source Code

//package com.java2s;
// Licensed under the terms of the New BSD License. Please see associated LICENSE file for terms.

public class Main {
    /**/*  w w w . jav a 2s .  c  o  m*/
     * Algorithm: <BR>
     * (terms: s = firstString. t = secondString)<BR>
     * 1<BR>
     * Set n to be the length of s.<BR>
     * Set m to be the length of t.<BR>
     * If n = 0, return m and exit.<BR>
     * If m = 0, return n and exit.<BR>
     * Construct a matrix containing 0..m rows and 0..n columns.<BR>
     * 2<BR>
     * Initialize the first row to 0..n.<BR>
     * Initialize the first column to 0..m.<BR>
     * 3<BR>
     * Examine each character of s (i from 1 to n)<BR>
     * 4<BR>
     * Examine each character of t (j from 1 to m)<BR>
     * 5<BR>
     * If s[i] equals t[j], the cost is 0.<BR>
     * If s[i] doesn't equal t[j], the cost is 1.<BR>
     * 6<BR>
     * Set cell d[i,j] of the matrix equal to the minimum of:<BR>
     * a. The cell immediately above plus 1: d[i-1,j] + 1.<BR>
     * b. The cell immediately to the left plus 1: d[i,j-1] + 1.<BR>
     * c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.<BR>
     * 7<BR>
     * After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].<BR>
     * 
     * @return the distance
     * @throws RuntimeException illegal strings
     */
    private static long computeDistanceImpl(final String firstStringInAlgorithm,
            final String secondStringInAlgorithm) throws RuntimeException {
        int firstStringLength = firstStringInAlgorithm.length();
        int secondStringLength = secondStringInAlgorithm.length();

        long matrix[][]; // matrix

        // Step 1
        if (firstStringLength == 0) {
            return secondStringLength;
        }
        if (secondStringLength == 0) {
            return firstStringLength;
        }
        matrix = new long[firstStringLength + 1][secondStringLength + 1];

        // Step 2
        for (int firstStringIndex = 0; firstStringIndex <= firstStringLength; firstStringIndex++) {
            matrix[firstStringIndex][0] = firstStringIndex;
        }

        for (int secondStringIndex = 0; secondStringIndex <= secondStringLength; secondStringIndex++) {
            matrix[0][secondStringIndex] = secondStringIndex;
        }

        // Step 3
        for (int firstStringIndex = 1; firstStringIndex <= firstStringLength; firstStringIndex++) {

            char firstStringChar = firstStringInAlgorithm.charAt(firstStringIndex - 1);

            // Step 4
            for (int secondStringIndex = 1; secondStringIndex <= secondStringLength; secondStringIndex++) {

                char secondStringChar = secondStringInAlgorithm.charAt(secondStringIndex - 1);
                long cost = 0;

                // Step 5
                if (firstStringChar == secondStringChar) {
                    cost = 0;
                } else {
                    cost = 1;
                }

                // Step 6
                matrix[firstStringIndex][secondStringIndex] = minOfThree(
                        matrix[firstStringIndex - 1][secondStringIndex] + 1,
                        matrix[firstStringIndex][secondStringIndex - 1] + 1,
                        matrix[firstStringIndex - 1][secondStringIndex - 1] + cost);

            }
        }

        // Step 7
        return matrix[firstStringLength][secondStringLength];
    }

    private static final long minOfThree(final long a, final long b, final long c) {
        long minimum;
        minimum = a;
        if (b < minimum) {
            minimum = b;
        }
        if (c < minimum) {
            minimum = c;
        }
        return minimum;
    }
}

Related

  1. computeDistance(Double startLat, Double startLon, Double endLat, Double endLon)
  2. computeDistance(final int[] position1, final int[] position2)
  3. computeDistance(int p1x, int p1y, int p2x, int p2y)
  4. computeDistance(long[] a, long[] b)
  5. computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)
  6. computeDistanceInMiles(Double lat1, Double lon1, Double lat2, Double lon2)
  7. computeDistanceMap(int[][] image)
  8. computeDistanceSqr(double t, double b1, double b2, double b3, double b4, double b5, double b6)
  9. computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)