算法的思想就不多说了。主要对代码解释一下。
graph = [[0,4,3,2], [4,0,1,4], [3,1,0,2], [2,4,2,0]] n = 4 flags = [True,True,True,True] queue = [] #flags是存储节点访问情况的,true为待访问节点 #queue存储节点访问顺序 queue.append(0) flags[0] = False <pre name="code" class="python">#现将初始节点加入queuewhile(len(queue) != n): minedge = 10000 for i in range(len(queue)): for j in range(n): if(flags[j] and minedge > graph[queue[i]][j]): minedge = graph[queue[i]][j] minj = j print minedge
原文地址:http://blog.csdn.net/u010352695/article/details/44679781