Network Action Set
Pattern Matching of a Directed 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 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 pattern matching algorithm on the directed graph G shown in Figure 11.
Figure 11: Directed Graph G

The directed graph G can be represented using the links data set, Links, and nodes data set, Nodes, that are created by the following DATA steps:
data Links;
input from $ to $ @@;
datalines;
A B A I A H
B D B E B C
C F C G C A
F G F A G B
E C I H D G
F C C D D E
;
data Nodes;
input node $ @@;
label=node;
datalines;
A B C D E F G H I
;
The following DATA steps load the Links and Nodes data sets into CAS data tables named mycas.Links and mycas.Nodes. These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.Links;
set Links;
run;
data mycas.Nodes;
set Nodes;
run;
Each node has a node attribute label that is defined as the name of the node.
In this example, you want to find all subgraphs that are directed cycles of length 3 that pass through node C. The query graph Q that defines the pattern to search for is shown in Figure 12.
Figure 12: Query Graph Q

The query graph Q can be represented using the links data set, LinksQuery, and nodes data set, NodesQuery, that are created by the following DATA steps:
data LinksQuery;
input from to;
datalines;
1 2
2 3
3 1
;
data NodesQuery;
input node label $;
datalines;
2 C
;
The following DATA steps load the LinksQuery and NodesQuery data sets into CAS data tables named mycas.LinksQuery and mycas.NodesQuery. These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.LinksQuery;
set LinksQuery;
run;
data mycas.NodesQuery;
set NodesQuery;
run;
You can use the following statements to find all subgraphs that have the specified pattern:
proc cas;
loadactionset "network";
action patternMatch result=r status=s /
direction = "directed"
links = {name = "Links"}
nodes = {name = "Nodes"}
linksQuery = {name = "LinksQuery"}
nodesQuery = {name = "NodesQuery"}
nodesVar = {vars = "label"}
nodesQueryVar = {vars = "label"}
outMatchNodes = {name = "OutMatchNodes", replace=true}
outMatchLinks = {name = "OutMatchLinks", replace=true};
run;
print r.ProblemSummary; run;
print r.SolutionSummary; run;
action table.fetch / table = "OutMatchNodes" sortBy = {"match","nodeQ","node"}; run;
action table.fetch / table = "OutMatchLinks" sortBy = {"match","from","to"}; run;
quit;
The problem summary output from this action is shown in Output 22.11.1.
Output 22.11.1: Problem Summary
| Problem Summary | |
|---|---|
| Number of Nodes | 9 |
| Number of Links | 18 |
| Graph Direction | Directed |
The solution summary output from this action is shown in Output 22.11.2.
Output 22.11.2: Solution Summary
| Solution Summary | |
|---|---|
| Problem Type | Pattern Match |
| Solution Status | OK |
| Number of Matches | 3 |
| CPU Time | 0.01 |
| Real Time | 0.00 |
The output data table OutMatchNodes now contains the mapping from nodes in the query graph to nodes in the input graph for each pattern match, as shown in Output 22.11.3.
Output 22.11.3: Node Mappings for Pattern Matches
| Selected Rows from Table OUTMATCHNODES | ||||
|---|---|---|---|---|
| _Index_ | match | nodeQ | node | label |
| 1 | 0 | 1 | B | B |
| 2 | 0 | 2 | C | C |
| 3 | 0 | 3 | A | A |
| 4 | 1 | 1 | E | E |
| 5 | 1 | 2 | C | C |
| 6 | 1 | 3 | D | D |
| 7 | 2 | 1 | B | B |
| 8 | 2 | 2 | C | C |
| 9 | 2 | 3 | G | G |
The output data table OutMatchLinks now contains the subgraphs for each pattern match, as shown in Output 22.11.4.
Output 22.11.4: Subgraphs for Pattern Matches
| Selected Rows from Table OUTMATCHLINKS | |||
|---|---|---|---|
| _Index_ | match | from | to |
| 1 | 0 | A | B |
| 2 | 0 | B | C |
| 3 | 0 | C | A |
| 4 | 1 | C | D |
| 5 | 1 | D | E |
| 6 | 1 | E | C |
| 7 | 2 | B | C |
| 8 | 2 | C | G |
| 9 | 2 | G | B |
The results are displayed graphically in Output 22.11.5.
Output 22.11.5: Subgraphs
| |
|
Pattern Matching of a Directed 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 Links data to the comma-separated-value (CSV) file Links.csv, convert the Nodes data to the CSV file Nodes.csv, convert the LinksQuery data to the CSV file LinksQuery.csv, convert the NodesQuery data to the CSV file NodesQuery.csv, and then use the following code to load the CSV files into CAS:
s:loadtable{casLib="casuser", path="Links.csv"}
s:loadtable{casLib="casuser", path="Nodes.csv"}
s:loadtable{casLib="casuser", path="LinksQuery.csv"}
s:loadtable{casLib="casuser", path="NodesQuery.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
You can use the following statements to find all subgraphs that have the specified pattern:
s:network_patternMatch{
direction = "directed",
links = {name = "Links"},
nodes = {name = "Nodes"},
linksQuery = {name = "LinksQuery"},
nodesQuery = {name = "NodesQuery"},
nodesVar = {vars = "label"},
nodesQueryVar = {vars = "label"},
outMatchNodes = {name = "OutMatchNodes", replace=true},
outMatchLinks = {name = "OutMatchLinks", replace=true}}
Pattern Matching of a Directed 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 Links data to the comma-separated-value (CSV) file Links.csv, convert the Nodes data to the CSV file Nodes.csv, convert the LinksQuery data to the CSV file LinksQuery.csv, convert the NodesQuery data to the CSV file NodesQuery.csv, and then use the following code to load the CSV files into CAS:
s.upload_file('Links.csv')
s.upload_file('Nodes.csv')
s.upload_file('LinksQuery.csv')
s.upload_file('NodesQuery.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
You can use the following statements to find all subgraphs that have the specified pattern:
s.network.patternMatch(
direction = "directed",
links = {"name":"Links"},
nodes = {"name":"Nodes"},
linksQuery = {"name":"LinksQuery"},
nodesQuery = {"name":"NodesQuery"},
nodesVar = {"vars":"label"},
nodesQueryVar = {"vars":"label"},
outMatchNodes = {"name":"OutMatchNodes", "replace":True},
outMatchLinks = {"name":"OutMatchLinks", "replace":True})
Pattern Matching of a Directed Graph
This example is not available for the R programming language.


