Easy DAWG创建算法?

时间:2012-09-08 14:47:32

标签: java arrays algorithm graph

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

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

2 个答案:

答案 0 :(得分:1)

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

我已经在一个程序员友好的程序员中轻易地转录了算法的伪代码"格式比在论文中给出的格式(免责声明:我可能已经做了一些转录错误;你应该看看原文以确定是否有):

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,它还提供了其他实现不具备的几个漂亮功能(包括添加和删除字符串 - the-fly),由我维护!

答案 1 :(得分:0)

我必须在C中为我的一个客户端实现一次这样的结构。最终结构从描述字符集和dawg的xml文件加载,另一个进程从单词列表创建xml文件。

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

我们用过:

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;

siblibgs按升序排序,词尾特殊字符小于任何其他字符。 算法非常简单:

// 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:

<dawg>
  <charset ....>
    ...
  </charset>

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

第2步:最后的dagw

结构更简单

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;

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