2012-04-25 54 views
0

假设我有一个字符串数组,表示像{“222/111”,“333/222”,“444/333”,“888/999”,“777/999 “}。如何建立从上述路径的项目树/图等,我得到两个图:使用给定的随机路径构建图形

444333222111和888777999

(伪认识)

回答

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) 
0

,我想你应该检查JGraphT项目,其中也有许多基于图形的算法和示例代码,它也推广了您的顶点/边缘对象表示!