Network Action Set

Detecting Communities for an Undirected Graph

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Visual Data Mining and Machine Learning: Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

This example illustrates the use of the community action on the undirected graph G shown in Figure 4.

Figure 4: An Undirected Graph G

An Undirected Graph


The undirected graph G can be represented by the following links data set, LinkSetIn:

data LinkSetIn;
   input from $ to $ @@;
   datalines;
A B  A F  A G  B C  B D
B E  C D  E F  G I  G H
H I
;

The following DATA step loads the LinkSetIn data set into a CAS data table named mycas.LinkSetIn. These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.LinkSetIn;
   set LinkSetIn;
run;

The following statements perform community detection and output the results in the specified data tables:

proc cas;
   loadactionset "network";
   action network.community result=r status=s /
      indexOffset    = 1
      links          = {name = "LinkSetIn"}
      outNodes       = {name = "NodeSetOut", replace=true}
      outLevel       = {name = "CommLevelOut", replace=true}
      outCommunity   = {name = "CommOut", replace=true}
      outOverlap     = {name = "CommOverlapOut", replace=true}
      outCommLinks   = {name = "CommLinksOut", replace=true}
      resolutionList = {1.0, 0.5};
   run;
   print r.ProblemSummary; run;
   print r.SolutionSummary; run;
   action table.fetch / table = "NodeSetOut"; run;
   action table.fetch / table = "CommLevelOut" sortBy = "level"; run;
   action table.fetch / table = "CommOut" sortBy = {"community","level"}; run;
   action table.fetch / table = "CommOverlapOut" sortBy = "community"; run;
   action table.fetch / table = "CommLinksOut" sortBy = "level"; run;
quit;

The problem summary output from this action is shown in Output 22.4.1.

Output 22.4.1: Problem Summary

Problem Summary
Number of Nodes9
Number of Links11
Graph DirectionUndirected


The solution summary output from this action is shown in Output 22.4.2.

Output 22.4.2: Solution Summary

Solution Summary
Problem TypeCommunity Detection
Solution StatusOK
CPU Time0.00
Real Time0.00


The output data table NodeSetOut contains the community identifier of each node, as shown in Output 22.4.3.

Output 22.4.3: Community Detection on an Undirected Graph: Nodes Output

Selected Rows from Table NODESETOUT
_Index_nodecommunity_1community_2
1A11
2B21
3C21
4D21
5E11
6F11
7G32
8H32
9I32


The output data table CommLevelOut contains summary information at each resolution level, as shown in Output 22.4.4.

Output 22.4.4: Community Detection on an Undirected Graph: Level Output

Selected Rows from Table COMMLEVELOUT
_Index_levelresolutioncommunitiesmodularity
11130.3925619835
220.520.3429752066


The output data table CommOut contains properties about each community, as shown in Output 22.4.5.

Output 22.4.5: Community Detection on an Undirected Graph: Community Summary

Selected Rows from Table COMMOUT
_Index_levelresolutioncommunitynodesintra_linksinter_linksdensitycut_ratioconductance
11113230.66666666670.16666666670.4285714286
220.516710.46666666670.05555555560.0666666667
311233210.11111111110.25
420.5233110.05555555560.1428571429
511333110.05555555560.1428571429


The output data table CommOverlapOut contains community overlap information, as shown in Output 22.4.6.

Output 22.4.6: Community Detection on an Undirected Graph: Community Overlap

Selected Rows from Table COMMOVERLAPOUT
_Index_nodecommunityintensity
1C11
2B11
3A10.6666666667
4D11
5E11
6F11
7G10.3333333333
8H21
9G20.6666666667
10I21
11A20.3333333333


The output data table CommLinksOut describes how the communities are connected, as shown in Output 22.4.7.

Output 22.4.7: Community Detection on an Undirected Graph: Intercommunity Links

Selected Rows from Table COMMLINKSOUT
_Index_levelresolutionfrom_communityto_communitylink_weight
111131
211122
320.5121


Detecting Communities for an Undirected Graph

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the LinkSetIn data to the comma-separated-value (CSV) file LinkSetIn.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="LinkSetIn.csv"}

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

The following statements perform community detection and output the results in the specified data tables:

s:network_community{
   indexOffset    = 1,
   links          = {name = "LinkSetIn"},
   outNodes       = {name = "NodeSetOut", replace=true},
   outLevel       = {name = "CommLevelOut", replace=true},
   outCommunity   = {name = "CommOut", replace=true},
   outOverlap     = {name = "CommOverlapOut", replace=true},
   outCommLinks   = {name = "CommLinksOut", replace=true},
   resolutionList = {1.0, 0.5}}

Detecting Communities for an Undirected Graph

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the LinkSetIn data to the comma-separated-value (CSV) file LinkSetIn.csv and then use the following code to load the CSV file into CAS:

s.upload_file('LinkSetIn.csv')

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

The following statements perform community detection and output the results in the specified data tables:

s.network.community(
    indexOffset    = 1,
    links          = {"name": "LinkSetIn"},
    outNodes       = {"name": "NodeSetOut", "replace":True},
    outLevel       = {"name": "CommLevelOut", "replace":True},
    outCommunity   = {"name": "CommOut", "replace":True},
    outOverlap     = {"name": "CommOverlapOut", "replace":True},
    outCommLinks   = {"name": "CommLinksOut", "replace":True},
    resolutionList = {1.0, 0.5})

Detecting Communities for an Undirected Graph

This example is not available for the R programming language.

Last updated: November 04, 2020