Network Action Set
Pattern Matching of an Undirected Graph Using FCMP Filter Functions
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 pattern matching algorithm on the undirected graph G shown in Figure 14.
Figure 14: Undirected Graph G

The undirected graph G has one link attribute (weight) and one node attribute (color). You can represent the graph by using the nodes data table, mycas.Nodes, and links data table, mycas.Links, that are created by the following DATA steps:
data Links;
input from $ to $ weight @@;
datalines;
A B 5 A C 5 A D 5 A E 3 B C 5
B D 5 B E 5 C D 5 C E 5 D E 5
F G 4 F H 3 G H 3 E F 2 E J 2
J I 4 J K 4 J L 4 I K 4 I L 4
K L 4 I O 1 K N 1 L M 2 M N 4
M O 4 M P 4 N P 4 N O 4 P O 4
;
data Nodes;
input node $ color $ @@;
datalines;
A red B blue C green D red E green
F blue G red H green I purple J blue
K green L red M blue N yellow O purple
P green
;
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;
In this example, you want to find pairs of triangles (cliques of size 3) that are connected by a link whose weight is 1. In addition, you want the outer nodes of the triangle to have the colors blue and green. The query graph Q that defines the pattern to search for is shown in Figure 15.
Figure 15: Query Graph Q

You can represent the structure of the query graph Q by using the nodes data table, mycas.NodesQuery, and links data table, mycas.LinksQuery, that are created by the following DATA steps:
data LinksQuery;
input from to @@;
datalines;
1 2 1 3 2 3 3 4 4 5
5 6 4 6
;
data NodesQuery;
input node @@;
datalines;
1 2 5 6
;
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;
Rather than providing the color variable in the NodesQuery table, the refinement of node color is accomplished by using the following node filter function and associated function call:
source myFilter;
function myNodeFilter(nodeQ, color $);
if (nodeQ in (1,6)) then return (color='green');
else if (nodeQ in (2,5)) then return (color='blue');
else return (1);
endsub;
endsource;
action patternMatch /
code = myFilter
nodeFilter = "myNodeFilter(nodesQuery.node,nodes.color)"
For query nodes 1, 2, 5, and 6, the function myNodeFilter checks the color attribute of a candidate node in the main graph. Query nodes 1 and 6 must map to a node in the main graph whose color is green. Query nodes 2 and 5 must map to a node in the main graph whose color is blue. For all other query nodes, the color of the mapped node in the main graph can take any value.
Similarly, rather than providing the weight variable in the LinksQuery table, the refinement of link weight is accomplished by using the following link filter function and associated function call:
source myFilter;
function myLinkFilter(fromQ, toQ, weight);
if (fromQ=3 and toQ=4) then return (weight=1);
else return (1);
endsub;
endsource;
action patternMatch /
code = myFilter
linkFilter = "myLinkFilter(linksQuery.from,linksQuery.to,links.weight)"
For query link , the function
myLinkFilter checks the weight attribute of a candidate link in the main graph. Query link must map to a link in the main graph whose weight is 1. For all other query links, the weight of the mapped link in the main graph can take any value.
The following statements use a source code block and the patternMatch action to find all subgraphs that have the pattern that is specified by the query input data tables and the FCMP filter functions. You can specify the source code block that defines the filter functions by using the code parameter.
proc cas;
source myFilter;
function myNodeFilter(nodeQ, color $);
if (nodeQ in (1,6)) then return (color='green');
else if (nodeQ in (2,5)) then return (color='blue');
else return (1);
endsub;
function myLinkFilter(fromQ, toQ, weight);
if (fromQ=3 and toQ=4) then return (weight=1);
else return (1);
endsub;
endsource;
loadactionset "network";
action patternMatch result=r status=s /
code = myFilter
links = {name = "Links"}
nodes = {name = "Nodes"}
linksQuery = {name = "LinksQuery"}
nodesQuery = {name = "NodesQuery"}
linksVar = {vars = "weight"}
nodesVar = {vars = "color"}
outMatchNodes = {name = "OutMatchNodes", replace=true}
outMatchLinks = {name = "OutMatchLinks", replace=true}
nodeFilter = "myNodeFilter(nodesQuery.node,nodes.color)"
linkFilter = "myLinkFilter(linksQuery.from,linksQuery.to,links.weight)";
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 28.13.1.
Output 28.13.1: Problem Summary
| Problem Summary | |
|---|---|
| Number of Nodes | 16 |
| Number of Links | 30 |
| Graph Direction | Undirected |
The solution summary output from this action is shown in Output 28.13.2.
Output 28.13.2: Solution Summary
| Solution Summary | |
|---|---|
| Problem Type | Pattern Match |
| Solution Status | OK |
| Number of Matches | 2 |
| CPU Time | 0.01 |
| Real Time | 0.07 |
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 28.13.3.
Output 28.13.3: Node Mappings for Pattern Matches
| Selected Rows from Table OUTMATCHNODES | ||||
|---|---|---|---|---|
| _Index_ | match | nodeQ | node | color |
| 1 | 0 | 1 | K | green |
| 2 | 0 | 2 | J | blue |
| 3 | 0 | 3 | I | purple |
| 4 | 0 | 4 | O | purple |
| 5 | 0 | 5 | M | blue |
| 6 | 0 | 6 | P | green |
| 7 | 1 | 1 | P | green |
| 8 | 1 | 2 | M | blue |
| 9 | 1 | 3 | O | purple |
| 10 | 1 | 4 | I | purple |
| 11 | 1 | 5 | J | blue |
| 12 | 1 | 6 | K | green |
The output data table OutMatchLinks now contains the subgraphs for each pattern match, as shown in Output 28.13.4.
Output 28.13.4: Subgraphs for Pattern Matches
| Selected Rows from Table OUTMATCHLINKS | ||||
|---|---|---|---|---|
| _Index_ | match | from | to | weight |
| 1 | 0 | I | K | 4 |
| 2 | 0 | I | O | 1 |
| 3 | 0 | J | I | 4 |
| 4 | 0 | J | K | 4 |
| 5 | 0 | M | O | 4 |
| 6 | 0 | M | P | 4 |
| 7 | 0 | P | O | 4 |
| 8 | 1 | I | K | 4 |
| 9 | 1 | I | O | 1 |
| 10 | 1 | J | I | 4 |
| 11 | 1 | J | K | 4 |
| 12 | 1 | M | O | 4 |
| 13 | 1 | M | P | 4 |
| 14 | 1 | P | O | 4 |
The results are displayed graphically in Output 28.13.5.
Output 28.13.5: Subgraphs
| |
Pattern Matching of an Undirected Graph Using FCMP Filter Functions
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:
myFilter = [[
function myNodeFilter(nodeQ, color $);
if (nodeQ in (1,6)) then return (color='green');
else if (nodeQ in (2,5)) then return (color='blue');
else return (1);
endsub;
function myLinkFilter(fromQ, toQ, weight);
if (fromQ=3 and toQ=4) then return (weight=1);
else return (1);
endsub;
]]
s:network_patternMatch{
code = myFilter,
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},
nodeFilter = "myNodeFilter(nodesQuery.node,nodes.color)",
linkFilter = "myLinkFilter(linksQuery.from,linksQuery.to,links.weight)"}
Pattern Matching of an Undirected Graph Using FCMP Filter Functions
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:
myFilter = """
function myNodeFilter(nodeQ, color $);
if (nodeQ in (1,6)) then return (color='green');
else if (nodeQ in (2,5)) then return (color='blue');
else return (1);
endsub;
function myLinkFilter(fromQ, toQ, weight);
if (fromQ=3 and toQ=4) then return (weight=1);
else return (1);
endsub;
"""
s.network.patternMatch(
code = myFilter,
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},
nodeFilter = "myNodeFilter(nodesQuery.node,nodes.color)",
linkFilter = "myLinkFilter(linksQuery.from,linksQuery.to,links.weight)")
Pattern Matching of an Undirected Graph Using FCMP Filter Functions
This example is not available for the R programming language.

