Class GraphMetrics

java.lang.Object
cloud.opencode.base.graph.algorithm.GraphMetrics

public final class GraphMetrics extends Object
Graph Metrics - Utility class for computing graph metrics and statistics 图度量 - 计算图度量和统计信息的工具类

Provides static methods for computing various graph metrics including density, eccentricity, diameter, radius, center, average path length, and clustering coefficient.

提供计算各种图度量的静态方法,包括密度、离心率、直径、半径、中心、平均路径长度和聚类系数。

Features | 主要功能:

  • Edge density calculation - 边密度计算
  • Eccentricity, diameter, and radius - 离心率、直径和半径
  • Graph center identification - 图中心识别
  • Average path length (BFS-based, unweighted) - 平均路径长度(基于BFS,无权)
  • Local and average clustering coefficient - 局部和平均聚类系数
  • Full graph summary - 完整图摘要

Usage Examples | 使用示例:

Graph<String> graph = OpenGraph.undirected();
graph.addEdge("A", "B");
graph.addEdge("B", "C");

double d = GraphMetrics.density(graph);
int diam = GraphMetrics.diameter(graph);
GraphMetrics.GraphSummary summary = GraphMetrics.summary(graph);
Since:
JDK 25, opencode-base-graph V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Graph summary record containing key metrics 包含关键度量的图摘要记录
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V> double
    Compute the average clustering coefficient over all vertices 计算所有顶点的平均聚类系数
    static <V> double
    Compute the average shortest path length (BFS-based, unweighted) 计算平均最短路径长度(基于BFS,无权)
    static <V> Set<V>
    center(Graph<V> graph)
    Find the center of a graph (vertices with eccentricity equal to the radius) 查找图的中心(离心率等于半径的顶点集合)
    static <V> double
    clusteringCoefficient(Graph<V> graph, V vertex)
    Compute the local clustering coefficient of a vertex 计算顶点的局部聚类系数
    static <V> double
    density(Graph<V> graph)
    Compute the edge density of a graph 计算图的边密度
    static <V> int
    diameter(Graph<V> graph)
    Compute the diameter of a graph (max eccentricity over all vertices) 计算图的直径(所有顶点的最大离心率)
    static <V> int
    eccentricity(Graph<V> graph, V vertex)
    Compute the eccentricity of a vertex (BFS-based, unweighted) 计算顶点的离心率(基于BFS,无权)
    static <V> int
    radius(Graph<V> graph)
    Compute the radius of a graph (min eccentricity over all vertices) 计算图的半径(所有顶点的最小离心率)
    summary(Graph<V> graph)
    Compute a full summary of graph metrics 计算完整的图度量摘要

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • density

      public static <V> double density(Graph<V> graph)
      Compute the edge density of a graph 计算图的边密度

      For directed graphs: E / (V * (V - 1)). For undirected graphs: 2 * E / (V * (V - 1)). Returns 0 if V <= 1.

      有向图: E / (V * (V - 1))。无向图: 2 * E / (V * (V - 1))。当 V <= 1 时返回 0。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the edge density in range [0, 1] | 边密度,范围 [0, 1]
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • eccentricity

      public static <V> int eccentricity(Graph<V> graph, V vertex)
      Compute the eccentricity of a vertex (BFS-based, unweighted) 计算顶点的离心率(基于BFS,无权)

      The eccentricity is the maximum shortest path distance from the given vertex to any other reachable vertex. Returns Integer.MAX_VALUE if any vertex is unreachable from the given vertex.

      离心率是从给定顶点到任何其他可达顶点的最大最短路径距离。 如果存在从该顶点不可达的顶点,则返回 Integer.MAX_VALUE

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      vertex - the vertex | 顶点
      Returns:
      the eccentricity | 离心率
      Throws:
      NullPointerException - if graph or vertex is null | 当图或顶点为null时抛出
      IllegalArgumentException - if vertex is not in the graph | 当顶点不在图中时抛出
    • diameter

      public static <V> int diameter(Graph<V> graph)
      Compute the diameter of a graph (max eccentricity over all vertices) 计算图的直径(所有顶点的最大离心率)

      Time complexity: O(V * (V + E)).

      时间复杂度: O(V * (V + E))。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the diameter, or 0 for empty/single-vertex graphs, Integer.MAX_VALUE for disconnected graphs | 直径,空图或单顶点图返回0,断开图返回Integer.MAX_VALUE
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • radius

      public static <V> int radius(Graph<V> graph)
      Compute the radius of a graph (min eccentricity over all vertices) 计算图的半径(所有顶点的最小离心率)
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the radius, or 0 for empty/single-vertex graphs, Integer.MAX_VALUE for disconnected graphs | 半径,空图或单顶点图返回0,断开图返回Integer.MAX_VALUE
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • center

      public static <V> Set<V> center(Graph<V> graph)
      Find the center of a graph (vertices with eccentricity equal to the radius) 查找图的中心(离心率等于半径的顶点集合)
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      set of center vertices | 中心顶点集合
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • averagePathLength

      public static <V> double averagePathLength(Graph<V> graph)
      Compute the average shortest path length (BFS-based, unweighted) 计算平均最短路径长度(基于BFS,无权)

      Only counts reachable pairs. Returns 0 if no reachable pairs exist.

      仅计算可达的顶点对。如果不存在可达的顶点对,返回0。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the average path length | 平均路径长度
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • clusteringCoefficient

      public static <V> double clusteringCoefficient(Graph<V> graph, V vertex)
      Compute the local clustering coefficient of a vertex 计算顶点的局部聚类系数

      For a vertex v with k neighbors, the clustering coefficient is the ratio of actual edges among neighbors to the maximum possible edges. For undirected: edges / (k*(k-1)/2). For directed: edges / (k*(k-1)). Returns 0 if k < 2.

      对于具有k个邻居的顶点v,聚类系数是邻居之间实际边数与最大可能边数的比率。 无向图: edges / (k*(k-1)/2)。有向图: edges / (k*(k-1))。当k < 2时返回0。

      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      vertex - the vertex | 顶点
      Returns:
      the local clustering coefficient in range [0, 1] | 局部聚类系数,范围 [0, 1]
      Throws:
      NullPointerException - if graph or vertex is null | 当图或顶点为null时抛出
    • averageClusteringCoefficient

      public static <V> double averageClusteringCoefficient(Graph<V> graph)
      Compute the average clustering coefficient over all vertices 计算所有顶点的平均聚类系数
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the average clustering coefficient | 平均聚类系数
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出
    • summary

      public static <V> GraphMetrics.GraphSummary summary(Graph<V> graph)
      Compute a full summary of graph metrics 计算完整的图度量摘要
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      the graph summary | 图摘要
      Throws:
      NullPointerException - if graph is null | 当图为null时抛出