forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAhoCorasick.cs
165 lines (142 loc) · 3.91 KB
/
AhoCorasick.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Collections.Generic;
namespace Algorithms.Strings
{
/// <summary>
/// The substring search algorithm implements the search for multiple substrings from the dictionary in a given string.
/// </summary>
public class AhoCorasick
{
/// <summary>
/// Tree in which each vertex denotes a row (the root denotes a zero row - $).
/// We will store the Tree as an array of vertices, where each vertex has its own unique number, and the root has a zero value (root = 0)
/// </summary>
private readonly List<AhoCorasickVertex> Tree = new List<AhoCorasickVertex>();
public AhoCorasick()
{
// Add root vertex.
Tree.Add(new AhoCorasickVertex(0, '$'));
}
public void AddPattern(string pattern)
{
int num = 0;
foreach (char ch in pattern.ToCharArray())
{
if (!Tree[num].NextVertex.ContainsKey(ch)) // sign of no rib.
{
Tree.Add(new AhoCorasickVertex(num, ch));
Tree[num].NextVertex.Add(ch, Tree.Count - 1);
}
num = Tree[num].NextVertex[ch];
}
Tree[num].IsPattern = true;
Tree[num].Str = pattern;
}
public void ClearPatterns()
{
Tree.Clear();
// Add root vertex.
Tree.Add(new AhoCorasickVertex(0, '$'));
}
public bool Exist(string pattern)
{
int num = 0;
foreach(var ch in pattern)
{
if(!Tree[num].NextVertex.ContainsKey(ch))
{
return false;
}
num = Tree[num].NextVertex[ch];
}
return Tree[num].IsPattern;
}
private int GetSuffLink(int index)
{
AhoCorasickVertex node = Tree[index];
if (node.SuffLink == -1)
{
node.SuffLink = (index == 0 || node.Parent == 0) ? 0 : GetAutoMove(GetSuffLink(node.Parent), node.Symbol);
}
return node.SuffLink;
}
/// <summary>
/// Transition from the state of the automaton are interconnected.
/// </summary>
/// <param name="index">Vertex index.</param>
/// <param name="ch">Transition symbol.</param>
private int GetAutoMove(int index, char ch)
{
AhoCorasickVertex node = Tree[index];
if (!node.AutoMove.ContainsKey(ch))
{
// if there is an vertex with the symbol ch from the current vertex, then we will follow it,
// otherwise we will follow the suffix link and start recursively from the new vertex.
int autoMove;
if (node.NextVertex.ContainsKey(ch))
{
autoMove = node.NextVertex[ch];
}
else
{
autoMove = (index == 0) ? 0 : GetAutoMove(GetSuffLink(index), ch);
}
node.AutoMove.Add(ch, autoMove);
}
return node.AutoMove[ch];
}
private int GetGoodSuffLink(int index)
{
AhoCorasickVertex node = Tree[index];
if (node.GoodSuffLink == -1)
{
int slink = GetSuffLink(index);
if (slink == 0)
{
// Suffix link is root vertex.
node.GoodSuffLink = 0;
}
else
{
// If flag = true for the vertex by the suffix link, then this is the desired vertex; otherwise, we start recursively from the same vertex.
node.GoodSuffLink = Tree[slink].IsPattern ? slink : GetGoodSuffLink(slink);
}
}
return node.GoodSuffLink;
}
/// <summary>
/// Walking on "good" suffix links.
/// </summary>
/// <param name="index">Current position of the automaton.</param>
/// <returns>For tests.</returns>
private List<string> Check(int index)
{
List<string> patterns = new List<string>();
while (index != 0)
{
AhoCorasickVertex node = Tree[index];
if (node.IsPattern)
{
patterns.Add(node.Str);
}
index = GetGoodSuffLink(index);
}
return patterns;
}
/// <summary>
/// Search for all patterns in a string.
/// </summary>
/// <param name="line">Line in which the search occurs.</param>
/// <returns>For tests.</returns>
public List<string> FindAllOccurrences(string line)
{
List<string> occurences = new List<string>();
int index = 0;
for (int i = 0; i < line.Length; i++)
{
index = GetAutoMove(index, line[i]);
occurences.AddRange(Check(index));
}
return occurences;
}
}
}