raphtory 0.17.0

raphtory, a temporal graph library
Documentation
//! Local Clustering coefficient - measures the degree to which nodes in a graph tend to cluster together.
//!
//! It is calculated by dividing the number of triangles (sets of three nodes that are all
//! connected to each other) in the graph by the total number of possible triangles.
//! The resulting value is a number between 0 and 1 that represents the density of
//! clustering in the graph.
//!
//! A high clustering coefficient indicates that nodes tend to be
//! connected to nodes that are themselves connected to each other, while a low clustering
//! coefficient indicates that nodes tend to be connected to nodes that are not connected
//! to each other.
//!
//! In a social network of a particular community_detection, we can compute the clustering
//! coefficient of each node to get an idea of how strongly connected and cohesive
//! that node's neighborhood is.
//!
//! A high clustering coefficient for a node in a social network indicates that the
//! node's neighbors tend to be strongly connected with each other, forming a tightly-knit
//! group or community_detection. In contrast, a low clustering coefficient for a node indicates that
//! its neighbors are relatively less connected with each other, suggesting a more fragmented
//! or diverse community_detection.
//!
//! # Examples
//!
//! ```rust
//! use raphtory::algorithms::metrics::clustering_coefficient::local_clustering_coefficient::local_clustering_coefficient;
//! use raphtory::prelude::*;
//!
//! let g = Graph::new();
//! let windowed_graph = g.window(0, 7);
//! let vs = vec![
//!     (1, 1, 2),
//!     (2, 1, 3),
//!     (3, 2, 1),
//!     (4, 3, 2),
//!     (5, 1, 4),
//!     (6, 4, 5),
//! ];
//!
//! for (t, src, dst) in &vs {
//!     g.add_edge(*t, *src, *dst, NO_PROPS, None);
//! }
//!
//! let actual = (1..=5)
//! .map(|v| local_clustering_coefficient(&windowed_graph, v))
//! .collect::<Vec<_>>();
//!
//! println!("local clustering coefficient of all nodes: {:?}", actual);
//! ```

use crate::{
    algorithms::motifs::local_triangle_count::local_triangle_count,
    core::entities::nodes::node_ref::AsNodeRef, db::api::view::*,
};

/// Local clustering coefficient - measures the degree to which a single node in a graph tend to cluster together.
///
///
/// # Arguments
/// - `graph`: Raphtory graph, can be directed or undirected but will be treated as undirected.
/// - `v`: node id or name
///
/// # Returns
/// the local clustering coefficient of node v in g.
pub fn local_clustering_coefficient<G: StaticGraphViewOps, V: AsNodeRef>(
    graph: &G,
    v: V,
) -> Option<f64> {
    let v = v.as_node_ref();
    if let Some(node) = graph.node(v) {
        if let Some(triangle_count) = local_triangle_count(graph, v) {
            let triangle_count = triangle_count as f64;
            let mut degree = node.degree() as f64;
            if graph.has_edge(node.node, node.node) {
                degree -= 1.0;
            }
            if degree > 1.0 {
                Some((2.0 * triangle_count) / (degree * (degree - 1.0)))
            } else {
                Some(0.0)
            }
        } else {
            None
        }
    } else {
        None
    }
}