Recursive rollback in tree structure

I have this algorithm, and I want to implement a graph search using recursive inverse tracking.

First of all, my code:

 public static boolean buildTree(GenericTreeNode<String> inputNode){

    while(!interruptFlag)
    {
      try { Thread.sleep(200); } catch(InterruptedException e) {} 

      gui.frame.MainWindow.progress.setText("Iterations Deployment: " + c);
      gui.panel.ResultMatrix.setResult(mappingList);
      Multimap<String,String> openList = LinkedHashMultimap.create();

      openList = UtilityClasses.getOpenList.getOpenList(dataMap, ApplicationList,   HardwareList, mappingList);

    if(openList.isEmpty() && !mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI))
            {
                gui.frame.MainWindow.labelSuccess.setText("Mapping not succesful!");
                return false;

            }
    if(openList.isEmpty() && mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI))
            {
                System.out.println(calculateOverallCost.getOverallCosts());
                System.out.println("Mapping done:" + " " + mappingList);
                gui.panel.ResultMatrix.setResult(mappingList);

                return true;
            }

    if(!openList.isEmpty() && (!mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI)))
        {

          for(String s : openList.keySet())
          {
              for(String h : openList.get(s))
              {
                GenericTreeNode<String> child = new GenericTreeNode<String>(s + ":" + h); 
                inputNode.addChild(child);
                child.setCosts(UtilityClasses.CostFunction.calculateCostFunction(s, h));
              }
          }
          List<GenericTreeNode<String>> childlist = inputNode.getChildren();  
          Collections.sort(childlist);

          for(int i = 0; i < childlist.size() ; i++)
         {               
             inputNode = childlist.get(i);
                     // do something      
             if (buildTree(inputNode))
                 {
                 return true;
                 }
             else
             {
            // undo something
             }

         }

Here is the code I have. He creates a tree in everystep. Each node in the tree is a possible solution, ordered by a heuristic function. The first 2 if-sentences are the conditions for completion and return. If there is a solution, it looks pretty smooth. But if there is no quick fix, I need to undo the last step and try other combinations. In the worst case, each combination should be checked.

childlist , . , , . , . , , node . openList. node, .

openList - , → .

, : enter image description here , . , , , , . , , , , , , .

+4
1

, - .

, , ( ):

public static boolean buildTree(GenericTreeNode<String> inputNode) {
    if (interruptFlag) {
        // search was interrupted
        // answer has not be found yet
        return false;
    }

    boolean something = openList.isEmpty() && !mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI);
    if (something) {
        // ... Mapping not succesful!
        // answer can't be found
        return false;

    }

    boolean answerFound = openList.isEmpty() && (mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI));
    if (answerFound) {
        // ...
        return true;
    }

    // answer has not been found
    // visit each children
    // order children list by cost
    // ...
    List<GenericTreeNode<String>> childlist = // ...
    Collections.sort(childlist);

    for (int i = 0; i < childlist.size(); i++) {
        inputNode = childlist.get(i);
        // do something
        boolean childHasAnswer = buildTree(inputNode);
        if (childHasAnswer) {
            // answer was found
            return true;
        } // else: our children do not have the answer
    }

    // neither we or our children have the answer, let go to the parent
    return false;
}

.

+2

Source: https://habr.com/ru/post/1529283/


All Articles