我在网上看到有很多c➕➕实现的wagner算法实现求无向有权图的全局最小割,但是备注都看不太懂且c加加语法我也不太懂,哪位小伙伴能提供java版的store wagner算法求无向有权图的全局最小割吗,最好能有注释,谢谢小伙伴!
有题目吗?给个题目的链接
import java.util.*;
/**寻找割点*/
public class FindArt {
static class Node
{
Node(String name)
{
this.name=name;
Childen=new ArrayList<Node>();
}
boolean visited;
int num;
int low;
String name;
Node parent;
List<Node> Childen;
public boolean equals(Node node)
{
if(this.name==node.name) return true;
return false;
}
}
public static void main(String args[])
{
Node A=new Node("A");
Node B=new Node("B");
Node C=new Node("C");
Node D=new Node("D");
Node E=new Node("E");
Node F=new Node("F");
Node G=new Node("G");
A.Childen.add(B);
A.Childen.add(D);
B.Childen.add(A);
B.Childen.add(C);
C.Childen.add(B);
C.Childen.add(D);
C.Childen.add(G);
D.Childen.add(A);
D.Childen.add(C);
D.Childen.add(E);
D.Childen.add(F);
E.Childen.add(D);
E.Childen.add(F);
F.Childen.add(D);
F.Childen.add(E);
G.Childen.add(C);
find(A);
count=1;
print(A);
}
private static int count=1;
private static List<Node> points=new ArrayList<Node>();
private static List<Node> mynode=new ArrayList<Node>();
public static void find(Node node)
{
List<Node> childs=node.Childen;
node.num=count++;
node.low=node.num;
node.visited=true;
for(Node n:childs)
{
if(!n.visited)
{
n.parent=node;
//前向遍历,给每个节点编号
find(n);
//判断是否是割点
if(n.low>=node.num&&node.num!=1)
{
points.add(node);
System.out.println(node.name+"是割点");
}
//后向遍历,计算low
node.low=Math.min(node.low,n.low);
}
else
{
//背向边中num
if(node.parent!=null&&!node.parent.equals(n))
node.low=Math.min(node.low,n.num);
}
}
}
public static void print(Node node)
{
mynode.add(node);
List<Node> childs=node.Childen;
System.out.println("name"+node.name+" num:"+node.num+" low:"+node.low);
for(Node n:childs)
{
if(!mynode.contains(n))
{
print(n);
}
}
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!