The Network Action Set
Example 16.13 Finding the Shortest Paths for All Source-Sink Pairs
Finding the Shortest Paths for All Source-Sink Pairs
This section contains PROC CAS code.
Note: Input data must be in a CAS table that is accessible in your CAS session. This 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 3: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.3: Procedures Guide. For more information about PROC CAS and programming in CASL, see Getting Started with CASL, SAS Cloud Analytic Services: CAS Procedure Programming Guide and Reference, and SAS Viya: System Programming Guide.
This example illustrates the use of the shortest path algorithm for all source-sink pairs on the simple undirected graph G shown in Figure 16.14.
Figure 14: A Simple 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; run;
quit;
The problem summary output from this action is presented in Output 16.13.1.
Output 16.13.1: Problem Summary
The solution summary output from this action is presented in Output 16.13.2.
Output 16.13.2: Solution Summary
The output data table ShortPathP contains the shortest paths, as shown in Output 16.13.3.
Output 16.13.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 | E | D | 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 | E | D | 2 |
| 11 | A | F | 4 | D | F | 1 |
| 12 | B | A | 1 | B | A | 3 |
| 13 | B | C | 1 | B | A | 3 |
| 14 | B | C | 2 | A | C | 2 |
| 15 | B | D | 1 | B | D | 5 |
| 16 | B | E | 1 | B | A | 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 | C | A | 2 |
| 21 | C | B | 1 | C | A | 2 |
| 22 | C | B | 2 | A | B | 3 |
| 23 | C | D | 1 | C | E | 1 |
| 24 | C | D | 2 | E | D | 2 |
| 25 | C | E | 1 | C | E | 1 |
| 26 | C | F | 1 | C | E | 1 |
| 27 | C | F | 2 | E | D | 2 |
| 28 | C | F | 3 | D | F | 1 |
| 29 | D | A | 1 | D | E | 2 |
| 30 | D | A | 2 | E | C | 1 |
| 31 | D | A | 3 | C | A | 2 |
| 32 | D | B | 1 | D | B | 5 |
| 33 | D | C | 1 | D | E | 2 |
| 34 | D | C | 2 | E | C | 1 |
| 35 | D | E | 1 | D | E | 2 |
| 36 | D | F | 1 | D | F | 1 |
| 37 | E | A | 1 | E | C | 1 |
| 38 | E | A | 2 | C | A | 2 |
| 39 | E | B | 1 | E | C | 1 |
| 40 | E | B | 2 | C | A | 2 |
| 41 | E | B | 3 | A | B | 3 |
| 42 | E | C | 1 | E | C | 1 |
| 43 | E | D | 1 | E | D | 2 |
| 44 | E | F | 1 | E | D | 2 |
| 45 | E | F | 2 | D | F | 1 |
| 46 | F | A | 1 | F | D | 1 |
| 47 | F | A | 2 | D | E | 2 |
| 48 | F | A | 3 | E | C | 1 |
| 49 | F | A | 4 | C | A | 2 |
| 50 | F | B | 1 | F | B | 5 |
| 51 | F | C | 1 | F | D | 1 |
| 52 | F | C | 2 | D | E | 2 |
| 53 | F | C | 3 | E | C | 1 |
| 54 | F | D | 1 | F | D | 1 |
| 55 | F | E | 1 | F | D | 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 16.13.4.
Output 16.13.4: All-Pairs Shortest Paths Summary
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 loaded into CAS. 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 loaded into CAS. 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.