首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的DAWG创建算法?

简单的DAWG创建算法?
EN

Stack Overflow用户
提问于 2012-09-08 22:47:32
回答 2查看 1.7K关注 0票数 3

我需要为我的拼字游戏播放器创建一个DAWG (http://en.wikipedia.org/wiki/Directed_acyclic_word_graph)结构,给定文件中的单词列表。我使用的是Java。我只需要做一次,然后将它存储在一个或多个文件中。到目前为止,我已经看到了两种方法: 1)构建Trie并将其简化为DAWG,或者2)立即构建DAWG。因为我只需要做一次,所以我想我只需要最简单的算法来实现它。速度和内存需求无关紧要。

另外,我想知道我应该如何在运行时将结构存储在内存中,以及如何将其保存在文件中?DAWG基本上是一个图形,它建议使用我编写的一些非常简单的类的一些节点和边/指针,但我看到使用数组和偏移量(在这个数组中)的实现,这似乎很复杂和难以辨认。这一次,我关心内存大小(在运行时和保存的文件)和加载DAWG /使用DAWG的速度。

EN

回答 2

Stack Overflow用户

发布于 2016-07-02 05:28:51

最简单和最有效的DAWG构造算法是在this paper中定义的,并且需要DAWG要表示的单词集进行排序。假设您计划从预先存在的单词列表构建一个DAWG,那么该列表可能已经排序,或者可以用于此目的。

我粗略地将算法的伪代码转录成一种比论文中给出的格式更“程序员友好”的格式(免责声明:我可能犯了一些转录错误;您可能应该看看原始代码,以确定是否有错误):

代码语言:javascript
复制
Given:
        startState is the state from which traversal of the DAWG is to start
        register is a map of representations (hint: hashes) OF graphs which extend 
        from states in the DAWG TO said states

While there is newWord in wordList
    Get newWord from wordList
    Determine longestPrefix of newWord, starting from startState, which already exists in DAWG
    Get longestPrefixEndState, the state which the sequence of transitions defined by longestPrefix leads to
    Get suffix of newWord, the substring of newWord after longestPrefix
    if longestPrefixEndState has children 
        replace_or_register(longestPrefixEndState)
    endIf
    Create the sequence of transitions and states defined by suffix, starting from longestPrefixEndState
endWhile
replace_or_register(startState)


function replace_or_register(argState)
    Get greatestCharEndState of argState, the state which the lexicographically-greatest-char-labelled-transition in the outgoing transition set of argState leads to
    if greatestCharEndState has children
        replace_or_register(greatestCharEndState)
    endIf
    if there exists state in DAWG that is in the register and is equivalent (has an identical graph extending from it) to greatestCharEndState
        Redefine the transition that extends from argState to greatestCharEndState, as one that extends from argState to state
        Delete greatestCharEndState
    endIf
    else 
        add greatestCharEndState to the register
    endElse

假设您使用的是Java语言,那么您可以利用Serializable接口来处理所有的序列化和反序列化需求。

如果您对用Java语言实现上述算法的现有DAWG实现感兴趣,请查看MDAG,它还提供了其他实现没有的几个漂亮特性(包括动态添加和删除字符串),并且由我维护!

票数 1
EN

Stack Overflow用户

发布于 2012-09-08 23:47:32

有一次,我不得不用C语言为我的一个客户实现这样的结构。最后的结构是从描述字符集和dawg的xml文件加载的,另一个过程从单词列表创建xml文件。

步骤1:构建序列化为xml文件的第一个dawg的结构

我们使用:

代码语言:javascript
复制
typedef struct _s_build_node build_node_t;
struct _s_build_node {
  char_t letter;
  build_node_t* first_child;
  build_node_t* right_sibling;

  hash_t hash;
  size_t depth;
  size_t ID;
};
typedef struct _s_build_dawg {
  charset_t charset;
  node_t* all_nodes; // an array of all the created nodes
  node_t* root;
} build_dawg_t;

同级按升序排列,单词末尾的特殊字符少于任何其他字符。算法非常简单:

代码语言:javascript
复制
// create the build dawg
foreach word in wordlist
  insert(dawg, word)
// compact the dawg
compact(dawg)
// generate the xml file
xml_dump(dawg)

为了压缩dawg,我们计算了每个节点的散列值。具有相同散列的两个节点可以被分解。这一部分可能会很棘手。只保留深度最低的节点,其他节点将被删除,它们的父节点现在指向保留的节点。

压缩后,我们为每个节点分配一个唯一的ID (通过bfs,ID介于0和N-1之间,N是压缩的dawg中的节点数)。xml文件简单地描述了trie:

代码语言:javascript
复制
<dawg>
  <charset ....>
    ...
  </charset>

  <node ID="node_id" letter="letter" fist_child="first_child_ID" next_sibling="next_sibling_id" />
  <node .... />
  <node .... />
  <node .... />
</dawg>

第二步:最后的dagw

这个结构稍微简单一点

代码语言:javascript
复制
typedef struct {
  char_t letter;
  size_t first_child;
  size_t next_sibling;
} node_t;

typedef struct {
  node_t nodes[];
  ... whatever you need ...
} dawg_t;

在这里,根是dawg.nodes,first_child/next_sibling是节点数组中的索引。从xml文件创建这样的结构很容易。主要缺点是,任何词表修改都会触发生成新的xml文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12331755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档