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
5
votes
4 answers

Optimization problem in connected graphs with profits

I am trying to develop an algorithm to solve a problem that I am not able to classify, I expose the subject: You have a map divided into sections that have a certain area and where a certain number of people live. The problem consists of finding…
5
votes
1 answer

Floyd-Warshall algorithm: get the shortest paths

Assume a graph is represented by a n x n dimension adjacency matrix. I know the how to get the shortest path matrix for all pairs. But I wonder is there a way to trace all the shortest paths? Blow is the python code implementation. v =…
Joey Lant
  • 59
  • 1
  • 1
  • 5
5
votes
2 answers

Finding all shortest paths and distances using Floyd-Warshall

First, a little background: I'm working on building a simple graph class with basic graph algorithms (Dijkstra, Floyd-Warshall, Bellman-Ford, etc) to use as a reference sheet for an upcoming programing competition. So far I have a functioning…
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
5
votes
2 answers

Floyd warshall implementation appears to be missing a shortest path

I'm collecting a count of the shortest paths that floyd warshall finds. For this particular graph the shortest path for 1 -> 3 is 5, and there are two paths with this weight: 1->4->2->3, and 1->4->3. I wasn't sure the best way to display the graph,…
Talen Kylon
  • 1,908
  • 7
  • 32
  • 60
5
votes
2 answers

What kind of cycle isn't allowed in Floyd–Warshall algorithm?

For example, Let's say 1->2 costs 100 2->4 costs 600 So 1->2->4 costs 700 What if there was an edge from 4 to 3 costing -500 ? And a different edge from 3 to 4 costing 200 4->3 costs -500 3->4 costs 200 So 1->2->4->3->4 costs 400 Which is less…
ABCD123
  • 137
  • 1
  • 6
5
votes
2 answers

Floyd-Warshall Algorithm - Representing "infinity"

Using Floyd-Warshall's algorithm for finding the shortest path between two vertices, how should I represent infinity when implementing in Java? I'm using infinity here to say there is no path between two vertices. Thanks
DJDMorrison
  • 1,302
  • 2
  • 17
  • 34
5
votes
2 answers

Optimise Floyd-Warshall for symmetric adjacency matrix

Is there an optimisation that lowers the constant factor of the runtime of Floyd-Warshall, if you are guaranteed to have a symmetric adjacency matrix?
JPvdMerwe
  • 3,328
  • 3
  • 27
  • 32
5
votes
3 answers

Floyd-Warshall visualisation suggestions?

I'm after some ideas for demonstrating the usefulness of Floyd-Warshall visually. So far all I can think of is generating a random graph, allowing the user to select a start/finish and highlight the shortest path. What are some more fun yet simple…
Timothy Pratley
  • 10,586
  • 3
  • 34
  • 63
5
votes
1 answer

patterns possible on 3x3 matrix of numbers

Possible Duplicate: android lock password combinations Respected sir, I came across a question which asked for finding all the unique pattern possible given a 3x3 matrix with numbers from 1-9. which is same as android lock screen. Can you help me…
user1502308
  • 87
  • 1
  • 2
  • 9
4
votes
1 answer

Simple Floyd-Warshall Algorithm Java Implementation doesn't seem to work?

I've been trying to implement the Floyd-Warshall Algorithm in Java without using the "three for-loop-nested" way, but I can't seem to figure out where I've gone wrong in the code. This is the map that shows how my vertices are connected. The white…
ngserdna
  • 41
  • 1
4
votes
1 answer

Floyd-Warshall algorithm with loops?

I'm making an implementation of the Floyd-Warshall algorithm, and I have one question: If I have a loop in my graph (I mean, the cost of going from A to A is 1), what should the algorithm output, 0 (because the cost of going from any node to the…
4
votes
3 answers

Floyd Warshall algorithm with maximum steps allowed

Is it possible to modify Floyd-Warshall algorithm when solving the shortest path problem for a directed weighted graph with n nodes, such that each shortest path have no more than m steps? More precisely, for each pair of nodes i and j, you are…
4
votes
1 answer

Floyd/Warshall Algorithm mod to find cheapest path at max length k

I'm editing Floyd's algorithm so instead of each Dk where k is the highest intermediate vertex, k is the max path length. Eventually it will have the same output as Floyd's, but every subiteration could be different. For instance, if there are 4…
gooberdope
  • 75
  • 1
  • 3
  • 13
3
votes
2 answers

Does rearranging the outerloop in Floyd-Warshall algorithm as most inner loop change the algorithm

The following code is for Floyd-Warshall algorithm for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } I propose to rewrite…
Andy Lee
  • 31
  • 2
3
votes
1 answer

adjacency matrix/Floyd/Warshall in lisp

Apparently my teacher believes that even if we don't have time to learn stuff (nor enough examples) we should move on, so I now need to know how to make Floyd-Warshall's and Warshall's algorithms in clisp. As I did with prolog, my problem is to…
Kirby
  • 455
  • 6
  • 23
1
2
3
13 14