
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Wagner and Fisher Algorithm for Online String Matching in C++
Here, we will see how to use Wagner and Fisher algorithm for string matching in C++. Using this algorithm, we can find how many minimum changes are required to match those strings.
Wagner Fisher Algorithm
Wagner Fisher is a dynamic programming algorithm that is used to find the minimum edit distance or levenshtein distance between two input strings. Levenshtein distance between two strings means the number of changes (ie, insertion, deletion or updation) required to convert one string into another. In simpler words, this algorithm will calculate a minimum number of changes required to convert one string into another.
For example, if we have two strings "Support" and "Suppose", then the minimum number of changes required to convert one string into another is 2. The changes are:
Input: Two strings "Support" & "Suppose" Output: Minimum number of required changes: 2 Changes: 1. Change 'r' to 'o' 2. Change 't' to 's'
Steps to Implement Wagner-Fischer Algorithm
Following are steps to implement the Wagner-Fischer algorithm for string matching:
- Step 1: Initialize a 2D matrix dp of size (m+1) x (n+1).
- Step 2: Set the first row and column with incremental values (ie, the base cases dp).
- Step 3: Now, iterate through the matrix and calculate the cost for each cell using insertion, deletion, and substitution operations.
- Step 4: The value at dp[m][n] will be the final edit distance between the strings.
C++ Program to Implement Wagner-Fischer Algorithm
In the below code, we compute the edit distance between two strings using the Wagner-Fischer dynamic programming algorithm.
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int editDistance(const string& s1, const string& s2) { int m = s1.length(); int n = s2.length(); // Create a (m+1) x (n+1) DP table vector<vector<int>> dp(m + 1, vector<int>(n + 1)); // Initialize base cases for (int i = 0; i <= m; ++i) dp[i][0] = i; for (int j = 0; j <= n; ++j) dp[0][j] = j; // Fill DP table for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1; dp[i][j] = min(min(dp[i - 1][j] + 1, // Deletion dp[i][j - 1] + 1), // Insertion dp[i - 1][j - 1] + cost); // Substitution } } return dp[m][n]; } int main() { string str1 = "kitten"; string str2 = "sitting"; cout << "Edit Distance between " << str1 << " and " << str2 << ": "; cout << editDistance(str1, str2) << endl; return 0; }
The output of above code will be:
Edit Distance between kitten and sitting: 3
Time and Space Complexity
Time Complexity: O(m x n), where m and n are the lengths of the two input strings.
Space Complexity: O(m x n), due to the 2D dynamic programming table used for storing intermediate results.