j2ME 最快的 Dijkstra 算法
有人可以帮助我更快地实现 Dijkstra 算法的 j2ME 吗? 我有两个循环,一个在另一个循环内。 像这样,
while(for each item in Q)
{
//...do something.
//the following loop is to find the minimum
for(all un-visited nodes in Q)
{
//.. do something to get min.
}
}
我有近 23000 个节点和连接它们的 50000 个边...在下面提到的所有改进之后,内部循环平均执行 169330131 次。 在我的 w910i 手机上需要 5 分钟才能完成,在模拟器上则需要几分钟以上。 这对我来说看起来太多了。 有什么改进建议吗? 我已经实施了以下改进。
- 使用数组而不是向量。
- 确保内部循环不考虑访问过的节点。 我访问的所有节点都位于数组的末尾,并且我的节点知道计数。 所以,我可以轻松地完全跳过它们。
Can anybody help me to make j2ME implementation of Dijkstra algorithm faster ? I have two loops, one inside another. Like this
while(for each item in Q)
{
//...do something.
//the following loop is to find the minimum
for(all un-visited nodes in Q)
{
//.. do something to get min.
}
}
I have almost 23000 nodes and 50000 edges connecting them...The inner loop executes an average of 169330131 times after all improvements mentioned below. This takes 5 minutes to complete on my w910i mobile and more than minutes on my emulator. This looks too much for me. Any suggestions for improvement? I have the following improvements already implemented.
- Using array instead of vector.
- Make sure that the inner loop does not consider the visited nodes. All my visited nodes are at the end of the array and I node know the count. So, I can easily skip them altogether.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题中的算法是错误的。 内部循环应该查看外部循环中项目的每个未访问的邻居:
将其与 进行比较维基百科中的伪代码实现:
I think your algorithm in the question is wrong. The inner loop should be looking at each unvisited neighbour of the item in the outer loop:
Compare it with the pseudocode implementation in wikipedia:
您的实施有问题。 其复杂度为 O(E + V * log2 (V))。
这意味着 50000 + 23000 * ~15 = 400 000 次迭代。
你当前的复杂度几乎是 O(V^2)。
Something wrong with your implementation. It's complexity is O(E + V * log2 (V)).
That means 50000 + 23000 * ~15 = 400 000 iterations.
Your current complexity is almost O(V^2).
我参考了这个算法。 我在其他地方找到了一个更简单的算法。 请注意,如果我必须实现维基百科中的一个,则有两个内部循环。
第二个内环较小。 它最多可能执行 4-5 个,因为每个节点最多有 5 个边。 总共 23000 个节点中,具有 2 条以上边的节点数量为 1000 个。 因此,内循环的执行时间可以忽略不计。 第一个内循环是问题所在。 寻找最小的节点。 由于我必须在 j2ME 设备上执行此操作,因此我必须使其占用尽可能少的空间。
I referred this algorithm. I found a simpler algorithm some other place. Please note that If I had to implement the one in Wikipedia, there are two inner loops.
The second inner loop is smaller. It might execute a maximum of 4-5 as there are at most 5 edges for each node. The number of nodes having more than 2 edges is 1000 out of 23000 total nodes. So, the execution time for inner loop is negligible. The first inner loop is the problem. Finding the smallest node. As I have to execute this on a j2ME device, I have to make it to take as much less space as possible.