Network Action Set

Finding the Shortest Paths for All Source-Sink Pairs

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 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 shortest path algorithm for all source-sink pairs on the undirected graph G shown in Figure 20.

Figure 20: An Undirected Graph G

An Undirected Graph


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

data LinkSetIn;
   input from $ to $ weight @@;
   datalines;
A B 3  A C 2  A D 6  A E 4  B D 5
B F 5  C E 1  D E 2  D F 1  E F 4
;

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 the shortest paths for all source-sink pairs:

proc cas;
   loadactionset "network";
   action network.shortestPath result=r status=s /
      indexOffset = 1
      links       = {name = "LinkSetIn"}
      outWeights  = {name = "ShortPathW", replace=true}
      outPaths    = {name = "ShortPathP", replace=true};
   run;
   print r.ProblemSummary; run;
   print r.SolutionSummary; run;
   action table.fetch / table  = "ShortPathP" to=1000
                        sortBy = {"source","sink","order"}; run;
   action table.fetch / table = "ShortPathW" to=1000
                        sortBy = {"source","sink","path_weight"}; run;
quit;

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

Output 28.17.1: Problem Summary

Problem Summary
Number of Nodes6
Number of Links10
Graph DirectionUndirected


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

Output 28.17.2: Solution Summary

Solution Summary
Problem TypeShortest Path
Solution StatusOK
Number of Paths30
CPU Time0.00
Real Time0.02


The output data table ShortPathP contains the shortest paths, as shown in Output 28.17.3.

Output 28.17.3: All-Pairs Shortest Paths

Selected Rows from Table SHORTPATHP
_Index_sourcesinkorderfromtoweight
1AB1AB3
2AC1AC2
3AD1AC2
4AD2CE1
5AD3DE2
6AE1AC2
7AE2CE1
8AF1AC2
9AF2CE1
10AF3DE2
11AF4DF1
12BA1AB3
13BC1AB3
14BC2AC2
15BD1BD5
16BE1AB3
17BE2AC2
18BE3CE1
19BF1BF5
20CA1AC2
21CB1AC2
22CB2AB3
23CD1CE1
24CD2DE2
25CE1CE1
26CF1CE1
27CF2DE2
28CF3DF1
29DA1DE2
30DA2CE1
31DA3AC2
32DB1BD5
33DC1DE2
34DC2CE1
35DE1DE2
36DF1DF1
37EA1CE1
38EA2AC2
39EB1CE1
40EB2AC2
41EB3AB3
42EC1CE1
43ED1DE2
44EF1DE2
45EF2DF1
46FA1DF1
47FA2DE2
48FA3CE1
49FA4AC2
50FB1BF5
51FC1DF1
52FC2DE2
53FC3CE1
54FD1DF1
55FE1DF1
56FE2DE2


The output data table ShortPathW contains the path weights of the shortest paths of each source-sink pair, as shown in Output 28.17.4.

Output 28.17.4: All-Pairs Shortest Paths Summary

Selected Rows from Table SHORTPATHW
_Index_sourcesinkpath_weight
1AB3
2AC2
3AD5
4AE3
5AF6
6BA3
7BC5
8BD5
9BE6
10BF5
11CA2
12CB5
13CD3
14CE1
15CF4
16DA5
17DB5
18DC3
19DE2
20DF1
21EA3
22EB6
23EC1
24ED2
25EF3
26FA6
27FB5
28FC4
29FD1
30FE3


Finding the Shortest Paths for All Source-Sink Pairs

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 the shortest paths for all source-sink pairs:

s:network_shortestPath{
   indexOffset = 1,
   links       = {name = "LinkSetIn"},
   outPaths    = {name = "ShortPathP", replace=true},
   outWeights  = {name = "ShortPathW", replace=true}}

Finding the Shortest Paths for All Source-Sink Pairs

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 the shortest paths for all source-sink pairs:

s.network.shortestPath(
    indexOffset = 1,
    links       = {"name": "LinkSetIn"},
    outPaths    = {"name": "ShortPathP", "replace":True},
    outWeights  = {"name": "ShortPathW", "replace":True})

Finding the Shortest Paths for All Source-Sink Pairs

This example is not available for the R programming language.

Last updated: August 04, 2026