j2ME 最快的 Dijkstra 算法

发布于 2024-07-18 13:38:26 字数 548 浏览 6 评论 0原文

有人可以帮助我更快地实现 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 分钟才能完成,在模拟器上则需要几分钟以上。 这对我来说看起来太多了。 有什么改进建议吗? 我已经实施了以下改进。

  1. 使用数组而不是向量。
  2. 确保内部循环不考虑访问过的节点。 我访问的所有节点都位于数组的末尾,并且我的节点知道计数。 所以,我可以轻松地完全跳过它们。

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.

  1. Using array instead of vector.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2024-07-25 13:38:26

我认为你的问题中的算法是错误的。 内部循环应该查看外部循环中项目的每个未访问的邻居:

for each (item in Q)
{
  for each (unvisited neighbour of item)
  {
  }
}

将其与 进行比较维基百科中的伪代码实现

 1  function Dijkstra(Graph, source):
 2      for each vertex v in Graph:           // Initializations
 3          dist[v] := infinity               // Unknown distance function from source to v
 4          previous[v] := undefined          // Previous node in optimal path from source
 5      dist[source] := 0                     // Distance from source to source
 6      Q := the set of all nodes in Graph
        // All nodes in the graph are unoptimized - thus are in Q
 7      while Q is not empty:                 // The main loop
 8          u := vertex in Q with smallest dist[]
 9          if dist[u] = infinity:
10              break                         // all remaining vertices are inaccessible
11          remove u from Q
12          for each neighbor v of u:         // where v has not yet been removed from Q.
13              alt := dist[u] + dist_between(u, v) 
14              if alt < dist[v]:             // Relax (u,v,a)
15                  dist[v] := alt
16                  previous[v] := u
17      return previous[]

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:

for each (item in Q)
{
  for each (unvisited neighbour of item)
  {
  }
}

Compare it with the pseudocode implementation in wikipedia:

 1  function Dijkstra(Graph, source):
 2      for each vertex v in Graph:           // Initializations
 3          dist[v] := infinity               // Unknown distance function from source to v
 4          previous[v] := undefined          // Previous node in optimal path from source
 5      dist[source] := 0                     // Distance from source to source
 6      Q := the set of all nodes in Graph
        // All nodes in the graph are unoptimized - thus are in Q
 7      while Q is not empty:                 // The main loop
 8          u := vertex in Q with smallest dist[]
 9          if dist[u] = infinity:
10              break                         // all remaining vertices are inaccessible
11          remove u from Q
12          for each neighbor v of u:         // where v has not yet been removed from Q.
13              alt := dist[u] + dist_between(u, v) 
14              if alt < dist[v]:             // Relax (u,v,a)
15                  dist[v] := alt
16                  previous[v] := u
17      return previous[]
送你一个梦 2024-07-25 13:38:26

您的实施有问题。 其复杂度为 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).

甜味拾荒者 2024-07-25 13:38:26

我参考了这个算法。 我在其他地方找到了一个更简单的算法。 请注意,如果我必须实现维基百科中的一个,则有两个内部循环。

while Q is not empty: //Outer loop. 
  u := vertex in Q with smallest dist[];// First inner loop. 
  .... 
  for each neighbor v of u: //Second inner loop. 

第二个内环较小。 它最多可能执行 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.

while Q is not empty: //Outer loop. 
  u := vertex in Q with smallest dist[];// First inner loop. 
  .... 
  for each neighbor v of u: //Second inner loop. 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文