Algorithms: Design and Analysis, Part 1, Programming Assignment #5
来源:互联网 发布:张国荣和四大天王 知乎 编辑:程序博客网 时间:2024/05/29 08:43
import heapqimport sysfname = 'dijkstraData.txt'#fname = "tc.txt"fh = open(fname)lines = [line.rstrip('\n') for line in fh]# Generate graph as a list of dictionaries.# Index of list is the vertex, value is a disctionary where key is the outgoing vertex and value is the length of the edge.neighbor = []d = dict()neighbor.append(d)for line in lines: part = line.split() v = int(part[0]) d = dict() for i in part[1:]: values = map(int, i.split(',')) d[values[0]] = values[1] neighbor.append(d)# Generating graph ends# Dijkstras = 1dest = [7,37,59,82,99,115,133,165,188,197]#dest = range(12)res = []# vertices processded so far in a dictionaryx = dict()# Computed shortest path distances in a dictionary/heapa = dict()x[s] = 0a[s] = 0while len(x) != len(neighbor): f = False for v in x.keys(): for w, e in neighbor[v].items(): if w in x: continue f = True cur = a[v] + e if w in a: a[w] = min(cur, a[w]) else: a[w] = cur #print "v:a[%d]:%d, e:%d, w:a[%d]:%d" % (v, a[v], e, w, a[w]) if f: min_e = sys.maxint min_v = 0 for w, e in a.items(): if (w not in x) and (e < min_e): min_e = e min_v = w x[min_v] = min_e #print "x[%d]:%d" % (min_v, x[min_v]) else: for w, e in a.items(): x[w] = e #print "exit" break #print "end of loop, a", afor d in dest: res.append(x.get(d, 0))print res
0 0
- Algorithms: Design and Analysis, Part 1, Programming Assignment #5
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 5 Assignment: Dijkstra
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 6 Assignment: hash table and heap
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 1 Assignment: Greedy and Prim
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 2 Assignment: QuickSort
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 3 Assignment: Contraction
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 4 Assignment: strongly connected component
- Cousera:Algorithms: Design and Analysis, Part 1
- Stanford Algorithms: Design and Analysis, Part 1[week 5]
- Algorithms: Design and Analysis, Part 1Problem Set #5
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering
- Stanford Algorithms: Design and Analysis, Part 2[week 5]
- Stanford Algorithms: Design and Analysis, Part 1[week 1]
- Stanford Algorithms: Design and Analysis, Part 1[week 2]
- Stanford Algorithms: Design and Analysis, Part 1[week 3]
- Stanford Algorithms: Design and Analysis, Part 1[week 4]
- Stanford Algorithms: Design and Analysis, Part 1[week 6]
- cs231深度学习课程,注意点总结(2)
- spring context:property-placeholder无法加载配置文件,导致属性无效
- rang xrange
- secureCTR破解
- rails 网络接口模块
- Algorithms: Design and Analysis, Part 1, Programming Assignment #5
- js弹出框、对话框、提示框、弹窗总结
- 十分钟教程,eclipse配置maven + 创建maven项目
- 面试 程序媛的博客
- Siege web 压测工具代码分析
- Google Charts -- 对图表创建事件
- HashSet知识
- codevs 1068 乌龟棋(四维dp)
- IOS网络高级编程学习第一章 网络功能介绍