Traversal algorithms with .NET

8 views
Skip to first unread message

Sparsity T

unread,
Feb 14, 2013, 8:20:37 AM2/14/13
to
Since 4.5 DEX includes traversal algorithms for .Net (see old blog announcement).

Here is an example of use for .Net:

First, remember to include in your code the algorithms package by adding:

using com.sparsity.dex.algorithms;

We assume here, that you already have a created db with nodes and edges included.

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

System.Console.WriteLine("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.Console.WriteLine("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.Console.WriteLine("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);
        // Limit the depth to 3 hops from the starting node
        // dfs.SetMaximumHops(3);
        // Get the nodes
        while (dfs.HasNext())
        {
            long nodeid = dfs.Next();
            int depth = dfs.GetCurrentDepth();
            System.Console.WriteLine("Node "+nodeid+" at depth "+depth+".");
        }
        // Close the traversal
        dfs.Close();

Reply all
Reply to author
Forward
0 new messages