Cohesion API Reference

Components, cliques, k-cores, cutpoints, bicomponents and geodesics.

Components

SNA.component_dist — Function
component_dist(net; connected=:strong, missing=:error) -> NamedTuple

The component structure of the network, R sna::component.dist: (membership, csize, cdist), where

  • membership[v] is the component of vertex v, components being numbered in the order of their lowest-numbered vertex;
  • csize[c] is the number of vertices in component c;
  • cdist[s] is the number of components with s vertices, s = 1, …, n.

connected selects the component type:

  • :strong (default): strong components (mutual reachability);
  • :weak: weak components (direction ignored);
  • :recursive: components of the mutual-tie graph.

On an undirected network the three coincide. sna's unilateral components are not unique and are not implemented.

Before 0.2.0 component_dist returned the component sizes sorted in decreasing order; that is sort(component_dist(net).csize; rev=true).

missing=:error refuses masked dyads; missing=:face takes their stored face values as ties.

Example

using SNA
net = network(5)
add_edges!(net, [(1, 2), (2, 1), (2, 3), (4, 5)])
cd = component_dist(net; connected=:weak)
cd.membership                        # [1, 1, 1, 2, 2]
cd.csize                             # [3, 2]
component_dist(net).csize            # strong: [2, 1, 1, 1]
source
SNA.largest_component — Function
largest_component(net; connected=:strong, missing=:error) -> Vector{Int}

The vertices of the largest component (the first one, if several share the largest size), sorted. connected is as in component_dist. R's component.largest returns the same set as a logical vector.

missing=:error refuses masked dyads; missing=:face takes their stored face values as ties.

Example

using SNA
net = network(5; directed=false)
add_edges!(net, [(1, 2), (2, 3), (4, 5)])
largest_component(net)               # [1, 2, 3]
source
SNA.bicomponents — Function
bicomponents(net; symmetrize=:strong, min_size=3, missing=:error) -> Vector{Vector{Tuple{Int,Int}}}

The biconnected components of the network, as lists of edges. The default min_size=3 excludes bridges, as R sna::bicomponent.dist does; min_size=2 includes the bridge components.

Biconnectivity is an undirected concept; directed networks use mutual arcs by default (symmetrize=:strong), as in sna. symmetrize=:weak accepts an arc in either direction.

missing=:error refuses masked dyads; missing=:face takes their stored face values as ties.

Example

using SNA
net = network(5; directed=false)
add_edges!(net, [(1, 2), (2, 3), (1, 3), (3, 4), (4, 5)])
length(bicomponents(net))            # 1: the triangle
source

Subgroups

SNA.cliques — Function
cliques(net; min_size=3, symmetrize=:strong, missing=:error) -> Vector{Vector{Int}}

All maximal cliques (complete subgraphs) with at least min_size vertices.

Cliques are an undirected concept; as in R sna::clique.census, directed networks use mutual arcs (symmetrize=:strong), and symmetrize=:weak accepts an arc in either direction. (sna's clique.census returns clique counts by size; this function lists the cliques.) Enumerating cliques takes exponential time in the worst case.

missing=:error refuses masked dyads; missing=:face lets them enter (or break) cliques at their stored face values.

Example

using SNA
net = network(5; directed=false)
add_edges!(net, [(1, 2), (1, 3), (2, 3), (3, 4), (4, 5)])
cliques(net)                         # [[1, 2, 3]] (in some order)
source
SNA.kcores — Function
kcores(net; cmode=:freeman, missing=:error) -> Vector{Int}

The core number of every vertex, R sna::kcores: vertex v has core number k when it belongs to the k-core (the largest subgraph in which every vertex has degree at least k) but not to the (k+1)-core.

cmode is the degree used on a directed network: :freeman (in + out, sna's default), :indegree or :outdegree. Undirected networks use the ordinary degree. Self-loops are ignored, as in sna.

Before 0.2.0 kcores(net; k) returned the vertices of the k-core; they are findall(>=(k), kcores(net)). Passing k raises an ArgumentError that says so.

missing=:error refuses masked dyads; missing=:face takes their stored face values as ties.

Example

using SNA
net = network(5; directed=false)
add_edges!(net, [(1, 2), (1, 3), (2, 3), (3, 4), (4, 5)])
kcores(net)                          # [2, 2, 2, 1, 1]
findall(>=(2), kcores(net))          # the 2-core: [1, 2, 3]
source

Vulnerability

SNA.cutpoints — Function
cutpoints(net; connected=:strong, symmetrize=nothing, missing=:error) -> Vector{Int}

The cutpoints (articulation points) of the network, R sna::cutpoints: the vertices whose removal increases the number of components.

Directed networks default to strong connectivity (connected=:strong, sna's default): a cutpoint splits a strong component. connected=:weak ignores direction; connected=:recursive uses mutual arcs. An explicit symmetrize=:strong or :weak also requests articulation of that undirected graph.

missing=:error refuses masked dyads; missing=:face takes their stored face values as ties.

Example

using SNA
path = network(4; directed=false)
add_edges!(path, [(1, 2), (2, 3), (3, 4)])
sort(cutpoints(path))                # [2, 3]
source

Bridges (ties whose removal disconnects the network) are Graphs.jl's bridges, which works on an undirected Network.

Paths

SNA.geodist — Function
geodist(net; inf_replace=Inf, missing=:error) -> NamedTuple

Geodesic distances and geodesic counts, R sna::geodist: (counts, gdist), where gdist[i, j] is the length of a shortest directed path from i to j (inf_replace, by default Inf, when j is unreachable) and counts[i, j] is the number of such paths (0 when unreachable). The diagonal has distance 0 and count 1. Self-loops play no part.

The diameter of a network, which sna does not provide, is maximum(geodist(net).gdist) (Inf when some vertex cannot reach another). Graphs.diameter is Graphs.jl's function and keeps Graphs' semantics.

missing=:error refuses masked dyads; missing=:face traverses them at their stored face values.

Example

using SNA
net = network(4)
add_edges!(net, [(1, 2), (1, 3), (2, 4), (3, 4)])
g = geodist(net)
g.gdist[1, 4], g.counts[1, 4]        # (2.0, 2.0): two geodesics of length 2
g.gdist[4, 1]                        # Inf
source
SNA.reachability — Function
reachability(net; missing=:error) -> Matrix{Bool}

The reachability matrix, R sna::reachability: entry (i, j) is true when there is a directed path from i to j. Every vertex reaches itself.

missing=:error refuses masked dyads; missing=:face traverses them at their stored face values.

Example

using SNA
chain = network(3)
add_edges!(chain, [(1, 2), (2, 3)])
reachability(chain)[1, 3], reachability(chain)[3, 1]     # (true, false)
source
SNA.average_path_length — Function
average_path_length(net; missing=:error) -> Float64

The mean geodesic distance over ordered pairs of distinct vertices that are connected by a path (unreachable pairs are left out). Inf when no vertex reaches another. This is not an sna function.

missing=:error refuses masked dyads; missing=:face traverses them at their stored face values.

Example

using SNA
path = network(3; directed=false)
add_edges!(path, [(1, 2), (2, 3)])
average_path_length(path)            # (1 + 1 + 2) × 2 / 6 = 1.3333
source