图-无权图-拓扑排序

package k_graph.A_Graph.D_DAG;


/**
 * 拓扑排序 有向图
 * 
 * @author Administrator
 *
 */
public class TopoApp {
public static void main(String[] args) {
Graph theGraph = new Graph();


theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('E');
theGraph.addVertex('F');
theGraph.addVertex('G');
theGraph.addVertex('H');


theGraph.addEdge(0, 3);
theGraph.addEdge(0, 4);
theGraph.addEdge(1, 4);
theGraph.addEdge(2, 5);
theGraph.addEdge(3, 6);
theGraph.addEdge(4, 6);
theGraph.addEdge(5, 7);
theGraph.addEdge(6, 7);


theGraph.topo();
}
}package k_graph.A_Graph.D_DAG;


public class Graph {
private final int MAX_VERTS = 20;
private Vertex[] vertexList;
private int[][] adjMat;
private int nVerts;
private char sortArray[];


public Graph() {
vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
sortArray = new char[MAX_VERTS];
for (int i = 0; i < MAX_VERTS; i++)
for (int j = 0; j < MAX_VERTS; j++)
adjMat[i][j] = 0;
}


public void addVertex(char lab) {
vertexList[nVerts++] = new Vertex(lab);
}


public void addEdge(int start, int end) {
adjMat[start][end] = 1;
}


public void displayVertex(int v) {
System.out.print(vertexList[v].label);
}


/**
* 拓扑排序
*/
public void topo() {
int orig_nVerts = nVerts;
while (nVerts > 0) {
// 获取后继顶点
int currentVerts = noSuccessors();// 查找无后继的顶点
if (currentVerts == -1) {
System.out.println("ERROR:Graph has cycles/错误,图为循环 ");
return;
}
sortArray[nVerts - 1] = vertexList[currentVerts].label;//入数组,从后往前存
deleteVertex(currentVerts);//删除顶点
}
// 遍历
System.out.println("拓扑排序:");
//从前显示后面
for (int j = 0; j < orig_nVerts; j++) {
System.out.print(sortArray[j] + " ");
}


}


/**
* 查找无后继的顶点

* @return
*/
public int noSuccessors() {
boolean isEdge;
int index = -1;
for (int row = 0; row < nVerts; row++) {
isEdge = false;
for (int col = 0; col < nVerts; col++) {
// 当邻接矩阵里面含有大于0的数据项,说明此行没有后继顶点,结束
if (adjMat[row][col] > 0) {
isEdge = true;
break;
}
}
// 当邻接矩阵没有大于0的数据项,说明无后继顶点
if (!isEdge) {
index = row;
break;
}
}
return index;
}


/**
* 删除顶点

* @param delVert
*/
public void deleteVertex(int delVert) {
if (delVert != nVerts - 1) {// 如果不是最后一个
// 删除顶点,顶点数组前移
for (int j = delVert; j < nVerts - 1; j++) {
vertexList[j] = vertexList[j + 1];
}
// 删除邻接矩阵行
for (int row = delVert; row < nVerts - 1; row++) {
moveRowUp(row, nVerts);
}
// 删除列,这里nVerts - 1之所有减1,是因为前面删除了一行
for (int col = delVert; col < nVerts - 1; col++) {
moveColLeft(col, nVerts - 1);
}
}


nVerts--;
}


/**
* 删除行: 行向上移动一索引

* @param row
* @param length
*/
private void moveColLeft(int row, int length) {
for (int col = 0; col < length; col++) {
// row + 1 ,行向上移动,覆盖,起到删除的效果
adjMat[row][col] = adjMat[row + 1][col];
}
}


/**
* 删除列: 列向左移动一索引

* @param col
* @param length
*/
private void moveRowUp(int col, int length) {
for (int row = 0; row < length; row++) {
// col + 1 列向左移动一索引,覆盖,起到删除的效果
adjMat[row][col] = adjMat[row][col + 1];
}
}
}package k_graph.A_Graph.D_DAG;


public class Vertex {
public char label;


public Vertex(char label) {
this.label = label;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值