Questions tagged [floyd-warshall]

The Floyd-Warshall algorithm is an O(|V|^3) algorithm for computing all-pairs shortest paths in a directed weighted graph.

During the work of Floyd–Warshall algorithm all possible paths through the directed weighted graph between each pair of vertices are compared. The complexity is O(n^3) where n is a number of vertices.

Minimal pseudo code to get only shortest paths of a graph:

for k = 1 to n
  for i = 1 to n
    for j = 1 to n
      W[i][j] = min(W[i][j], W[i][k] + W[k][j])

Where W is a matrix (size n x n) which stores all shortest paths. At the start W is filled with infinity (any large number which exceeds the sum of weights of a graph). For each edge (u,v) of a graph the weight of the edge (u,v) has to be placed to the matrix W.

Algorithm is allowed path reconstruction.

This algorithm is effective for full-packed graph especially stored as connectivity matrix.

Wiki article about Floyd-Warshall algorithm

207 questions
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
22
votes
2 answers

Am I right about the differences between Floyd-Warshall, Dijkstra's and Bellman-Ford algorithms?

I've been studying the three and I'm stating my inferences from them below. Could someone tell me if I have understood them accurately enough or not? Thank you. Dijkstra's algorithm is used only when you have a single source and you want to know…
GrowinMan
  • 4,891
  • 12
  • 41
  • 58
16
votes
4 answers

Floyd-Warshall: all shortest paths

I've implemented Floyd-Warshall to return the distance of the shortest path between every pair of nodes/vertices and a single shortest path between each of these pairs. Is there any way to get it to return every shortest path, even when there are…
user1507844
  • 5,973
  • 10
  • 38
  • 55
13
votes
3 answers

Floyd-Warshall with negative cycles. How do I find all undefined paths?

I have implemented the Floyd Warshall algorithm and it works, but the problem is that I don't know how I can find all paths which are not defined. I have searched around the web but I can only find answers to how to detect if a graph has negative…
mseln
  • 351
  • 1
  • 2
  • 9
13
votes
4 answers

Time complexity of Floyd Warshall algorithm

The Skiena's book of algorithm contains the following explaination of Floyd Warshall algorithm: floyd(adjacency_matrix *g) { int i,j; /* dimension counters */ int k; /* intermediate vertex counter */ int through_k; /* distance through…
Jake
  • 16,329
  • 50
  • 126
  • 202
11
votes
4 answers

What's the simplest algorithm/solution for a single pair shortest path through a real-weighted undirected graph?

I need to find a shortest path through an undirected graph whose nodes are real (positive and negative) weighted. These weights are like resources which you can gain or loose by entering the node. The total cost (resource sum) of the path isn't very…
foobars
  • 200
  • 2
  • 11
9
votes
1 answer

Understanding the minimax/maximin paths (Floyd-Warshall)

I've implemented the Floyd-Warshall-Algorithm to solve the All-pairs shortest path problem. Now I found out I can also compute the minimax or maximin path with easy modifications. But I don't understand what the result means (what a minimax path…
d.hill
  • 669
  • 3
  • 9
  • 16
8
votes
3 answers

Printing shortest path b/w given nodes using modified floyd warshall

I read the approach given by Wikipedia to print the shortes path b/w two given points in a graph by modifying Floyd Warshall algorithm . I coded this, but its not really giving the expected output : Initialize all the elements in…
Amit Tomar
  • 4,800
  • 6
  • 50
  • 83
8
votes
1 answer

Performance of Floyd-Warshall in Haskell – Fixing a space leak

I wanted to write an efficient implementation of the Floyd-Warshall all pairs shortest path algorithm in Haskell using Vectors to hopefully get good performance. The implementation is quite straight-forward, but instead of using a 3-dimensional…
beta
  • 2,380
  • 21
  • 38
7
votes
2 answers

why does floyd warshall just use one distance matrix?

I read the pseudocode of the floyd warshall algorithm 1 let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity) 2 for each vertex v 3 dist[v][v] ← 0 4 for each edge (u,v) 5 dist[u][v] ← w(u,v) // the weight of the edge…
Shangtong Zhang
  • 1,579
  • 1
  • 11
  • 18
7
votes
1 answer

How to use Warshall's algorithm for transitive closure to determine canonical LR(1) parser closures?

I'm trying to implement Warshall's algorithm to quickly compute LR(1) closures. I think I understand how it works for LR(0): The nodes of the graph are LR items, like A → B • C The edges are "transitions" starting from A → B • C to C → • D The…
user541686
  • 205,094
  • 128
  • 528
  • 886
7
votes
2 answers

Fastest algorithm to detect if there is negative cycle in a graph

I use a matrix d to present a graph. d.(i).(j) means the distance between i and j; v denotes the number of nodes in the graph. It is possible that there is negative cycle in this graph. I would like to check if a negative cycle exists. I have…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
6
votes
2 answers

How do I select the node that minimizes the maximum shortest distance to the other nodes in a graph?

I have an undirected, connected, positively-weighted graph G = . I need to find one node v_min in V such that the maximum distance between v_min and the other nodes are minimized. I've learned that this is a special problem of the k-center…
6
votes
2 answers

Floyd-Warshall Algorithm Logic - Stuck

I am trying to use this logic to understand what is going on with the adjacency matrix, but i am massivley confused where it says about interspacing for a b c d ..... Could anyone explain what is going on here? Thank you (tagged as java as its the…
stan
  • 433
  • 1
  • 5
  • 7
6
votes
2 answers

All pairs shortest path - warm restart?

Is it possible to warm start any of the well known algorithms (Dijkstra/Floyd-Warshall etc) for the APSP problem so as to be able to reduce the time complexity, and potentially the computation time? Let's say the graph is represented by a NxN…
1
2 3
13 14