当前位置: 首页 > 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);}}
}

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

相关文章:

  • oracle 结果集操作符(求交集、并集、差集)
  • 手机如何在线生成gif?一个妙招在线制作
  • 蓝桥杯算法赛第4场小白入门赛强者挑战赛
  • JavaScript-数组新增-字符串遍历解析
  • 第3讲 谈谈final、finally、 finalize有什么不同?
  • Python+Appium实现自动化测试
  • 计算机网络——网络层(3)
  • 贝锐蒲公英全新网页认证,保障企业访客无线网络安全
  • 2024 高级前端面试题之 移动端多端开发 「精选篇」
  • transformer_正余弦位置编码代码笔记
  • 服务器为什么老是被攻击?被攻击了怎么办?
  • 十一、常用API——练习
  • 《HTML 简易速速上手小册》第8章:HTML 表单高级技术(2024 最新版)
  • 【云上建站】快速在云上构建个人网站3——网站选型和搭建
  • 用ASM HEMT模型提取GaN器件的参数
  • [ JavaScript ] 数据结构与算法 —— 链表
  • “大数据应用场景”之隔壁老王(连载四)
  • Android开源项目规范总结
  • Java 网络编程(2):UDP 的使用
  • Java-详解HashMap
  • js对象的深浅拷贝
  • KMP算法及优化
  • leetcode386. Lexicographical Numbers
  • php中curl和soap方式请求服务超时问题
  • vue-loader 源码解析系列之 selector
  • Vue小说阅读器(仿追书神器)
  • 机器学习 vs. 深度学习
  • 容器服务kubernetes弹性伸缩高级用法
  • 数据科学 第 3 章 11 字符串处理
  • 第二十章:异步和文件I/O.(二十三)
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • (39)STM32——FLASH闪存
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (三)Honghu Cloud云架构一定时调度平台
  • (十八)SpringBoot之发送QQ邮件
  • (转)http-server应用
  • **python多态
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .net framework4与其client profile版本的区别
  • .NetCore 如何动态路由
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .NET企业级应用架构设计系列之技术选型
  • @private @protected @public
  • @Transient注解
  • [<死锁专题>]
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [C++] cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
  • [C++]命名空间等——喵喵要吃C嘎嘎
  • [cb]UIGrid+UIStretch的自适应
  • [ComfyUI进阶教程] animatediff视频提示词书写要点
  • [C语言]编译和链接