Class GraphMetrics
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 ClassesModifier and TypeClassDescriptionstatic final recordGraph summary record containing key metrics 包含关键度量的图摘要记录 -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> doubleaverageClusteringCoefficient(Graph<V> graph) Compute the average clustering coefficient over all vertices 计算所有顶点的平均聚类系数static <V> doubleaveragePathLength(Graph<V> graph) Compute the average shortest path length (BFS-based, unweighted) 计算平均最短路径长度(基于BFS,无权)static <V> Set<V> Find the center of a graph (vertices with eccentricity equal to the radius) 查找图的中心(离心率等于半径的顶点集合)static <V> doubleclusteringCoefficient(Graph<V> graph, V vertex) Compute the local clustering coefficient of a vertex 计算顶点的局部聚类系数static <V> doubleCompute the edge density of a graph 计算图的边密度static <V> intCompute the diameter of a graph (max eccentricity over all vertices) 计算图的直径(所有顶点的最大离心率)static <V> inteccentricity(Graph<V> graph, V vertex) Compute the eccentricity of a vertex (BFS-based, unweighted) 计算顶点的离心率(基于BFS,无权)static <V> intCompute the radius of a graph (min eccentricity over all vertices) 计算图的半径(所有顶点的最小离心率)static <V> GraphMetrics.GraphSummaryCompute a full summary of graph metrics 计算完整的图度量摘要
-
Method Details
-
density
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
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_VALUEif 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
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
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
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
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
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
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
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时抛出
-