Interface Graph<N>

Type Parameters:
N - node type | 节点类型
All Known Implementing Classes:
MutableGraph

public interface Graph<N>
Graph - Graph Interface Graph - 图接口

An interface for graph data structures supporting directed and undirected graphs.

支持有向图和无向图的图数据结构接口。

Features | 主要功能:

  • Node operations - 节点操作
  • Edge operations - 边操作
  • Directed/undirected support - 有向/无向支持
  • Adjacency queries - 邻接查询

Usage Examples | 使用示例:

Graph<String> graph = MutableGraph.directed();
graph.addNode("A");
graph.addNode("B");
graph.addEdge("A", "B");

Set<String> neighbors = graph.successors("A");  // [B]
boolean hasEdge = graph.hasEdge("A", "B");     // true

Security | 安全性:

  • Thread-safe: No (interface, implementation-dependent) - 否(接口,取决于实现)
  • Null-safe: No - 否
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Endpoint pair representing an edge.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return all adjacent nodes (both successors and predecessors).
    boolean
    Check if this graph allows self-loops.
    int
    degree(N node)
    Return the degree of a node (number of edges incident to it).
    default int
    Return the number of edges.
    Return the set of all edges.
    boolean
    hasEdge(N nodeU, N nodeV)
    Check if the edge exists.
    boolean
    hasNode(N node)
    Check if the node exists.
    Return the edges incident to a node.
    int
    inDegree(N node)
    Return the in-degree of a node (number of incoming edges).
    boolean
    Check if this graph is directed.
    default int
    Return the number of nodes.
    Return the set of all nodes.
    int
    outDegree(N node)
    Return the out-degree of a node (number of outgoing edges).
    Return the predecessors (incoming neighbors) of a node.
    successors(N node)
    Return the successors (outgoing neighbors) of a node.
  • Method Details

    • isDirected

      boolean isDirected()
      Check if this graph is directed. 检查此图是否有向。
      Returns:
      true if directed | 如果有向则返回 true
    • allowsSelfLoops

      boolean allowsSelfLoops()
      Check if this graph allows self-loops. 检查此图是否允许自环。
      Returns:
      true if allows self-loops | 如果允许自环则返回 true
    • nodes

      Set<N> nodes()
      Return the set of all nodes. 返回所有节点的集合。
      Returns:
      the set of nodes | 节点集合
    • edges

      Return the set of all edges. 返回所有边的集合。
      Returns:
      the set of edges | 边集合
    • hasNode

      boolean hasNode(N node)
      Check if the node exists. 检查节点是否存在。
      Parameters:
      node - the node | 节点
      Returns:
      true if exists | 如果存在则返回 true
    • successors

      Set<N> successors(N node)
      Return the successors (outgoing neighbors) of a node. 返回节点的后继(出边邻居)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of successors | 后继集合
    • predecessors

      Set<N> predecessors(N node)
      Return the predecessors (incoming neighbors) of a node. 返回节点的前驱(入边邻居)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of predecessors | 前驱集合
    • adjacentNodes

      Set<N> adjacentNodes(N node)
      Return all adjacent nodes (both successors and predecessors). 返回所有邻接节点(后继和前驱)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of adjacent nodes | 邻接节点集合
    • degree

      int degree(N node)
      Return the degree of a node (number of edges incident to it). 返回节点的度数(入射边的数量)。
      Parameters:
      node - the node | 节点
      Returns:
      the degree | 度数
    • inDegree

      int inDegree(N node)
      Return the in-degree of a node (number of incoming edges). 返回节点的入度(入边数量)。
      Parameters:
      node - the node | 节点
      Returns:
      the in-degree | 入度
    • outDegree

      int outDegree(N node)
      Return the out-degree of a node (number of outgoing edges). 返回节点的出度(出边数量)。
      Parameters:
      node - the node | 节点
      Returns:
      the out-degree | 出度
    • hasEdge

      boolean hasEdge(N nodeU, N nodeV)
      Check if the edge exists. 检查边是否存在。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      true if exists | 如果存在则返回 true
    • incidentEdges

      Set<Graph.EndpointPair<N>> incidentEdges(N node)
      Return the edges incident to a node. 返回入射到节点的边。
      Parameters:
      node - the node | 节点
      Returns:
      the set of incident edges | 入射边集合
    • nodeCount

      default int nodeCount()
      Return the number of nodes. 返回节点数量。
      Returns:
      the number of nodes | 节点数量
    • edgeCount

      default int edgeCount()
      Return the number of edges. 返回边数量。
      Returns:
      the number of edges | 边数量