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

Undirected Graph


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

Query Graph


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 StartSet 3 comma 4 EndSet, the function myLinkFilter checks the weight attribute of a candidate link in the main graph. Query link StartSet 3 comma 4 EndSet 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 Nodes16
Number of Links30
Graph DirectionUndirected


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

Output 28.13.2: Solution Summary

Solution Summary
Problem TypePattern Match
Solution StatusOK
Number of Matches2
CPU Time0.01
Real Time0.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_matchnodeQnodecolor
101Kgreen
202Jblue
303Ipurple
404Opurple
505Mblue
606Pgreen
711Pgreen
812Mblue
913Opurple
1014Ipurple
1115Jblue
1216Kgreen


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_matchfromtoweight
10IK4
20IO1
30JI4
40JK4
50MO4
60MP4
70PO4
81IK4
91IO1
101JI4
111JK4
121MO4
131MP4
141PO4


The results are displayed graphically in Output 28.13.5.

Output 28.13.5: Subgraphs

pmatch2_1 pmatch2_2


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.

Last updated: August 04, 2026