Class UndirectedGraph<V>

java.lang.Object
cloud.opencode.base.graph.UndirectedGraph<V>
Type Parameters:
V - the vertex type | 顶点类型
All Implemented Interfaces:
Graph<V>

public class UndirectedGraph<V> extends Object implements 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
    Constructor
    Description
    Create an empty undirected graph 创建空的无向图
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEdge(V from, V to)
    Add an unweighted edge 添加无权重边
    void
    addEdge(V from, V to, double weight)
    Add a weighted edge 添加加权边
    void
    addVertex(V vertex)
    Add a vertex to the graph 添加顶点到图
    void
    Clear the graph 清空图
    boolean
    containsEdge(V from, V to)
    Check if the graph contains an edge 检查图是否包含边
    boolean
    containsVertex(V vertex)
    Check if the graph contains a vertex 检查图是否包含顶点
    int
    Get the number of edges 获取边数量
    Get all edges 获取所有边
    double
    getWeight(V from, V to)
    Get the weight of an edge 获取边的权重
    inEdges(V vertex)
    Get incoming edges to a vertex 获取顶点的入边
    boolean
    Check if this is a directed graph 检查是否为有向图
    neighbors(V vertex)
    Get neighboring vertices 获取邻接顶点
    outEdges(V vertex)
    Get outgoing edges from a vertex 获取顶点的出边
    void
    removeEdge(V from, V to)
    Remove an edge 移除边
    void
    removeVertex(V vertex)
    Remove a vertex and all its edges 移除顶点及其所有边
     
    int
    Get the number of vertices 获取顶点数量
    Get all vertices 获取所有顶点

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Graph

    inDegree, isEmpty, outDegree, snapshot
  • Constructor Details

    • UndirectedGraph

      public UndirectedGraph()
      Create an empty undirected graph 创建空的无向图
  • Method Details

    • addVertex

      public void addVertex(V vertex)
      Description copied from interface: Graph
      Add a vertex to the graph 添加顶点到图
      Specified by:
      addVertex in interface Graph<V>
      Parameters:
      vertex - the vertex to add | 要添加的顶点
    • addEdge

      public void addEdge(V from, V to)
      Description copied from interface: Graph
      Add an unweighted edge 添加无权重边
      Specified by:
      addEdge in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
    • addEdge

      public void addEdge(V from, V to, double weight)
      Description copied from interface: Graph
      Add a weighted edge 添加加权边
      Specified by:
      addEdge in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      weight - the edge weight | 边权重
    • removeVertex

      public void removeVertex(V vertex)
      Description copied from interface: Graph
      Remove a vertex and all its edges 移除顶点及其所有边
      Specified by:
      removeVertex in interface Graph<V>
      Parameters:
      vertex - the vertex to remove | 要移除的顶点
    • removeEdge

      public void removeEdge(V from, V to)
      Description copied from interface: Graph
      Remove an edge 移除边
      Specified by:
      removeEdge in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
    • vertices

      public Set<V> vertices()
      Description copied from interface: Graph
      Get all vertices 获取所有顶点
      Specified by:
      vertices in interface Graph<V>
      Returns:
      set of vertices | 顶点集合
    • edges

      public Set<Edge<V>> edges()
      Description copied from interface: Graph
      Get all edges 获取所有边
      Specified by:
      edges in interface Graph<V>
      Returns:
      set of edges | 边集合
    • neighbors

      public Set<V> neighbors(V vertex)
      Description copied from interface: Graph
      Get neighboring vertices 获取邻接顶点
      Specified by:
      neighbors in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of neighboring vertices | 邻接顶点集合
    • outEdges

      public Set<Edge<V>> outEdges(V vertex)
      Description copied from interface: Graph
      Get outgoing edges from a vertex 获取顶点的出边
      Specified by:
      outEdges in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of outgoing edges | 出边集合
    • inEdges

      public Set<Edge<V>> inEdges(V vertex)
      Description copied from interface: Graph
      Get incoming edges to a vertex 获取顶点的入边
      Specified by:
      inEdges in interface Graph<V>
      Parameters:
      vertex - the vertex | 顶点
      Returns:
      set of incoming edges | 入边集合
    • vertexCount

      public int vertexCount()
      Description copied from interface: Graph
      Get the number of vertices 获取顶点数量
      Specified by:
      vertexCount in interface Graph<V>
      Returns:
      vertex count | 顶点数量
    • edgeCount

      public int edgeCount()
      Description copied from interface: Graph
      Get the number of edges 获取边数量
      Specified by:
      edgeCount in interface Graph<V>
      Returns:
      edge count | 边数量
    • containsVertex

      public boolean containsVertex(V vertex)
      Description copied from interface: Graph
      Check if the graph contains a vertex 检查图是否包含顶点
      Specified by:
      containsVertex in interface Graph<V>
      Parameters:
      vertex - the vertex to check | 要检查的顶点
      Returns:
      true if contains | 如果包含返回true
    • containsEdge

      public boolean containsEdge(V from, V to)
      Description copied from interface: Graph
      Check if the graph contains an edge 检查图是否包含边
      Specified by:
      containsEdge in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      true if contains | 如果包含返回true
    • getWeight

      public double getWeight(V from, V to)
      Description copied from interface: Graph
      Get the weight of an edge 获取边的权重
      Specified by:
      getWeight in interface Graph<V>
      Parameters:
      from - the source vertex | 源顶点
      to - the target vertex | 目标顶点
      Returns:
      the edge weight, or Double.MAX_VALUE if no edge exists | 边权重,如果边不存在则返回Double.MAX_VALUE
    • isDirected

      public boolean isDirected()
      Description copied from interface: Graph
      Check if this is a directed graph 检查是否为有向图
      Specified by:
      isDirected in interface Graph<V>
      Returns:
      true if directed | 如果是有向图返回true
    • clear

      public void clear()
      Description copied from interface: Graph
      Clear the graph 清空图
      Specified by:
      clear in interface Graph<V>
    • toString

      public String toString()
      Overrides:
      toString in class Object