无向网图的最小生成树---完整代码

 前两篇只有算法部分,现在附上完整代码

public class ShortPath {
	public static void main(String[] args) {
		MGraph mg=new MGraph();
		
		mg.addVertex('A');
		mg.addVertex('B');
		mg.addVertex('C');
		mg.addVertex('D');
		mg.addVertex('E');
		mg.addVertex('F');
		mg.addVertex('G');
		mg.addVertex('H');
		mg.addVertex('I');
		
		mg.addEdge(0,1,1);
		mg.addEdge(0,2,5);
		mg.addEdge(1,2,3);
		mg.addEdge(1,3,7);
		mg.addEdge(1,4,5);
		mg.addEdge(2,4,1);
		mg.addEdge(2,5,7);
		mg.addEdge(3,4,2);
		mg.addEdge(3,6,3);
		mg.addEdge(4,5,3);
		mg.addEdge(4,6,6);
		mg.addEdge(4,7,9);
		mg.addEdge(5,7,5);
		mg.addEdge(6,8,7);
		mg.addEdge(6,7,2);
		mg.addEdge(7,8,4);
		
		//mg.displayAdjMatr();
		//int shortPath[]=new int[MGraph.MAXVEX];  //保存最短路径
		//int parent[]=new int[MGraph.MAXVEX];     //保存前驱结点下标 parent[i]=k,表示结点i的前驱结点为k,即最短路径中k的下一个点为j
		//mg.shortPath_Dijkstra(0, shortPath, parent);
		
		int PathMatrix[][]=new int[MGraph.MAXVEX][MGraph.MAXVEX];
		int ShortPathTable[][]=new int[MGraph.MAXVEX][MGraph.MAXVEX];
		mg.shortPath_Floyd(PathMatrix, ShortPathTable);
	}
}

class MGraph{
	public static final int MAXVEX=10;
	char vertexList[];   //结点数组
	int adjMatr[][];     //邻接矩阵
	int nVert;           //结点个数
	
	public MGraph(){
		vertexList=new char[MAXVEX];
		adjMatr=new int[MAXVEX][MAXVEX];
		nVert=0;
		for(int i=0;i
   
   
    
    PathMatrix[i][k]+PathMatrix[k][j]){    //如果经过下标为k的顶点的路径比原来两点间路径更短,修改两点间权值
						PathMatrix[i][j]=PathMatrix[i][k]+PathMatrix[k][j];
						ShortPathTable[i][j]=k;      //路径设置为经过下标为k的顶点
					}
				}
			}
		}
		for(int i=0;i
    
     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值