forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDistance.cs
95 lines (83 loc) · 3.79 KB
/
EditDistance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
namespace Algorithms.Strings
{
/// <summary>
/// The Edit Distance computation algorithm.
/// Uses a custom class for receiving the costs.
/// </summary>
public static class EditDistance
{
/// <summary>
/// Computes the Minimum Edit Distance between two strings.
/// </summary>
public static Int64 GetMinDistance(string source, string destination, EditDistanceCostsMap<Int64> distances)
{
// Validate parameters and TCost.
if (source == null || destination == null || distances == null)
throw new ArgumentNullException("Some of the parameters are null.");
else if (source == destination)
return 0;
// Dynamic Programming 3D Table
long[,] dynamicTable = new long[source.Length + 1, destination.Length + 1];
// Initialize table
for (int i = 0; i <= source.Length; ++i)
dynamicTable[i, 0] = i;
for (int i = 0; i <= destination.Length; ++i)
dynamicTable[0, i] = i;
// Compute min edit distance cost
for (int i = 1; i <= source.Length; ++i)
{
for (int j = 1; j <= destination.Length; ++j)
{
if (source[i - 1] == destination[j - 1])
{
dynamicTable[i, j] = dynamicTable[i - 1, j - 1];
}
else
{
long insert = dynamicTable[i, j - 1] + distances.InsertionCost;
long delete = dynamicTable[i - 1, j] + distances.DeletionCost;
long substitute = dynamicTable[i - 1, j - 1] + distances.SubstitutionCost;
dynamicTable[i, j] = Math.Min(insert, Math.Min(delete, substitute));
}
}
}
// Get min edit distance cost
return dynamicTable[source.Length, destination.Length];
}
/// <summary>
/// Overloaded method for 32-bits Integer Distances
/// </summary>
public static Int32 GetMinDistance(string source, string destination, EditDistanceCostsMap<Int32> distances)
{
// Validate parameters and TCost.
if (source == null || destination == null || distances == null)
throw new ArgumentNullException("Some of the parameters are null.");
else
{
var longDistance = new EditDistanceCostsMap<long>(
insertionCost: Convert.ToInt64(distances.InsertionCost),
deletionCost: Convert.ToInt64(distances.DeletionCost),
substitutionCost: Convert.ToInt64(distances.InsertionCost));
return Convert.ToInt32(EditDistance.GetMinDistance(source, destination, longDistance));
}
}
/// <summary>
/// Overloaded method for 16-bits Integer Distances
/// </summary>
public static Int16 GetMinDistance(string source, string destination, EditDistanceCostsMap<Int16> distances)
{
// Validate parameters and TCost.
if (source == null || destination == null || distances == null)
throw new ArgumentNullException("Some of the parameters are null.");
else
{
var longDistance = new EditDistanceCostsMap<long>(
insertionCost: Convert.ToInt64(distances.InsertionCost),
deletionCost: Convert.ToInt64(distances.DeletionCost),
substitutionCost: Convert.ToInt64(distances.InsertionCost));
return Convert.ToInt16(EditDistance.GetMinDistance(source, destination, longDistance));
}
}
}
}