0

假设我有一个字符串数组,它们表示 {"222/111"、"333/222"、"444/333"、"888/999"、"777/999"} 等路径。如何从上述路径项构建树/图,以便获得两个图:

444,333,222,111 和 888,777,999

(感谢伪代码)

4

2 回答 2

0

I suppose you should check JGraphT project, which also has many graph-based algorithms and sample codes, also it generalize your vertex/edge object representation!

于 2012-04-25T23:33:06.610 回答
0
while (strings not empty)
    path = strings.pop
    foreach (s:strings)
       if (s starts with path end)
          strings.remove(s)
          path.append(s)
          restart loop
       if (s ends with path start)
          strings.remove(s)
          path.prepend(s)
          restart loop
    paths.add(path)
于 2012-04-25T23:12:09.357 回答