Class ConnectedComponentsUtil
java.lang.Object
cloud.opencode.base.graph.algorithm.ConnectedComponentsUtil
Connected Components Util
连通分量工具类
Utility class for finding connected components in graphs.
查找图中连通分量的工具类。
Features | 特性:
- Find all connected components | 查找所有连通分量
- Check if two vertices are connected | 检查两个顶点是否连通
- Check if graph is fully connected | 检查图是否完全连通
Time Complexity | 时间复杂度: O(V + E)
Usage Examples | 使用示例:
// Find all connected components
List<Set<String>> components = ConnectedComponentsUtil.find(graph);
// Check if two vertices are connected
boolean connected = ConnectedComponentsUtil.isConnected(graph, "A", "B");
// Check if graph is fully connected
boolean fullyConnected = ConnectedComponentsUtil.isFullyConnected(graph);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (returns empty/false for null inputs) - 空值安全: 是(null输入返回空/false)
Performance | 性能特性:
- Time complexity: O(V + E) - 时间复杂度: O(V + E)
- Space complexity: O(V) - 空间复杂度: O(V)
- Since:
- JDK 25, opencode-base-graph V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> intGet the number of connected components 获取连通分量的数量Find all connected components 查找所有连通分量static <V> Set<V> getComponentContaining(Graph<V> graph, V vertex) Get the component containing a specific vertex 获取包含特定顶点的分量static <V> Set<V> getLargestComponent(Graph<V> graph) Get the largest connected component 获取最大连通分量static <V> Set<V> getSmallestComponent(Graph<V> graph) Get the smallest connected component 获取最小连通分量static <V> booleanisConnected(Graph<V> graph, V v1, V v2) Check if two vertices are connected 检查两个顶点是否连通static <V> booleanisFullyConnected(Graph<V> graph) Check if graph is fully connected (single component) 检查图是否完全连通(单一分量)
-
Method Details
-
find
-
isConnected
Check if two vertices are connected 检查两个顶点是否连通- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图v1- first vertex | 第一个顶点v2- second vertex | 第二个顶点- Returns:
- true if connected | 如果连通返回true
-
isFullyConnected
Check if graph is fully connected (single component) 检查图是否完全连通(单一分量)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if fully connected | 如果完全连通返回true
-
count
Get the number of connected components 获取连通分量的数量- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- number of connected components | 连通分量数量
-
getLargestComponent
-
getSmallestComponent
-
getComponentContaining
Get the component containing a specific vertex 获取包含特定顶点的分量- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图vertex- the vertex | 顶点- Returns:
- the component containing the vertex, or empty set if vertex not found | 包含该顶点的分量,顶点未找到时返回空集合
-