Traversal algorithms with Java

22 views
Skip to first unread message

DEX graphdb

unread,
Feb 14, 2013, 8:24:55 AM2/14/13
to dex...@googlegroups.com
Since 4.5 DEX includes traversal algorithms for Java (see old blog announcement: http://sparsity-technologies.com/blog/?p=540).

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.

If you want to perform a BFS search (More info: http://en.wikipedia.org/wiki/Breadth-first_search):

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();

Reply all
Reply to author
Forward
0 new messages