Java 简易DAWG创建算法?

Java 简易DAWG创建算法?,java,arrays,algorithm,graph,Java,Arrays,Algorithm,Graph,我需要创建一个道格(http://en.wikipedia.org/wiki/Directed_acyclic_word_graph)给我的拼字游戏玩家一个文件中的单词列表的结构。我正在使用Java。我只需要做一次,然后将其存储在一个或多个文件中。到目前为止,我已经看到了两种方法:1)构建Trie并将其简化为DAWG,或2)立即构建DAWG。因为我只需要做一次,我想我只需要最简单的算法来实现它。速度和内存要求并不重要 我还想知道在运行时如何将结构存储在内存中,以及如何将其保存在文件中?DAWG基

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


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

我曾经为我的一个客户端用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;
SIBLIBG按升序排列,字尾特殊字符比任何其他字符都少。 算法非常简单:

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

这里的根是dawg.nodes[0],第一个\u子节点/下一个\u同级节点是节点数组中的索引。从xml文件创建这样的结构很容易。主要缺点是任何单词表修改都会触发新xml文件的生成。

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

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

如果您使用的是Java,那么可以利用该接口来处理所有序列化和反序列化需求

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

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;
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