简体   繁体   English

用Java实现最短路径算法的数据结构

[英]Data structure for implementing shortest path algorithm in Java

I have been doing some indoor navigation stuffs and need to find the shortest path between two specific nodes. 我一直在做一些室内导航,需要找到两个特定节点之间的最短路径。 By doing some search found that A* algorithm is more efficient than dijkstra's algorithm. 通过进行一些搜索,发现A *算法比dijkstra算法更有效。 But I have no ideas what to do with my raw data which are: 但是我不知道该如何处理我的原始数据:

  1. node information: containing node IDs and x, y coordinates, totally three numbers. 节点信息:包含节点ID和x,y坐标,共三个数字。 for example, {node id = 1, x = 0, y = 0}, {node id = 2, x = 1, y = 1} 例如, {node id = 1, x = 0, y = 0}, {node id = 2, x = 1, y = 1}
  2. path information: containing a set of node IDs as intermediates to the path. 路径信息:包含一组节点ID作为路径的中间节点。 for example, path = {1, 2, 3, 4} 例如,路径= {1, 2, 3, 4}

As you can see, these information don't have "distance" values which are used as "weight" in many shortest path algorithms, this means that I have to compute distance between two adjacent nodes before implement any path finding algorithm. 如您所见,这些信息没有在许多最短路径算法中用作“权重”的“距离”值,这意味着我必须在实现任何路径查找算法之前计算两个相邻节点之间的距离。

This can be just overwhelming to me not have much coding experience. 没有太多的编码经验,这可能会让我不知所措。 Can someone help me from the basic concept on how to create a data structure storing node information and path information and further reference it by node id. 有人可以从关于如何创建存储节点信息和路径信息并进一步通过节点ID引用数据结构的基本概念中帮助我。

This is somehow opinion-based, and depends on the way you want to implement it. 这是以某种方式基于意见的,并且取决于您要实现它的方式。

I would suggest one class to represent a node and one for edges: 我建议一个类代表一个节点,一个代表边缘:

class Node {
    Node previousNode;
    double weight;    // actual cost to reach this node
    ...  // problem specific attributes (id, x, y)
}

class Edge {
    Node node1, node2;
    double weight;    // cost to 'use' this edge
    ... // problem specific attributes (road number?)
}

Eventually it would be helpful to add a field to Node to hold all Edges that start/ends at that Node. 最终,将一个字段添加到Node以容纳在该Node处开始/结束的所有Edge会很有帮助。

The Edge class maybe be not necessary for navigation if cost equals distance and the distance between two connected nodes is just the Euclidean distance. 如果成本等于距离并且两个相连节点之间的距离只是欧几里得距离,则Edge类对于导航可能不是必需的。

Because you are going to use heuristics I would recommend: 因为您将要使用启发式方法,所以我建议:

class Node {
    Node prevNode;  //parent node
    int x;
    int y;
    int gn;    // cost to reach this node
    float hn;  // node's heuristic value
    float fn;  // evaluation function. Optional, you could do gn + hn
}

Before expanding a node you need to calculate the distance to reach each of its child nodes. 扩展节点之前,您需要计算到达每个子节点的距离。

When you expand a node (using evaluation function): 扩展节点时(使用评估功能):

g (Node) = g (Parent) + distance (Parent-Node) g(节点) = g(父 节点) + 距离(父节点)

f (Node) = g (Node) + h (Node) f(节点) = g(节点) + h(节点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM