Interface Graph<V>

Type Parameters:
V - the vertex type | 顶点类型
All Known Subinterfaces:
WeightedGraph<V>
All Known Implementing Classes:
DirectedGraph, ImmutableGraph, UndirectedGraph

public interface Graph<V>
Graph Interface 图接口

Core interface for graph data structure operations.

图数据结构操作的核心接口。

Features | 主要功能:

  • Add/remove vertices and edges - 添加/删除顶点和边
  • Query neighbors and degree - 查询邻居和度
  • Support weighted and unweighted edges - 支持加权和无权边
  • Implementations: DirectedGraph, UndirectedGraph, WeightedGraph - 实现类

Usage Examples | 使用示例:

Graph<String> graph = OpenGraph.directed();
graph.addVertex("A");
graph.addEdge("A", "B", 1.0);
Set<String> neighbors = graph.neighbors("A");

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Implementations reject null vertices - 空值安全: 实现类拒绝null顶点
Since:
JDK 25, opencode-base-graph V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEdge(V from, V to)
    Add an unweighted edge 添加无权重边
    void
    addEdge(V from, V to, double weight)
    Add a weighted edge 添加加权边
    void
    addVertex(V vertex)
    Add a vertex to the graph 添加顶点到图
    void
    Clear the graph 清空图
    boolean
    containsEdge(V from, V to)
    Check if the graph contains an edge 检查图是否包含边
    boolean
    containsVertex(V vertex)
    Check if the graph contains a vertex 检查图是否包含顶点
    int
    Get the number of edges 获取边数量
    Get all edges 获取所有边
    double
    getWeight(V from, V to)
    Get the weight of an edge 获取边的权重
    default int
    inDegree(V vertex)
    Get the in-degree of a vertex 获取顶点的入度
    inEdges(V vertex)
    Get incoming edges to a vertex 获取顶点的入边
    boolean
    Check if this is a directed graph 检查是否为有向图
    default boolean
    Check if the graph is empty 检查图是否为空
    neighbors(V vertex)
    Get neighboring vertices 获取邻接顶点
    default int
    outDegree(V vertex)
    Get the out-degree of a vertex 获取顶点的出度
    outEdges(V vertex)
    Get outgoing edges from a vertex 获取顶点的出边
    void
    removeEdge(V from, V to)
    Remove an edge 移除边
    void
    removeVertex(V vertex)
    Remove a vertex and all its edges 移除顶点及其所有边
    default Graph<V>
    Create an immutable snapshot of this graph 创建此图的不可变快照
    int
    Get the number of vertices 获取顶点数量
    Get all vertices 获取所有顶点
  • Method Details

    • addVertex

      void addVertex(V vertex)
      Add a vertex to the graph 添加顶点到图
      Parameters:
      vertex - the vertex to add | 要添加的顶点
    • addEdge

      void addEdge(V from, V to)
      Add an unweighted edge 添加无权重边
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
    • addEdge

      void addEdge(V from, V to, double weight)
      Add a weighted edge 添加加权边
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      weight - the edge weight | 边权重
    • removeVertex

      void removeVertex(V vertex)
      Remove a vertex and all its edges 移除顶点及其所有边
      Parameters:
      vertex - the vertex to remove | 要移除的顶点
    • removeEdge

      void removeEdge(V from, V to)
      Remove an edge 移除边
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
    • vertices

      Set<V> vertices()
      Get all vertices 获取所有顶点
      Returns:
      set of vertices | 顶点集合
    • edges

      Set<Edge<V>> edges()
      Get all edges 获取所有边
      Returns:
      set of edges | 边集合
    • neighbors

      Set<V> neighbors(V vertex)
      Get neighboring vertices 获取邻接顶点
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of neighboring vertices | 邻接顶点集合
    • outEdges

      Set<Edge<V>> outEdges(V vertex)
      Get outgoing edges from a vertex 获取顶点的出边
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of outgoing edges | 出边集合
    • inEdges

      Set<Edge<V>> inEdges(V vertex)
      Get incoming edges to a vertex 获取顶点的入边
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of incoming edges | 入边集合
    • vertexCount

      int vertexCount()
      Get the number of vertices 获取顶点数量
      Returns:
      vertex count | 顶点数量
    • edgeCount

      int edgeCount()
      Get the number of edges 获取边数量
      Returns:
      edge count | 边数量
    • containsVertex

      boolean containsVertex(V vertex)
      Check if the graph contains a vertex 检查图是否包含顶点
      Parameters:
      vertex - the vertex to check | 要检查的顶点
      Returns:
      true if contains | 如果包含返回true
    • containsEdge

      boolean containsEdge(V from, V to)
      Check if the graph contains an edge 检查图是否包含边
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      true if contains | 如果包含返回true
    • getWeight

      double getWeight(V from, V to)
      Get the weight of an edge 获取边的权重
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      the edge weight, or Double.MAX_VALUE if no edge exists | 边权重,如果边不存在则返回Double.MAX_VALUE
    • isDirected

      boolean isDirected()
      Check if this is a directed graph 检查是否为有向图
      Returns:
      true if directed | 如果是有向图返回true
    • outDegree

      default int outDegree(V vertex)
      Get the out-degree of a vertex 获取顶点的出度
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      the out-degree | 出度
    • inDegree

      default int inDegree(V vertex)
      Get the in-degree of a vertex 获取顶点的入度
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      the in-degree | 入度
    • isEmpty

      default boolean isEmpty()
      Check if the graph is empty 检查图是否为空
      Returns:
      true if empty | 如果为空返回true
    • clear

      void clear()
      Clear the graph 清空图
    • snapshot

      default Graph<V> snapshot()
      Create an immutable snapshot of this graph 创建此图的不可变快照
      Returns:
      an immutable copy of this graph | 此图的不可变副本