I'm trying to write a routing/path finding algorithm in JavaScript for the following network which consists of nodes. Each node has rules governing whether it can serve as an entry point, exit point, and/or be traversed.
Given a valid entry point is selected, I need to solve the following two problems:
Rules of the network:
I have represented the network as an array of objects, which each object in the array representing a node on the network.
var network = [{
id: 'A',
canBeEntry: true,
ifEntryRouteIs: 'CW',
isExitIfRouteIs: 'N/A',
neighbourCW: ['B'],
neighbourCCW: ['H']
}, {
id: 'B',
canBeEntry: true,
ifEntryRouteIs: 'CW&CCW',
isExitIfRouteIs: 'CW',
neighbourCW: ['C'],
neighbourCCW: ['A']
},
.
.
.
{
id: 'H',
canBeEntry: false,
ifEntryRouteIs: 'N/A',
isExitIfRouteIs: 'N/A',
neighbourCW: ['A'],
neighbourCCW: ['G']
}];
The graphs describing the valid entry/exit nodes, transitions, and weightings would be as follows:
I've done a bit or research on algorithms related to pathfinding and routing such as Dijkstra's or A* search, but they seem to be based on costs and weightings, which I don't think applies to my network.
Does anyone have any ideas or approach on how to write an algorithm in JavaScript to first determine the exit points, then the route?
The jsfiddle of what I'm attempting to implement in JavaScript is here http://jsfiddle.net/mkov/dgGHm/
If I understood correctly, you want to find given a starting node and the direction, the possible exits and the shortest path to them. This should work: (had to modify the original data a bit):
var validExitPoints =[];
var nodes=[];
var tree_path=[];
nodes.push({node:entryPoint});
visited[entryPoint]=true;
while(!nodes.length==0){
var node=network_obj[nodes[0].node];
nodes.shift();
if(canExit(node.id,dir)){
validExitPoints.push({node:node.id,path:''});
}
var neighbours=getNeighbours(node.id,dir);
for(var i=0;i<neighbours.length;i++){
if(visited[neighbours[i]]==false){
nodes.push({node:neighbours[i]});
visited[neighbours[i]]=true;
tree_path[neighbours[i]]=node.id;
}
}
}
Thats the main code, for full code go here:
http://jsfiddle.net/juvian/dgGHm/6/
I used Breadth-first search for this, you can read more about it here: