shortTablePath

package main

import (
	"fmt"
)

const MAXVEX int = 9
const MAXWEIGHT int = 1000

var shortTablePath = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT}

func main() {
	graph := NewGraph()

	var TablePathMin int       //存放shortTablePath中,未遍历的最小结点的值
	var Vx int                 //存放shortTablePath中,未遍历的最小结点的下标
	var isgetPath [MAXVEX]bool //记录结点是否已经找到v0到vx的最小路径

	// 获取v0这一行的权值数组
	for v := 0; v < len(graph); v++ {
		shortTablePath[v] = graph[0][v]
	}
	shortTablePath[0] = 0
	isgetPath[0] = true

	//遍历v1 ~ v8
	for v := 1; v < len(graph); v++ {
		TablePathMin = MAXWEIGHT

		//找出shortTablePath中,未遍历的最小结点的值
		for w := 0; w < len(graph); w++ {
			if !isgetPath[w] && shortTablePath[w] < TablePathMin {
				Vx = w
				TablePathMin = shortTablePath[w]
			}
		}
		isgetPath[Vx] = true
		for j := 0; j < len(graph); j++ {
			if !isgetPath[j] && TablePathMin+graph[Vx][j] < shortTablePath[j] {
				shortTablePath[j] = TablePathMin + graph[Vx][j]
			}
		}

		fmt.Println("遍历完V", v, "后:", shortTablePath)

	}

	//输出
	for i := 0; i < len(shortTablePath); i++ {
		fmt.Println("V0到V", i, "最小路径:", shortTablePath[i])
	}

}

func NewGraph() [MAXVEX][MAXVEX]int {
	var graph [MAXVEX][MAXVEX]int
	var v0 = [MAXVEX]int{0, 1, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT}
	var v1 = [MAXVEX]int{1, 0, 3, 7, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT}
	var v2 = [MAXVEX]int{5, 3, 0, MAXWEIGHT, 1, 7, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT}
	var v3 = [MAXVEX]int{MAXWEIGHT, 7, MAXWEIGHT, 0, 2, MAXWEIGHT, 3, MAXWEIGHT, MAXWEIGHT}
	var v4 = [MAXVEX]int{MAXWEIGHT, 5, 1, 2, 0, 3, 6, 9, MAXWEIGHT}
	var v5 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, 7, MAXWEIGHT, 3, 0, MAXWEIGHT, 5, MAXWEIGHT}
	var v6 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 3, 6, MAXWEIGHT, 0, 2, 7}
	var v7 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 9, 5, 2, 0, 4}
	var v8 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 7, 4, 0}
	graph[0] = v0
	graph[1] = v1
	graph[2] = v2
	graph[3] = v3
	graph[4] = v4
	graph[5] = v5
	graph[6] = v6
	graph[7] = v7
	graph[8] = v8
	return graph
}

遍历完V 1 后: [0 1 4 8 6 1000 1000 1000 1000]
遍历完V 2 后: [0 1 4 8 5 11 1000 1000 1000]
遍历完V 3 后: [0 1 4 7 5 8 11 14 1000]
遍历完V 4 后: [0 1 4 7 5 8 10 14 1000]
遍历完V 5 后: [0 1 4 7 5 8 10 13 1000]
遍历完V 6 后: [0 1 4 7 5 8 10 12 17]
遍历完V 7 后: [0 1 4 7 5 8 10 12 16]
遍历完V 8 后: [0 1 4 7 5 8 10 12 16]
V0到V 0 最小路径: 0
V0到V 1 最小路径: 1
V0到V 2 最小路径: 4
V0到V 3 最小路径: 7
V0到V 4 最小路径: 5
V0到V 5 最小路径: 8
V0到V 6 最小路径: 10
V0到V 7 最小路径: 12
V0到V 8 最小路径: 16

时间: 2024-10-11 02:34:20

shortTablePath的相关文章

Android中的数据结构

数据结构在Android中也有着大量的运用,这里采用数据结构与源代码分析相结合,来认识Android的数据结构 线性表 线性表可分为顺序存储结构和链式存储结构 顺序存储结构-ArrayList 通过对源代码的产看得知,ArrayList继承自AbstractList,实现了多个接口,其中List里面就实现了常用的一些操作,包括增删改查清除大小等等 public class ArrayList<E> extends AbstractList<E> implements List<

图的最短路径和拓扑排序

图的最短路径 从某顶点出发,沿图的边到达另一顶点所经过的路径中,各边上权值之和最小的一条路径叫做最短路径 图的最短路径有许多重要的应用. 例如:上图中v0-v8有9个点,可以看做不同的地点,现在要规划出v0到其它某个点地点的最短路线规划 构建最短路径中比较常见的一种算法即为dijstra(迪杰斯特拉)算法 二.dijstra(迪杰斯特拉)算法 究竟什么是迪杰斯特拉算法?它是如何寻找图中顶点的最短路径呢? 这个算法的本质,是不断刷新起点与其他各个顶点之间的 “距离表”. 让我们来演示一下迪杰斯特拉