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

2023-11-03 23:52
文章标签 遍历 割点

本文主要是介绍桥和割点,以及图的遍历树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

什么是桥

寻找桥的算法

代码实现

什么是割点

​寻找割点的算法

         代码实现


什么是桥

寻找桥的算法

 

 

代码实现

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

什么是割点



寻找割点的算法

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

代码实现

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.chinasem.cn/article/341762

相关文章

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

图的割点、割线问题

图的割点问题 邻接矩阵表示图 package com.hyl.algorithm.other;import java.util.Arrays;import com.hyl.algorithm.search.base.SearchIntf;import com.hyl.algorithm.search.base.SearchIntfFactory;/*** 寻找图的割点* <p>** @aut

PHP实现二叉树遍历(非递归方式,栈模拟实现)

二叉树定义是这样的:一棵非空的二叉树由根结点及左、右子树这三个基本部分组成,根据节点的访问位置不同有三种遍历方式: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderT

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

hashmap的存值,各种遍历方法

package com.jefflee;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HashmapTest {// 遍历Hashmap的四种方法public static void main(String[] args) {//hashmap可以存一个null,把

Knight Moves -uva 简单的BFS遍历

昨天刚学了BFS的遍历,在uva上找了个题敲了出来,感觉还不错,最近敲代码挺有手感的,希望这种状态保持下去 #include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 10 + 5#define LEN 100 + 10using namespace std;in

笔试强训,[NOIP2002普及组]过河卒牛客.游游的水果大礼包牛客.买卖股票的最好时机(二)二叉树非递归前序遍历

目录 [NOIP2002普及组]过河卒 牛客.游游的水果大礼包 牛客.买卖股票的最好时机(二) 二叉树非递归前序遍历 [NOIP2002普及组]过河卒 题里面给的提示很有用,那个马的关系,后面就注意,dp需要作为long的类型。 import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息publ

二叉树的层次遍历(10道)

(写给未来遗忘的自己) 102.二叉数的层序遍历(从上到下) 题目: 代码: class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> node; if (root == nullptr) {

关于双指针遍历

今晚跟一个朋友突然问我,你懂双指针遍历吗?并叫我敲出代码。当时自己愣住了,但是还是写出来了,第一个版本是: #include <iostream> using namespace std; int main(int argc, char** argv, char** arge) { cout<<"输出参数个数:"<<argc<<endl; cout<<"输出字符串数组:"<<endl; f

C语言手撕实战代码_二叉树_构造二叉树_层序遍历二叉树_二叉树深度的超详细代码实现

二叉树习题1.通过前序序列和中序序列构造二叉树2.通过层次遍历序列和中序序列创建一棵二叉树3.求一棵二叉树的结点个数4.求一棵二叉树的叶子节点数5.求一棵二叉树中度为1的节点个数6.求二叉树的高度7.求一棵二叉树中值为x的节点作为根节点的子树深度8.判断两棵树是否相似,相似返回1,不相似返回09.设计算法利用叶子结点中的空指针域将所有叶子结点链接成一个带头结的双链表10.假设一个仅包含二元运算的算