The Network Action Set

Example 16.12 Reading a Simple Directed Graph

Reading a Simple Directed Graph

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 describes how to input a graph by using the readGraph action. Let define a graph that contains a set N of nodes and a set A of links. Consider the directed graph shown in Figure 16.13.

Figure 13: A Simple Directed Graph

A Simple Directed Graph


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

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

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 read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.

proc cas;
   loadactionset "network";
   action network.readGraph result=r status=s /
      direction = "directed"
      links     = {name = "LinkSetIn"}
      outNodes  = {name = "NodeSetOut", replace=true}
      outLinks  = {name = "LinkSetOut", replace=true};
   run;
   print r.ProblemSummary; run;
   action table.fetch / table = "NodeSetOut"; run;
   action table.fetch / table = "LinkSetOut"; run;
quit;

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

Output 16.12.1: Problem Summary

Problem Summary
Number of Nodes9
Number of Links15
Graph DirectionDirected


The output data table NodeSetOut, shown in Output 16.12.2, now contains the nodes that are read from the input links data table. The variable node shows the label that is associated with each node.

Output 16.12.2: Nodes Data Table of a Simple Directed Graph

Selected Rows from Table
NODESETOUT
_Index_node
1A
2B
3C
4D
5E
6F
7G
8H
9I


The output data table LinkSetOut, shown in Output 16.12.3, contains the links that were read from the input links data table. The variables from and to show the associated node labels.

Output 16.12.3: Links Data Table of a Simple Directed Graph

Selected Rows from Table LINKSETOUT
_Index_fromtoweight
1AB1
2AC2
3AD4
4BC1
5BE2
6BF5
7CE1
8DE1
9ED1
10EF2
11FG6
12GH1
13HG2
14GI1
15HI3


Reading a Simple Directed 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 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 read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.

s:network_readGraph{
    direction = "directed",
    links     = {name = "LinkSetIn"},
    outNodes  = {name = "NodeSetOut", replace=true},
    outLinks  = {name = "LinkSetOut", replace=true}}

Reading a Simple Directed 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 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 read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.

s.network.readGraph(
    direction = "directed",
    links     = {"name":"LinkSetIn"},
    outNodes  = {"name":"NodeSetOut", "replace":True},
    outLinks  = {"name":"LinkSetOut", "replace":True})

Reading a Simple Directed Graph

This example is not available for the R programming language.

Last updated: June 07, 2018