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

Catalina :

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.

Yonlif :

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.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=367112&siteId=1