Network Optimization Action Set

Enumerating the Cycles of a Directed 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 1, Introduction (SAS Optimization: The OPTNETWORK Procedure). 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 provides an example of using the cycle detection algorithm on the directed graph G shown in Figure 5.

Figure 5: A Directed Graph G

A Directed Graph


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

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

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 find all the cycles in the graph and output the results in the data table Cycles:

proc cas;
   loadactionset "optNetwork";
   action optNetwork.cycle result=r status=s /
      indexOffset = 1
      direction   = "directed"
      links       = {name = "LinkSetIn"}
      out         = {name = "Cycles", replace=true}
      maxCycles   = "all";
   run;
   print r.ProblemSummary; run;
   print r.SolutionSummary; run;
   action table.fetch / table = "Cycles" to=1000 sortBy = {"cycle","order"}; run;
quit;

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

Output 2.5.1: Problem Summary

Problem Summary
Number of Nodes6
Number of Links10
Graph DirectionDirected


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

Output 2.5.2: Solution Summary

Solution Summary
Problem TypeCycle
Solution StatusOK
Number of Cycles7
CPU Time0.00
Real Time0.00


The output data table Cycles now contains all the cycles in the input graph, as shown in Output 2.5.3.

Output 2.5.3: All Cycles in a Directed Graph

Selected Rows from Table CYCLES
_Index_cycleordernode
111A
212B
313C
414A
521A
622E
723B
824C
925A
1031A
1132E
1233C
1334A
1441B
1542C
1643D
1744E
1845B
1951B
2052C
2153D
2254F
2355E
2456B
2561C
2662D
2763E
2864C
2971C
3072D
3173F
3274E
3375C


The cycles are shown graphically in Output 2.5.4.

Output 2.5.4: Cycles

upper A right-arrow upper B right-arrow upper C right-arrow upper A upper A right-arrow upper E right-arrow upper B right-arrow upper C right-arrow upper A upper A right-arrow upper E right-arrow upper C right-arrow upper A
cycle1_1 cycle1_2 cycle1_3
upper B right-arrow upper C right-arrow upper D right-arrow upper E right-arrow upper B upper B right-arrow upper C right-arrow upper D right-arrow upper F right-arrow upper E right-arrow upper B upper E right-arrow upper C right-arrow upper D right-arrow upper E
cycle1_4 cycle1_5 cycle1_7
upper E right-arrow upper C right-arrow upper D right-arrow upper F right-arrow upper E
cycle1_6


Enumerating the Cycles 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 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 all the cycles in the graph and output the results in the data table Cycles:

s:optNetwork_cycle{
   indexOffset = 1,
   direction   = "directed",
   links       = {name = "LinkSetIn"},
   out         = {name = "Cycles", replace=true},
   maxCycles   = "all"}

Enumerating the Cycles 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 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 all the cycles in the graph and output the results in the data table Cycles:

s.optNetwork.cycle(
    indexOffset = 1,
    direction   = "directed",
    links       = {"name": "LinkSetIn"},
    out         = {"name": "Cycles", "replace":True},
    maxCycles   = "all")

Enumerating the Cycles of a Directed Graph

This example is not available for the R programming language.

Last updated: July 02, 2026