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

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 Nodes | 6 |
| Number of Links | 10 |
| Graph Direction | Directed |
The solution summary output from this action is shown in Output 2.5.2.
Output 2.5.2: Solution Summary
| Solution Summary | |
|---|---|
| Problem Type | Cycle |
| Solution Status | OK |
| Number of Cycles | 7 |
| CPU Time | 0.00 |
| Real Time | 0.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_ | cycle | order | node |
| 1 | 1 | 1 | A |
| 2 | 1 | 2 | B |
| 3 | 1 | 3 | C |
| 4 | 1 | 4 | A |
| 5 | 2 | 1 | A |
| 6 | 2 | 2 | E |
| 7 | 2 | 3 | B |
| 8 | 2 | 4 | C |
| 9 | 2 | 5 | A |
| 10 | 3 | 1 | A |
| 11 | 3 | 2 | E |
| 12 | 3 | 3 | C |
| 13 | 3 | 4 | A |
| 14 | 4 | 1 | B |
| 15 | 4 | 2 | C |
| 16 | 4 | 3 | D |
| 17 | 4 | 4 | E |
| 18 | 4 | 5 | B |
| 19 | 5 | 1 | B |
| 20 | 5 | 2 | C |
| 21 | 5 | 3 | D |
| 22 | 5 | 4 | F |
| 23 | 5 | 5 | E |
| 24 | 5 | 6 | B |
| 25 | 6 | 1 | C |
| 26 | 6 | 2 | D |
| 27 | 6 | 3 | E |
| 28 | 6 | 4 | C |
| 29 | 7 | 1 | C |
| 30 | 7 | 2 | D |
| 31 | 7 | 3 | F |
| 32 | 7 | 4 | E |
| 33 | 7 | 5 | C |
The cycles are shown graphically in Output 2.5.4.
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.






