java - 在单个java文件中搜索算法(图)

标签 java algorithm

我必须为学校作业实现搜索算法。现在,我在使用单一 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/

相关文章:

java - 类似于 Guava 的 EventBus 的消息服务

java - Blogger API 示例代码

java - 如何从 JPA 中的父实体检索子数据

java - 在 Java 中获取加起来为 100 的 3 个数字的所有可能组合的算法

algorithm - 哈希表中的删除

java - Map<String,AtomicLong> 足以保证线程安全吗?

java - Spring 4 - MVC - URL参数和静态资源路径

algorithm - 给定一棵树,在 Log(n) 中找到从节点 'a' 到节点 'b' 的路径中的第 k 个节点?

arrays - 如果我们将数组划分为 log(n) 而不是 2,时间复杂度是多少?

java - 如何处理列表整数的列表并查找邻居?