I have to implement a search algorithm for a school assignment. Right now, i’m having problems with sole java implementation. Here’s the code i have at the moment (i have been basing myself in some code i found here in stackoverflow for the dfs search, then i have to add verifications to meet the project criteria):
import java.util.*;
public class dfs<Grafo> {
public static void main(String[] args) {
class Grafo{
private Map<Integer, LinkedHashSet<Integer>> map = new HashMap();
public void addEdge(int source, int destiny) {
LinkedHashSet<Integer> adjacente = map.get(source);
if(adjacente==null) {
adjacente = new LinkedHashSet();
map.put(source, adjacente);
}
adjacente.add(destiny);
}
public void addLink(int source, int destiny) {
addEdge(source, destiny);
addEdge(destiny, source);
}
public LinkedList<Integer> adjacentNodes(int last) {
LinkedHashSet<Integer> adjacente = map.get(last);
if(adjacente==null) {
return new LinkedList();
}
return new LinkedList<Integer>(adjacente);
}
}
Scanner input = new Scanner(System.in);
int numVertices = input.nextInt();
int numLinks = input.nextInt();
int startNode = input.nextInt();
int endNode = startNode;
Grafo mapa = new Grafo();
for(int i = 0; i<numLinks; i++){
mapa.addLink(input.nextInt(), input.nextInt());
}
List<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
Integer currentNode = startNode;
List<Integer> visited = new ArrayList<Integer>();
visited.add(startNode);
new dfs().findAllPaths(mapa, visited, paths, currentNode);
for(ArrayList<Integer> path : paths){
for (Integer node : path) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
private void findAllPaths(Grafo mapa, List<Integer> visited,
List<ArrayList<Integer>> paths, Integer currentNode) {
if (currentNode.equals(startNode)) {
paths.add(new ArrayList(Arrays.asList(visited.toArray())));
return;
}
else {
LinkedList<Integer> nodes = mapa.adjacentNodes(currentNode);
for (Integer node : nodes) {
if (visited.contains(node)) {
continue;
}
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(visited);
temp.add(node);
findAllPaths(mapa, temp, paths, node);
}
}
}
}
The problem i have with this, is that i can only turn in a single java file, so i’m putting everything in this single file.
When i get to the findAllPaths function he does not recognise the “startNode” constant (eclipse says that it cannot be be resolved to a variable) and he says “adjacentNodes” function is not defined for the type Grafo.
Is there anyway i can solve this problem or do i have to rethink the way i’m doing this, if so, what’s a good way to implement this?
Others seem to have addressed some of your coding issues, so I just wanted to illustrate how to work with the single file constraint.
Suppose you had 3 classes, Edge, Node, and Graph that you developed in three separate files. Here’s how you could combine them:
You seem to have made some mistakes trying to force this into one file. If it helps you, go ahead and develop each class separately and then combine them.