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

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 Nodes | 6 |
| Number of Links | 10 |
| Graph Direction | Undirected |
The solution summary output from this action is shown in Output 28.17.2.
Output 28.17.2: Solution Summary
| Solution Summary | |
|---|---|
| Problem Type | Shortest Path |
| Solution Status | OK |
| Number of Paths | 30 |
| CPU Time | 0.00 |
| Real Time | 0.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_ | source | sink | order | from | to | weight |
| 1 | A | B | 1 | A | B | 3 |
| 2 | A | C | 1 | A | C | 2 |
| 3 | A | D | 1 | A | C | 2 |
| 4 | A | D | 2 | C | E | 1 |
| 5 | A | D | 3 | D | E | 2 |
| 6 | A | E | 1 | A | C | 2 |
| 7 | A | E | 2 | C | E | 1 |
| 8 | A | F | 1 | A | C | 2 |
| 9 | A | F | 2 | C | E | 1 |
| 10 | A | F | 3 | D | E | 2 |
| 11 | A | F | 4 | D | F | 1 |
| 12 | B | A | 1 | A | B | 3 |
| 13 | B | C | 1 | A | B | 3 |
| 14 | B | C | 2 | A | C | 2 |
| 15 | B | D | 1 | B | D | 5 |
| 16 | B | E | 1 | A | B | 3 |
| 17 | B | E | 2 | A | C | 2 |
| 18 | B | E | 3 | C | E | 1 |
| 19 | B | F | 1 | B | F | 5 |
| 20 | C | A | 1 | A | C | 2 |
| 21 | C | B | 1 | A | C | 2 |
| 22 | C | B | 2 | A | B | 3 |
| 23 | C | D | 1 | C | E | 1 |
| 24 | C | D | 2 | D | E | 2 |
| 25 | C | E | 1 | C | E | 1 |
| 26 | C | F | 1 | C | E | 1 |
| 27 | C | F | 2 | D | E | 2 |
| 28 | C | F | 3 | D | F | 1 |
| 29 | D | A | 1 | D | E | 2 |
| 30 | D | A | 2 | C | E | 1 |
| 31 | D | A | 3 | A | C | 2 |
| 32 | D | B | 1 | B | D | 5 |
| 33 | D | C | 1 | D | E | 2 |
| 34 | D | C | 2 | C | E | 1 |
| 35 | D | E | 1 | D | E | 2 |
| 36 | D | F | 1 | D | F | 1 |
| 37 | E | A | 1 | C | E | 1 |
| 38 | E | A | 2 | A | C | 2 |
| 39 | E | B | 1 | C | E | 1 |
| 40 | E | B | 2 | A | C | 2 |
| 41 | E | B | 3 | A | B | 3 |
| 42 | E | C | 1 | C | E | 1 |
| 43 | E | D | 1 | D | E | 2 |
| 44 | E | F | 1 | D | E | 2 |
| 45 | E | F | 2 | D | F | 1 |
| 46 | F | A | 1 | D | F | 1 |
| 47 | F | A | 2 | D | E | 2 |
| 48 | F | A | 3 | C | E | 1 |
| 49 | F | A | 4 | A | C | 2 |
| 50 | F | B | 1 | B | F | 5 |
| 51 | F | C | 1 | D | F | 1 |
| 52 | F | C | 2 | D | E | 2 |
| 53 | F | C | 3 | C | E | 1 |
| 54 | F | D | 1 | D | F | 1 |
| 55 | F | E | 1 | D | F | 1 |
| 56 | F | E | 2 | D | E | 2 |
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_ | source | sink | path_weight |
| 1 | A | B | 3 |
| 2 | A | C | 2 |
| 3 | A | D | 5 |
| 4 | A | E | 3 |
| 5 | A | F | 6 |
| 6 | B | A | 3 |
| 7 | B | C | 5 |
| 8 | B | D | 5 |
| 9 | B | E | 6 |
| 10 | B | F | 5 |
| 11 | C | A | 2 |
| 12 | C | B | 5 |
| 13 | C | D | 3 |
| 14 | C | E | 1 |
| 15 | C | F | 4 |
| 16 | D | A | 5 |
| 17 | D | B | 5 |
| 18 | D | C | 3 |
| 19 | D | E | 2 |
| 20 | D | F | 1 |
| 21 | E | A | 3 |
| 22 | E | B | 6 |
| 23 | E | C | 1 |
| 24 | E | D | 2 |
| 25 | E | F | 3 |
| 26 | F | A | 6 |
| 27 | F | B | 5 |
| 28 | F | C | 4 |
| 29 | F | D | 1 |
| 30 | F | E | 3 |
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.