KR-IST - Lecture 2b: Route finding in Java

Chris Thornton


Introduction

This lecture looks at a search program for finding routes in a toy map of campus.

CampusSearch class

  import java.util.*;

  public class CampusSearch {

     String links[][] = {
        {"office", "debuggingRoom"},
           {"office", "CHI343"},
           {"CHI343", "informaticsSchoolOffice"},
           {"informaticsSchoolOffice", "ITS"},
           {"ITS", "PEV11A6"},
           {"PEV11A6", "PEV12A11"},
           {"PEV11A6", "library"},
           {"CHI343", "PEV11A6"},
           {"PEV12A11", "library"},
           {"library", "bridgeTeaBar"},
           {"library", "meetingHouse"},
           {"meetingHouse", "bridgeTeaBar"},
           {"bridgeTeaBar", "debuggingRoom"}};

     String goal = "meetingHouse";

Successor function

  ArrayList<String> getSuccessors(String location) {
     ArrayList<String> successors = new ArrayList<String>();
     for (int i = 0; i < links.length; i++) {
        if (links[i][0].equals(location)) {
           successors.add(links[i][1]); }
        if (links[i][1].equals(location)) {
           successors.add(links[i][0]); }
     }
     return(successors);
  }

Main loop

  void run() {
     ArrayList<ArrayList<String>> open = new ArrayList<ArrayList<String>>();
     ArrayList<String> path = new ArrayList<String>();
     path.add("office");
     open.add(path);

     while (open.size() > 0) {
        path = open.remove(0);
        String parent = path.get(path.size()-1);
        if (parent.equals(goal)) {
           System.out.println("ROUTE: " + path); }
        ArrayList<String> successors = getSuccessors(parent);
        for (int i = 0; i < successors.size(); i++) {
           String child = successors.get(i);
           if (!path.contains(child)) {
              ArrayList<String> newPath = new ArrayList<String>(path);
              newPath.add(child);
              open.add(newPath); }
        }
     }
  }

main method

     public static void main(String args[]) { // do the search
        new CampusSearch().run();
     }
  }

Running the program the traditional way

Compile the program with "javac", then run it with "java" command:

  javac CampusSearch.java
  java CampusSearch

Running it from BlueJ

If you are running Java via BlueJ, then you would run the program by explicitly calling the `run' method of the CampusSearch class.

You could also explicitly call the `main' method; i.e., right-click on the CampusSearch box, choose `void main(args)'. Then press `Ok'.

Output generated

The program generates all possible routes connecting `office' with `meetingHouse', ordered by length.

  ROUTE: [office, debuggingRoom, bridgeTeaBar, meetingHouse]
  ROUTE: [office, debuggingRoom, bridgeTeaBar, library, meetingHouse]
  ROUTE: [office, CHI343, PEV11A6, library, meetingHouse]
  ROUTE: [office, CHI343, PEV11A6, PEV12A11, library, meetingHouse]
  ROUTE: [office, CHI343, PEV11A6, library, bridgeTeaBar, meetingHouse]
  ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, library, meetingHouse]
  ROUTE: [office, CHI343, PEV11A6, PEV12A11, library, bridgeTeaBar, meetingHouse]
  ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, PEV12A11, library, meetingHouse]
  ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, library, bridgeTeaBar, meetingHouse]
  ROUTE: [office, CHI343, informaticsSchoolOffice, ITS, PEV11A6, PEV12A11, library, bridgeTeaBar, meetingHouse]

Summary

Questions

Exercises

Adapt the CampusSearch program for use with data derived from the toy map of Brighton featured in the previous exercises. Check that the program produces valid routes for that map.

Exercises cont.