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

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

图:
在这里插入图片描述

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

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.kler.cn/a/227511.html

相关文章:

  • 【Linux网络编程三】Udp套接字编程网络应用场景
  • 【Django-ninja】使用Django ninja 进行auth鉴权
  • 清华系2B模型杀出,性能吊打LLaMA-13B
  • 如何在linux下使用openssl自签https的ip证书配置nginx
  • 笔记:《NCT全国青少年编程能力等级测试教程Python语言编程二级》
  • 单臂路由实验(华为)
  • 单片机基础知识
  • 20.HarmonyOS App(JAVA)表格布局Layout使用方法
  • 【大数据面试题】001 Flink 的 Checkpoint 原理
  • KubeMQ简介
  • 机器学习算法之分类和回归树(CART)
  • 腾讯云幻兽帕鲁Palworld服务器价格表,2024年2月最新
  • 100183. 最大好子数组和
  • vue实现瀑布流
  • Linux下的线程操作
  • Java 集合List相关面试题
  • java hutool工具类实现将数据下载到excel
  • 蓝桥杯备战——13.PCF8591芯片的使用
  • Windows11通过Hyper-V创建VM,然后通过vscode连接vm进行开发
  • QT Creator 的代码重构工具