// DepthFirstSearch.java
//
// Copyright Mark Watson, 1998. Open Source and Open Content.
//
import java.util.Vector;
public class DepthFirstSearch 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 */
public int [] findPath(int node_1, int node_2) { // return an array of node indices
System.out.println("Entered DepthFirstSearch.findPath(" +
node_1 + ", " + node_2 + ")");
num_path = 1;
path[0] = node_1; // the starting node
return findPathHelper(path, 1, node_2);
}
public int [] findPathHelper(int [] path, int num_path, int goal_node) {
System.out.println("Entered DepthFirstSearch.findPathHelper(...," +
num_path + ", " + goal_node + ")");
System.out.println("ici1");
repaintPath(path, num_path);
if (goal_node == path[num_path - 1]) {
int [] ret = new int[num_path];
for (int i=0; i
Back to main page