// dfs.java // demonstrates depth-first search // to run this program: C>java DFSApp //////////////////////////////////////////////////////////////// class StackX { private final int SIZE = 20; private int[] st; private int top; // ------------------------------------------------------------ public StackX() // constructor { st = new int[SIZE]; // make array top = -1; } // ------------------------------------------------------------ public void push(int j) // put item on stack { st[++top] = j; } // ------------------------------------------------------------ public int pop() // take item off stack { return st[top--]; } // ------------------------------------------------------------ public int peek() // peek at top of stack { return st[top]; } // ------------------------------------------------------------ public boolean isEmpty() // true if nothing on stack { return (top == -1); } // ------------------------------------------------------------ } // end class StackX //////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. 'A') public boolean wasVisited; // ------------------------------------------------------------ public Vertex(char lab) // constructor { label = lab; wasVisited = false; } // ------------------------------------------------------------ } // end class Vertex //////////////////////////////////////////////////////////////// class Graph { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix private int nVerts; // current number of vertices private StackX theStack; // ------------------------------------------------------------ public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int y=0; y