AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

calculate

描述

HeuristicPolicyDijkstra类中的calculate方法用于计算从起点到所有顶点的最短路径并返回该最短路径。

语法

calculate(graph, source)

参数

  • graph:必需,表示要计算最短路径的图形。
  • source:必需,指定起点。

返回值

返回一个对象,其中包含 distancepredecessor 两个属性,分别表示从起点到每个顶点的最短路径长度和最短路径上的前一个顶点。

抛出异常

如果 graphsource 参数无效,则会引发异常。

示例

const graph = new Graph();
graph.addEdge('A', 'B', 1);
graph.addEdge('A', 'C', 3);
graph.addEdge('B', 'D', 3);
graph.addEdge('C', 'D', 1);

const heuristicPolicyDijkstra = new HeuristicPolicyDijkstra();
const result = heuristicPolicyDijkstra.calculate(graph, 'A');

console.log(result.distance); // {A: 0, B: 1, C: 3, D: 4}
console.log(result.predecessor); // {B: "A", C: "A", D: "B"}

上面的示例创建了一个包含四个节点和四条边的图形,并使用HeuristicPolicyDijkstra类的calculate方法计算从'A'到每个节点的最短路径。 最短路径长度存储在result对象的 distance 属性中,而最短路径上的前一个节点存储在 predecessor 属性中。 最后,该示例在控制台上打印了这两个属性的值。