示例#1
0
文件: Maze.java 项目: arybin/homework
 /**
  * Main method to solve the maze. pre: args[0] contains the name of the input file.
  *
  * @param args Command line argument
  */
 public static void main(String[] args) {
   int numV = 0; // The number of vertices.
   Graph theMaze = null;
   // Load the graph data from a file.
   try {
     Scanner scan = new Scanner(new FileReader(args[0]));
     theMaze = AbstractGraph.createGraph(scan, false, "List");
     numV = theMaze.getNumV();
   } catch (IOException ex) {
     System.err.println("IO Error while reading graph");
     System.err.println(ex.toString());
     System.exit(1);
   }
   // Perform breadth-first search.
   int parent[] = BreadthFirstSearch.breadthFirstSearch(theMaze, 0);
   // Construct the path.
   Deque<Integer> thePath = new ArrayDeque<Integer>();
   int v = numV - 1;
   while (parent[v] != -1) {
     thePath.push(new Integer(v));
     v = parent[v];
   }
   // Output the path.
   System.out.println("The Shortest path is:");
   while (!thePath.isEmpty()) {
     System.out.println(thePath.pop());
   }
 }
        @Override
        public Paint transform(VertexNode nn) {

          VertexNode source = g.getStartNode();
          if (source == null) {
            return Color.RED;
          }

          if (distances.getDistance(source, nn) == null) {
            return Color.RED;
          }

          if (nn.isStartNode() && nn.isEndNode()) {
            return Color.ORANGE;
          }
          if (nn.isStartNode() && nn.isRandomNode()) {
            return Color.YELLOW;
          }
          if (nn.isEndNode()) {
            return Color.MAGENTA;
          }
          if (nn.isStartNode()) {
            return Color.GREEN;
          }
          if (nn.isRandomNode()) {
            return Color.CYAN;
          }
          return Color.WHITE;
        }
 @Override
 /**
  * @return The id of the parent graph
  * @see org.graphstream.graph.implementations.AbstractElement#myGraphId()
  */
 protected String myGraphId() {
   return graph.getId();
 }
 @Override
 /**
  * This implementation calls the corresponding method of the parent graph
  *
  * @see org.graphstream.graph.implementations.AbstractElement#newEvent()
  */
 protected long newEvent() {
   return graph.newEvent();
 }
 @Override
 /**
  * This implementation calls the corresponding method of the parent graph
  *
  * @see org.graphstream.graph.implementations.AbstractElement#nullAttributesAreErrors()
  */
 protected boolean nullAttributesAreErrors() {
   return graph.nullAttributesAreErrors();
 }
 public void startElement(String uri, String name, String qName, Attributes atts) {
   // System.out.println("Start element: " + name );
   if ("node".equals(name)) {
     double initialTag = RandomGenerator.getInstance().nextDouble();
     double initialTolerance = RandomGenerator.getInstance().nextDouble();
     AbstractVertex newVertex =
         new TagScoreVertex(graph, atts.getValue("id"), initialTag, initialTolerance);
     graph.addVertex(newVertex);
   } else if ("edge".equals(name)) {
     if (atts.getValue("id") != null) {
       graph.addEdge(atts.getValue("source"), atts.getValue("target"), atts.getValue("id"));
     } else {
       graph.addEdge(atts.getValue("source"), atts.getValue("target"));
     }
   } else if ("graph".equals(name)) {
     if ("directed".equals(atts.getValue("edgedefault"))) {
       graph = new DirectedGraph();
     } else {
       // graph = new UndirectedGraph();
       graph = new DirectedGraph();
     }
   }
   return;
 }
 /**
  * This implementation uses {@link #getEdgeBetween(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeBetween(String)
  */
 public <T extends Edge> T getEdgeBetween(String id) {
   return getEdgeBetween(graph.getNode(id));
 }
 /**
  * This implementation uses {@link #getEdgeBetween(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeBetween(int)
  */
 public <T extends Edge> T getEdgeBetween(int index) {
   return getEdgeBetween(graph.getNode(index));
 }
 /**
  * This implementation uses {@link #getEdgeFrom(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeFrom(String)
  */
 public <T extends Edge> T getEdgeFrom(String id) {
   return getEdgeFrom(graph.getNode(id));
 }
 /**
  * This implementation uses {@link #getEdgeFrom(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeFrom(int)
  */
 public <T extends Edge> T getEdgeFrom(int index) {
   return getEdgeFrom(graph.getNode(index));
 }
 /**
  * This implementation uses {@link #getEdgeToward(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeToward(String)
  */
 public <T extends Edge> T getEdgeToward(String id) {
   return getEdgeToward(graph.getNode(id));
 }
 /**
  * This implementation uses {@link #getEdgeToward(Node)}
  *
  * @see org.graphstream.graph.Node#getEdgeToward(int)
  */
 public <T extends Edge> T getEdgeToward(int index) {
   return getEdgeToward(graph.getNode(index));
 }