2

I have to solve the following problem: Write a program that, given a directed graph with costs and two vertices, finds a lowest cost walk between the given vertices, or prints a message if there are negative cost cycles in the graph. The program shall use the matrix multiplication algorithm.

I implemented the matrix multiplication algorithm as it is defined: a pseudo-matrix multiplication, where addition is replaced by minimization and multiplication with addition. But by doing this, I ended up with the Floyd-Warshall algorithm Also, I can't easily determine the existence of a negative-cost cycle this way.

I assume there is a major difference between my algorithm, and the real matrix multiplication graph algorithm, but what is that exactly?

Imi Mali
  • 23
  • 1
  • 5
  • [get_student](https://stackoverflow.com/users/9011833/get-student) asked, as an answer: "Would you mind sharing the algorithm if you still have it somewhere? I got confused about the part with retrieving the path using the auxiliary matrix you were talking about in the comments." – Paul Roub May 01 '18 at 17:19
  • 1
    you can find it here:https://github.com/imimali/Graph-algorithms/tree/master/practicalWork3/practicalWork3 If something is not clear there, feel free to contact me :) – Imi Mali May 02 '18 at 18:14

1 Answers1

1
  1. You can determine the existence of negative cycles with Floyd-Warshall:

https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Behavior_with_negative_cycles

Nevertheless, if there are negative cycles, the Floyd–Warshall algorithm can be used to detect them. The intuition is as follows:

  • The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices (i,j), including where i=j;
  • Initially, the length of the path (i,i) is zero;
  • A path [i,k, ... ,i] can only improve upon this if it has length less than zero, i.e. denotes a negative cycle;
  • Thus, after the algorithm, (i,i) will be negative if there exists a negative-length path from i back to i.
  1. Some differences between two algorithms:

    • Matrix algo can find minimal path with specific number of edges (for example, to find minimal pathes between all pairs of vertices with number of edges <= k), FW cannot.

    • Matrix multiplication algorithm requires O(n^2) additional space, Floyd-Warshall can be used in-place.

    • Matrix multiplication algorithm has O(n^3*log(n)) complexity with repeated squaring or O(n^4) with simple implementation, Floyd-Warshall complexity is O(n^3)

DAle
  • 8,990
  • 2
  • 26
  • 45
  • That's indeed a nice thing about the Floyd-Warshall, I was just insecure about it because I still don't know why was I told to use, "the matrix multiplication graph algorithm", instead of "Floyd-Warshall", because I see no difference between the two. Huge thanks anyway. – Imi Mali May 01 '17 at 13:31
  • @ImiMali, I've added some differences between these two algorithms in the answer. – DAle May 01 '17 at 13:52
  • thank you very much, I clearly understand now what the differences are, just the similarity of the ideas confused me a bit. In the end, I implemented the matrix multiplication algorithm with repeated squaring, and works fine. So thank you again. – Imi Mali May 02 '17 at 15:03
  • also, I found a way to reconstruct the involved paths too, by using another matrix that contained lists of edges, and the paths were reconstructed by concatenating these lists at each multiplication. – Imi Mali May 02 '17 at 15:13
  • 1
    I know exactly what's the difference between the matr. multiplication algorithm and Floyd-Warshall now. The one thing I didn't see was that Floyd uses the SAME matrix and updates it in each step, while matrix multiplication uses another, and squares it over and over again.This way, Floyd will be done after only one "multiplication", while the other has to square the result log(n) times, because it was not updated by time. That was what I was looking for from the start.Though I made my program work a month ago too, this was the one thing I was looking for, as a difference – Imi Mali Jun 03 '17 at 09:33
  • Also, matrix multiplication can find the shortest path with negative weight cycles just fine, for paths bounded to a specific length. It'll also let you detect negative weight cycles looking at the diagonal. While Floyd-Warshall doesn't have a built in bound for number of paths. Also, the matrix multiplication method is arguably easier and more intuitive to understand. – saolof Mar 08 '21 at 09:03