本文介绍了样本定向图和拓扑排序代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都知道在哪里可以获得Directed Graph的示例实现和用于在有向图上执行拓扑排序的示例代码? (最好在Java中)解决方案
这是从:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class Graph {
static class Node {
public final String name;
public final HashSet< Edge> inEdges;
public final HashSet< Edge> outEdges;
public Node(String name){
this.name = name;
inEdges = new HashSet< Edge>();
outEdges = new HashSet< Edge>();
}
public Node addEdge(Node node){
Edge e = new Edge(this,node);
outEdges.add(e);
node.inEdges.add(e);
返回这个;
}
@Override
public String toString(){
return name;
}
}
static class Edge {
public final Node from;
public final Node to;
public Edge(Node,Node){
this.from = from;
this.to = to;
}
@Override
public boolean equals(Object obj){
Edge e =(Edge)obj;
return e.from == from&&& e.to == to;
}
}
public static void main(String [] args){
Node seven = new Node(7);
Node 5 = new Node(5);
节点3 =新节点(3);
节点11 =新节点(11);
Node 8 =新节点(8);
节点2 =新节点(2);
节点9 =新节点(9);
Node ten = new Node(10);
seven.addEdge(十一).addEdge(八);
five.addEdge(十一);
three.addEdge(八).addEdge(十);
eleven.addEdge(two).addEdge(nine).addEdge(ten);
eight.addEdge(九).addEdge(十);
Node [] allNodes = {七,五,三,十一,八,二,九,十};
// L< - 将包含排序元素的空列表
ArrayList&Node L = new ArrayList< Node>();
// S< - 没有进入边的所有节点的集合
HashSet< Node> S = new HashSet< Node>();
(节点n:allNodes){
if(n.inEdges.size()== 0){
S.add(n);
}
}
//而S不为空do
while(!S.isEmpty()){
//删除一个节点n从S
Node n = S.iterator()。next();
S.remove(n);
//将n插入L
L.add(n);对于(Iterator< Edge> it = n.outEdges.iterator(); it.hasNext();)的每个节点m,具有从n到m的边e e
// {
//从图中删除边e e
Edge e = it.next();
Node m = e.to;
it.remove(); //从n
中删除边缘m.inEdges.remove(e); //从边框中删除边框
//如果m没有其他进入边缘然后插入到S
如果(m.inEdges.isEmpty()){
S.add(m);
}
}
}
//检查所有边缘是否被删除
boolean cycle = false;
(节点n:allNodes){
if(!n.inEdges.isEmpty()){
cycle = true;
break;
}
}
if(cycle){
System.out.println(循环呈现,拓扑排序不可能);
} else {
System.out.println(拓扑排序:+ Arrays.toString(L.toArray()));
}
}
}
Anyone know where I can obtain a sample implementation of a Directed Graph and sample code for performing a topological sort on a directed graph? (preferably in Java)
解决方案
Here is a simple implementation of the first algorithm from the Wikipedia page on Topological Sort:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class Graph {
static class Node{
public final String name;
public final HashSet<Edge> inEdges;
public final HashSet<Edge> outEdges;
public Node(String name) {
this.name = name;
inEdges = new HashSet<Edge>();
outEdges = new HashSet<Edge>();
}
public Node addEdge(Node node){
Edge e = new Edge(this, node);
outEdges.add(e);
node.inEdges.add(e);
return this;
}
@Override
public String toString() {
return name;
}
}
static class Edge{
public final Node from;
public final Node to;
public Edge(Node from, Node to) {
this.from = from;
this.to = to;
}
@Override
public boolean equals(Object obj) {
Edge e = (Edge)obj;
return e.from == from && e.to == to;
}
}
public static void main(String[] args) {
Node seven = new Node("7");
Node five = new Node("5");
Node three = new Node("3");
Node eleven = new Node("11");
Node eight = new Node("8");
Node two = new Node("2");
Node nine = new Node("9");
Node ten = new Node("10");
seven.addEdge(eleven).addEdge(eight);
five.addEdge(eleven);
three.addEdge(eight).addEdge(ten);
eleven.addEdge(two).addEdge(nine).addEdge(ten);
eight.addEdge(nine).addEdge(ten);
Node[] allNodes = {seven, five, three, eleven, eight, two, nine, ten};
//L <- Empty list that will contain the sorted elements
ArrayList<Node> L = new ArrayList<Node>();
//S <- Set of all nodes with no incoming edges
HashSet<Node> S = new HashSet<Node>();
for(Node n : allNodes){
if(n.inEdges.size() == 0){
S.add(n);
}
}
//while S is non-empty do
while(!S.isEmpty()){
//remove a node n from S
Node n = S.iterator().next();
S.remove(n);
//insert n into L
L.add(n);
//for each node m with an edge e from n to m do
for(Iterator<Edge> it = n.outEdges.iterator();it.hasNext();){
//remove edge e from the graph
Edge e = it.next();
Node m = e.to;
it.remove();//Remove edge from n
m.inEdges.remove(e);//Remove edge from m
//if m has no other incoming edges then insert m into S
if(m.inEdges.isEmpty()){
S.add(m);
}
}
}
//Check to see if all edges are removed
boolean cycle = false;
for(Node n : allNodes){
if(!n.inEdges.isEmpty()){
cycle = true;
break;
}
}
if(cycle){
System.out.println("Cycle present, topological sort not possible");
}else{
System.out.println("Topological Sort: "+Arrays.toString(L.toArray()));
}
}
}
这篇关于样本定向图和拓扑排序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!