Class MutableGraph<N>
java.lang.Object
cloud.opencode.base.collections.graph.MutableGraph<N>
- Type Parameters:
N- node type | 节点类型
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface Graph
Graph.EndpointPair<N> -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a node to the graph.adjacentNodes(N node) Return all adjacent nodes (both successors and predecessors).booleanCheck if this graph allows self-loops.static <N> MutableGraph<N> create(boolean directed, boolean allowsSelfLoops) Create a graph with custom configuration.intReturn the degree of a node (number of edges incident to it).static <N> MutableGraph<N> directed()Create a directed mutable graph.static <N> MutableGraph<N> Create a directed mutable graph allowing self-loops.edges()Return the set of all edges.booleanbooleanCheck if the edge exists.inthashCode()booleanCheck if the node exists.incidentEdges(N node) Return the edges incident to a node.intReturn the in-degree of a node (number of incoming edges).booleanCheck if this graph is directed.nodes()Return the set of all nodes.intReturn the out-degree of a node (number of outgoing edges).predecessors(N node) Return the predecessors (incoming neighbors) of a node.booleanAdd an edge to the graph, adding nodes if necessary.booleanremoveEdge(N nodeU, N nodeV) Remove an edge from the graph.booleanremoveNode(N node) Remove a node and all its edges from the graph.successors(N node) Return the successors (outgoing neighbors) of a node.toString()static <N> MutableGraph<N> Create an undirected mutable graph.static <N> MutableGraph<N> Create an undirected mutable graph allowing self-loops.
-
Method Details
-
directed
Create a directed mutable graph. 创建有向可变图。- Type Parameters:
N- node type | 节点类型- Returns:
- new directed graph | 新有向图
-
undirected
Create an undirected mutable graph. 创建无向可变图。- Type Parameters:
N- node type | 节点类型- Returns:
- new undirected graph | 新无向图
-
directedAllowingSelfLoops
Create a directed mutable graph allowing self-loops. 创建允许自环的有向可变图。- Type Parameters:
N- node type | 节点类型- Returns:
- new directed graph | 新有向图
-
undirectedAllowingSelfLoops
Create an undirected mutable graph allowing self-loops. 创建允许自环的无向可变图。- Type Parameters:
N- node type | 节点类型- Returns:
- new undirected graph | 新无向图
-
create
Create a graph with custom configuration. 创建自定义配置的图。- Type Parameters:
N- node type | 节点类型- Parameters:
directed- whether directed | 是否有向allowsSelfLoops- whether allows self-loops | 是否允许自环- Returns:
- new graph | 新图
-
addNode
Add a node to the graph. 添加节点到图。- Parameters:
node- the node to add | 要添加的节点- Returns:
- true if added (node was not present) | 如果添加成功则返回 true
-
putEdge
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
Remove a node and all its edges from the graph. 从图中移除节点及其所有边。- Parameters:
node- the node to remove | 要移除的节点- Returns:
- true if removed | 如果移除成功则返回 true
-
removeEdge
-
isDirected
public boolean isDirected()Description copied from interface:GraphCheck if this graph is directed. 检查此图是否有向。- Specified by:
isDirectedin interfaceGraph<N>- Returns:
- true if directed | 如果有向则返回 true
-
allowsSelfLoops
public boolean allowsSelfLoops()Description copied from interface:GraphCheck if this graph allows self-loops. 检查此图是否允许自环。- Specified by:
allowsSelfLoopsin interfaceGraph<N>- Returns:
- true if allows self-loops | 如果允许自环则返回 true
-
nodes
-
edges
-
hasNode
-
successors
-
predecessors
-
adjacentNodes
-
degree
-
inDegree
-
outDegree
-
hasEdge
-
incidentEdges
Description copied from interface:GraphReturn the edges incident to a node. 返回入射到节点的边。- Specified by:
incidentEdgesin interfaceGraph<N>- Parameters:
node- the node | 节点- Returns:
- the set of incident edges | 入射边集合
-
equals
-
hashCode
-
toString
-