Questions tagged [shortest-path]

Shortest path problems are problems addressing finding the shortest path from a single source to a target source, usually in a graph.

Shortest path problems are problems addressing finding the shortest path from a single source to a target source, usually in a graph.

Some of the commonly used shortest path algorithms are:

1932 questions
193
votes
13 answers

Why doesn't Dijkstra's algorithm work for negative weight edges?

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.
Madu
  • 4,849
  • 9
  • 44
  • 78
175
votes
9 answers

How does a Breadth-First Search work when looking for Shortest Path?

I've done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don't understand how exactly it will get me to a specific path, as opposed to just telling me where each…
Jake
  • 3,142
  • 4
  • 30
  • 48
131
votes
9 answers

Negative weights using Dijkstra's Algorithm

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…
Meir
  • 1,691
  • 2
  • 14
  • 15
106
votes
19 answers

Knight's Shortest Path on Chessboard

I've been practicing for an upcoming programming competition and I have stumbled across a question that I am just completely bewildered at. However, I feel as though it's a concept I should learn now rather than cross my fingers that it never comes…
Kyle Hughes
  • 1,369
  • 3
  • 14
  • 13
73
votes
8 answers

Bellman-Ford vs Dijkstra: Under what circumstances is Bellman-Ford better?

After a lot of Googling, I've found that most sources say that the Dijkstra algorithm is "more efficient" than the Bellman-Ford algorithm. But under what circumstances is the Bellman-Ford algorithm better than the Dijkstra algorithm? I know…
crazyCoder
  • 1,552
  • 3
  • 20
  • 25
72
votes
9 answers

A* Algorithm for very large graphs, any thoughts on caching shortcuts?

I'm writing a courier/logistics simulation on OpenStreetMap maps and have realised that the basic A* algorithm as pictured below is not going to be fast enough for large maps (like Greater London). The green nodes correspond to ones that were put…
drspa44
  • 1,041
  • 8
  • 12
67
votes
5 answers

What is difference between BFS and Dijkstra's algorithms when looking for shortest path?

I was reading about Graph algorithms and I came across these two algorithms: Dijkstra's algorithm Breadth-first search What is the difference between Dijkstra's algorithm and BFS while looking for the shortest-path between nodes? I searched a lot…
harrythomas
  • 1,407
  • 1
  • 13
  • 17
50
votes
3 answers

Using BFS for Weighted Graphs

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…
user2125722
  • 1,289
  • 3
  • 18
  • 29
36
votes
8 answers

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

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…
Alan_AI
  • 1,529
  • 3
  • 22
  • 32
35
votes
7 answers

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

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…
user1946334
  • 389
  • 1
  • 4
  • 7
34
votes
4 answers

Dijkstra vs. Floyd-Warshall: Finding optimal route on all node pairs

I am reading up on Dijkstra's algorithm and the Floyd-Warshall algorithm. I understand that Dijkstra's finds the optimal route from one node to all other nodes and Floyd-Warshall finds the optimal route for all node pairings. My question is would…
pyt
  • 341
  • 1
  • 3
  • 3
32
votes
0 answers

What is the difference between Dijkstra and Prim's algorithm?

Can any one tell me the difference between Dijkstra's and Prim's algorithms? I know what each of the algorithms do. But they look the same to me. Dijkstra's algorithm stores a summation of minimum cost edges whereas Prim's algorithm stores at most…
Pradit
  • 709
  • 5
  • 12
  • 27
30
votes
1 answer

What is meant by diameter of a network?

The diagram shown on this link of the "A graph with 6 vertices and 7 edges where the vertex no 6 on the far-left is a leaf vertex or a pendant vertex." has DIAMETER 4? right or wrong? Definitions are The diameter of a graph is the maximum …
user287745
  • 3,071
  • 10
  • 56
  • 99
30
votes
1 answer

AStar - explanation of name

I am looking for an explanation why the AStar / A* algorithm is called AStar. All similar (shortest path problem) algorithms are often named like its developer(s), so what is AStar standing for?
29
votes
4 answers

Finding kth-shortest paths?

Finding the shortest path between two points in a graph is a classic algorithms question with many good answers (Dijkstra's algorithm, Bellman-Ford, etc.) My question is whether there is an efficient algorithm that, given a directed, weighted…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1
2 3
99 100