当前位置: 首页 > news >正文

有向图查询所有环,非递归

图:
在这里插入图片描述

有向图查询所有环,非递归:

import java.util.*;public class CycleTest {private final int V; // 顶点数private final List<List<Integer>> adjList; // 邻接表public CycleTest(int vertices) {this.V = vertices;this.adjList = new ArrayList<>(vertices);for (int i = 0; i < vertices; i++) {adjList.add(new LinkedList<>());}}// 添加有向边public void addEdge(int src, int dest) {adjList.get(src).add(dest);}// 查找所有环public List<List<Integer>> findAllCycles() {List<List<Integer>> cycles = new ArrayList<>();Stack<Integer> stack = new Stack<>();Stack<Integer> pathStack = new Stack<>();Stack<Integer> neighborPoint = new Stack<>();Stack<Integer> levelStack = new Stack<>();boolean[] visited = new boolean[V];int level = 1;for (int startVertex = 0; startVertex < V; startVertex++) {if (visited[startVertex]) {continue;}stack.push(startVertex);pathStack.push(startVertex);while (!stack.isEmpty() || !neighborPoint.isEmpty()) {if (stack.isEmpty()) {int l = levelStack.pop();// 返回上一个邻接点搜索Integer p = neighborPoint.pop();stack.push(p);while (pathStack.size() >= l) {pathStack.pop();}pathStack.push(p);level--;}int vertex = stack.pop();List<Integer> neighbors = adjList.get(vertex);for (int i = 0; i < neighbors.size(); i++) {Integer neighbor = neighbors.get(i);if (i == 0) {if (!pathStack.contains(neighbor)) {stack.push(neighbor);pathStack.push(neighbor);} else {// 找到环List<Integer> cycle = new ArrayList<>();List<Integer> path = pathStack.stream().toList();for(int j = path.size() - 1; j >= 0; j--) {Integer p = path.get(j);cycle.add(p);visited[p] = true;if (Objects.equals(p, neighbor)) {break;}}Collections.reverse(cycle);cycles.add(cycle);}level++;} else {// 存储邻接点neighborPoint.push(neighbor);levelStack.push(level);}}}// 清除路径栈状态pathStack.clear();levelStack.clear();level = 1;}return cycles;}public static void main(String[] args) {CycleTest graph = new CycleTest(4);graph.addEdge(0, 1);graph.addEdge(1, 2);graph.addEdge(2, 0);graph.addEdge(1, 3);graph.addEdge(3, 2);List<List<Integer>> cycles = graph.findAllCycles();System.out.println("Cycles in the directed graph:");for (List<Integer> cycle : cycles) {System.out.println(cycle);}}
}

结果:
在这里插入图片描述

http://www.lryc.cn/news/291867.html

相关文章:

  • SpringBoot 使用WebSocket功能
  • HTML5的新特性
  • Filter过滤器学习使用
  • 关于修改数据库服务器时间导致达梦数据库集群裂开
  • 自定义包的设计与实现
  • 时机成熟了
  • Linux 驱动开发基础知识——总线设备驱动模型(八)
  • SpringBoot+BCrypt算法加密
  • 更新至2023年,2002-2023年3月中国国债发行数据
  • 2024最新版TypeScript安装使用指南
  • 国外知名的农业机器人公司
  • 【EI会议征稿中|ACM出版】#先投稿,先送审#第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)​
  • 【笔试常见易错选择题01】else、表达式、二维数组、%m.ns、%m.nf、常量指针和指针常量、宏定义、传参、数组越界、位段
  • Unity中常见的单词
  • 【仅需一步,1分钟极速开服】幻兽帕鲁保姆级教程
  • Zoho Mail 2023:回顾过去,展望未来:不断进化的企业级邮箱解决方案
  • python执行linux系统命令的三种方式
  • 协会认证!百望云荣获信创工委会年度“卓越贡献成员单位”称号
  • 神经网络激活函数到底是什么?
  • 【智慧工业】东胜物联定位与跟踪解决方案,为方案商提供蓝牙网关、信标等物联网智能硬件设备
  • C#中使用OpenCvSharp4库读取本地图像并显示
  • Stable Diffusion系列(四):提示词规则与使用
  • vue3动态循环引入本地静态图片资源
  • k8s从私有库harbor中拉取镜像
  • HCIA-Datacom实验指导手册:4.2 实验二:AAA配置实验
  • 黑马程序员前端web入门:新浪新闻
  • 力扣_字符串2—最长有效括号
  • 小程序接入企业微信「联系我」功能
  • jdk17新特性—— 密封类(Sealed Classes)
  • 【亿级数据专题】「分布式消息引擎」 盘点本年度我们探索服务的HA高可用解决方案