Class UndirectedGraph<V>
java.lang.Object
cloud.opencode.base.graph.UndirectedGraph<V>
- Type Parameters:
V- the vertex type | 顶点类型
- All Implemented Interfaces:
Graph<V>
Undirected Graph
无向图
Implementation of an undirected graph using adjacency list.
使用邻接表实现的无向图。
Features | 特性:
- Thread-safe (uses ConcurrentHashMap) | 线程安全(使用ConcurrentHashMap)
- Supports weighted edges | 支持加权边
- Bidirectional edge storage | 双向边存储
Usage Examples | 使用示例:
Graph<String> graph = new UndirectedGraph<>();
graph.addEdge("A", "B");
// Both A->B and B->A are created
Set<String> aNeighbors = graph.neighbors("A"); // ["B"]
Set<String> bNeighbors = graph.neighbors("B"); // ["A"]
Security | 安全性:
- Thread-safe: Yes (uses ConcurrentHashMap) - 线程安全: 是(使用ConcurrentHashMap)
- Null-safe: Yes (rejects null vertices) - 空值安全: 是(拒绝null顶点)
- Since:
- JDK 25, opencode-base-graph V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
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 获取边的权重Get incoming edges to a vertex 获取顶点的入边booleanCheck if this is a directed graph 检查是否为有向图Get neighboring vertices 获取邻接顶点Get outgoing edges from a vertex 获取顶点的出边voidremoveEdge(V from, V to) Remove an edge 移除边voidremoveVertex(V vertex) Remove a vertex and all its edges 移除顶点及其所有边toString()intGet the number of vertices 获取顶点数量vertices()Get all vertices 获取所有顶点
-
Constructor Details
-
UndirectedGraph
public UndirectedGraph()Create an empty undirected graph 创建空的无向图
-
-
Method Details
-
addVertex
-
addEdge
-
addEdge
-
removeVertex
Description copied from interface:GraphRemove a vertex and all its edges 移除顶点及其所有边- Specified by:
removeVertexin interfaceGraph<V>- Parameters:
vertex- the vertex to remove | 要移除的顶点
-
removeEdge
-
vertices
-
edges
-
neighbors
-
outEdges
-
inEdges
-
vertexCount
public int vertexCount()Description copied from interface:GraphGet the number of vertices 获取顶点数量- Specified by:
vertexCountin interfaceGraph<V>- Returns:
- vertex count | 顶点数量
-
edgeCount
-
containsVertex
Description copied from interface:GraphCheck if the graph contains a vertex 检查图是否包含顶点- Specified by:
containsVertexin interfaceGraph<V>- Parameters:
vertex- the vertex to check | 要检查的顶点- Returns:
- true if contains | 如果包含返回true
-
containsEdge
-
getWeight
Description copied from interface:GraphGet the weight of an edge 获取边的权重 -
isDirected
public boolean isDirected()Description copied from interface:GraphCheck if this is a directed graph 检查是否为有向图- Specified by:
isDirectedin interfaceGraph<V>- Returns:
- true if directed | 如果是有向图返回true
-
clear
-
toString
-