Java 关节点算法的实现
我一直在试图理解并实现用Java在图形中查找连接点的算法,但在实现该算法时遇到了问题。有人能帮我纠正一下这个代码吗 代码尝试打印给定图形中的所有关节点Java 关节点算法的实现,java,algorithm,data-structures,graph,tree,Java,Algorithm,Data Structures,Graph,Tree,我一直在试图理解并实现用Java在图形中查找连接点的算法,但在实现该算法时遇到了问题。有人能帮我纠正一下这个代码吗 代码尝试打印给定图形中的所有关节点 import java.util.*; public class Articulation { static ArrayList<LinkedList<Integer>> list; static boolean[] visited; static int[] dfsnumber; st
import java.util.*;
public class Articulation {
static ArrayList<LinkedList<Integer>> list;
static boolean[] visited;
static int[] dfsnumber;
static int[] lowvalue;
static int dfscounter = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int vertices = s.nextInt();
int edges = s.nextInt();
list = new ArrayList<>();
visited = new boolean[vertices + 1];
dfsnumber = new int[vertices + 1];
lowvalue = new int[vertices + 1];
for (int i = 0; i <= vertices; i++) {
list.add(i, new LinkedList<Integer>());
}
for (int i = 1; i <= edges; i++) {
int start = s.nextInt();
int end = s.nextInt();
list.get(start).add(end);
list.get(end).add(start);
}
Arrays.fill(dfsnumber, -1);
for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
dfs(i);
}
}
}
public static void dfs(int vertex) {
visited[vertex] = true;
dfsnumber[vertex] = dfscounter++;
lowvalue[vertex] = dfsnumber[vertex];
for (Integer child : list.get(vertex)) {
if (!visited[child]) {
visited[child] = true;
dfs(child);
lowvalue[vertex] = Math.min(lowvalue[vertex], lowvalue[child]);
if (lowvalue[child] >= dfsnumber[vertex]) {
System.out.println(vertex);
}
} else if (!list.get(child).contains(vertex)) {
lowvalue[vertex] = Math.min(lowvalue[vertex], dfsnumber[child]);
}
}
}
}
请帮助。我已经完成了维基百科伪代码中的步骤
静态数组列表;
静态布尔值;
静态int[]数字;
静态int[]低值;
静态int[]父;
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int顶点=scanner.nextInt();
int edges=scanner.nextInt();
列表=新的ArrayList();
已访问=新布尔值[顶点+1];
dfsNumber=新整数[顶点+1];
低值=新整数[顶点+1];
父项=新整数[顶点+1];
对于(int i=0;i 1)
System.out.println(“输出:+i”);
}
我已经完成了维基百科伪代码中的步骤
静态数组列表;
静态布尔值;
静态int[]数字;
静态int[]低值;
静态int[]父;
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
int顶点=scanner.nextInt();
int edges=scanner.nextInt();
列表=新的ArrayList();
已访问=新布尔值[顶点+1];
dfsNumber=新整数[顶点+1];
低值=新整数[顶点+1];
父项=新整数[顶点+1];
对于(int i=0;i 1)
System.out.println(“输出:+i”);
}
包com.Algorithms.Graphs;
导入java.util.*;
公共类MattsGraphBook{
静态数组列表;
静态布尔值;
静态int[]父;
静态布尔[]ap;
静态int[]数字;
静态int[]低值;
静态计数器=0;
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
int顶点=s.nextInt();
int edges=s.nextInt();
列表=新的ArrayList();
已访问=新布尔值[顶点+1];
父项=新整数[顶点+1];
ap=新布尔值[顶点+1];
dfsnumber=新整数[顶点+1];
低值=新整数[顶点+1];
对于(int i=0;i 1){
ap[顶点]=真;
}
}else if(子对象!=父对象[顶点]){
lowvalue[vertex]=Math.min(lowvalue[vertex],dfsnumber[child]);
}
}
}
}
最后,我找到了答案。package com.Algorithms.Graphs;
导入java.util.*;
公共类MattsGraphBook{
静态数组列表;
静态布尔值;
静态int[]父;
静态布尔[]ap;
静态int[]数字;
静态int[]低值;
静态计数器=0;
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
int顶点=s.nextInt();
int edges=s.nextInt();
列表=新的ArrayList();
已访问=新布尔值[顶点+1];
父项=新整数[顶点+1];
ap=新布尔值[顶点+1];
dfsnumber=新整数[顶点+1];
低值=新整数[顶点+1];
对于(int i=0;i 1){
ap[顶点]=真;
}
}else if(子对象!=父对象[顶点]){
lowvalue[vertex]=Math.min(lowvalue[vertex],dfsnumber[child]);
}
}
}
}
好的,最后,我算出来了。你能把样本输入和预期输出放在一起吗?让我检查一下好的,我知道我犯了一些小错误,但我无法解决。你能把样本输入和预期输出放在一起吗?让我检查一下好的,我知道我犯了一些小错误,但我无法解决。你可能想重新检查一下,它没有给出正确的输出。@DeepeshPandey你能放一些测试用例并正确输出吗?再添加一些测试用例。@DeepeshPandey,我已经更新了它,它按预期工作,输出应该在for循环之外:)@DeepeshPandey,如果您愿意,这是另一个答案,您可以投票表决解决方案:)您可能需要重新检查,它没有给出正确的输出。@DeepeshPandey您可以放置一些测试用例并正确输出吗?再添加一些测试用例。@DeepeshPandey,我已经更新了它,它按预期工作,输出应该在for循环之外:)@DeepeshPandey,如果你愿意,这是另一个答案,你能投票表决解决方案吗:)
Sample Input :
4 // number of vertices
3 // number of edges and next few lines follow the (v, u) edge pair.
3 0
0 1
1 2
Sample Output (expected) :
0 1 // articulation points
Sample Input :
7
8
0 1
1 2
2 0
1 3
1 4
1 6
3 5
4 5
Sample Output (expected):
1
Sample Input :
5
5
1 0
0 2
2 1
0 3
3 4
Sample Output (expected) :
0 3
static ArrayList<LinkedList<Integer>> list;
static boolean[] visited;
static int[] dfsNumber;
static int[] lowValue;
static int[] parent;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int vertices = scanner.nextInt();
int edges = scanner.nextInt();
list = new ArrayList<>();
visited = new boolean[vertices + 1];
dfsNumber = new int[vertices + 1];
lowValue = new int[vertices + 1];
parent = new int[vertices + 1];
for (int i = 0; i <= vertices; i++) {
list.add(i, new LinkedList<Integer>());
}
for (int i = 1; i <= edges; i++) {
int v = scanner.nextInt();
int w = scanner.nextInt();
list.get(v).add(w);
list.get(w).add(v);
}
Arrays.fill(dfsNumber, -1);
Arrays.fill(parent, -1);
for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
dfs(i, i);
}
}
scanner.close();
}
public static void dfs(int i, int vertex) {
visited[vertex] = true;
dfsNumber[i] = vertex;
lowValue[i] = vertex;
boolean isArticulation = false;
int childCount = 0;
for (Integer w : list.get(vertex)) {
if (!visited[w]) {
parent[w] = i;
visited[w] = true;
dfs(w, w);
childCount = childCount + 1;
lowValue[vertex] = Math.min(lowValue[vertex], lowValue[w]);
if (lowValue[w] >= dfsNumber[vertex]) {
isArticulation = true;
}
} else if (w != parent[i]) {
lowValue[vertex] = Math.min(lowValue[i], dfsNumber[w]);
}
}
if (parent[i] != -1 && isArticulation || parent[i] == -1 && childCount > 1)
System.out.println("The output: " + i);
}
package com.Algorithms.Graphs;
import java.util.*;
public class MattsGraphBook {
static ArrayList<LinkedList<Integer>> list;
static boolean[] visited;
static int[] parent;
static boolean[] ap;
static int[] dfsnumber;
static int[] lowvalue;
static int dfscounter = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int vertices = s.nextInt();
int edges = s.nextInt();
list = new ArrayList<>();
visited = new boolean[vertices + 1];
parent = new int[vertices + 1];
ap = new boolean[vertices + 1];
dfsnumber = new int[vertices + 1];
lowvalue = new int[vertices + 1];
for (int i = 0; i <= vertices; i++) {
list.add(i, new LinkedList<Integer>());
}
for (int i = 1; i <= edges; i++) {
int start = s.nextInt();
int end = s.nextInt();
list.get(start).add(end);
list.get(end).add(start);
}
Arrays.fill(dfsnumber, -1);
Arrays.fill(lowvalue, -1);
Arrays.fill(parent, -1);
Arrays.fill(ap, false);
Arrays.fill(visited, false);
for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
dfs(i);
}
}
for (int i = 0; i < ap.length; i++) {
if (ap[i]){
System.out.print(i + " ");
}
}
}
public static void dfs(int vertex) {
visited[vertex] = true;
dfsnumber[vertex] = ++dfscounter;
lowvalue[vertex] = dfsnumber[vertex];
int c = 0;
for (int i = 0; i < list.get(vertex).size(); i++) {
int child = list.get(vertex).get(i);
if (!visited[child]) {
c++;
visited[child] = true;
parent[child] = vertex;
dfs(child);
lowvalue[vertex] = Math.min(lowvalue[vertex], lowvalue[child]);
if ((lowvalue[child] >= dfsnumber[vertex]) && parent[vertex] != -1) {
ap[vertex] = true;
}
if ((parent[vertex] == -1) && c > 1) {
ap[vertex] = true;
}
} else if (child != parent[vertex]) {
lowvalue[vertex] = Math.min(lowvalue[vertex], dfsnumber[child]);
}
}
}
}