在C#中寻找后缀树实现?

时间:2008-10-04 23:49:00

标签: c# data-structures suffix-tree

我已经实施了一项研究项目的基本搜索。我正在尝试通过构建suffix tree来提高搜索效率。我对Ukkonen算法的C#实现很感兴趣。如果存在这样的实现,我不想浪费时间自己动手。

3 个答案:

答案 0 :(得分:12)

难题。这是我能找到的最接近的匹配:http://www.codeproject.com/KB/recipes/ahocorasick.aspx,它是Aho-Corasick字符串匹配算法的实现。现在,该算法使用类似后缀树的结构:http://en.wikipedia.org/wiki/Aho-Corasick_algorithm

现在,如果你想要一个前缀树,本文声称有一个实现:http://www.codeproject.com/KB/recipes/prefixtree.aspx

< 幽默>既然我完成了你的作业,你怎么样我的草坪。 (参考:http://flyingmoose.org/tolksarc/homework.htm)< / HUMOR >

编辑:我发现了一个C#后缀树实现,它是在博客上发布的C ++端口:http://code.google.com/p/csharsuffixtree/source/browse/#svn/trunk/suffixtree

编辑:Codeplex上有一个专注于后缀树的新项目:http://suffixtree.codeplex.com/

答案 1 :(得分:3)

这是一个合理有效的后缀树的实现。我还没有研究过Ukkonen的实现,但我相信这个算法的运行时间非常合理,约为O(N Log N)。请注意,创建的树中的内部节点数等于父字符串中的字母数。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;

namespace FunStuff
{
    public class SuffixTree
    {
        public class Node
        {
            public int Index = -1;
            public Dictionary<char, Node> Children = new Dictionary<char, Node>();
        }

        public Node Root = new Node();
        public String Text;

        public void InsertSuffix(string s, int from)
        {             
            var cur = Root;
            for (int i = from; i < s.Length; ++i)
            {
                var c = s[i];
                if (!cur.Children.ContainsKey(c))
                {
                    var n = new Node() {Index = from};
                    cur.Children.Add(c, n);

                    // Very slow assertion. 
                    Debug.Assert(Find(s.Substring(from)).Any());

                    return;
                }
                cur = cur.Children[c];
            }
            Debug.Assert(false, "It should never be possible to arrive at this case");
            throw new Exception("Suffix tree corruption");
        }

        private static IEnumerable<Node> VisitTree(Node n)
        {
            foreach (var n1 in n.Children.Values)
                foreach (var n2 in VisitTree(n1))
                    yield return n2;
            yield return n;
        }

        public IEnumerable<int> Find(string s)
        {
            var n = FindNode(s);
            if (n == null) yield break;
            foreach (var n2 in VisitTree(n))
                yield return n2.Index;
        }

        private Node FindNode(string s)
        {
            var cur = Root;
            for (int i = 0; i < s.Length; ++i)
            {
                var c = s[i];
                if (!cur.Children.ContainsKey(c))
                {
                    // We are at a leaf-node.
                    // What we do here is check to see if the rest of the string is at this location. 
                    for (var j=i; j < s.Length; ++j)
                        if (cur.Index + j >= Text.Length || Text[cur.Index + j] != s[j])
                            return null;
                    return cur;
                }
                cur = cur.Children[c];
            }
            return cur;
        }

        public SuffixTree(string s)
        {
            Text = s;
            for (var i = s.Length - 1; i >= 0; --i)
                InsertSuffix(s, i);
            Debug.Assert(VisitTree(Root).Count() - 1 == s.Length);
        }
    }

    [TestFixture]
    public class TestSuffixTree
    {
        [Test]
        public void TestBasics()
        {
            var s = "banana";
            var t = new SuffixTree(s);
            var results = t.Find("an").ToArray();
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual(1, results[0]);
            Assert.AreEqual(3, results[1]);
        }
    } 
}

答案 2 :(得分:3)

Hei,刚刚完成了包含不同trie实现的.NET(c#)库。其中:

  • 古典特里
  • Patricia trie
  • 后缀trie
  • 使用 Ukkonen 算法
  • 的特里

我尝试使源代码易于阅读。用法也很直接:

using Gma.DataStructures.StringSearch;

...

var trie = new UkkonenTrie<int>(3);
//var trie = new SuffixTrie<int>(3);

trie.Add("hello", 1);
trie.Add("world", 2);
trie.Add("hell", 3);

var result = trie.Retrieve("hel");

该库经过了充分测试,并以TrieNet NuGet包发布。

请参阅github.com/gmamaladze/trienet

相关问题