- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在java中进行了简单的练习,但是eclipse总是显示错误NullPointerException
。
我必须编写一个可以从文件(顶点)读取数据的程序,接下来从这个数据我应该计算图形大小,接下来我必须使用 DFS 算法来计算一致的组件。
我知道我的代码不好,但我仍在学习 java(和英语:P)。
测试.txt
0 1 0 4
1 2 1 4 1 5 1 8 1 11
2 6
3 1 3 7
4 8
5 2 5 8
6 5 6 7 6 9
8 4
9 5 9 7
10 8 10 11
11 8 11 9 11 12
12 3 12 6 12 9
异常
Exception in thread "main" java.lang.NullPointerException
at Search.DFS(Search.java:17)
at AppClient.main(AppClient.java:25)
AppClient.class
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOError;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AppClient {
private static final String filename = "C:\\test.txt";
private static List<Integer> sizeGraphList = new ArrayList<Integer>();
private static List<Integer> numberOfVertex = new ArrayList<Integer>();
static Graph graph;
static Search search;
public static void main(String[] args) throws IOException {
graph = new Graph(getGraphSize());
addVertexToGraph();
search = new Search();
int result = search.DFS();
System.out.println("Składowe: " + result);
}
private static int getGraphSize() throws IOException {
String Path = filename;
Path filePath = Paths.get(Path);
Scanner in = new Scanner(filePath);
while (in.hasNext()) {
if (in.hasNextInt()) {
sizeGraphList.add(in.nextInt());
} else {
in.next();
}
}
for (Integer x : sizeGraphList) {
System.out.println(x);
}
int sizeGraph = getSizeOfVertex(sizeGraphList);
System.out.println("Size: " + sizeGraph);
return sizeGraph;
}
private static int getSizeOfVertex(List<Integer> listOfVertex) {
for (int i = 0; i < listOfVertex.size(); i++) {
numberOfVertex.add(listOfVertex.get(i));
}
for (int i = 0; i < numberOfVertex.size(); i++) {
for (int j = 1; j < numberOfVertex.size(); j++) {
if (i != j && numberOfVertex.get(i) == numberOfVertex.get(j)) {
numberOfVertex.remove(j);
}
}
}
int size = numberOfVertex.size();
return size;
}
private static void addVertexToGraph() {
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line = br.readLine();
while (line != null) {
// String[] edge = line.trim().split("[^\\d]+");
String[] edge = line.trim().split("\\s+");
for (int i = 0; i < edge.length - 1; i += 2) {
int v = Integer.parseInt(edge[i]);
int u = Integer.parseInt(edge[i + 1]);
//System.out.println(v + ", " + u);
graph.addEdge(v, u);
}
line = br.readLine();
}
} catch (IOException e) {
System.out.println("File does not exist!!!");
}
}
}
图.类
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class Graph {
public List<LinkedList<Integer>> graphVertices;
public int[] processingRange;
Graph(int graphSize) {
graphVertices = new ArrayList<>();
processingRange = new int[graphSize+1];
for (int i = 0; i < graphSize; i++) {
graphVertices.add(i, new LinkedList<Integer>());
}
}
void addEdge(int v, int u) {
LinkedList<Integer> list = graphVertices.get(v);
list.add(u);
graphVertices.set(v, list);
}
}
搜索.class
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class Search extends AppClient{
private Stack<Integer> vertexesStack;
private int range;
private int scc;
Graph graph;
public Search() {
}
int DFS() {
boolean visited[] = new boolean[graph.graphVertices.size()+1];
for (int i = 0; i < graph.graphVertices.size(); i++)
if (!visited[i]) {
dfsSearch(i, visited);
}
return scc;
}
private void dfsSearch(int v, boolean visited[]) {
graph.processingRange[v] = range++;
visited[v] = true;
vertexesStack.push(v);
if (!checkDistance(v, visited)) {
int top;
do {
top = vertexesStack.pop();
graph.processingRange[top] = graph.graphVertices.size();
} while (top != v);
scc++;
}
}
private boolean checkDistance(int v, boolean[] visited) {
int minRange = graph.processingRange[v];
for (int vertex : graph.graphVertices.get(v)) {
if (!visited[vertex]){
dfsSearch(vertex, visited);
}
if (graph.processingRange[vertex] < minRange) {
minRange = graph.processingRange[vertex];
}
}
if (minRange < graph.processingRange[v]) {
graph.processingRange[v] = minRange;
return true;
}
return false;
}
}
最佳答案
您没有在AppClient.java
中初始化搜索
addVertexToGraph();
search = new Search(); // add this
int result = search.DFS(); //possibly add graph as a parameter here
还要初始化 Search.java
中的 vertexesStack
并删除局部变量 graph
,因为您想引用 AppClient 中的变量。 java
private Stack<Integer> vertexesStack = new Stack<>(); // add this
private int range;
private int scc;
// Graph graph; // delete this
由于您的代码顶部没有 package x.y.z;
,您可能还需要更改 int DFS()
-> int DFS(Graph graph )
,然后在 AppClient.java
中将图表传递给您对 DFS()
关于java - 图 DFS 算法 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605355/
我正在寻找我的代码的复杂度计算。简单来说就是DFS中的DFS(depth first search)。 DFS 从头到尾(向后搜索)在图(状态机)上运行。每当到达开始时,它都会累积使其到达开始的字符串
我通过实现堆栈编写了一个迭代 DFS。现在我试图递归地编写相同的 DFS,但我遇到了问题。 我的问题是,当我迭代编写它时,我可以保留某些全局变量,例如 paths=[] 并在找到新路径时添加到其中。
此程序用于图的 dfs 遍历,一个函数是迭代方法,另一个函数是递归方法,但两者给出的答案不同从迭代中我得到 01234从递归我得到 02341 谁能解释我为什么? NOTE -> User is en
这个问题在这里已经有了答案: Iterative DFS vs Recursive DFS and different elements order (4 个答案) 关闭 8 年前。 我试图了解 D
首先请原谅我的英语这可能不太可能,或者以前没有人经历过这种情况,我真的非常需要帮助。这可能是我第一次在这里提问 我正在尝试在 azure 订阅中的两台服务器(LIVE1 和 LIVE2)上设置 dfs
我以迭代方式和递归方式实现了深度优先搜索算法。它们对于小尺寸(小于 1 MB)的文件都可以正常工作。然而,当我尝试在 50 MB 的文件上运行它们时,递归 DFS(9 秒)似乎比使用迭代方法(至少几分
我正在解决这个 dfs/bfs问题。 我编写了 DFS 的迭代版本和递归版本。 节点访问的顺序不同,我不明白为什么。 迭代 DFS: static void DFS (Integer root, Gr
我遇到了一个问题,我要在图中寻找一种特殊类型的节点。该算法按以下方式工作: bool findSpecial(Node n) { if(isSpecial(n)) return
我写了一个递归DFS算法来遍历一个图: void Graph::DFS(Node n) { std::cout void Graph::IterativeDFS(Node n) {
在我的算法中,我将通过 DFS 方法创建频繁模式,例如,我生成 A-A, A-A-B, A-A-B-C, .. .顺序。(这三种模式为频繁子图模式,A,B,C为节点,- 表示两个节点之间存在一条边。)
我对 hadoop 中的 dfs 有疑问。有人知道如何解决我的问题吗? [hduser@evghost ~]$ start-dfs.sh Starting namenodes on [evghost]
据说在未加权的图中不能用DFS求最短路径。我已阅读多篇文章和博客,但并不满意,因为对 DFS 稍加修改就可以实现。 我认为如果我们以这种方式使用改进的 DFS,那么我们可以找到距源的最短距离。 Ini
考虑到一个具有 14,000 个顶点和 14,000 个边的图,我想知道为什么 GraphX 比图的 java 实现花费更多的时间来获取从顶点到叶子的所有路径? java 实现:几秒钟 Graphx
我已按照 Apache“单节点设置”说明在单节点上设置 dfs.replication。 但是后来我按照“Cluster Setup”进行操作,但它没有提到这个属性,所以我不知道这是要在 Nameno
有没有办法修改 pd.read_html 使其返回数据帧而不是数据帧列表? 语境: 我正在尝试使用 pandas read_html 从网站导入表格。我知道 pd.read_html 返回一个 dfs
为什么 hdfs dfs -ls 指向与 hdfs dfs -ls/ 不同的位置? 从下面的截图中可以清楚地看到两个命令给出不同的输出: 以上输出的主要原因是什么? 最佳答案 来自官方源码org.ap
我没有在 hdfs-site.xml 文件中设置 dfs.name.dir 和 dfs.data.dir 值没有设置。他们会怎样?有趣的是,他们默认接受什么值? (如何接收他们的当前值?) 最佳答案
我试图用一个名称节点和四个数据节点配置 hadoop。我能够在一台机器上成功配置名称节点和作业跟踪器并将其启动。 但是在我要配置数据节点的机器上,我做了以下操作: 我将 hadoop-2.0.0-cd
我正在尝试在 ec2-instance 上安装 Hadoop-2.6.0。 我下载并安装了 Hadoop。我还设置了环境变量。尝试启动 hdfs 服务时出现以下错误。 [ec2-user@ip-10-
我是 hadoop 框架的新手,目前我正在处理大数据项目,在 Windows 7 中使用 cygwin、hadoop-0.19.1、eclipse-3.3.1 (Europa)。现在我正在尝试从 ha
我是一名优秀的程序员,十分优秀!