Network Action Set
Finding the Connected Components of an Undirected Graph
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as one or more CAS tables or as one or more transient-scope tables. 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 Viya: 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 connectedComponents action on the undirected graph G shown in Figure 6.
Figure 6: An Undirected Graph G

You can represent the directed graph G by using the following nodes data set, NodeSetIn, and links data set, LinkSetIn:
data NodeSetIn;
input node $;
datalines;
J
;
data LinkSetIn;
input from $ to $ @@;
datalines;
A B A C B C C H D E D F D G F E G I K L
;
The following DATA steps load the NodeSetIn and LinkSetIn data sets into CAS data tables named mycas.NodeSetIn and 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.NodeSetIn;
set NodeSetIn;
run;
data mycas.LinkSetIn;
set LinkSetIn;
run;
The following statements find the connected components and output the results in the data table NodeSetOut:
proc cas;
loadactionset "network";
action network.connectedComponents result=r status=s /
indexOffset = 1
nodes = {name = "NodeSetIn"}
links = {name = "LinkSetIn"}
outNodes = {name = "NodeSetOut", replace=true};
run;
print r.ProblemSummary; run;
print r.SolutionSummary; run;
action table.fetch / table = "NodeSetOut" sortBy = "concomp"; run;
quit;
The problem summary output from this action is shown in Output 28.6.1.
Output 28.6.1: Problem Summary
| Problem Summary | |
|---|---|
| Number of Nodes | 12 |
| Number of Links | 10 |
| Graph Direction | Undirected |
The solution summary output from this action is shown in Output 28.6.2.
Output 28.6.2: Solution Summary
| Solution Summary | |
|---|---|
| Problem Type | Connected Components |
| Solution Status | OK |
| Number of Components | 4 |
| CPU Time | 0.00 |
| Real Time | 0.00 |
The output data table NodeSetOut contains the connected components of the input graph, as shown in Output 28.6.3.
Output 28.6.3: Connected Components of an Undirected Graph
| Selected Rows from Table NODESETOUT | ||
|---|---|---|
| _Index_ | node | concomp |
| 1 | C | 1 |
| 2 | B | 1 |
| 3 | A | 1 |
| 4 | H | 1 |
| 5 | D | 2 |
| 6 | E | 2 |
| 7 | I | 2 |
| 8 | G | 2 |
| 9 | F | 2 |
| 10 | J | 3 |
| 11 | L | 4 |
| 12 | K | 4 |
Finding the Connected Components of 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 find the connected components and output the results in the data table NodeSetOut:
s:network_connectedComponents{
indexOffset = 1,
nodes = {name = "NodeSetIn"},
links = {name = "LinkSetIn"},
outNodes = {name = "NodeSetOut", replace=true}}
Finding the Connected Components of 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 find the connected components and output the results in the data table NodeSetOut:
s.network.connectedComponents(
indexOffset = 1,
nodes = {"name": "NodeSetIn"},
links = {"name": "LinkSetIn"},
outNodes = {"name": "NodeSetOut", "replace":True})
Finding the Connected Components of an Undirected Graph
This example is not available for the R programming language.