【算法题】有环无向图,寻找导致形成环的最后一条边

import java.util.*;

class Graph {
    // 图的邻接表表示
    Map<Integer, List<Integer>> keyToAdjacencyListMap;

    // 用于记录节点的访问状态
    Map<Integer,Boolean> visited;

    // 用于记录路径
    Stack<Integer> path;

    // 用于记录环的起点
    Map<Integer,Integer> parent;

    // 构造函数
    Graph(Map<Integer, List<Integer>> keyToAdjacencyListMap) {
        this.keyToAdjacencyListMap = keyToAdjacencyListMap;
        visited = new HashMap<>();
        path = new Stack<>();
        parent = new HashMap<>();
    }

    // 深度优先搜索算法查找环
    boolean findCycleDFS(int node, int parentNode) {
        if (visited.containsKey(node) && visited.get(node)) {
            if (parent.containsKey(node)) {
                path.add(node);
                return true;
            }
            return false;
        }
        visited.put(node,true);
        parent.put(node, parentNode);
        path.push(node);
        boolean hasCycle = false;
        for (int neighbor : keyToAdjacencyListMap.getOrDefault(node, Collections.emptyList())) {
            if (neighbor != parentNode && findCycleDFS(neighbor, node)) {
                hasCycle = true;
                break;
            }
        }
        if (!hasCycle) {
            path.pop();
            visited.put(node, false);
        }
        return hasCycle;
    }

    // 调用函数,找出环
    boolean findCycle() {
        for (int node : keyToAdjacencyListMap.keySet()) {
            if (visited.containsKey(node) && visited.get(node)) {
                continue;
            }
            if (findCycleDFS(node, -1)) {
                return true;
            }
        }
        return false;
    }
}

// 使用示例
public class Main {
    public static void main(String[] args) {
        // 构建无向图的邻接表
        Integer[][] edges = {{1,2},{1,3},{2,4},{4,6},{5,6},{4,5}};
        Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
        for(Integer[] edge:edges){
            Integer left = edge[0];
            Integer right = edge[1];
            if(adjacencyList.containsKey(left)){
                List list = adjacencyList.get(left);
                if(!list.contains(right))
                    adjacencyList.get(left).add(right);
            }else{
                List<Integer> list = new ArrayList<>();
                list.add(right);
                adjacencyList.put(left, list);
            }

            if(adjacencyList.containsKey(right)){
                List list = adjacencyList.get(right);
                if(!list.contains(left))
                    adjacencyList.get(right).add(left);
            }else{
                List<Integer> list = new ArrayList<>();
                list.add(left);
                adjacencyList.put(right, list);
            }
        }

        Graph graph = new Graph(adjacencyList);
        boolean hasCycle = graph.findCycle();
        if (hasCycle) {
            int left = graph.path.pop();
            int right = graph.path.pop();
            System.out.println("this edge cause cycle: "+ left + "->" + right);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deamon Tree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值