假设我有一个字符串数组,它们表示 {"222/111"、"333/222"、"444/333"、"888/999"、"777/999"} 等路径。如何从上述路径项构建树/图,以便获得两个图:
444,333,222,111 和 888,777,999
(感谢伪代码)
假设我有一个字符串数组,它们表示 {"222/111"、"333/222"、"444/333"、"888/999"、"777/999"} 等路径。如何从上述路径项构建树/图,以便获得两个图:
444,333,222,111 和 888,777,999
(感谢伪代码)
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!
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)