Class MutableGraph<N>

java.lang.Object
cloud.opencode.base.collections.graph.MutableGraph<N>
Type Parameters:
N - node type | 节点类型
All Implemented Interfaces:
Graph<N>, Serializable

public final class MutableGraph<N> extends Object implements Graph<N>, Serializable
MutableGraph - Mutable Graph Implementation MutableGraph - 可变图实现

A mutable graph that supports adding and removing nodes and edges.

支持添加和移除节点和边的可变图。

Features | 主要功能:

  • Add/remove nodes - 添加/移除节点
  • Add/remove edges - 添加/移除边
  • Directed/undirected support - 有向/无向支持
  • Self-loop configuration - 自环配置

Usage Examples | 使用示例:

// Create directed graph - 创建有向图
MutableGraph<String> directed = MutableGraph.directed();
directed.addNode("A");
directed.addNode("B");
directed.putEdge("A", "B");

// Create undirected graph - 创建无向图
MutableGraph<Integer> undirected = MutableGraph.undirected();
undirected.putEdge(1, 2);  // Nodes added automatically

// Remove operations - 移除操作
directed.removeEdge("A", "B");
directed.removeNode("A");

Performance | 性能特性:

  • addNode: O(1) - addNode: O(1)
  • addEdge: O(1) - addEdge: O(1)
  • hasEdge: O(1) - hasEdge: O(1)
  • removeNode: O(degree) - removeNode: O(度数)

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: No (nulls not allowed) - 空值安全: 否(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • directed

      public static <N> MutableGraph<N> directed()
      Create a directed mutable graph. 创建有向可变图。
      Type Parameters:
      N - node type | 节点类型
      Returns:
      new directed graph | 新有向图
    • undirected

      public static <N> MutableGraph<N> undirected()
      Create an undirected mutable graph. 创建无向可变图。
      Type Parameters:
      N - node type | 节点类型
      Returns:
      new undirected graph | 新无向图
    • directedAllowingSelfLoops

      public static <N> MutableGraph<N> directedAllowingSelfLoops()
      Create a directed mutable graph allowing self-loops. 创建允许自环的有向可变图。
      Type Parameters:
      N - node type | 节点类型
      Returns:
      new directed graph | 新有向图
    • undirectedAllowingSelfLoops

      public static <N> MutableGraph<N> undirectedAllowingSelfLoops()
      Create an undirected mutable graph allowing self-loops. 创建允许自环的无向可变图。
      Type Parameters:
      N - node type | 节点类型
      Returns:
      new undirected graph | 新无向图
    • create

      public static <N> MutableGraph<N> create(boolean directed, boolean allowsSelfLoops)
      Create a graph with custom configuration. 创建自定义配置的图。
      Type Parameters:
      N - node type | 节点类型
      Parameters:
      directed - whether directed | 是否有向
      allowsSelfLoops - whether allows self-loops | 是否允许自环
      Returns:
      new graph | 新图
    • addNode

      public boolean addNode(N node)
      Add a node to the graph. 添加节点到图。
      Parameters:
      node - the node to add | 要添加的节点
      Returns:
      true if added (node was not present) | 如果添加成功则返回 true
    • putEdge

      public boolean putEdge(N nodeU, N nodeV)
      Add an edge to the graph, adding nodes if necessary. 添加边到图,必要时添加节点。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      true if edge was added | 如果边被添加则返回 true
      Throws:
      IllegalArgumentException - if self-loop not allowed | 如果不允许自环则抛出异常
    • removeNode

      public boolean removeNode(N node)
      Remove a node and all its edges from the graph. 从图中移除节点及其所有边。
      Parameters:
      node - the node to remove | 要移除的节点
      Returns:
      true if removed | 如果移除成功则返回 true
    • removeEdge

      public boolean removeEdge(N nodeU, N nodeV)
      Remove an edge from the graph. 从图中移除边。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      true if removed | 如果移除成功则返回 true
    • isDirected

      public boolean isDirected()
      Description copied from interface: Graph
      Check if this graph is directed. 检查此图是否有向。
      Specified by:
      isDirected in interface Graph<N>
      Returns:
      true if directed | 如果有向则返回 true
    • allowsSelfLoops

      public boolean allowsSelfLoops()
      Description copied from interface: Graph
      Check if this graph allows self-loops. 检查此图是否允许自环。
      Specified by:
      allowsSelfLoops in interface Graph<N>
      Returns:
      true if allows self-loops | 如果允许自环则返回 true
    • nodes

      public Set<N> nodes()
      Description copied from interface: Graph
      Return the set of all nodes. 返回所有节点的集合。
      Specified by:
      nodes in interface Graph<N>
      Returns:
      the set of nodes | 节点集合
    • edges

      public Set<Graph.EndpointPair<N>> edges()
      Description copied from interface: Graph
      Return the set of all edges. 返回所有边的集合。
      Specified by:
      edges in interface Graph<N>
      Returns:
      the set of edges | 边集合
    • hasNode

      public boolean hasNode(N node)
      Description copied from interface: Graph
      Check if the node exists. 检查节点是否存在。
      Specified by:
      hasNode in interface Graph<N>
      Parameters:
      node - the node | 节点
      Returns:
      true if exists | 如果存在则返回 true
    • successors

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

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

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

      public int degree(N node)
      Description copied from interface: Graph
      Return the degree of a node (number of edges incident to it). 返回节点的度数(入射边的数量)。
      Specified by:
      degree in interface Graph<N>
      Parameters:
      node - the node | 节点
      Returns:
      the degree | 度数
    • inDegree

      public int inDegree(N node)
      Description copied from interface: Graph
      Return the in-degree of a node (number of incoming edges). 返回节点的入度(入边数量)。
      Specified by:
      inDegree in interface Graph<N>
      Parameters:
      node - the node | 节点
      Returns:
      the in-degree | 入度
    • outDegree

      public int outDegree(N node)
      Description copied from interface: Graph
      Return the out-degree of a node (number of outgoing edges). 返回节点的出度(出边数量)。
      Specified by:
      outDegree in interface Graph<N>
      Parameters:
      node - the node | 节点
      Returns:
      the out-degree | 出度
    • hasEdge

      public boolean hasEdge(N nodeU, N nodeV)
      Description copied from interface: Graph
      Check if the edge exists. 检查边是否存在。
      Specified by:
      hasEdge in interface Graph<N>
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      true if exists | 如果存在则返回 true
    • incidentEdges

      public Set<Graph.EndpointPair<N>> incidentEdges(N node)
      Description copied from interface: Graph
      Return the edges incident to a node. 返回入射到节点的边。
      Specified by:
      incidentEdges in interface Graph<N>
      Parameters:
      node - the node | 节点
      Returns:
      the set of incident edges | 入射边集合
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object