简体   繁体   English

如何在特殊图中获取从叶子到根的所有路径

[英]How to get all paths from leaf to the root in a special graph

I am trying to implement an algorithm in a special data structure in graph form.我正在尝试以图形形式在特殊数据结构中实现算法。 my goal ist to get all paths from leaf (Target element) to the root.我的目标是获取从叶子(目标元素)到根的所有路径。 This is how the graph looks like the image below.这就是图表的外观,如下图所示。

We need to remember all of the path that we had in order to traverse in all of the different ways on the graph, therefore only a states list isn't enough, we need a paths list.我们需要记住我们拥有的所有路径,以便在图上以所有不同的方式遍历,因此只有状态列表是不够的,我们需要一个路径列表。 For every path we will make it longer by one if it has one parent and if it has two or more we will duplicate this list and add the parent to each one.对于每条路径,如果它有一个父级,我们会将它加长一个,如果它有两个或更多,我们将复制此列表并将父级添加到每个路径中。

Well I am not great at Java and I can not run this code so I can't guarantee this will run but the algorithm is ok.好吧,我不擅长 Java,我无法运行这段代码,所以我不能保证它会运行,但算法没问题。

public static List<ARGState> getAllErrorStatesReversed(ReachedSet reachedSet) {
  ARGState pIsStart =
      AbstractStates.extractStateByType(reachedSet.getFirstState(), ARGState.class);
  ARGState pEnd = targetStates.get(0);

  List<List<ARGState>> results = new ArrayList<>();
  List<List<ARGState>> paths = new ArrayList<>();

  paths.add(new ArrayList<ARGState>(pEnd));

  // This is assuming from each node there is a way to go to the start
  // Go on until all the paths got the the start
  while (!paths.empty()) {
    // Expand the last path on your list
    List<ARGState> curPath = paths.remove(paths.size() - 1);
    // If there is no more to expand - add this path and continue
    if (curPath.get(curPath.size() - 1) == pIsStart) {
      results.append(curPath);
      continue;
    }

    // Expand the path
    Iterator<ARGState> parents = curPath.get(curPath.size() - 1).getParents().iterator();
    // Add all parents
    while (parentElement.hasNext()) {
      ARGState parentElement = parents.next();
      List<ARGState> tmp = new ArrayList<ARGState>(List.copyOf(curPath));
      tmp.add(parentElement);
      paths.add(tmp);
    }
  }
  return results;
}

Hope you understand, more than welcome to ask.希望你理解,更欢迎提问。
Good Luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM