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

Limitation on cost 0 #17

Open
atanarro opened this issue May 1, 2017 · 3 comments
Open

Limitation on cost 0 #17

atanarro opened this issue May 1, 2017 · 3 comments
Milestone

Comments

@atanarro
Copy link

atanarro commented May 1, 2017

Looking at the function isValidNode I see that a cost of 0 is forbidden.

function isValidNode(val) {
  const cost = Number(val);

  if (isNaN(cost) || cost <= 0) {
    return false;
  }

  return true;
}

I think a cost of 0 is a valid situation meaning that there is a connection between 2 nodes while it is costless.

Is there any reason for this limitation?

@albertorestifo
Copy link
Owner

The cost in the Dijkstra algorithm is expected to be a factor of the distance between nodes. If a node cost in 0, it means that node is in the same position as the neighboring node. This means the node will always be visited, no matter what.

Supposing the two nodes have the same connections, this means the node will always appear in the result. Suppose we have:

a:
   b: 3
   c: 2

b:
   a: 0
   c: 2

c:
   d: 1
   a: 2
   b: 2

If we start from A to D, the path will be A - B - C - D, despite A - B being in the same spot.

Supposing the two nodes have a different connection. If the cost between them is 0, it means they are in the same spot. Consequentially they should be merged into one.

Maybe I'm missing a use-case where you want a connection with cost 0? It would not break the algorithm but might lead to results containing more nodes than necessary, which is why there is the check.

@atanarro
Copy link
Author

atanarro commented May 5, 2017

The problem I was solving is this one.

Basically the solution is using graphs like:

1:
   2: 1
2:
   1: 0
   3: 2
3:
   2: 0
   4: 3
   5: 1
4:
   3: 0
   6: 1
   5: 4
5:
   4: 0
   6: 5
6:
   5: 0

It has a cost to move from 1 to 2 but it is free to move the other way around (2 to 1). On this case the nodes are different and it is ok to include them on the path. For instance moving from 1 to 6 is faster 1-2-3-5-4-6 than 1-2-3-4-5-6.

@albertorestifo albertorestifo added this to the v2.5.1 milestone May 5, 2017
@albertorestifo albertorestifo modified the milestones: v2.5.1, v2.6.0 May 10, 2018
@lucasjvw
Copy link

I agree that costs of 0 should be valid.
https://stackoverflow.com/questions/49460439/can-dijkstras-algorithm-work-on-a-graph-with-weights-of-0

Dijkstra's algorithm doesn't necessarily mean distance - just cost. Maybe it is free to move from node A to node B, but not free to move from node A to node C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants