Java数据结构与算法(有向图)

news/2024/10/18 6:02:03/

前言

有向图(Directed Graph)是一种由顶点和有方向的边组成的图数据结构。

实现原理

使用邻接表表示法实现有向图相对简单明了,步骤也相对简单。

1:首先创建有向图

2.创建顶点

3.顶点间创建边

具体代码实现

java">package test13;import java.util.*;
class Vertex {private String label;public Vertex(String label) {this.label = label;}public String getLabel() {return label;}@Overridepublic boolean equals(Object obj) {if (this == obj) return true;if (obj == null || getClass() != obj.getClass()) return false;Vertex vertex = (Vertex) obj;return label.equals(vertex.label);}@Overridepublic int hashCode() {return label.hashCode();}@Overridepublic String toString() {return label;}
}class DirectedGraph {private Map<Vertex, List<Vertex>> adjVertices;public DirectedGraph() {adjVertices = new HashMap<>();}public void addVertex(String label) {adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>());}public void removeVertex(String label) {Vertex v = new Vertex(label);adjVertices.values().forEach(e -> e.remove(v));adjVertices.remove(new Vertex(label));}public void addEdge(String label1, String label2) {Vertex v1 = new Vertex(label1);Vertex v2 = new Vertex(label2);adjVertices.get(v1).add(v2);}public void removeEdge(String label1, String label2) {Vertex v1 = new Vertex(label1);Vertex v2 = new Vertex(label2);List<Vertex> eV1 = adjVertices.get(v1);if (eV1 != null)eV1.remove(v2);}public List<Vertex> getAdjVertices(String label) {return adjVertices.get(new Vertex(label));}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();for (Vertex v : adjVertices.keySet()) {sb.append(v).append(": ");for (Vertex w : adjVertices.get(v)) {sb.append(w).append(" ");}sb.append("\n");}return sb.toString();}public static void main(String[] args) {DirectedGraph graph = new DirectedGraph();graph.addVertex("A");graph.addVertex("B");graph.addVertex("C");graph.addVertex("D");graph.addEdge("A", "B");graph.addEdge("A", "C");graph.addEdge("B", "C");graph.addEdge("C", "D");System.out.println(graph);}
}

QA:待定


http://www.ppmy.cn/news/1466247.html

相关文章

向量叉乘的方向

向量叉乘的方向 最近在百度上看到这样一个帖子&#xff1a; 可以根据这个判断是顺时针还是逆时针的 ab的方向&#xff1a;四指由a开始&#xff0c;指向b&#xff0c;拇指的指向就是ab的方向&#xff0c;垂直于a和b所在的平面&#xff1b; ba的方向&#xff1a;四指由b开始&a…

Apache Calcite - 自定义标量函数

前言 上一篇文章中我们介绍了calcite中内置函数的使用。实际需求中会遇到一些场景标准内置函数无法满足需求&#xff0c;这时候就需要用到自定义函数。在 Apache Calcite 中添加自定义函数&#xff0c;以便在 SQL 查询中使用自定义的逻辑。这对于执行特定的数据处理或分析任务…

动态库(DLL)和静态库(LIB)的区别

链接时间&#xff1a; 静态库&#xff08;LIB&#xff09;在编译链接时整合到程序中。动态库&#xff08;DLL&#xff09;在程序运行时动态加载。 内存共享&#xff1a; 静态库导致每个程序副本都包含库代码。动态库允许多个程序共享同一份代码。 更新维护&#xff1a; DLL更新…

Rejected the attempt to advance SCN问题的分析处理

一、故障描述 5月8日下午12点30分左右&#xff0c;应用厂家反馈&#xff0c;IP是130.XXXXX(jyfx)的数据库无法连接&#xff0c;检查数据库告警日志&#xff0c;提示内容如下&#xff1a; Rejected the attempt to advance SCN over limit by 124166 hours worth to 0x15cb.a9a2…

【AD21】原理图PDF文件的输出

原理图PDF文件可以共享给团队成员&#xff0c;用于设计审核、讨论和协同工作。 菜单栏中点击文件->智能PDF。 在弹出的界面点击Next&#xff0c;勾选当前项目&#xff0c;修改文件名&#xff0c;避免与制造装备图PDF文件重名将其覆盖&#xff0c;点击Next。 只输出原理图…

Layui2.5.6树形表格TreeTable使用

1、问题概述? Layui2.5.6的树形表格-TreeTable终于用明白了,步骤详细,提供源码下载。 如果你使用的是Layui2.8+版本,那么点个赞,赶紧去官网看吧,官网更行了。 更新地址:树表组件 treeTable - Layui 文档 最近在项目中需要使用到树形表格,用来显示菜单的层级关系,当…

深入解析Spring与MyBatis框架注解及其实例应用

在现代Java开发中&#xff0c;Spring与MyBatis框架已经成为了不可或缺的利器。它们提供了丰富的注解&#xff0c;用于简化开发流程、提高代码可读性和可维护性。让我们深入探讨这些注解&#xff0c;并结合实际场景进行详细分析。 1. Spring框架注解 1.1 组件注解 Component&…

Go 语言字符串及 strings 和 strconv 包

在 Go 语言编程中&#xff0c;字符串是最基本、最常用的数据类型之一。无论是处理用户输入、读取文件内容&#xff0c;还是生成输出&#xff0c;字符串操作无处不在。为了方便开发者对字符串进行各种操作&#xff0c;Go 语言提供了强大的 strings 包和 strconv 包。strings 包包…