// BreadthFirstSearch.java // // Copyright Mark Watson, 1998. Open Source and Open Content. // import java.util.Vector; public class BreadthFirstSearch extends SearchApplet { public void init() { // Define a test network before calling the super class 'init': addNode("0", 0.0f, 0.0f); addNode("1", 1.0f, 1.0f); addNode("2", 5.0f, 2.0f); addNode("3", 2.0f, 5.0f); addNode("4", 7.0f, 5.0f); addNode("5", 8.0f, 8.0f); addNode("6", 10.0f, 5.0f); addNode("7", 8.0f, 2.0f); addNode("8", 12.0f, 8.0f); addNode("9", 13.0f, 5.0f); addLink(0,1); addLink(1,2); addLink(2,3); addLink(2,4); addLink(4,5); addLink(4,6); addLink(6,8); addLink(8,9); addLink(2,7); addLink(7,9); // Now that we have defined the network to search, we // can now call the super class init: super.init(); } /** findPath - abstract method in super class */ // Breadth first search algorithm from "Algorithms" // (Cormen, Leiserson, Rivest) page 460: public int [] findPath(int node_1, int node_2) { // return an array of node indices System.out.println("Entered BreadthFirstSearch.findPath(" + node_1 + ", " + node_2 + ")"); if (node_1 == node_2) { repaintPath(null, 0); return null; } // data structures for depth first search: boolean [] visitedFlag = new boolean[numNodes]; float [] distanceToNode = new float[numNodes]; int [] predecessor = new int[numNodes]; Queue queue = new Queue(numNodes + 2); // data structures for graphical display (only): int [] display_path = new int[2 * numNodes + 1]; int num_display_path = 0; for (int i=0; i 0) { // re-calculate the plot display points: display_path[num_display_path++] = ret2[i-1]; display_path[num_display_path++] = ret2[i]; } } // now re-calculate the display path points: repaintPath(display_path, num_display_path); return ret2; } // Queue algorithm from "Algorithms" (Cormen, Leiserson, Rivest) page 202: protected class Queue { public Queue(int num) { queue = new int[num]; head = tail = 0; len = num; } public Queue() { this(400); } private int [] queue; int tail, head, len; public void enqueue(int n) { queue[tail] = n; if (tail >= (len - 1)) { tail = 0; } else { tail++; } } public int dequeue() { int ret = queue[head]; if (head >= (len - 1)) { head = 0; } else { head++; } return ret; } public boolean isEmpty() { return head == (tail + 1); } public int head() { return queue[head]; } } protected int [] connected_nodes(int node) { int [] ret = new int[SearchApplet.MAX]; int num = 0; for (int n=0; n