Here is an example of use for Java API:
First, remember to include in your code the algorithms package by adding:
import com.sparsity.dex.algorithms.TraversalBFS;
import com.sparsity.dex.algorithms.TraversalDFS;
We assume that the db, nodes and edges have been created.
System.out.println("Traversal BFS");
// Create a new BFS traversal from the node "startingNode"
TraversalBFS bfs = new TraversalBFS(sess, startingNode);
// Allow the use of all the node types
bfs.addAllNodeTypes();
// Allow the use of all the edge types but only in outgoing direction
bfs.addAllEdgeTypes(EdgesDirection.Outgoing);
// Limit the depth to 3 hops from the starting node
bfs.setMaximumHops(3);
// Get the nodes
while (bfs.hasNext())
{
long nodeid = bfs.next();
int depth = bfs.getCurrentDepth();
System.out.println("Node "+nodeid+" at depth "+depth+".");
}
// Close the traversal
bfs.close();
If otherwise you want to traverse the graph in depth (DFS:
http://en.wikipedia.org/wiki/Depth-first_search):
System.out.println("Traversal DFS");
// Create a new DFS traversal from the node "startingNode"
TraversalDFS dfs = new TraversalDFS(sess, startingNode);
// Allow the use of all the node types
dfs.addAllNodeTypes();
// Allow the use of all the edge types but only in outgoing direction
dfs.addAllEdgeTypes(EdgesDirection.Outgoing);
// Get the nodes
while (dfs.hasNext())
{
long nodeid = dfs.next();
int depth = dfs.getCurrentDepth();
System.out.println("Node "+nodeid+" at depth "+depth+".");
}
// Close the traversal
dfs.close();