Network Optimization Action Set

Calculating a Minimum Spanning Forest for an Undirected 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.

As an example, consider the weighted undirected graph in Figure 13.

Figure 13: An Undirected Graph

An Undirected Graph


The links data set can be represented as follows:

data LinkSetIn;
   input from $ to $ weight @@;
   datalines;
A B  7  A D  5  B C 8  B D 9  B E 7
C E  5  D E 15  D F 6  E F 8  E G 9
F G 11  H I  1  I J 3  H J 2
;

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 calculate a minimum spanning forest and output the results in the data table MinSpanForest:

proc cas;
   loadactionset "optNetwork";
   action optNetwork.minSpanTree result=r status=s /
      links = {name = "LinkSetIn"}
      out   = {name = "MinSpanForest", replace = true};
   run;
   print r.ProblemSummary; run;
   print r.SolutionSummary; run;
   action table.fetch / table = "MinSpanForest"; run;
quit;

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

Output 2.11.1: Problem Summary

Problem Summary
Number of Nodes10
Number of Links14
Graph DirectionUndirected


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

Output 2.11.2: Solution Summary

Solution Summary
Problem TypeMinimum Spanning Tree
Solution StatusOptimal
Objective Value42
CPU Time0.00
Real Time0.00


The output data table MinSpanForest now contains the links that belong to a minimum spanning forest, shown in Output 2.11.3.

Output 2.11.3: Minimum Spanning Forest

Selected Rows from Table MINSPANFOREST
_Index_fromtoweight
1HI1
2HJ2
3AD5
4CE5
5DF6
6AB7
7BE7
8EG9


The minimal cost links are shown in green in Figure 14.

Figure 14: Minimum Spanning Forest

Minimum Spanning Forest


Calculating a Minimum Spanning Forest for an Undirected 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 calculate a minimum spanning forest and output the results in the data table MinSpanForest:

s:optNetwork_minSpanTree{
   links = {name = "LinkSetIn"},
   out   = {name = "MinSpanForest", replace=true}}

Calculating a Minimum Spanning Forest for an Undirected 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 calculate a minimum spanning forest and output the results in the data table MinSpanForest:

s.optNetwork.minSpanTree(
    links = {"name":"LinkSetIn"},
    out   = {"name":"MinSpanForest", "replace":True})

Calculating a Minimum Spanning Forest for an Undirected Graph

This example is not available for the R programming language.

Last updated: July 02, 2026