# java 图论二 有向图 拓扑排序

1.查看连通矩阵是否还有剩余节点，如果有继续2,3操作，如果没有结束拓扑排序

2.找到没有后继的节点

3.如果找到了，从联通矩阵中删除；如果没找到，则此联通矩阵不是DAG，不能进行拓扑排序

```package com.Construction;

import java.util.Queue;
import java.util.Stack;

public class GraphSimpleExample {
private final int MAX_VERTS = 20;
private GraphNodeBean nodeList[];
private int nVerts;

public GraphSimpleExample(){
nodeList = new GraphNodeBean[MAX_VERTS];
nVerts = 0;
for (int i = 0; i < MAX_VERTS; i++) {
for (int j = 0; j < MAX_VERTS; j++) {
}
}
}

nodeList[nVerts++] = new GraphNodeBean(label);
}

}

public void displayGraphNode(int v){
System.out.println(nodeList[v].label);
}

/**
* 获得未访问节点
* @param v
* @return
*/
for (int i = 0; i < nVerts; i++) {
return i;
}
return -1;
}

/**
* deft first
*/
public void dfs(){
@SuppressWarnings("rawtypes")
Stack<Integer> theStack = new Stack<Integer>();
nodeList[0].isVisited = true;
displayGraphNode(0);
theStack.push(0);

while(!theStack.isEmpty()){
if(v == -1)
theStack.pop();
else{
nodeList[v].isVisited = true;
displayGraphNode(v);
theStack.push(v);
}
}
//		for (int i = 0; i < nVerts; i++) {
//			nodeList[i].isVisited = false;
//		}
}

public void bds(){

nodeList[0].isVisited = true;
displayGraphNode(0);
theQueue.offer(0);
int v2;

while(!theQueue.isEmpty()){
int v1 = theQueue.poll();
nodeList[v2].isVisited = true;
displayGraphNode(v2);
}
}
//		for (int i = 0; i < nVerts; i++) {
//		nodeList[i].isVisited = false;
//	}

}

/**
* topologically output all the node 简单的拓扑排序
* 找到无后续节点，并输出，直到没有节点位置。
* 可以输出所有有顺序的关系，但是正确的输出不是唯一滴
* note: the graph only be DAG - including 不连通树, can't has cycles 环合树
*/
public void topo(){
int orig_nVerts = nVerts;
GraphNodeBean[] sortedArray = new GraphNodeBean[nVerts];

while(nVerts > 0){
int currentVertex = noSuccessor();
if(currentVertex == -1){
System.out.println("Error : Graph has cycles");
return;
}
sortedArray[nVerts - 1] = nodeList[currentVertex];
deleteVertex(currentVertex);
}
System.out.println("TOPOlogically sorted order:");
for (int i = 0; i < orig_nVerts; i++) {
System.out.println(sortedArray[i].label);
}
}

/**
* within topology graph add a edge
* @param start
* @param end
*/
}

/**
* 找到没有后继的节点
* @return
*/
public int noSuccessor(){
boolean isEdge;
for (int i = 0; i < nVerts; i++) {
isEdge = false;
for (int j = 0; j < nVerts; j++) {
isEdge = true;
break;
}
}
if(!isEdge)
return i;
}
return -1;
}

/**
* delete certain node on the graph
* @param delVert
*/
public void deleteVertex(int delVert){
if(delVert != nVerts - 1){
movingVertexData(delVert);
for (int i = delVert; i < nVerts; i++)
this.moveRowUp(i, nVerts);
for (int i = delVert; i < nVerts; i++)
this.moveColLeft(i, nVerts-1);
}
nVerts --;
}

/**
* delete data from graph
*/
public void movingVertexData(int index){
for (int i = index; i < nVerts -1; i++) {
nodeList[i] = nodeList[i+1];
}
}

/**
* row move up
* @param length  table column length
*/
public void moveRowUp(int row,int length){
for (int i = 0; i < length; i++) {
}
}

/**
* col move left
* @param length table row lenglth;
*/
public void moveColLeft(int col,int length){
for (int i = 0; i < length; i++) {
}
}

}
```

java 图论二 有向图 拓扑排序

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊