dijkstra

Dijkstra's Algorithm Issue

爱⌒轻易说出口 提交于 2020-01-12 23:19:12
问题 I'm working on a program where I have to find the shortest path between 12 cities, starting in Seattle and ending in Miami. I'm using Dijkstra's Algorithm because the paths are weighted. Here is my code so far, it all works except the answer I get is not the one I need, although it is correct. This part of the code sets everything up as well as creates the sorting algorithm. class Graph { Dictionary<string, Dictionary<string, int>> vertices = new Dictionary<string, Dictionary<string, int>>();

最短路径的Dijkstra算法

房东的猫 提交于 2020-01-11 14:24:30
核心思想:基于已经求出的最短路径的基础上,求得更远顶点的最短路径。 P数组:用来记录前驱顶点,如P[8]=6表示v8的前驱顶点是v6; D数组:D[i]表示vi和v0的最短距离; 在每次大循环内: 1.在D数组中找出当前离v0最近的顶点vk( vk是在所有还未被确定最短路径的顶点中选出。因为已经被确定最短路径的顶点和v0的距离已经是最短的了,而且是必定不会改变的 ),并在flag数组中把下标为k的元素置1; 2.在已经找到v0与vk的最短路径的基础上,对和vk直接相连的且未被确定的顶点进行计算,得到v0与它们的当前距离,并 对D数组、和P数组进行修正 。 大循环结束后,flag数组全为1,表明所有的顶点都完成了最短路径的查找工作,即v0到任何一个顶点的最短路径都已经被求出。 特点:对于每次循环确定的vk,v0与vk的距离是 不断增加 的。 # include <iostream> using namespace std ; # define MAXVEX 9 # define INFINITY 65536 typedef struct { int arc [ MAXVEX ] [ MAXVEX ] ; //邻接矩阵 int numVEXS ; //顶点个数 } MGraph ; void ShortestPath_Dijkstra ( MGraph & G ) ; int main

Python Dijkstra k shortest paths

怎甘沉沦 提交于 2020-01-10 08:59:13
问题 I'm trying to make a small public transport routing application. My data is represented in a following structure: graph = {'A': {'B':3, 'C':5}, 'B': {'C':2, 'D':2}, 'C': {'D':1}, 'D': {'C':3}, 'E': {'F':8}, 'F': {'C':2}} Where: graph dict key is a node subdict key is an edge between 2 nodes subdict value is an edge weight I was using find_shortest_path algorithm described here https://www.python.org/doc/essays/graphs/ but it is rather slow because of recursion and has no support of weights.

Dijkstra算法原理

浪子不回头ぞ 提交于 2020-01-08 12:10:49
https://blog.csdn.net/yalishadaa/article/details/55827681 算法有两个集合,集合在动态更新中。 https://www.cnblogs.com/skywang12345/p/3603935.html 学习完了原理 接下来就开始代码实践吧! 邻接矩阵 邻接矩阵是指用矩阵来表示图。它是采用矩阵来描述图中顶点之间的关系(及弧或边的权)。 假设图中顶点数为n,则邻接矩阵定义为: 下面通过示意图来进行解释。 图中的G1是无向图和它对应的邻接矩阵。上面的图G1包含了"A,B,C,D,E,F,G"共7个顶点,而且包含了"(A,C),(A,D),(A,F),(B,C),(C,D),(E,G),(F,G)"共7条边。由于这是无向图,所以边(A,C)和边(C,A)是同一条边;这里列举边时,是按照字母先后顺序列举的。 上图右边的矩阵是G1在内存中的邻接矩阵示意图。A[i][j]=1表示第i个顶点与第j个顶点是邻接点,A[i][j]=0则表示它们不是邻接点;而A[i][j]表示的是第i行第j列的值;例如,A[1,2]=1,表示第1个顶点(即顶点B)和第2个顶点©是邻接点。 邻接矩阵无向图(undirected graph)的代码说明 基本定义 class MatrixUDG { private : char mVexs [ MAX ] ; //

Dijkstra's (or other shortest path algorithm) where end node can be start node

时间秒杀一切 提交于 2020-01-07 07:11:13
问题 The traditional* implementation of Dijkstra's does not handle this case well. I think I've come up with some solutions that will work, but they are not particularly elegant**. Is this is a known problem with a standard solution? This is assuming the non-trivial solution i.e. a path like A->B->C->A rather than just A->A. * When I say traditional I mean marking each node as visited. ** Storing the number of times each node has been visited and basing the terminating conditions on whether the

Boost:: Dijkstra Shortest Path, how to get vertice index from path iterator?

强颜欢笑 提交于 2020-01-06 01:51:31
问题 Before you start reading, to help you understand my issue, I am telling that I have copied the code from this link: Dijkstra Shortest Path with VertexList = ListS in boost graph So.. I am rewriting my program code to use boost, but now when 99% is ready I am stuck with my GPS (for a game). I have a list of nodes, which I added in a way which fortunately was easy to convert to the boost method. The thing I needed to do was just create a vertice variable like this: Vertex Vx[MAX_NODES]; I

Dijkstra's algorithm - priority queue issue

社会主义新天地 提交于 2020-01-05 08:37:11
问题 I have a Graph class with a bunch of nodes, edges, etc. and I'm trying to perform Dijkstra's algorithm. I start off adding all the nodes to a priority queue. Each node has a boolean flag for whether it is already 'known' or not, a reference to the node that comes before it, and an int dist field that stores its length from the source node. After adding all the nodes to the PQ and then flagging the source node appropriately, I've noticed that the wrong node is pulled off the PQ first. It

Shortest path in a maze with health loss

杀马特。学长 韩版系。学妹 提交于 2020-01-04 09:24:09
问题 Suppose you have a dungeon, represented by a 2D matrix. You have a start point S (x1,y1) and an end point E (x2, y2). Along the way, some cells have a number in them, which subtract from your health score. Other cells are obstacles that you can't go through. You start with 5 health points, and you need to find the shortest path from S to E where you don't die on the way. I know that Dijikstra is used to find shortest paths. But in this case the shortest path might be one in which you die

Shortest path in a maze with health loss

眉间皱痕 提交于 2020-01-04 09:23:07
问题 Suppose you have a dungeon, represented by a 2D matrix. You have a start point S (x1,y1) and an end point E (x2, y2). Along the way, some cells have a number in them, which subtract from your health score. Other cells are obstacles that you can't go through. You start with 5 health points, and you need to find the shortest path from S to E where you don't die on the way. I know that Dijikstra is used to find shortest paths. But in this case the shortest path might be one in which you die

About “Improved Dijkstra Algorithm” Paper

左心房为你撑大大i 提交于 2020-01-04 05:32:13
问题 I want to ask a few questions about the algorithm in this paper. https://pdfs.semanticscholar.org/9208/65aee7b072c054afa628ba413adda6f3e962.pdf You may wonder why I don't ask the authors directly, actually I have, but unfortunately, I've got no response and I couldn't find direct emails to the authors. You can see the core formula (8) of the algorithm. d(n) = weight value from starting point to current node r(n) = constraint function ɷ = weighted value denoting the impact factor θn = angle