コード例 #1
0
  // recursive path-checking dfs. Private, because it has the extra
  // parameters needed for recursion.
  private List<UUSearchNode> dfsrpc(
      UUSearchNode currentNode, HashSet<UUSearchNode> currentPath, int depth, int maxDepth) {

    // you write this method
    currentPath.add(currentNode);
    // keep track of stats; these calls charge for the current node
    updateMemory(currentPath.size());

    int i; // count for the successors
    List<UUSearchNode> path = new ArrayList<UUSearchProblem.UUSearchNode>();
    // base case
    if (currentNode.goalTest() == true) {
      path.add(currentNode);
      return path;
    }
    //		recursive case
    else {
      ArrayList<UUSearchNode> successors = new ArrayList<UUSearchNode>();
      successors = currentNode.getSuccessors();

      for (i = 0; i < successors.size(); i++) {
        incrementNodeCount();
        if (successors.get(i).getDepth() <= maxDepth // depth must smaller than maxDepth
            && currentPath.contains(successors.get(i)) == false) // the state cannot be visited 	
        { // go ahead to search the successor(i)

          List<UUSearchNode> suc_path = new ArrayList<UUSearchNode>();
          suc_path = dfsrpc(successors.get(i), currentPath, successors.get(i).getDepth(), maxDepth);
          if (!suc_path.isEmpty()) {
            // currentPath.clear();
            path.add(currentNode);
            path.addAll(suc_path); // the path to goal
            return path;
          } else
            currentPath.remove(
                successors.get(
                    i)); // becareful to the change of the currentpath, I add current node in every
                         // begin point of a fuction nomatter whether it has sub_path
        }
      }
      return path;
    }
  }
コード例 #2
0
  // recursive memoizing dfs. Private, because it has the extra
  // parameters needed for recursion.
  private List<UUSearchNode> dfsrm(
      UUSearchNode currentNode, HashMap<UUSearchNode, Integer> visited, int depth, int maxDepth) {
    List<UUSearchNode> current_path = new ArrayList<UUSearchNode>();

    // keep track of stats; these calls charge for the current node
    updateMemory(visited.size());
    int i; // count for the successors
    // you write this method.  Comments *must* clearly show the
    //  "base case" and "recursive case" that any recursive function has.
    // base case
    if (currentNode.goalTest() == true) {
      current_path.add(currentNode);
      return current_path;
    }
    //		recursive case
    else {
      ArrayList<UUSearchNode> successors = new ArrayList<UUSearchNode>();
      successors = currentNode.getSuccessors();

      for (i = 0; i < successors.size(); i++) {
        incrementNodeCount();
        if (successors.get(i).getDepth() <= maxDepth // depth must smaller than maxDepth
            && (visited.containsKey(successors.get(i)) == false // the state cannot be visited
                || successors.get(i).getDepth()
                    < visited.get(
                        successors.get(i)))) // or if the depth is smaller than the visited one
        { // go ahead to search the successor(i)
          visited.put(successors.get(i), successors.get(i).getDepth());
          List<UUSearchNode> suc_path = new ArrayList<UUSearchNode>();
          suc_path = dfsrm(successors.get(i), visited, successors.get(i).getDepth(), maxDepth);
          if (!suc_path.isEmpty()) {
            current_path.clear();

            current_path.add(currentNode);
            current_path.addAll(suc_path); // the path to goal
            return current_path;
          }
        }
      }
      return current_path;
    }
  }
コード例 #3
0
  public List<UUSearchNode> breadthFirstSearch() {
    resetStats();
    // You will write this method
    int i;
    HashMap<UUSearchNode, UUSearchNode> explored =
        new HashMap<UUSearchNode, UUSearchNode>(); // explored:tracking the explored nodes
    List<UUSearchNode> path = new ArrayList<UUSearchNode>(); // the path to goal
    Queue<UUSearchNode> queue = new LinkedList<UUSearchProblem.UUSearchNode>();

    queue.add(this.startNode); // initializing, add start node in queue
    explored.put(startNode, null);
    incrementNodeCount(); // update the node it explored
    updateMemory(explored.size());

    while (!queue.isEmpty()) {
      // dequeue the head of queue to be the current node
      UUSearchNode current_node = (UUSearchNode) queue.remove();

      if (current_node.goalTest() == true) {
        path = backchain(current_node, explored);
      } else {
        // add unexplored successors to fringe
        ArrayList<UUSearchNode> successors = new ArrayList<UUSearchNode>();
        successors = current_node.getSuccessors();
        for (i = 0; i < successors.size(); i++) {
          incrementNodeCount(); // update the node it explored before check for explored ones
          if (explored.containsKey(successors.get(i)) == false) {
            explored.put(successors.get(i), current_node);
            queue.add(successors.get(i));
            updateMemory(explored.size());
          }
        }
      }
    }
    return path;
  }