Search code examples
javascriptalgorithmnetwork-programmingtreepath-finding

Routing or pathfinding algorithm for multi-direction network in JavaScript


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.

Routable Network

Given a valid entry point is selected, I need to solve the following two problems:

  1. What are the valid and available exit points for the network
  2. Once a valid exit point is determined and selected, what is the optimal route (nodes that need to be traversed)

Rules of the network:

  1. You can only enter nodes with entry points i.e. Nodes A, B C, D, E, F, G
  2. You can only exit nodes with exit points i.e. Nodes B, C, E,, F, G
  3. The direction(s) of travel is dictated by the entry node - this governs which direction you can travel and exit. .e.g. Clockwise travel and exit when entering via Node A
  4. You cannot change direction once you have entered 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:

Network Graphs

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/


Solution

  • 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:

    http://en.wikipedia.org/wiki/Breadth-first_search