Question: I ' m learning depth - first search, and for some reason, my output is different from the correct output. The correct output should be

I'm learning depth-first search, and for some reason, my output is different from the correct output. The correct output should be DFS: A E F G B C D. However, my code switches the last two nodes, giving the result of DFS: A E F G B D C. The only part of the program that can be modified is the depthFirstSearch method.
I would love to know what I'm doing wrong.
GraphInfo.txt (NOT MODIFIABLE)
A,C,D,E
B,E
C,A,E,F,G
D,A,E
E,A,B,C,D,F
F,C,E,G
G,C,F
Node.java(NOT MODIFIABLE)
import java.util.ArrayList;
public class Node
{
private String name;
private ArrayList adjancencyList;
public Node(String name)
{
this.name = name;
adjancencyList = new ArrayList();
}
public String getName()
{
return name;
}
public ArrayList getAdjacencyList()
{
return adjancencyList;
}
public void addAdjacentNode(Node n)
{
adjancencyList.add(n);
}
@Override
public boolean equals(Object o)
{
if(!(o instanceof Node))
return false;
Node n =(Node) o;
return this.name.equalsIgnoreCase(n.name);
}
public String toString()
{
String str ="Node: "+ name +" Adjacency List:";
for(Node a : adjancencyList)
{
str +=""+ a.getName();
}
return str;
}
}
Graph.java (ONLY the depthFirstSearch method is modifiable)
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
public class Graph
{
public static void main(String[] args) throws IOException
{
ArrayList graph = createGraph();
displayGraph(graph);
depthFirstSearch(graph);
}
public static ArrayList createGraph() throws IOException
{
ArrayList graph = new ArrayList();
File file = new File("GraphInfo.txt");
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext())
{
String line = inputFile.nextLine();
String[] tokens = line.split(",");
if(tokens.length >0)
{
Node n = new Node(tokens[0]);
int nodeIndex = graph.indexOf(n);
if(nodeIndex !=-1)
n = graph.get(nodeIndex);
else
graph.add(n);
for(int i =1; i < tokens.length; i++)
{
Node adj = new Node(tokens[i]);
int adjNodeIndex = graph.indexOf(adj);
if(adjNodeIndex !=-1)
adj = graph.get(adjNodeIndex);
else
graph.add(adj);
n.addAdjacentNode(adj);
}
}
}
return graph;
}
public static void displayGraph(ArrayList graph)
{
for(Node n : graph)
{
System.out.println(n);
}
}
public static void depthFirstSearch(ArrayList graph)
{
System.out.println("DFS:");
Node startNode = graph.get(0);
Stack stack = new Stack<>();
ArrayList visitedSet = new ArrayList<>();
visitedSet.add(startNode);
stack.push(startNode);
while (!stack.isEmpty()){
Node currentNode=stack.pop();
visitedSet.add(currentNode);
System.out.print(currentNode.getName()+"");
ArrayList adjacentNodes = currentNode.getAdjacencyList();
for (Node adjNode : adjacentNodes){
if (!visitedSet.contains(adjNode)){
visitedSet.add(adjNode);
stack.push(adjNode);
}
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!