Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose parent in distance function #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions a-star/a-greedy-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports.l1 = heuristics.l1;
* while allowing the graph to be reused without rebuilding.
* @param {Function(a, b)} options.heuristic - a function that returns estimated distance between
* nodes `a` and `b`. Defaults function returns 0, which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, link, parent)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
* @param {Boolean} options.oriented - whether graph should be considered oriented or not.
*
Expand Down Expand Up @@ -164,7 +164,7 @@ function aStarBi(graph, options) {
function reconstructBiDirectionalPath(a, b) {
var pathOfNodes = [];
var aParent = a;
while(aParent) {
while (aParent) {
pathOfNodes.push(aParent.node);
aParent = aParent.parent;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ function aStarBi(graph, options) {
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherSearchState.node, cameFrom.node, link);
var tentativeDistance = cameFrom.distanceToSource + distance(otherSearchState.node, cameFrom.node, link, cameFrom.parent && cameFrom.parent.node);

if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
Expand Down
4 changes: 2 additions & 2 deletions a-star/a-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports.l1 = heuristics.l1;
* nodes `a` and `b`. This function should never overestimate actual distance between two
* nodes (otherwise the found path will not be the shortest). Defaults function returns 0,
* which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, c)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
* @param {Boolean} options.oriented - whether graph should be considered oriented or not.
*
Expand Down Expand Up @@ -123,7 +123,7 @@ function aStarPathSearch(graph, options) {
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link);
var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link, cameFrom.parent && cameFrom.parent.node);
if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
return;
Expand Down
6 changes: 3 additions & 3 deletions a-star/nba/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports.l1 = heuristics.l1;
* nodes `a` and `b`. This function should never overestimate actual distance between two
* nodes (otherwise the found path will not be the shortest). Defaults function returns 0,
* which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, link, parent)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
*
* @returns {Object} A pathfinder with single method `find()`.
Expand Down Expand Up @@ -186,7 +186,7 @@ function nba(graph, options) {

if (blocked(cameFrom.node, otherNode, link)) return;

var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link);
var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link, cameFrom.p1 && cameFrom.p1.node);

if (tentativeDistance < otherSearchState.g1) {
otherSearchState.g1 = tentativeDistance;
Expand Down Expand Up @@ -216,7 +216,7 @@ function nba(graph, options) {

if (blocked(cameFrom.node, otherNode, link)) return;

var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link);
var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link, cameFrom.p2 && cameFrom.p2.node);

if (tentativeDistance < otherSearchState.g2) {
otherSearchState.g2 = tentativeDistance;
Expand Down
16 changes: 8 additions & 8 deletions dist/ngraph.path.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module.exports.l1 = heuristics.l1;
* while allowing the graph to be reused without rebuilding.
* @param {Function(a, b)} options.heuristic - a function that returns estimated distance between
* nodes `a` and `b`. Defaults function returns 0, which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, link, parent)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
* @param {Boolean} options.oriented - whether graph should be considered oriented or not.
*
Expand Down Expand Up @@ -289,7 +289,7 @@ function aStarBi(graph, options) {
function reconstructBiDirectionalPath(a, b) {
var pathOfNodes = [];
var aParent = a;
while(aParent) {
while (aParent) {
pathOfNodes.push(aParent.node);
aParent = aParent.parent;
}
Expand Down Expand Up @@ -333,7 +333,7 @@ function aStarBi(graph, options) {
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherSearchState.node, cameFrom.node, link);
var tentativeDistance = cameFrom.distanceToSource + distance(otherSearchState.node, cameFrom.node, link, cameFrom.parent && cameFrom.parent.node);

if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
Expand Down Expand Up @@ -397,7 +397,7 @@ module.exports.l1 = heuristics.l1;
* nodes `a` and `b`. This function should never overestimate actual distance between two
* nodes (otherwise the found path will not be the shortest). Defaults function returns 0,
* which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, c)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
* @param {Boolean} options.oriented - whether graph should be considered oriented or not.
*
Expand Down Expand Up @@ -490,7 +490,7 @@ function aStarPathSearch(graph, options) {
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link);
var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link, cameFrom.parent && cameFrom.parent.node);
if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
return;
Expand Down Expand Up @@ -711,7 +711,7 @@ module.exports.l1 = heuristics.l1;
* nodes `a` and `b`. This function should never overestimate actual distance between two
* nodes (otherwise the found path will not be the shortest). Defaults function returns 0,
* which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* @param {Function(a, b, link, parent)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
*
* @returns {Object} A pathfinder with single method `find()`.
Expand Down Expand Up @@ -868,7 +868,7 @@ function nba(graph, options) {

if (blocked(cameFrom.node, otherNode, link)) return;

var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link);
var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link, cameFrom.p1 && cameFrom.p1.node);

if (tentativeDistance < otherSearchState.g1) {
otherSearchState.g1 = tentativeDistance;
Expand Down Expand Up @@ -898,7 +898,7 @@ function nba(graph, options) {

if (blocked(cameFrom.node, otherNode, link)) return;

var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link);
var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link, cameFrom.p2 && cameFrom.p2.node);

if (tentativeDistance < otherSearchState.g2) {
otherSearchState.g2 = tentativeDistance;
Expand Down
Loading