A connected component of a graph is a set of nodes that are all reachable from each other. That is, if two nodes are in the same component, then there exists a path between them. For a directed graph, there are two types of components: a strongly connected component has a directed path between any two nodes, and a weakly connected component ignores direction and requires only that a path exist between any two nodes.
In PROC OPTNET, you can invoke connected components by using the CONCOMP statement. The options for this statement are described in the section CONCOMP Statement.
There are two main algorithms for finding connected components in an undirected graph: a depth-first search algorithm (ALGORITHM=DFS) and a union-find algorithm (ALGORITHM=UNION_FIND). For a graph , both algorithms run in time and can usually scale to very large graphs. The default is the union-find algorithm. For directed graphs, only the depth-first search algorithm is available.
The results of the connected components algorithm are written to the output node data set that you specify in the OUT_NODES=
option in the PROC OPTNET statement. For each node in the node data set, the variable concomp
identifies its component. The component identifiers are numbered sequentially starting from 1.
The connected components algorithm reports status information in a macro variable called _OROPTNET_CONCOMP_. For more information about this macro variable, see the section Macro Variable _OROPTNET_CONCOMP_.
This section illustrates the use of the connected components algorithm on the simple undirected graph G that is shown in Figure 2.24.
Figure 2.24: A Simple Undirected Graph G
The undirected graph G can be represented by the following links data set, LinkSetIn
:
data LinkSetIn; input from $ to $ @@; datalines; A B A C B C C H D E D F D G F E G I K L ;
The following statements calculate the connected components and output the results in the data set NodeSetOut
:
proc optnet data_links = LinkSetIn out_nodes = NodeSetOut; concomp; run;
The data set NodeSetOut
contains the connected components of the input graph and is shown in Figure 2.25.
Figure 2.25: Connected Components of a Simple Undirected Graph
Notice that the graph is defined by using only the links data set. As seen in Figure 2.24, this graph also contains a singleton node labeled J, which has no associated links. By definition, this node defines its
own component. But because the input graph was defined by using only the links data set, it did not show up in the results
data set. To define a graph by using nodes that have no associated links, you should also define the input nodes data set.
In this case, define the nodes data set NodeSetIn
as follows:
data NodeSetIn; input node $ @@; datalines; A B C D E F G H I J K L ;
Now, when you calculate the connected components, you define the input graph by using both the nodes input data set and the links input data set:
proc optnet data_nodes = NodeSetIn data_links = LinkSetIn out_nodes = NodeSetOut; concomp; run;
The resulting data set, NodeSetOut
, includes the singleton node J as its own component, as shown in Figure 2.26.
Figure 2.26: Connected Components of a Simple Undirected Graph
This section illustrates the use of the connected components algorithm on the simple directed graph G that is shown in Figure 2.27.
Figure 2.27: A Simple Directed Graph G
The directed graph G can be represented by the following links data set, LinkSetIn
:
data LinkSetIn; input from $ to $ @@; datalines; A B B C B E B F C G C D D C D H E A E F F G G F H G H D ;
The following statements calculate the connected components and output the results in the data set NodeSetOut
:
proc optnet graph_direction = directed data_links = LinkSetIn out_nodes = NodeSetOut; concomp; run;
The data set NodeSetOut
, shown in Figure 2.28, now contains the connected components of the input graph.
Figure 2.28: Connected Components of a Simple Directed Graph
The connected components are represented graphically in Figure 2.29.
Figure 2.29: Strongly Connected Components of Graph G