[英]Trying to Implement dijkstra's algorithm (Running into Exception in thread that I can't quite fix)
I have to implement dijkstra's algorithm for an assignment to find the shortest path between various cities that are interconnected with roads.我必须实现 dijkstra 的分配算法,以找到与道路相连的各个城市之间的最短路径。 Each road has a fixed cost and each city has a fixed cost of stay.
每条道路都有固定的成本,每个城市都有固定的停留成本。 The assignment is just to find the shortest path between the first and second node with a minimum number of 3 nodes but I wrote mine to find the shortest path between a source and all nodes.
任务只是找到第一个和第二个节点之间的最短路径,最少有 3 个节点,但我写了我的来找到源和所有节点之间的最短路径。
The data is input by first giving the number of cities (nodes) and then the number of roads (links).通过首先给出城市(节点)的数量,然后是道路(链接)的数量来输入数据。 Then Each city(node) has it's cost read in in the format "#citie cost".
然后每个城市(节点)都以“#city cost”格式读取它的成本。 Then the links are read in as "#first_city #second_city cost".
然后将链接读入为“#first_city #second_city cost”。 I store them in a 2D array where the first dimension is the cities and the second is the cost of traveling to other cities from each one.
我将它们存储在一个二维数组中,其中第一个维度是城市,第二个维度是从每个城市前往其他城市的成本。 I originally had costs between cities that had no link's initialized as null but I started getting the Exception I'm currently dealing with.
我最初在没有链接初始化为 null 的城市之间有成本,但我开始得到我目前正在处理的异常。
Below is how I am inputting the data:以下是我输入数据的方式:
private Integer num_cities;
private Integer num_roads;
public static void main(String[] args) {
test p = new test();
p.num_cities = readInt();
p.num_roads = readInt();
Integer [][] roads = new Integer[p.num_cities][p.num_roads];
Integer [] hotel_cost = new Integer[p.num_cities];
//Reading in Hotel Costs at Each City
hotel_cost[0] = 0;
hotel_cost[1] = 0;
for (Integer i = 2; i < p.num_cities; i++) {
Integer hotel = readInt() - 1;
hotel_cost[hotel] = readInt();
}
//filling with no connections
for (Integer i = 1; i < p.num_cities; i++) {
for (Integer j = 0; j < p.num_roads; j++) {
roads[i][j] = 0;
}
}
//Reading in Gas Costs on Each Road
for (Integer i = 0; i < p.num_roads; i++) {
Integer a = readInt() - 1;
Integer b = readInt() - 1;
Integer cost = readInt();
roads[a][b] = cost;
roads[b][a] = cost;
}
p.dijkstra(roads, hotel_cost, 0);
}
}
My current dijkstra function is defined as below:我当前的 dijkstra function 定义如下:
public Integer minDistance(Integer cost[], Boolean b[]) {
Integer min = Integer.MAX_VALUE, index = -1;
for (Integer x = 0; x < num_cities; x++) {
if (b[x] == false && cost[x] <= min) {
min = cost[x];
index = x;
}
}
return index;
}
public void dijkstra(Integer roads[][], Integer hotel_cost[], Integer src) {
Integer cost[] = new Integer[num_cities];
Boolean b[] = new Boolean[num_cities];
for(Integer i = 0; i < num_cities; i++) {
cost[i] = Integer.MAX_VALUE;
b[i] = false;
}
cost[src] = 0;
for (Integer count = 0; count < num_cities; count++) {
Integer u = minDistance(cost, b);
b[u] = true;
for (Integer x = 0; x < num_roads; x++) {
if (!b[x] && (roads[u][x] + hotel_cost[u]) != 0 && cost[u] != Integer.MAX_VALUE && cost[u] + roads[u][x] + hotel_cost[u] < cost[x] ) {
cost[x] = cost[u] + roads[u][x] + hotel_cost[x];
}
}
}
printCostChart(cost, num_cities);
}
I keep getting the following exception:我不断收到以下异常:
Exception in thread "main" java.lang.NullPointerException
at cmsc401.dijkstra(cmsc401.java:72)
at cmsc401.main(cmsc401.java:116)
My current test input is:我当前的测试输入是:
5
7
3 78
4 87
5 98
1 4 98
5 4 45
1 5 140
4 3 87
2 5 150
3 5 109
3 2 73
The correct output for the cost to travel from the first node to the second node should be $388.从第一个节点到第二个节点的旅行成本的正确 output 应该是 388 美元。
I've tested a few things mainly trying to isolate the exception and it seemed to be in this part of the if statement:我测试了一些东西,主要是为了隔离异常,它似乎在 if 语句的这一部分:
!b[x] && (roads[u][x] + hotel_cost[u]) != 0
If you'd like to test it I can post the github link but I prefer to keep things private to stop any copying.如果您想对其进行测试,我可以发布 github 链接,但我更愿意将其保密以阻止任何复制。
As I've already stated in my comment I'd have to guess but I assume the attempt to unbox roads[u][x]
throws the NPE.正如我在评论中已经说过的那样,我不得不猜测,但我认为拆箱 road
roads[u][x]
的尝试会引发 NPE。
Why?为什么? Because you intialize it like this:
因为你像这样初始化它:
for (Integer i = 1; i < p.num_cities; i++) {
for (Integer j = 0; j < p.num_roads; j++) {
roads[i][j] = 0;
}
}
As you can see roads[0][j]
might never be initialized so whenever u
is 0 you get the NPE because all elements in roads[0]
are still null.正如您所看到的,
roads[0][j]
可能永远不会被初始化,所以每当u
为 0 时,您都会得到 NPE,因为 road roads[0]
中的所有元素仍然是 null。
You have a loop that sets roads[a][b]
etc. (commented with "//Reading in Gas Costs on Each Road") but since a
and b
are set to readInt() - 1;
您有一个循环设置
roads[a][b]
等(注释为“//读取每条道路的 Gas Costs”),但由于a
和b
设置为readInt() - 1;
those might never be 0 as well (at least I can't tell due to the missing code).那些也可能永远不会是 0 (至少由于缺少代码我无法分辨)。
TLDR: you might get hard to spot and understand NullPointerExceptions when using primitive wrappers (or arrays of those) like Integer
and Boolean
in situations where a primitive ( int
, boolean
etc.) is needed and auto-unboxing tries to convert null
to a primitive. TLDR: you might get hard to spot and understand NullPointerExceptions when using primitive wrappers (or arrays of those) like
Integer
and Boolean
in situations where a primitive ( int
, boolean
etc.) is needed and auto-unboxing tries to convert null
to a primitive .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.