Class OpenGraph
java.lang.Object
cloud.opencode.base.graph.OpenGraph
Open Graph
图组件入口类
Main entry point for graph operations.
图操作的主要入口点。
Features | 特性:
- Graph creation (directed/undirected/immutable) | 图创建(有向/无向/不可变)
- Graph traversal (BFS/DFS) | 图遍历(BFS/DFS)
- Shortest path (Dijkstra, A-star, Bellman-Ford, Floyd-Warshall) | 最短路径(Dijkstra、A-star、Bellman-Ford、Floyd-Warshall)
- Topological sort & DAG operations | 拓扑排序与DAG操作
- Connectivity & Strongly connected components | 连通性与强连通分量
- Articulation points & bridges | 关节点与桥
- Bipartite detection | 二部图检测
- Graph metrics & statistics | 图度量与统计
- Graph diff, transform, snapshot | 图比较、转换、快照
Usage Examples | 使用示例:
// Create a directed graph
Graph<String> graph = OpenGraph.directed();
graph.addEdge("A", "B", 1.0);
graph.addEdge("B", "C", 2.0);
// BFS traversal
List<String> bfsResult = OpenGraph.bfs(graph, "A");
// Shortest path
List<String> path = OpenGraph.shortestPath(graph, "A", "C");
// Topological sort
List<String> order = OpenGraph.topologicalSort(graph);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (delegates to null-safe implementations) - 空值安全: 是(委托给空值安全的实现)
- Since:
- JDK 25, opencode-base-graph V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> FloydWarshallUtil.AllPairsResult<V> allPairsShortestPaths(Graph<V> graph) Floyd-Warshall all-pairs shortest paths Floyd-Warshall全源最短路径static <V> Set<V> articulationPoints(Graph<V> graph) Find all articulation points (cut vertices) 查找所有关节点(割点)static <V> List<V> aStar(Graph<V> graph, V source, V target, BiFunction<V, V, Double> heuristic) Find shortest path using A* algorithm 使用A*算法查找最短路径bellmanFord(Graph<V> graph, V source) Bellman-Ford single-source shortest paths (supports negative weights) Bellman-Ford单源最短路径(支持负权边)static <V> List<V> Breadth-first search 广度优先搜索static <V> List<V> bidirectionalBfs(Graph<V> graph, V source, V target) Find path using bidirectional BFS (efficient for large graphs) 使用双向BFS查找路径(对大图高效)static <V> BipartiteUtil.BipartiteResult<V> bipartitePartition(Graph<V> graph) Compute bipartite partition or odd cycle witness 计算二部图分区或奇环证据Find all bridges (cut edges) 查找所有桥(割边)static <V> booleancanTopologicalSort(Graph<V> graph) Check if topological sort is possible (graph is DAG) 检查是否可以进行拓扑排序(图是DAG)static <V> NetworkFlowUtil.FlowResult<V> computeFlow(Graph<V> graph, V source, V sink) Compute flow result with detailed information 计算包含详细信息的流结果condensation(Graph<V> graph) Get condensation graph (DAG of SCCs) 获取冷凝图(强连通分量的DAG)static <V> intconnectedComponentCount(Graph<V> graph) Get the number of connected components 获取连通分量数量connectedComponents(Graph<V> graph) Find all connected components 查找所有连通分量static <V> doubleGet graph density 获取图密度static <V> List<V> Depth-first search 深度优先搜索static <V> List<V> dfsIterative(Graph<V> graph, V start) Safe iterative depth-first search (avoids stack overflow) 安全的迭代式深度优先搜索(避免栈溢出)static <V> intGet graph diameter 获取图直径static <V> GraphDiff.DiffResult<V> Compare two graphs and get differences 比较两个图并获取差异Dijkstra's shortest path algorithm Dijkstra最短路径算法static <V> Graph<V> directed()Create a directed graph 创建有向图static <V> GraphBuilder<V> Create a graph builder for directed graph 创建有向图构建器static <V> WeightedGraph<V> Create a directed weighted graph 创建有向加权图static <V> Graph<V> filterEdges(Graph<V> graph, Predicate<Edge<V>> predicate) Filter edges from a graph by predicate 按谓词过滤图的边static <V> Graph<V> filterVertices(Graph<V> graph, Predicate<V> predicate) Filter vertices from a graph by predicate 按谓词过滤图的顶点static <V> List<V> Find one cycle if exists 如果存在则找到一个环Get flow on each edge 获取每条边上的流量static <V> booleanCheck if graph contains a cycle 检查图是否包含环static <V> booleanhasNegativeCycle(Graph<V> graph, V source) Check for negative weight cycle reachable from source 检查从源顶点可达的负权环static <V> booleanhasSpanningTree(Graph<V> graph) Check if a graph has a spanning tree (is connected) 检查图是否有生成树(是否连通)static <V> booleanisBiconnected(Graph<V> graph) Check if graph is biconnected 检查图是否双连通static <V> booleanisBipartite(Graph<V> graph) Check if graph is bipartite 检查图是否为二部图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 检查图是否完全连通static <V> booleanisStronglyConnected(Graph<V> graph) Check if graph is strongly connected 检查图是否强连通Find minimum spanning tree using Kruskal's algorithm 使用Kruskal算法查找最小生成树static <V> List<V> longestPath(Graph<V> graph) Find longest path in DAG (critical path) 查找DAG中的最长路径(关键路径)static <V,R> Graph <R> mapVertices(Graph<V> graph, Function<V, R> mapper) Transform graph vertices using mapping function 使用映射函数转换图顶点static <V> doubleCalculate maximum flow using Ford-Fulkerson algorithm (Edmonds-Karp) 使用Ford-Fulkerson算法(Edmonds-Karp)计算最大流Find minimum cut edges 查找最小割边static <V> doubleCalculate the total weight of the minimum spanning tree 计算最小生成树的总权重Find minimum spanning tree using Prim's algorithm 使用Prim算法查找最小生成树Find minimum spanning tree using Prim's algorithm starting from a vertex 使用Prim算法从指定顶点开始查找最小生成树static <V> Graph<V> Reverse a directed graph 反转有向图static <V> List<V> shortestPath(Graph<V> graph, V source, V target) Find shortest path between two vertices 查找两个顶点之间的最短路径static <V> Graph<V> Create an immutable snapshot of a graph 创建图的不可变快照stronglyConnectedComponents(Graph<V> graph) Find all strongly connected components (Tarjan's algorithm) 查找所有强连通分量(Tarjan算法)static <V> GraphMetrics.GraphSummaryGet graph summary with all key metrics 获取包含所有关键指标的图摘要static <V> List<V> topologicalSort(Graph<V> graph) Topological sort using Kahn's algorithm 使用Kahn算法进行拓扑排序static <V> Graph<V> transitiveClosure(Graph<V> graph) Compute transitive closure of DAG 计算DAG的传递闭包static <V> Graph<V> transitiveReduction(Graph<V> graph) Compute transitive reduction of DAG 计算DAG的传递归约static <V> Graph<V> Create an undirected graph 创建无向图static <V> GraphBuilder<V> Create a graph builder for undirected graph 创建无向图构建器static <V> WeightedGraph<V> Create an undirected weighted graph 创建无向加权图
-
Method Details
-
directed
Create a directed graph 创建有向图- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new directed graph | 新的有向图
-
undirected
Create an undirected graph 创建无向图- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new undirected graph | 新的无向图
-
directedWeighted
Create a directed weighted graph 创建有向加权图- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new directed weighted graph | 新的有向加权图
-
undirectedWeighted
Create an undirected weighted graph 创建无向加权图- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new undirected weighted graph | 新的无向加权图
-
directedBuilder
Create a graph builder for directed graph 创建有向图构建器- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new graph builder | 新的图构建器
-
undirectedBuilder
Create a graph builder for undirected graph 创建无向图构建器- Type Parameters:
V- the vertex type | 顶点类型- Returns:
- a new graph builder | 新的图构建器
-
bfs
-
dfs
-
dfsIterative
Safe iterative depth-first search (avoids stack overflow) 安全的迭代式深度优先搜索(避免栈溢出)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph to traverse | 要遍历的图start- the starting vertex | 起始顶点- Returns:
- list of vertices in DFS order | DFS顺序的顶点列表
-
dijkstra
-
shortestPath
Find shortest path between two vertices 查找两个顶点之间的最短路径- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点target- the target vertex | 目标顶点- Returns:
- list of vertices in the shortest path | 最短路径的顶点列表
-
aStar
public static <V> List<V> aStar(Graph<V> graph, V source, V target, BiFunction<V, V, Double> heuristic) Find shortest path using A* algorithm 使用A*算法查找最短路径- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点target- the target vertex | 目标顶点heuristic- the heuristic function | 启发式函数- Returns:
- list of vertices in the shortest path | 最短路径的顶点列表
-
bidirectionalBfs
Find path using bidirectional BFS (efficient for large graphs) 使用双向BFS查找路径(对大图高效)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点target- the target vertex | 目标顶点- Returns:
- list of vertices in the path | 路径的顶点列表
-
topologicalSort
-
canTopologicalSort
Check if topological sort is possible (graph is DAG) 检查是否可以进行拓扑排序(图是DAG)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if topological sort is possible | 如果可以进行拓扑排序返回true
-
hasCycle
Check if graph contains a cycle 检查图是否包含环- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if graph contains a cycle | 如果图包含环返回true
-
findCycle
-
connectedComponents
-
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 检查图是否完全连通- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if fully connected | 如果完全连通返回true
-
connectedComponentCount
Get the number of connected components 获取连通分量数量- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- number of connected components | 连通分量数量
-
prim
-
prim
Find minimum spanning tree using Prim's algorithm starting from a vertex 使用Prim算法从指定顶点开始查找最小生成树- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the undirected graph | 无向图start- the starting vertex | 起始顶点- Returns:
- set of edges in the MST | 最小生成树的边集合
-
kruskal
-
mstWeight
Calculate the total weight of the minimum spanning tree 计算最小生成树的总权重- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the undirected graph | 无向图- Returns:
- total weight of MST | 最小生成树的总权重
-
hasSpanningTree
Check if a graph has a spanning tree (is connected) 检查图是否有生成树(是否连通)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if graph is connected | 如果图连通返回true
-
maxFlow
Calculate maximum flow using Ford-Fulkerson algorithm (Edmonds-Karp) 使用Ford-Fulkerson算法(Edmonds-Karp)计算最大流- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the directed graph with edge weights as capacities | 边权重作为容量的有向图source- the source vertex | 源顶点sink- the sink vertex | 汇顶点- Returns:
- the maximum flow value | 最大流值
-
getFlows
Get flow on each edge 获取每条边上的流量- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the directed graph | 有向图source- the source vertex | 源顶点sink- the sink vertex | 汇顶点- Returns:
- map of edge to flow value | 边到流量值的映射
-
minCut
Find minimum cut edges 查找最小割边- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the directed graph | 有向图source- the source vertex | 源顶点sink- the sink vertex | 汇顶点- Returns:
- set of edges in the minimum cut | 最小割中的边集合
-
computeFlow
Compute flow result with detailed information 计算包含详细信息的流结果- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the directed graph | 有向图source- the source vertex | 源顶点sink- the sink vertex | 汇顶点- Returns:
- the flow result with max flow, edge flows, and min-cut | 包含最大流、边流量和最小割的流结果
-
bellmanFord
Bellman-Ford single-source shortest paths (supports negative weights) Bellman-Ford单源最短路径(支持负权边)- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点- Returns:
- map of vertex to shortest distance | 顶点到最短距离的映射
-
hasNegativeCycle
Check for negative weight cycle reachable from source 检查从源顶点可达的负权环- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图source- the source vertex | 源顶点- Returns:
- true if negative cycle exists | 如果存在负权环返回true
-
allPairsShortestPaths
Floyd-Warshall all-pairs shortest paths Floyd-Warshall全源最短路径- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- all-pairs shortest path result | 全源最短路径结果
-
stronglyConnectedComponents
-
isStronglyConnected
Check if graph is strongly connected 检查图是否强连通- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if strongly connected | 如果强连通返回true
-
condensation
-
articulationPoints
-
bridges
-
isBiconnected
Check if graph is biconnected 检查图是否双连通- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if biconnected | 如果双连通返回true
-
isBipartite
Check if graph is bipartite 检查图是否为二部图- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- true if bipartite | 如果是二部图返回true
-
bipartitePartition
Compute bipartite partition or odd cycle witness 计算二部图分区或奇环证据- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- bipartite result | 二部图结果
-
longestPath
-
transitiveReduction
-
transitiveClosure
-
density
Get graph density 获取图密度- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- density value between 0 and 1 | 密度值,0到1之间
-
diameter
Get graph diameter 获取图直径- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- diameter | 直径
-
summary
Get graph summary with all key metrics 获取包含所有关键指标的图摘要- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the graph | 图- Returns:
- graph summary | 图摘要
-
diff
Compare two graphs and get differences 比较两个图并获取差异- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
before- the original graph | 原始图after- the modified graph | 修改后的图- Returns:
- diff result | 差异结果
-
mapVertices
Transform graph vertices using mapping function 使用映射函数转换图顶点- Type Parameters:
V- the source vertex type | 源顶点类型R- the target vertex type | 目标顶点类型- Parameters:
graph- the graph | 图mapper- the mapping function | 映射函数- Returns:
- transformed graph | 转换后的图
-
reverse
-
filterVertices
Filter vertices from a graph by predicate 按谓词过滤图的顶点- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图predicate- the vertex filter predicate | 顶点过滤谓词- Returns:
- a new graph containing only vertices matching the predicate | 仅包含匹配谓词的顶点的新图
-
filterEdges
Filter edges from a graph by predicate 按谓词过滤图的边- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图predicate- the edge filter predicate | 边过滤谓词- Returns:
- a new graph containing only edges matching the predicate | 仅包含匹配谓词的边的新图
-
snapshot
-