Avoiding nodes when finding a path
doesntgolf opened this issue · 1 comments
I'm trying to avoid certain nodes when finding a path. Right now, my intuition is to do route.removeNode('a')
for each of the nodes that need to be avoided, store the removed nodes in a temporary variable, then add them back in after finding the path. But I'm having a hard time with this since calling removeNode also removes all of that node's references in the rest of the graph.
The avoided nodes change for me on each path
call, so it'd be difficult for me to construct multiple graphs, and slow to re-create the graph each time I need to find a path.
I'm not sure if this makes sense (and would be a thing that can work) from the library's point of view, but as a user of the library it'd be really cool if I could specify nodes to avoid each time I call path
, something like route.path('a', 'z', { trim: false, cost: true, avoid: ['b', 'f', 'k', 'x'] })
.
Are there any potential solutions that I'm missing? And thanks, Alberto, for the awesome library!
That would be a nice-to-have feature indeed! It shouldn't be too difficult to implement so I'll try and have a crack at it when I have some time.
For your problem, a better workaround would be this (it only works with the latest version as it accepts a Map in the constructor):
let graph = new Graph();
// populate your graph in whichever way you prefer
// please don't use this in the future as it's an anti-pattern
// to access the graph directly
const fullGraph = graph.graph;
// remove the nodes you want to exclude
graph.removeNode('a');
// Do whatever you need to do
// create a new instance with the full graph
graph = new Graph(fullGraph);
I haven't tested it but it should work