I have an undirected connected graph. I have implemented it using an adjacency matrix which is a 2 dimensional array.
As I understand, a DFS visits children nodes before siblings. BFS visits siblings before children.
I implemented these two like this:
public void DFT(int firstVertex) {
connectedVertices = 0;
int v;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < numVertices; i++) {
if(vertex[i] != null){
vertex[i].setPushed(false);
}
}
stack.push(firstVertex);
connectedVertices++;
vertex[firstVertex].setPushed(true);
while(!stack.isEmpty()){
v = stack.pop();
vertex[v].visit();
for (int i = 0; i < numVertices; i++) {
if(adj[v][i] != 0 && !vertex[i].getPushed()){
stack.push(i);
connectedVertices++;
vertex[i].setPushed(true);
}
}
}
}
public void BFT(int firstVertex) {
connectedVertices = 0;
int v;
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < numVertices; i++) {
if(vertex[i] != null){
vertex[i].setPushed(false);
}
}
queue.add(firstVertex);
connectedVertices++;
vertex[firstVertex].setPushed(true);
while(!queue.isEmpty()){
v = queue.remove();
vertex[v].visit();
for (int i = 0; i < numVertices; i++) {
if(adj[v][i] != 0 && !vertex[i].getPushed()){
queue.add(i);
connectedVertices++;
vertex[i].setPushed(true);
}
}
}
}
As it is these methods take only one parameter, the start vertex. What if I am asked to give the DFS and BFS from one node to a different node? Here is a simple example of a connected undirected graph.

If I am asked to perform a DFS from D to E, would it be D,C,A,E or D,E. I thought DFS and BFS must visit every node, in this case B cannot be visited. I dont know how I should change my current methods to satisfy these requirements.