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

A Simple 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; run;
quit;

The problem summary output from this action is presented in Output 16.13.1.

Output 16.13.1: Problem Summary

Problem Summary
Number of Nodes6
Number of Links10
Graph DirectionUndirected


The solution summary output from this action is presented in Output 16.13.2.

Output 16.13.2: Solution Summary

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


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_sourcesinkorderfromtoweight
1AB1AB3
2AC1AC2
3AD1AC2
4AD2CE1
5AD3ED2
6AE1AC2
7AE2CE1
8AF1AC2
9AF2CE1
10AF3ED2
11AF4DF1
12BA1BA3
13BC1BA3
14BC2AC2
15BD1BD5
16BE1BA3
17BE2AC2
18BE3CE1
19BF1BF5
20CA1CA2
21CB1CA2
22CB2AB3
23CD1CE1
24CD2ED2
25CE1CE1
26CF1CE1
27CF2ED2
28CF3DF1
29DA1DE2
30DA2EC1
31DA3CA2
32DB1DB5
33DC1DE2
34DC2EC1
35DE1DE2
36DF1DF1
37EA1EC1
38EA2CA2
39EB1EC1
40EB2CA2
41EB3AB3
42EC1EC1
43ED1ED2
44EF1ED2
45EF2DF1
46FA1FD1
47FA2DE2
48FA3EC1
49FA4CA2
50FB1FB5
51FC1FD1
52FC2DE2
53FC3EC1
54FD1FD1
55FE1FD1
56FE2DE2


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

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


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.

Last updated: June 07, 2018