Edges are of equal value.
PHP Code:
/**
* Finds the path from one object to another through the nodes variable.
*
* Node is an object which has nodes variable containing a list of connected node.
* Nodes are single directions. In order to make it two-directions, you have to define
* them separately.
**/
function findPath( startNode, endNode )
{
temp.nodeList.add( {startNode, new[0]} );
temp.nodeIndex = 0;
while ( temp.nodeList.size() > temp.nodeIndex )
{
temp.node = temp.nodeList[ temp.nodeIndex ][0];
temp.nodePath = temp.nodeList[ temp.nodeIndex ][1];
temp.nodePath.add( temp.node );
if ( temp.node == endNode )
return temp.nodePath;
for ( temp.subnode: node.nodes )
{
if ( temp.subnode in temp.nodeList )
continue;
temp.nodeList.add( { temp.subnode, temp.nodePath } );
}
temp.nodeIndex ++;
}
return null;
}
Example of Usage:
PHP Code:
function onCreated()
{
node1 = new TStaticVar("Node-1");
node2 = new TStaticVar("Node-2");
node3 = new TStaticVar("Node-3");
node4 = new TStaticVar("Node-4");
node1.nodes = { node2, node3 };
node2.nodes = { node1, node3 };
node3.nodes = { node4 };
node4.nodes = { node1 };
path = findPath( node1, node4 );
for ( node: path )
echo( node.name );
}