我必须为学校作业实现搜索算法。现在,我在使用单一 Java 实现时遇到了问题。这是我目前拥有的代码(我一直以我在 stackoverflow 中找到的一些代码为基础进行 dfs 搜索,然后我必须添加验证以满足项目标准):
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);
}
}
}
}
我遇到的问题是,我只能提交一个 java 文件,所以我将所有内容都放在这个文件中。 当我到达 findAllPaths 函数时,他不识别“startNode”常量(eclipse 说它不能解析为变量)并且他说“adjacentNodes”函数没有为 Grafo 类型定义。
无论如何我都可以解决这个问题,还是我必须重新考虑我这样做的方式,如果可以,什么是实现这个的好方法?
最佳答案
其他人似乎已经解决了您的一些编码问题,所以我只想说明如何使用单个文件约束。
假设您在三个单独的文件中开发了 3 个类,Edge、Node 和 Graph。以下是如何组合它们:
// in file GraphSearchHomework.java
public class GraphSearchHomework {
public static class Edge {
// ... Edge code here ...
// You can reference Nodes and Graphs
// just as if this was in a separate file
}
public static class Node {
// ... Node code here ...
// You can reference Edges and Graphs
// just as if this was in a separate file
}
public static class Graph {
// ... Graph code here ...
// You can reference Edges and Nodes
// just as if this was in a separate file
}
public static void main(String args[]) {
// This main can instantiate Graphs, Nodes, Edges
// Keep this simple, though. Most code belongs outside of main.
}
}
您似乎在试图将其强制放入一个文件中时犯了一些错误。如果对您有帮助,请继续单独开发每个类,然后将它们组合起来。
关于java - 在单个java文件中搜索算法(图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788453/