bellman-ford

最短路径算法

陌路散爱 提交于 2019-12-09 18:09:23
单源最短路径问题 问题描述:给你一个顶点做源点,你想要知道,如何从源点到达其他所有点的最短路径。 OK,这个问题看起来没什么用。我们一般想知道的是A点到B点的最短路径,这个单源最短路径问题告诉我们A点到所有点的最短路径,会不会计算过多了呢? 有趣的是,解决A点到B点的最短路径算法不会比单源最短路径问题简单,我们所知的求A点到B点的最短路径算法就是求A点到任何点的最短路径。我们除了这样做,好像也没什么好办法了。 Dijkstra算法 基本原理: 每次新扩展一个距离最短的点,更新与其相邻的点的距离。当所有边权都为正时,由于不会存在一个距离更短的没扩展过的点,所以这个点的距离永远不会再被改变,因而保证了算法的正确性。不过根据这个原理,用Dijkstra求最短路的图 不能有负权边 ,因为扩展到负权边的时候会产生更短的距离,有可能就破坏了已经更新的点距离不会改变的性质。 适用条件与限制: 有向图 和 无向图 都可以使用本算法,无向图中的每条边可以看成相反的两条边。 用来求最短路的图中不能存在负权边。(可以利用拓扑排序检测) 算法流程: 在以下说明中,s为源,w[u,v]为点u和v之间的边的长度,结果保存在dist[] 初始化:源的距离dist[s]设为0,其他的点距离设为 正无穷大 ,同时把所有的点的状态设为没有扩展过。 循环n-1次: 在没有扩展过的点中取距离最小的点u

Parallel Bellman-Ford implementation

ⅰ亾dé卋堺 提交于 2019-12-08 13:11:29
问题 Can anyone point me to a good pseudocode of a simple parallel shortest path algorithm? Or any language, it doesn't matter. I'm having trouble finding good examples =[ 回答1: I eventually implemented it myself for a bitcoin bot using OpenMP: /*defines the chunk size as 1 contiguous iteration*/ #define CHUNKSIZE 1 /*forks off the threads*/ #pragma omp parallel private(i) { /*Starts the work sharing construct*/ #pragma omp for schedule(dynamic, CHUNKSIZE) list<list_node>::iterator i; for (int u =

Variations of Dijkstra's Algorithm for graphs with two weight properties

大兔子大兔子 提交于 2019-12-08 02:50:08
问题 I'm trying to find a heuristic for a problem that is mapped to a directed graph with say non-negative weight edges. However, each edge is associated with two weight properties as opposed to only one weight (e.g. say one is distance, and another one showing how good the road's 4G LTE coverage is!). Is there any specific variation of dijkstra , Bellman Ford , or any other algorithm that pursues this objective? Of course, a naive workaround is manually deriving a single weight property as a

Algorithm like Bellman-Ford, only for multiple start, single destination?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 10:01:11
问题 Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. However, in the program I'm writing, the starting vertex changes a lot more often than the destination vertex does. What algorithm is there that does the reverse--that is, given a single destination vertex, to find the shortest path from every starting vertex? 回答1: Just reverse all the edges, and treated destination as start node.

Variations of Dijkstra's Algorithm for graphs with two weight properties

蹲街弑〆低调 提交于 2019-12-06 11:17:04
I'm trying to find a heuristic for a problem that is mapped to a directed graph with say non-negative weight edges. However, each edge is associated with two weight properties as opposed to only one weight (e.g. say one is distance, and another one showing how good the road's 4G LTE coverage is!). Is there any specific variation of dijkstra , Bellman Ford , or any other algorithm that pursues this objective? Of course, a naive workaround is manually deriving a single weight property as a combination of all of them, but this does not look good. Can it be generalized to cases with multiple

how to create random single source random acyclic directed graphs with negative edge weights in python

杀马特。学长 韩版系。学妹 提交于 2019-12-06 07:42:16
问题 I want to do a execution time analysis of the bellman ford algorithm on a large number of graphs and in order to do that I need to generate a large number of random DAGS with the possibility of having negative edge weights. I am using networkx in python. There are a lot of random graph generators in the networkx library but what will be the one that will return the directed graph with edge weights and the source vertex. I am using networkx.generators.directed.gnc_graph() but that does not

Bellman-Ford: all shortest paths

拜拜、爱过 提交于 2019-12-06 05:09:13
问题 I've successfully implemented Bellman-Ford to find the distance of the shortest path when edges have negative weights/distances. I've not been able to get it to return all shortest paths (when there are ties for shortest). I managed to get all shortest paths (between a given pair of nodes) with Dijkstra. Is this possible with Bellman-Ford? (just want to know if I'm wasting my time trying) 回答1: If you alter the second step of the Bellman-Ford algorithm a little bit you can achieve something

Algorithm like Bellman-Ford, only for multiple start, single destination?

江枫思渺然 提交于 2019-12-05 14:46:15
Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. However, in the program I'm writing, the starting vertex changes a lot more often than the destination vertex does. What algorithm is there that does the reverse--that is, given a single destination vertex, to find the shortest path from every starting vertex? Just reverse all the edges, and treated destination as start node. Problem solved. If this is an undirected graph: I think inverting the problem would give you advantages.

Can we apply the Bellman-Ford algorithm to an Undirected Graph?

旧城冷巷雨未停 提交于 2019-12-04 18:48:33
问题 I know that Bellman-Ford Algorithm works for directed graphs. Will it will work for an undirected graph? It seems that with an undirected graph, it will not be able to detect cycles because parallel edges will be considered cycles. Is this true or not? Can the algorithm be applied? 回答1: As a matter of fact any undirected graph is also a directed graph. You just have to specify any edges {u, v} twice (u, v) and (v, u). But don't forget, that this also means any edge with a negative weight will

how to create random single source random acyclic directed graphs with negative edge weights in python

只愿长相守 提交于 2019-12-04 16:03:19
I want to do a execution time analysis of the bellman ford algorithm on a large number of graphs and in order to do that I need to generate a large number of random DAGS with the possibility of having negative edge weights. I am using networkx in python. There are a lot of random graph generators in the networkx library but what will be the one that will return the directed graph with edge weights and the source vertex. I am using networkx.generators.directed.gnc_graph() but that does not quite guarantee to return only a single source vertex. Is there a way to do this with or even without