The PHP implementation of editing distance and Hamming distance

Source: Internet
Author: User

When calculating the similarity of a string or graph, the two commonly used algorithms, one is the editing distance, the other is the Hamming distance.


Simply put:

Editing distance is the ability to turn a string into another string with several edits

Hamming distance is the corresponding position to compare, find out the different number of characters

To find out more, please search by yourself.


The following are their PHP code implementations.

<?php/** *  Calculate editing distance  * *  @param  string  $s 1 *  @param  string   $s 2 */function levdist ($s 1,  $s 2) {     $len 1 = strlen ($s 1);      $len 2 = strlen ($s 2);     if ($len 1 == 0)      {        return  $len 2;    }     if ($len 2 == 0)     {         return  $len 1;    }    for ($i  = 0;  $i  <=  $len 1;  $i + +)     {        $ matrix[$i][0] = 0;    }    for ($j  = 0;  $j  <=  $len 2;  $j + +)     {        $ matrix[0][$j] = 0;     }    for ($i  = 1;  $i  <=  $len 1;  $i + +)     {         $ch 1 =  $s 1[$i  -  1];        for ($j  = 1;  $j  <=  $len 2;   $J + +)         {              $ch 2  =  $s 2[$j  - 1];              $temp  =  $ch 1 ==  $ch 2 ? 0 : 1;              $arr  = array (                  $matrix [$i  - 1][ $j] + 1,                  $matrix [$i] [$j  - 1] + 1,                  $matrix [$i  - 1][$j  - 1] +  $temp              );             $ matrix[$i] [$j] = min ($arr);        }     }    return  $matrix [$len 1][$len 2];} /** *  calculating Hamming distance  * *  @param  string  $s 1 *  @param  string  $s 2  *  @return  boolean number */function hamdist ($s 1,  $s 2) {      $len 1 = strlen ($s 1);     $len 2 = strlen ($s 2);     if ($len 1 !=  $len 2)     {         return false;    }     $dist &nbsP;= 0;    for ($i  = 0;  $i  <  $len 1;  $i + +)      {        if ($s 1[$i] !=  $s 2[$i])          {            $ dist++;        }    }     return  $dist;} $s 1 =  "abcde", $s 2 =  "acdeb", echo levdist ($s 1,  $s 2);//  output 2echo hamdist ( $s 1,  $s 2);//  output 4


This article is from the "Van Star Technology Blog" blog, please be sure to keep this source http://ustb80.blog.51cto.com/6139482/1569542

The PHP implementation of editing distance and Hamming distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.