ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 图的Java数据结构 图的2要素:顶点集合和边集合 ~~~ public class Graph { // 顶点集 - 邻接表 public HashMap<String,Vertex> vertexs; // 边集 public Set<Edge> edges; public Graph() { this.vertexs = new HashMap<>(); this.edges = new HashSet<>(); } } ~~~ ~~~ // 顶点 class Vertex{ // 名称,值 public String value; // 入度 public Integer in; // 出度 public Integer out; // 相邻的顶点集合 public Set<Vertex> nexts; // 边集合 public Set<Edge> edges; public Vertex(String value) { this.value = value; this.in = 0; this.out = 0; this.nexts = new HashSet<>(); this.edges = new HashSet<>(); } } ~~~ ~~~ // 边 class Edge{ // 权重 public int weight; // 出发顶点 public Vertex from; // 到达顶点 public Vertex to; public Edge(int weight, Vertex from, Vertex to) { this.weight = weight; this.from = from; this.to = to; } } ~~~