Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating reachability matrix from a given adjacency matrix

What is the best algorithm for generating a reachability matrix from a given adjacency matrix. There is warshall's algorithm but it is not the best method. There are some other methods but the procedures are more theoretical. Is there any module or with which I can create a reachability matrix with ease. I am working on python 2.7.

like image 225
Debdipta Halder Avatar asked May 12 '16 15:05

Debdipta Halder


1 Answers

I don't think there is a way to do it faster than O(n³) in general case for directed graph.

That being said, you can try to use clever technics to reduce the constant.

For example, you can do the following:

  1. Convert your graph into DAG by finding all strongly connected components and replacing them with a single vertex. This can be done in Θ(V + E) or Θ(V²)

  2. On the new graph, run DFS to calculate reachability for all vertices, but, when updating reachability set for a vertex, do it in the fast vectorized way. This is technically Θ( (V + E) * V ) or Θ(V³), but the constant will be low (see below).

The proposed vectorized way is to have the reachability set for every vertex represented as the bit vector, residing on GPU. This way, the calculation of the union of two sets is performed in extremely fast parallelized manner on GPU. You can use any tensor library for GPU, for example, tf.bitwise.

After you calculated reachability bit vectors for every vertex on GPU, you can extract them into CPU memory in Θ(V²) time.

like image 83
Aivean Avatar answered Oct 21 '22 04:10

Aivean