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

图:
在这里插入图片描述

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

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);
        }
    }
}

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

相关推荐

  1. 2192. 中一个节点的所有祖先

    2024-02-01 12:38:05       8 阅读
  2. 通过文章id查询所有评论(xml)

    2024-02-01 12:38:05       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 12:38:05       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 12:38:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 12:38:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 12:38:05       18 阅读

热门阅读

  1. Spring Boot(番外):防止反编译之Jar包加密

    2024-02-01 12:38:05       42 阅读
  2. 什么是http状态码?

    2024-02-01 12:38:05       32 阅读
  3. 垃圾回收机制

    2024-02-01 12:38:05       33 阅读
  4. PTA-C语言-找完数(附解析)

    2024-02-01 12:38:05       29 阅读
  5. 每日OJ题_算法_前缀和⑧_力扣1314. 矩阵区域和

    2024-02-01 12:38:05       35 阅读
  6. 原生js创建节点

    2024-02-01 12:38:05       34 阅读
  7. python 中我对类与函数的理解

    2024-02-01 12:38:05       29 阅读
  8. ios app与H5页面交互踩坑

    2024-02-01 12:38:05       25 阅读
  9. 最佳解决Css一隐藏滚动条

    2024-02-01 12:38:05       31 阅读
  10. nginx升级openssl3.1.3

    2024-02-01 12:38:05       29 阅读
  11. 使用PyMysql模块连接mysql

    2024-02-01 12:38:05       30 阅读