Connected components algorithms with .Net

4 views
Skip to first unread message

DEX graphdb

unread,
Feb 14, 2013, 8:35:21 AM2/14/13
to dex...@googlegroups.com
Since 4.5 DEX includes connected components algorithms for .Net (see blog announcement: http://sparsity-technologies.com/blog/?p=540).

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 would like to indentify weak connected components, DFS is your algorithm:

        System.Console.WriteLine("Weak Connectivity DFS");
        // Create a new WeakConnectivityDFS
        WeakConnectivityDFS weakConnDFS = new WeakConnectivityDFS(sess);
        // Allow the user of all the edge types
        weakConnDFS.AddAllEdgeTypes();
        // Allow the use of all the node types
        weakConnDFS.AddAllNodeTypes();
        // Don't set a materialized attribute
   // Calculate the weakly connected components
        weakConnDFS.Run();
   // Get the connected components
        ConnectedComponents weakCC = weakConnDFS.GetConnectedComponents();
        long numWeakComponents = weakCC.GetCount();
        System.Console.WriteLine("Weakly connnected componennts: "+numWeakComponents);
        for (long ii = 0; ii < weakCC.GetCount(); ii++)
        {
            Objects ccNodes = weakCC.GetNodes(ii);
            long numNodes = ccNodes.Count();
            System.Console.WriteLine("Connected component "+ii+" has "+numNodes+" nodes.");
            ccNodes.Close();
        }
   // Close the connected components
        weakCC.Close();
        // Close the WeakConnectivityDFS
        weakConnDFS.Close();

Otherwise, if you want to retrieve strong connected components you should use Gabow instead (More info http://en.wikipedia.org/wiki/Cheriyan%E2%80%93Mehlhorn/Gabow_algorithm):

        System.Console.WriteLine("Strong Connectivity Gabow");
        // Create a new StrongConnectivityGabow
        StrongConnectivityGabow strongConnGabow = new StrongConnectivityGabow(sess);
        // Allow the user of all the edge types in outgoing direction
        strongConnGabow.AddAllEdgeTypes(EdgesDirection.Outgoing);
        // Allow the use of all the node types
        strongConnGabow.AddAllNodeTypes();
        // Don't set a materialized attribute
   // Calculate the weakly connected components
        strongConnGabow.Run();
   // Get the connected components
        ConnectedComponents strongCC = strongConnGabow.GetConnectedComponents();
        long numStrongComponents = strongCC.GetCount();
        System.Console.WriteLine("Strongly connnected componennts: "+numStrongComponents);
        for (long ii = 0; ii < strongCC.GetCount(); ii++)
        {
            Objects ccNodes = strongCC.GetNodes(ii);
            long numNodes = ccNodes.Count();
            System.Console.WriteLine("Connected component "+ii+" has "+numNodes+" nodes.");
            ccNodes.Close();
        }
   // Close the connected components
        strongCC.Close();
        // Close the StrongConnectivityGabow
        strongConnGabow.Close();


Reply all
Reply to author
Forward
0 new messages