Class GraphSerializer

java.lang.Object
cloud.opencode.base.graph.serializer.GraphSerializer

public final class GraphSerializer extends Object
Graph Serializer 图序列化器

Utility class for serializing and deserializing graphs.

用于序列化和反序列化图的工具类。

Supported Formats | 支持的格式:

  • DOT format (Graphviz) | DOT格式(Graphviz)
  • Adjacency list format | 邻接表格式
  • Edge list format | 边列表格式

Features | 主要功能:

  • Export to DOT format for Graphviz visualization - 导出为DOT格式用于Graphviz可视化
  • Serialize and parse adjacency list format - 序列化和解析邻接表格式
  • Serialize and parse edge list format - 序列化和解析边列表格式
  • Support for weighted and unweighted edges - 支持有权和无权边
  • Graph statistics calculation (density, degree distribution) - 图统计信息计算(密度、度分布)
  • Handles both directed and undirected graphs - 处理有向图和无向图

Usage Examples | 使用示例:

// Export to DOT format
String dot = GraphSerializer.toDot(graph);

// Export to adjacency list
String adjList = GraphSerializer.toAdjacencyList(graph);

// Parse from adjacency list
Graph<String> graph = GraphSerializer.fromAdjacencyList(adjListString, true);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty string for null graph) - 空值安全: 是(null图返回空字符串)

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty string for null graph) - 空值安全: 是(null图返回空字符串)

Performance | 性能特性:

  • Time complexity: O(V + E) - 时间复杂度: O(V + E)
  • Space complexity: O(V + E) - 空间复杂度: O(V + E)
Since:
JDK 25, opencode-base-graph V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • toDot

      public static <V> String toDot(Graph<V> graph)
      Convert graph to DOT format (Graphviz) 将图转换为DOT格式(Graphviz)
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to convert | 要转换的图
      Returns:
      DOT format string | DOT格式字符串
    • toDot

      public static <V> String toDot(Graph<V> graph, String graphName)
      Convert graph to DOT format with custom name 将图转换为带自定义名称的DOT格式
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to convert | 要转换的图
      graphName - the graph name | 图名称
      Returns:
      DOT format string | DOT格式字符串
    • toAdjacencyList

      public static <V> String toAdjacencyList(Graph<V> graph)
      Convert graph to adjacency list format 将图转换为邻接表格式
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to convert | 要转换的图
      Returns:
      adjacency list string | 邻接表字符串
    • fromAdjacencyList

      public static Graph<String> fromAdjacencyList(String input, boolean directed)
      Parse graph from adjacency list format 从邻接表格式解析图
      Parameters:
      input - the adjacency list string | 邻接表字符串
      directed - whether the graph is directed | 图是否有向
      Returns:
      the parsed graph | 解析的图
    • toEdgeList

      public static <V> String toEdgeList(Graph<V> graph)
      Convert graph to edge list format 将图转换为边列表格式
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to convert | 要转换的图
      Returns:
      edge list string | 边列表字符串
    • fromEdgeList

      public static Graph<String> fromEdgeList(String input, boolean directed)
      Parse graph from edge list format 从边列表格式解析图
      Parameters:
      input - the edge list string | 边列表字符串
      directed - whether the graph is directed | 图是否有向
      Returns:
      the parsed graph | 解析的图
    • getStatistics

      public static <V> String getStatistics(Graph<V> graph)
      Get graph statistics as string 获取图统计信息字符串
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph | 图
      Returns:
      statistics string | 统计信息字符串