Algorithms: Design and Analysis, Part 1

最短路径算法题目要求

本次要求对于一个200个点的无向图应用著名的Dijkstra算法求最短路径。可以选择用heap来计算,速度更快。我采用python语言,用了多个字典来加快速度。

In this programming problem you'll code up Dijkstra's shortest-path algorithm.
Download the text file here. (Right click and save link as).
The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200. Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has 6 as the first entry indicating that this row corresponds to the vertex labeled 6. The next entry of this row "141,8200" indicates that there is an edge between vertex 6 and vertex 141 that has length 8200. The rest of the pairs of this row indicate the other vertices adjacent to vertex 6 and the lengths of the corresponding edges.

Your task is to run Dijkstra's shortest-path algorithm on this graph, using 1 (the first vertex) as the source vertex, and to compute the shortest-path distances between 1 and every other vertex of the graph. If there is no path between a vertex v and vertex 1, we'll define the shortest-path distance between 1 and v to be 1000000.

You should report the shortest-path distances to the following ten vertices, in order: 7,37,59,82,99,115,133,165,188,197. You should encode the distances as a comma-separated string of integers. So if you find that all ten of these vertices except 115 are at distance 1000 away from vertex 1 and 115 is 2000 distance away, then your answer should be 1000,1000,1000,1000,1000,2000,1000,1000,1000,1000. Remember the order of reporting DOES MATTER, and the string should be in the same order in which the above ten vertices are given. Please type your answer in the space provided.

IMPLEMENTATION NOTES: This graph is small enough that the straightforward O(mn) time implementation of Dijkstra's algorithm should work fine. OPTIONAL: For those of you seeking an additional challenge, try implementing the heap-based version. Note this requires a heap that supports deletions, and you'll probably need to maintain some kind of mapping between vertices and their positions in the heap.

算法实现

这个算法比较简单,跟着课程走,实现要求的数据结构就ok了。当然最后要选择题目要求的点的距离,使用list comprehension可以加快这一过程。

整个算法实现中,全部以字典实现,速度较快。

代码如下:


f=open('dijkstraData.txt','r')
vertices=dict()
for line in f.readlines():tmp=line.split()try:vertices[int(tmp[0])]={int(x.split(',')[0]):int(x.split(',')[1]) for x in tmp[1:]}except:print('error'+tmp)
f.closelength=200
exped=[1]
noexped=list(range(2,length+1))
distance={x:0 for x in range(1,length+1)}while len(exped)<length:maxlimit=1000000tmpdist=maxlimitlenvw=tmpdistfor v in exped:for w in noexped:if w in vertices[v].keys():lenvw=distance[v]+vertices[v][w]if lenvw<tmpdist:tmpdist=lenvwtmpv=vtmpw=wif tmpdist==maxlimit:breakexped.append(tmpw)noexped.remove(tmpw)distance[tmpw]=tmpdistprint('distance is')
print(distance)
print('max distance is: '+str(max(distance.values())))
for ind in [7,37,59,82,99,115,133,165,188,197]:print(str(ind)+' is : '+str(distance[ind]))

在对大型数据进行处理前,不妨采用小的数据集进行测试。

这是测试使用的txt文件:

1 2,7 3,9 6,14
2 1,7 3,10 4,15
3 1,9 2,10 4,11 6,2
4 2,15 3,11 5,6
5 4,6 6,9
6 1,14 3,2 5,9

计算结果是:

distance is
{1: 0, 2: 7, 3: 9, 4: 20, 5: 20, 6: 11}
max distance is: 20

Algorithms Part 1-Question 5- Dijkstra's shortest-path-最短路径算法相关推荐

  1. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  2. python3dijkstra_python3 实现Dijkstra(迪杰斯特拉)最短路径算法

    Dijkstra 单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中 ...

  3. [召集令]-Dijkstra的单源最短路径算法

    2021.3.10 题目背景 墨家家主发出召集令,所有弟子得迅速到指定地点集合. 题目描述 给定一张地图,含有n个地点(n<=10000),地点从1开始编号,地图上还含有m条单向路(m<= ...

  4. 图的最短路径之Dijkstra求单源最短路径算法(C++)

    一个有向带权图求它的单源最短路径可以使用Dijkstra算法. 单源最短路径是指:从图中的某个顶点出发,到其余各个顶点权值最小的路径. Dijkstra算法需要用到三个辅助数组: dist[max]: ...

  5. dijkstra的matlab程序,最短路径算法dijkstra的matlab程序,让大家来找茬,交流

    %初始化 MAXNUM=5; MAXINT=32767; dij=MAXINT*ones(MAXNUM,MAXNUM); dij(1,2)=10; dij(1,4)=30; dij(1,5)=100; ...

  6. 图的最小生成树和最短路径算法思路总结(Prim,Kruskal,Dijkstra,Floyd)

    带权无向图->最小生成树算法->Prim算法: 思路: 首先,我们先设置两个集合,U_{}:一个用来放最小生成树的顶点,T_{}:一个用来放最小生成树的边.选取最开始的点V_0,将V_0放 ...

  7. Relaxation step(Dijkstra's 最短路径算法)

    翻译成中文就是"松弛",属于工程优化的范畴: Dijkstra 的单源最短路径算法,有一个重要的步奏,当访问到新的结点 uu (加入到集合 S),然后遍历 uu 的邻接顶点(Adj ...

  8. Dijkstra最优路径的算法

    Dijkstra最优路径的算法: 1 最短路径算法 在日常生活中,我们如果需要常常往返A地区和B地区之间,我们最希望知道的可能是从A地区到B地区间的众多路径中,那一条路径的路途最短.最短路径问题是图论 ...

  9. P - The Shortest Path in Nya Graph HDU - 4725

    P - The Shortest Path in Nya Graph HDU - 4725 最短路 不是 每两个点之间按层数设置边权 + 额外边权 TLE 是 相邻两层之间设置边权 + 额外边权 需注 ...

  10. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

最新文章

  1. 【组队学习】【29期】5. 李宏毅机器学习(含深度学习)
  2. java oscache 使用_OScache的使用(Java对象)
  3. AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking...
  4. win10使用Composer-Setup安装Composer以及使用Composer安装Yii2最新版
  5. Mysql基础代码(不断完善中)
  6. spark代码中添加logger_Spark RDD中Runtime流程解析
  7. 事件和数据回发机制的实现
  8. GreenSock (TweenMax) 极简入门指南
  9. 1.什么是计算机图形学?
  10. 修改BT种子的tracker服务器list
  11. Qt练习项目--鼠标连点器
  12. 计算机设置启动恢复出厂设置密码,bios怎么恢复出厂设置方法
  13. 2021阿里云ECS镜像导入本地VMware虚拟机
  14. 基于java后端的 krpano 功能化
  15. Java代码审计手册(1)
  16. JODD与数据页面绑定
  17. 区间求和Java,matlab中95%置信区间的求和高斯分布
  18. 用python画皇冠_用python做数字油画或者从一幅画学习风格,去画另一幅画
  19. 记录千万级数据库去重问题
  20. 火绒一面病毒样本分析

热门文章

  1. 以太坊服务器是什么_OKEX区块链60讲 | 第33集:什么是以太坊?
  2. c++ 数组置0_09c语言数组详解
  3. python函数的目的与意义_Python函数__new__及__init__作用及区别解析
  4. 计算机故障按照产生机理来分可分为,维修自测题
  5. 小学生计算机课学生心得,【小学信息技术学习体会13篇】_小学信息技术学习体会范文大全_2021年小学信息技术学习体会_东城教研...
  6. java wifi类_Java 接口——面向对象的精髓
  7. python撩妹代码_Python十行代码让你秒变撩妹达人!想学?
  8. mongodb数据库扩展名_MongoDB如何存储数据
  9. java数组复制_Java自学-数组 复制数组
  10. 计算机图形系统基本处理流程,计算机图形系统是进行图形处理的计算机系统-read.ppt...