桥和割点,以及图的遍历树

news/2024/7/15 18:04:09 标签: 算法, 深度优先, 图论, 数据结构, java

目录

什么是桥

寻找桥的算法

代码实现

什么是割点

​寻找割点的算法

         代码实现


什么是桥

寻找桥的算法

 

 

代码实现

java">import java.util.ArrayList;

public class FindBridges {

    private Graph G;
    private boolean[] visited;

    private int ord[];
    private int low[];
    private int cnt;

    private ArrayList<Edge> res;

    public FindBridges(Graph G){

        this.G = G;
        visited = new boolean[G.V()];

        res = new ArrayList<>();
        ord = new int[G.V()];
        low = new int[G.V()];
        cnt = 0;

        for(int v = 0; v < G.V(); v ++)
            if(!visited[v])
                dfs(v, v);
    }

    private void dfs(int v, int parent){

        visited[v] = true;
        ord[v] = cnt;
        low[v] = ord[v];
        cnt ++;

        for(int w: G.adj(v))
            if(!visited[w]){
                dfs(w, v);
                low[v] = Math.min(low[v], low[w]);
                if(low[w] > ord[v])
                    res.add(new Edge(v, w));
            }
            else if(w != parent)
                low[v] = Math.min(low[v], low[w]);
    }

    public ArrayList<Edge> result(){
        return res;
    }

    public static void main(String[] args){

        Graph g = new Graph("g.txt");
        FindBridges fb = new FindBridges(g);
        System.out.println("Bridges in g : " + fb.result());

        Graph g2 = new Graph("g2.txt");
        FindBridges fb2 = new FindBridges(g2);
        System.out.println("Bridges in g2 : " + fb2.result());

        Graph g3 = new Graph("g3.txt");
        FindBridges fb3 = new FindBridges(g3);
        System.out.println("Bridges in g3 : " + fb3.result());

        Graph tree = new Graph("tree.txt");
        FindBridges fb_tree = new FindBridges(tree);
        System.out.println("Bridges in tree : " + fb_tree.result());
    }
}

什么是割点



寻找割点的算法

孩子的数量是根据树来说的,而不是看根节点有多少个邻边,遍历树不同孩子数量也不同。

代码实现

java">import java.util.HashSet;

public class FindCutPoints {

    private Graph G;
    private boolean[] visited;

    private int[] ord;
    private int[] low;
    private int cnt;

    private HashSet<Integer> res;

    public FindCutPoints(Graph G){

        this.G = G;
        visited = new boolean[G.V()];

        res = new HashSet<>();
        ord = new int[G.V()];
        low = new int[G.V()];
        cnt = 0;

        for(int v = 0; v < G.V(); v ++)
            if(!visited[v])
                dfs(v, v);
    }

    private void dfs(int v, int parent){

        visited[v] = true;
        ord[v] = cnt;
        low[v] = ord[v];
        cnt ++;

        int child = 0;

        for(int w: G.adj(v))
            if(!visited[w]){
                dfs(w, v);
                low[v] = Math.min(low[v], low[w]);

                if(v != parent && low[w] >= ord[v])
                    res.add(v);

                child ++;
                if(v == parent && child > 1)
                    res.add(v);
            }
            else if(w != parent)
                low[v] = Math.min(low[v], ord[w]);
    }

    public HashSet<Integer> result(){
        return res;
    }

    public static void main(String[] args){

        Graph g = new Graph("g.txt");
        FindCutPoints fc = new FindCutPoints(g);
        System.out.println("Cut Points in g : " + fc.result());

        Graph g2 = new Graph("g2.txt");
        FindCutPoints fc2 = new FindCutPoints(g2);
        System.out.println("Cut Points in g2 : " + fc2.result());

        Graph g3 = new Graph("g3.txt");
        FindCutPoints fc3 = new FindCutPoints(g3);
        System.out.println("Cut Points in g3 : " + fc3.result());

        Graph tree = new Graph("tree.txt");
        FindCutPoints fc4 = new FindCutPoints(tree);
        System.out.println("Cut Points in tree : " + fc4.result());

    }
}


http://www.niftyadmin.cn/n/5148482.html

相关文章

【ChatOCR】OCR+LLM定制化关键信息抽取(附开源大语言模型汇总整理)

目录 背景技术方案存在的问题及解决思路关键信息提取结果其他解决方案替换文心一言LangChain大型多模态模型&#xff08;Large Multimodal Model, LMM&#xff09; 开源大模型汇总LLaMA —— Meta 大语言模型Stanford Alpaca —— 指令调优的 LLaMA 模型Lit-LLaMA —— 基于 na…

YOLOv7优化策略:IOU系列篇 | 引入MPDIoU,WIoU,SIoU,EIoU,α-IoU等创新

💡💡💡本文独家改进:MPDIoU,WIoU,SIoU,EIoU,α-IoU等二次创新,总有一种适合你的数据集 MPDIoU,WIoU,SIoU,EIoU,α-IoU | 亲测在多个数据集能够实现大幅涨点 收录: YOLOv7高阶自研专栏介绍: http://t.csdnimg.cn/tYI0c ✨✨✨前沿最新计算机顶会复现 …

Leetcode—100.相同的树【简单】明天写另一种解法!

2023每日刷题&#xff08;十八&#xff09; Leetcode—100.相同的树 递归实现代码 /*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/ bool isSameTree(struct TreeNode* p, struc…

C++类和对象(七)const成员 及其初始化列表

1.const成员 将const修饰的“成员函数”称之为const成员函数&#xff0c;const修饰类成员函数&#xff0c;实际修饰该成员函数隐含的this指针&#xff0c;表明在该成员函数中不能对类的任何成员进行修改。 成员函数定义的原则&#xff1a; 1.能定义成const的成员函数都应该定义…

PHY概述

文章目录 1 网关2. TCP/IP3. QoS (Quality of Service&#xff0c; 服务质量)4.物理层5.物理层功能6. 下行物理层常见缩写7. 星间链路和卫星链路8. 卫星和基站通信9. NTN终端参考场景10. LEO卫星&#xff0c;有较低的延迟11. RAN影响12. 多连接RAN影响&#xff1a;13. 多连接需…

Salesforce创建一个页面,能够配置各种提示语,而不需要修改代码

在Salesforce中创建一个页面&#xff0c;并使其能够配置各种提示语&#xff0c;可以使用自定义设置、自定义对象或自定义标签等方法来实现。以下是一种常见的方法&#xff1a; 自定义对象或自定义设置&#xff1a;您可以创建一个自定义对象或自定义设置来存储各种提示语的信息。…

docker 安装 mysql (单体架构)

文章归档&#xff1a;https://www.yuque.com/u27599042/coding_star/nckzqa73g47hgz3x 查询 MySQL 镜像 docker search mysql拉取 MySQL 镜像 docker pull mysql在宿主机创建映射目录 mkdir -p \ /home/docker/mysql/log \ /home/docker/mysql/data \ /home/docker/mysql/co…

shell之netstat的用法

shell之netstat的用法 所有参数应用举例 所有参数 1&#xff09;-A<网络类型>或–<网络类型> 列出该网络类型连线中的相关地址。 2&#xff09;-i 显示所有网络接口的信息。 3&#xff09;-r 显示路由表的信息。 4&#xff09;-s 显示按各个协议的统计信息。 5&am…