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 TypeMethodDescriptionvoidAdd an unweighted edge 添加无权重边voidAdd a weighted edge 添加加权边voidAdd a vertex to the graph 添加顶点到图voidclear()Clear the graph 清空图booleancontainsEdge(V from, V to) Check if the graph contains an edge 检查图是否包含边booleancontainsVertex(V vertex) Check if the graph contains a vertex 检查图是否包含顶点intGet the number of edges 获取边数量edges()Get all edges 获取所有边doubleGet the weight of an edge 获取边的权重default intGet the in-degree of a vertex 获取顶点的入度Get incoming edges to a vertex 获取顶点的入边booleanCheck if this is a directed graph 检查是否为有向图default booleanisEmpty()Check if the graph is empty 检查图是否为空Get neighboring vertices 获取邻接顶点default intGet the out-degree of a vertex 获取顶点的出度Get outgoing edges from a vertex 获取顶点的出边voidremoveEdge(V from, V to) Remove an edge 移除边voidremoveVertex(V vertex) Remove a vertex and all its edges 移除顶点及其所有边snapshot()Create an immutable snapshot of this graph 创建此图的不可变快照intGet the number of vertices 获取顶点数量vertices()Get all vertices 获取所有顶点
-
Method Details
-
addVertex
Add a vertex to the graph 添加顶点到图- Parameters:
vertex- the vertex to add | 要添加的顶点
-
addEdge
-
addEdge
-
removeVertex
Remove a vertex and all its edges 移除顶点及其所有边- Parameters:
vertex- the vertex to remove | 要移除的顶点
-
removeEdge
-
vertices
-
edges
-
neighbors
-
outEdges
-
inEdges
-
vertexCount
int vertexCount()Get the number of vertices 获取顶点数量- Returns:
- vertex count | 顶点数量
-
edgeCount
int edgeCount()Get the number of edges 获取边数量- Returns:
- edge count | 边数量
-
containsVertex
Check if the graph contains a vertex 检查图是否包含顶点- Parameters:
vertex- the vertex to check | 要检查的顶点- Returns:
- true if contains | 如果包含返回true
-
containsEdge
-
getWeight
-
isDirected
boolean isDirected()Check if this is a directed graph 检查是否为有向图- Returns:
- true if directed | 如果是有向图返回true
-
outDegree
Get the out-degree of a vertex 获取顶点的出度- Parameters:
vertex- the vertex | 顶点- Returns:
- the out-degree | 出度
-
inDegree
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
-