Skip to content
Snippets Groups Projects
Commit d2f3e022 authored by erayosso's avatar erayosso
Browse files

Implemented Algorithm for levenshtein Distance

parent fc218c50
Branches Aufgabe_03
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4"> <module classpath="CMake" type="CPP_MODULE" version="4" />
<component name="NewModuleRootManager"> \ No newline at end of file
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -3,4 +3,4 @@ project(magicCards) ...@@ -3,4 +3,4 @@ project(magicCards)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
add_executable(magicCards main.cpp) add_executable(magicCards main.cpp aufgabe1.h aufgabe1.cpp)
#include <iostream>
#include "aufgabe1.h"
//calculate minimum of three values
int min(int x, int y, int z) {
if (x < y && x < z) {
return x;
} else if (y < z) {
return y;
} else {
return z;
}
}
int levenshteinDistance(const std::string& s, const std::string& t) {
//Length of first string
int n = s.length();
//Length of second string
int m = t.length();
int cost = 0;
//Matrix to hold the levenshtein distance
int Matrix[n + 1][m + 1];
// Initialize the first row and column of the matrix,[0][0] is empty
for (int i = 0; i <= n; i++) {
Matrix[i][0] = i;
}
for (int j = 0; j <= m; j++) {
Matrix[0][j] = j;
}
// Calculate the Levenshtein distances for all pairs of characters
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i-1] == t[j-1])
cost = 0;
else
cost = 1;
//M[i][j] := min(rep, ins, del)
int replace = Matrix[i-1][j-1] + cost;
int insert = Matrix[i][j-1]+1;
int del = Matrix[i-1][j]+1;
Matrix[i][j] = min(replace, insert,del);
//Matrix[i][j] = min((Matrix[i-1][j-1] + cost), (Matrix[i][j-1]+1),(Matrix[i-1][j]+1));
}
}
int LevenDist = Matrix[n][m];
std::cout << "The Levenshtein distance between " << s << " and " << t << " is " << LevenDist << std::endl;
return 0;
}
//
// Created by erayd on 11.04.2023.
//
#pragma once
#include <string>
int min(int x, int y, int z);
int levenshteinDistance(const std::string& s, const std::string& t);
#include <iostream> #include <iostream>
#include "aufgabe1.h"
int main() { int main() {
std::cout << "Info 3 Projekt Nr.1" << std::endl; std::cout << "Info 3 Projekt Nr.1" << std::endl;
levenshteinDistance("kitten", "sitting");
levenshteinDistance("industry", "interests");
levenshteinDistance("abcdef", "ghijkl");
levenshteinDistance("hello", "hallo");
levenshteinDistance("s", "");
levenshteinDistance("", "s");
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment