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

If already a end distance is found, only ways which are shorter are calculated #15

Open
wants to merge 1 commit into
base: master
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
35 changes: 20 additions & 15 deletions shortestPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ const findShortestPath = (graph, startNode, endNode) => {
// find its distance from the start node & its child nodes
let distance = distances[node];
let children = graph[node];
// for each of those child nodes
for (let child in children) {
// make sure each child node is not the start node
if (String(child) === String(startNode)) {
continue;
} else {
// save the distance from the start node to the child node
let newdistance = distance + children[child];
// if there's no recorded distance from the start node to the child node in the distances object
// or if the recorded distance is shorter than the previously stored distance from the start node to the child node
// save the distance to the object
// record the path
if (!distances[child] || distances[child] > newdistance) {
distances[child] = newdistance;
parents[child] = node;

// if already a end distance is found, we allow only ways which are shorter
if (!distances[endNode] || distances[endNode] > distance) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is new

// for each of those child nodes
for (let child in children) {
// make sure each child node is not the start node
if (String(child) === String(startNode)) {
continue;
} else {
// save the distance from the start node to the child node
let newdistance = distance + children[child];
// if there's no recorded distance from the start node to the child node in the distances object
// or if the recorded distance is shorter than the previously stored distance from the start node to the child node
// save the distance to the object but if already a end distance is found, it must be shorter then it
// record the path
if ((!distances[child] || distances[child] > newdistance)
&& (!distances[endNode] || distances[endNode] > newdistance)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this line is new

distances[child] = newdistance;
parents[child] = node;
}
}
}
}
Expand Down
49 changes: 27 additions & 22 deletions shortestPathWithLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,34 @@ const findShortestPathWithLogs = (graph, startNode, endNode) => {
// find its distance from the start node & its child nodes
let distance = distances[node];
let children = graph[node];
// for each of those child nodes
for (let child in children) {
// make sure each child node is not the start node
if (String(child) === String(startNode)) {
console.log("don't return to the start node! 🙅");
continue;
} else {
console.log("startNode: " + startNode);
console.log("distance from node " + parents[node] + " to node " + node + ")");
console.log("previous distance: " + distances[node]);
// save the distance from the start node to the child node
let newdistance = distance + children[child];
console.log("new distance: " + newdistance);
// if there's no recorded distance from the start node to the child node in the distances object
// or if the recorded distance is shorter than the previously stored distance from the start node to the child node
// save the distance to the object
// record the path
if (!distances[child] || distances[child] > newdistance) {
distances[child] = newdistance;
parents[child] = node;
console.log("distance + parents updated");

// if already a end distance is found, we allow only ways which are shorter
if (!distances[endNode] || distances[endNode] > distance) {
// for each of those child nodes
for (let child in children) {
// make sure each child node is not the start node
if (String(child) === String(startNode)) {
console.log("don't return to the start node! 🙅");
continue;
} else {
console.log("not updating, because a shorter path already exists!");
console.log("startNode: " + startNode);
console.log("distance from node " + parents[node] + " to node " + node + ")");
console.log("previous distance: " + distances[node]);
// save the distance from the start node to the child node
let newdistance = distance + children[child];
console.log("new distance: " + newdistance);
// if there's no recorded distance from the start node to the child node in the distances object
// or if the recorded distance is shorter than the previously stored distance from the start node to the child node
// save the distance to the object but if already a end distance is found, it must be shorter then it
// record the path
if ((!distances[child] || distances[child] > newdistance)
&& (!distances[endNode] || distances[endNode] > newdistance)) {
distances[child] = newdistance;
parents[child] = node;
console.log("distance + parents updated");
} else {
console.log("not updating, because a shorter path already exists!");
}
}
}
}
Expand Down