Class SafeGraphTraversalUtil

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

public final class SafeGraphTraversalUtil extends Object
Safe Graph Traversal Util 安全图遍历工具类

Iterative implementations of graph traversal to avoid stack overflow.

图遍历的迭代实现,避免栈溢出。

Features | 特性:

  • Iterative DFS (no recursion) | 迭代式DFS(无递归)
  • Depth-limited traversal | 深度限制遍历
  • Suitable for large graphs | 适用于大图

Usage Examples | 使用示例:

// Iterative DFS for large graphs
List<String> result = SafeGraphTraversalUtil.dfsIterative(graph, "A");

// Depth-limited DFS
List<String> limited = SafeGraphTraversalUtil.dfsWithLimit(graph, "A", 100);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty results for null inputs) - 空值安全: 是(null输入返回空结果)

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 Details

    • dfsIterative

      public static <V> List<V> dfsIterative(Graph<V> graph, V start)
      Iterative Depth-First Search (avoids stack overflow) 迭代式深度优先搜索(避免栈溢出)
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      start - the starting vertex | 起始顶点
      Returns:
      list of vertices in DFS order | DFS顺序的顶点列表
    • dfsIterative

      public static <V> void dfsIterative(Graph<V> graph, V start, Consumer<V> visitor)
      Iterative DFS with visitor 带访问器的迭代式DFS
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      start - the starting vertex | 起始顶点
      visitor - the vertex visitor | 顶点访问器
    • dfsWithLimit

      public static <V> List<V> dfsWithLimit(Graph<V> graph, V start, int maxDepth)
      Depth-limited DFS 深度限制的DFS
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      start - the starting vertex | 起始顶点
      maxDepth - maximum depth to traverse | 最大遍历深度
      Returns:
      list of vertices within depth limit | 深度限制内的顶点列表
    • dfsIterativeWithLimit

      public static <V> List<V> dfsIterativeWithLimit(Graph<V> graph, V start, int maxDepth)
      Iterative depth-limited DFS 迭代式深度限制DFS
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      start - the starting vertex | 起始顶点
      maxDepth - maximum depth to traverse | 最大遍历深度
      Returns:
      list of vertices within depth limit | 深度限制内的顶点列表
    • bfsWithLimit

      public static <V> List<V> bfsWithLimit(Graph<V> graph, V start, int maxDistance)
      BFS with maximum distance limit 带最大距离限制的BFS
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      start - the starting vertex | 起始顶点
      maxDistance - maximum distance from start | 距起始点的最大距离
      Returns:
      list of vertices within distance limit | 距离限制内的顶点列表