//将取得的最小节点标识为已访问 weight[index] = short.MaxValue; tempvertex[index] = 0;
//从最新的节点出发,将此节点的weight比较赋值 for (int j = 0; j < graph.vertexNum; j++) { //已当前节点为出发点,重新选择最小边 if (graph.edges[index, j] < weight[j] && tempvertex[j] != used) { weight[j] = graph.edges[index, j];
//这里做的目的将较短的边覆盖点上一个节点的邻接点中的较长的边 tempvertex[j] = int.Parse(graph.vertex[index]); } } } } #endregion
#region dijkstra求出最短路径 /// <summary> /// dijkstra求出最短路径 /// </summary> /// <param></param> public void Dijkstra(MatrixGraph g) { int[] weight = new int[g.vertexNum];
int[] path = new int[g.vertexNum];
int[] tempvertex = new int[g.vertexNum];
Console.WriteLine("n请输入源点的编号:");
//让用户输入要遍历的起始点 int vertex = int.Parse(Console.ReadLine()) - 1;
for (int i = 0; i < g.vertexNum; i++) { //初始赋权值 weight[i] = g.edges[vertex, i];
if (weight[i] < short.MaxValue && weight[i] > 0) path[i] = vertex;
tempvertex[i] = 0; }
tempvertex[vertex] = 1; weight[vertex] = 0;
for (int i = 0; i < g.vertexNum; i++) { int min = short.MaxValue;
int index = vertex;
for (int j = 0; j < g.vertexNum; j++) { //顶点的权值中找出最小的 if (tempvertex[j] == 0 && weight[j] < min) { min = weight[j]; index = j; } }
tempvertex[index] = 1;
(编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|