Пример #1
0
  /**
   * Find the path from start to goal using breadth first search
   *
   * @param start The starting location
   * @param goal The goal location
   * @param nodeSearched A hook for visualization. See assignment instructions for how to use it.
   * @return The list of intersections that form the shortest (unweighted) path from start to goal
   *     (including both start and goal).
   */
  public List<GeographicPoint> bfs(
      GeographicPoint start, GeographicPoint goal, Consumer<GeographicPoint> nodeSearched) {
    // TODO: Implement this method in WEEK 2

    // Hook for visualization.  See writeup.
    // nodeSearched.accept(next.getLocation());

    Queue<GeographicPoint> queue = new LinkedList<GeographicPoint>();
    Set<GeographicPoint> visited = new HashSet<GeographicPoint>();
    Map<GeographicPoint, GeographicPoint> parentMap =
        new HashMap<GeographicPoint, GeographicPoint>();
    queue.add(start);
    visited.add(start);
    while (!queue.isEmpty()) {
      GeographicPoint curr = queue.poll();
      nodeSearched.accept(curr);
      if (curr.equals(goal)) { // reaches the goal
        return RetrievePath(parentMap, start, goal); // retrieve the path
      }
      MapVertex node = Vertices.get(curr); // the MapVertex associated with the location curr
      Set<MapEdge> edges = node.getEdges(); // the edges connected to the node
      for (MapEdge edge : edges) {
        MapVertex next = edge.getEnd(); // neighbor of the location curr
        GeographicPoint nextLoc = next.getLocation();
        if (!visited.contains(nextLoc)) {
          visited.add(nextLoc);
          parentMap.put(nextLoc, curr); // set curr as the parent of next in tha path map
          queue.add(nextLoc);
        }
      }
    }
    /////// if the goal is not achievable
    return null;
  }
Пример #2
0
  private List<GeographicPoint> RetrievePath(
      Map<GeographicPoint, GeographicPoint> parentMap,
      GeographicPoint start,
      GeographicPoint goal) {
    List<GeographicPoint> path = new ArrayList<GeographicPoint>();
    GeographicPoint curr = goal;
    while (!curr.equals(start)) {
      path.add(0, curr);
      curr = parentMap.get(curr);
    }
    path.add(0, curr);

    return path;
  }
Пример #3
0
  /**
   * Find the path from start to goal using breadth first search
   *
   * @param start The starting location
   * @param goal The goal location
   * @param nodeSearched A hook for visualization. See assignment instructions for how to use it.
   * @return The list of intersections that form the shortest (unweighted) path from start to goal
   *     (including both start and goal).
   */
  public List<GeographicPoint> bfs(
      GeographicPoint start, GeographicPoint goal, Consumer<GeographicPoint> nodeSearched) {
    // TODO: Implement this method in WEEK 2
    Set<GeographicPoint> visited = new HashSet<GeographicPoint>();
    Queue<GeographicPoint> searchQueue = new LinkedBlockingQueue<GeographicPoint>();
    Map<GeographicPoint, GeographicPoint> parentMap =
        new HashMap<GeographicPoint, GeographicPoint>();
    LinkedList<GeographicPoint> result = new LinkedList<GeographicPoint>();
    if (null == start || null == goal) throw new IllegalArgumentException();

    searchQueue.add(start);
    visited.add(start);
    GeographicPoint curr = null;

    while (!searchQueue.isEmpty()) {
      curr = searchQueue.remove();
      nodeSearched.accept(curr);
      if (curr.equals(goal)) break;
      for (GeographicPoint neighbor : getUnvisitedNeighbors(curr, visited)) {
        visited.add(neighbor);
        searchQueue.add(neighbor);
        parentMap.put(neighbor, curr);
      }
    }

    if (curr == null || !curr.equals(goal)) return null;

    for (GeographicPoint node = goal; node != null; node = parentMap.get(node)) {
      result.push(node);
    }

    return result;

    // Hook for visualization.  See writeup.
    // nodeSearched.accept(next.getLocation());

  }