yog/health

Network health and structural quality metrics.

These metrics measure the overall “health” and structural properties of your graph, including size, compactness, and connectivity patterns.

Overview

MetricFunctionMeasures
Diameterdiameter/5Maximum distance (worst-case reachability)
Radiusradius/5Minimum eccentricity (best central point)
Eccentricityeccentricity/6Maximum distance from a node
Assortativityassortativity/1Degree correlation (homophily)
Average Path Lengthaverage_path_length/6Typical separation

Example

import gleam/int
import gleam/order
import yog/health

let graph = // ... build your graph

// Check graph compactness
let diam = health.diameter(
  in: graph,
  with_zero: 0,
  with_add: int.add,
  with_compare: int.compare,
  with: fn(w) { w }
)
let rad = health.radius(
  in: graph,
  with_zero: 0,
  with_add: int.add,
  with_compare: int.compare,
  with: fn(w) { w }
)

// Small diameter = well-connected
// High assortativity = nodes cluster with similar nodes
let assort = health.assortativity(graph)

Values

pub fn assortativity(graph: model.Graph(n, e)) -> Float

Assortativity coefficient measures degree correlation.

Time Complexity: O(V+E)

Interpreting Assortativity

ValueMeaning
PositiveHigh-degree nodes preferentially connect to other high-degree nodes (assortative)
NegativeHigh-degree nodes connect to low-degree nodes (disassortative)
ZeroRandom mixing, or all nodes have the same degree (regular graph)

Common real-world patterns:

  • Social networks tend to be assortative (people with many friends know each other)
  • Biological and technological networks tend to be disassortative (hubs serve many leaves)
pub fn average_path_length(
  in graph: model.Graph(n, e),
  with_zero zero: e,
  with_add add: fn(e, e) -> e,
  with_compare compare: fn(e, e) -> order.Order,
  with weight_fn: fn(e) -> e,
  with_to_float to_float: fn(e) -> Float,
) -> option.Option(Float)

Average shortest path length across all node pairs. Returns None if the graph is disconnected or empty. Requires a function to convert edge weights to Float for averaging.

Time Complexity: O(V × (V+E) log V)

Interpreting Average Path Length

ValueMeaning
≈ 1.0Dense or highly connected graph (e.g. complete graph)
≈ 2.0Star-like or small-world structure
≈ V/3Chain-like or path-like topology
NoneDisconnected or empty graph

A low APL relative to the number of nodes indicates a small-world structure: the graph achieves global connectivity through a small number of hops.

pub fn diameter(
  in graph: model.Graph(n, e),
  with_zero zero: e,
  with_add add: fn(e, e) -> e,
  with_compare compare: fn(e, e) -> order.Order,
  with weight_fn: fn(e) -> e,
) -> option.Option(e)

The diameter is the maximum eccentricity (longest shortest path). Returns None if the graph is disconnected or empty.

Time Complexity: O(V × (V+E) log V)

Interpreting Diameter

ValueMeaning
1Complete graph — everyone is directly connected
2Small world — at most one hop between any pair
> log(V)Relatively sparse or stretched topology
NoneDisconnected or empty graph
pub fn eccentricity(
  in graph: model.Graph(n, e),
  node node: Int,
  with_zero zero: e,
  with_add add: fn(e, e) -> e,
  with_compare compare: fn(e, e) -> order.Order,
  with weight_fn: fn(e) -> e,
) -> option.Option(e)

Eccentricity is the maximum distance from a node to all other nodes. Returns None if the node cannot reach all other nodes.

Time Complexity: O((V+E) log V)

Interpreting Eccentricity

ValueMeaning
= radiusThe node is in the graph center
= diameterThe node is on the periphery (worst-case reachability)
1The node is adjacent to every other node
0Single-node graph
NoneThe node cannot reach all others (disconnected component)
pub fn radius(
  in graph: model.Graph(n, e),
  with_zero zero: e,
  with_add add: fn(e, e) -> e,
  with_compare compare: fn(e, e) -> order.Order,
  with weight_fn: fn(e) -> e,
) -> option.Option(e)

The radius is the minimum eccentricity. Returns None if the graph is disconnected or empty.

Time Complexity: O(V × (V+E) log V)

Interpreting Radius

ValueMeaning
= diameterHighly symmetric structure (e.g. cycle, complete graph)
< diameterCentralized topology with a clear hub (e.g. star)
1There exists a central node one hop from everyone else
NoneDisconnected or empty graph
Search Document