shortest-path

Using BFS for Weighted Graphs

℡╲_俬逩灬. 提交于 2019-11-27 00:37:30
问题 I was revising single source shortest path algorithms and in the video, the teacher mentions that BFS/DFS can't be used directly for finding shortest paths in a weighted graph (I guess everyone knows this already) and said to work out the reason on your own. I was wondering the exact reason/explanation as to why it can't be used for weighted graphs. Is it due to the weights of the edges or anything else ? Can someone explain me as I feel a little confused. PS: I went through this question and

Shortest path (fewest nodes) for unweighted graph

故事扮演 提交于 2019-11-27 00:11:57
问题 I'm trying build a method which returns the shortest path from one node to another in an unweighted graph. I considered the use of Dijkstra's but this seems a bit overkill since I only want one pair. Instead I have implemented a breadth-first search, but the trouble is that my returning list contains some of the nodes that I don't want - how can I modify my code to achieve my goal? public List<Node> getDirections(Node start, Node finish){ List<Node> directions = new LinkedList<Node>(); Queue

How to calculate the shortest path between two points in a grid

假如想象 提交于 2019-11-27 00:07:00
问题 I know that many algorithms are available for calculating the shortest path between two points in a graph or a grid, like breadth-first, all-pairs (Floyd's), Dijkstra's. However, as I noticed, all of these algorithms compute all the paths in that graph or grid, not only those between the two points we are interested in. MY QUESTION IS: if I have a grid, i.e. a two dimensional array, and I'm interested in computing the shortest path between two points, say P1 and P2, and if there are

Why doesn&#39;t Dijkstra&#39;s algorithm work for negative weight edges?

南楼画角 提交于 2019-11-26 15:45:46
Can somebody tell me why Dijkstra's algorithm for single source shortest path assumes that the edges must be non-negative. I am talking about only edges not the negative weight cycles. Recall that in Dijkstra's algorithm, once a vertex is marked as "closed" (and out of the open set) - the algorithm found the shortest path to it , and will never have to develop this node again - it assumes the path developed to this path is the shortest. But with negative weights - it might not be true. For example: A / \ / \ / \ 5 2 / \ B--(-10)-->C V={A,B,C} ; E = {(A,C,2), (A,B,5), (B,C,-10)} Dijkstra from A

The best shortest path algorithm

╄→гoц情女王★ 提交于 2019-11-26 15:41:33
问题 What is the difference between the "Floyd-Warshall algorithm" and "Dijkstra's Algorithm" , and which is the best for finding the shortest path in a graph? I need to calculate the shortest path between all the pairs in a net and save the results to an array as follows: **A B C D E** A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 回答1: Dijkstra's algorithm finds the shortest path between a node and every other node in the graph. You'd run it once for every node.

Negative weights using Dijkstra&#39;s Algorithm

左心房为你撑大大i 提交于 2019-11-26 12:40:56
I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths , I am trying to figure out the following scenario: 2 A-------B \ / 3 \ / -2 \ / C From the website: Assuming the edges are all directed from left to right, If we start with A, Dijkstra's algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is (A,C) and it sets d(A,C)=3. But it never finds the shortest path from A to B, via C, with total length 1.

Shortest path to transform one word into another

匆匆过客 提交于 2019-11-26 12:05:56
问题 For a Data Structures project, I must find the shortest path between two words (like \"cat\" and \"dog\" ), changing only one letter at a time. We are given a Scrabble word list to use in finding our path. For example: cat -> bat -> bet -> bot -> bog -> dog I\'ve solved the problem using a breadth first search, but am seeking something better (I represented the dictionary with a trie). Please give me some ideas for a more efficient method (in terms of speed and memory). Something ridiculous

Finding all the shortest paths between two nodes in unweighted undirected graph

喜夏-厌秋 提交于 2019-11-26 11:58:39
问题 I need help finding all the shortest paths between two nodes in an unweighted undirected graph . I am able to find one of the shortest paths using BFS, but so far I am lost as to how I could find and print out all of them. Any idea of the algorithm / pseudocode I could use? 回答1: As a caveat, remember that there can be exponentially many shortest paths between two nodes in a graph. Any algorithm for this will potentially take exponential time. That said, there is a relatively straightforward

Negative weights using Dijkstra&#39;s Algorithm

若如初见. 提交于 2019-11-26 03:03:36
问题 I am trying to understand why Dijkstra\'s algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario: 2 A-------B \\ / 3 \\ / -2 \\ / C From the website: Assuming the edges are all directed from left to right, If we start with A, Dijkstra\'s algorithm will choose the edge (A,x) minimizing d(A,A)+length(edge), namely (A,B). It then sets d(A,B)=2 and chooses another edge (y,C) minimizing d(A,y)+d(y,C); the only choice is