2

I have a graph like this:

enter image description here

And I implemented graph array like this:

G[i][j][k]

Khas only 4 cells and it shows whether the vertex is connected to its four neighbor vertices or not. For example for:

G[1][1][0] = 0 
G[1][1][1] = 1 
G[1][1][2] = 1 
G[1][1][3] = 0

It shows that the vertex(1, 1) is connected to 2 vertices.

I know the Floyd Warshall algorithm for normal types of graphs. But how can I implement Flyod Warshall Algorithm for this kind of graph?

Thank.

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
Sky
  • 4,244
  • 7
  • 54
  • 83
  • Why is this a different 'type' of graph. It's surely just an unweighted graph without multiple edges (a simple graph). There could even be simpler or faster algorithms than Floyd-Warshall for shortest path calculation. In fact, there definitely are - it's planar, for a start. – gilleain Jul 02 '15 at 08:26
  • @gilleain is right. What's different is not the graph but rather the data structure used to represent the adjacency relations – Simon Jul 02 '15 at 08:28
  • @gilleain [This type of graph](http://mathworld.wolfram.com/GridGraph.html) is called a [grid graph](https://en.wikipedia.org/wiki/Lattice_graph), and they are often far easier handled than general graphs. – G. Bach Jul 02 '15 at 09:20
  • @G.Bach Are there algorithms for finding the shortest path that are faster for grid graphs than arbitrary planar graphs? It seems possible. Not sure it really matters having such a specific classification of the input graph, however. – gilleain Jul 02 '15 at 10:33
  • 1
    @gilleain Seems unlikely asymptotically speaking, given that there's a linear-time algorithm for planar graphs that solves the same problem as Dijkstra's algorithm. – David Eisenstat Jul 02 '15 at 12:22
  • 1
    [Link to the algorithm David mentioned](http://theory.stanford.edu/~virgi/cs267/papers/planar-sssp.pdf), for the theoretically inclined. – G. Bach Jul 02 '15 at 14:27

2 Answers2

5

Floyd-Warshall algorithm would be very inefficient for such a sparse graph. The graph is sparse because every vertex connected to no more than 4 other vertices. In a dense graph a vertex can be connected to up to N-1 other vertices, where N is the number of vertices in the graph. That is where Floyd-Warshall algorithm would be more or less efficient, but still, if you don't need shortest paths between each pair of vertices or finding negative-length cycles, consider using priority queue for finding the shortest path between a source and all other vertices: https://en.wikipedia.org/wiki/Dijkstra's_algorithm#Using_a_priority_queue . Or even breadth first search can be used if the weights in your graph are equal for each edge (unweighted graph).

If you still want Floyd-Warshall algorithm for your grid, here it is. Consider the grid is N by M, with 1-based indexing so that the maximal entry in the grid is G[N][M][...]. Then Floyd-Warshall algorithm would be:

// edge offsets
const int offs[4][2] = {
    {-1, 0}, {0, 1}, {1, 0}, {0, -1}
};
const int INF_DIST = 1e9;
int D[N+1][M+1][N+1][M+1];
//// Initialize weights to infinity
// For each source row and column (i,j)
for(int i=1; i<=N; i++) {
    for(int j=1; j<=M; j++) {
        // For each destination row and column (k,l)
        for(int k=1; k<=N; k++) {
            for(int l=1; l<=M; l++) {
                D[i][j][k][l] = INF_DIST;
            }
        }
    }
}
//// Mark edges of the graph
// For each row
for(int i=1; i<=N; i++) {
    // For each column
    for(int j=1; j<=M; j++) {
        // For each of the directions: up(k=0), right(k=1), down(k=2) and left(k=3)
        for(int k=0; k<=3; k++) {
            if(G[i][j][k] == 0) {
                // Don't add this edge to the distance matrix
                //   if the edge is not in the grid graph
                continue;
            }
            // Calculate (r, c) as the coordinates of the vertex one step 
            //   in the direction k
            int r = i + offs[k][0];
            int c = j + offs[k][1];
            if(1<=r && r <= N && 1<=c && c<=M) {
                // Only add the edge (if exists) in case (r, c) is within the grid
                D[i][j][r][c] = G[i][j][k];
            }
        }
    }
}
//// Find shortest paths between each pair of vertices
// For each intermediate vertex (k,l)
for(k=1; k<=N; k++) {
    for(l=1; l<=M; l++) {
        // For each source vertex (i,j)
        for(int i=1; i<=N; i++) {
            for(int j=1; j<=M; j++) {
                // For each destination vertex (r,c)
                for(int r=1; r<=N; r++) {
                    for(int c=1; c<=M; c++) {
                        // Apply the triangle rule
                        int alternative = D[i][j][k][l] + D[k][l][r][c];
                        if(alternative < D[i][j][r][c]) {
                            D[i][j][r][c] = alternative;
                        }
                    }
                }
            }
        }
    }
}
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
3

Your graph representation is basically an adjacency list, for each vertex v= G[i][j], you have a list containing the edges the graph is connected to. In your case, the list is made by 4 boolean values - each is indicating if the (i,j) is connected to (i-1,j),(i+1,j),(i,j-1),(i,j+1), so using Floyd-Warshall algorithm with that understanding is pretty straight forward, if looking on the wikipedia pseudo code:

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 (u,v)
6 for k from 1 to |V|
7    for i from 1 to |V|
8       for j from 1 to |V|
9          if dist[i][j] > dist[i][k] + dist[k][j] 
10             dist[i][j] ← dist[i][k] + dist[k][j]
11         end if

The main difference is in lines 4-5, where:

for each edge(u,v):

is actually

for each x=0,1,...,n-1
   for each y=0,1,...,m-1
       for each i=0,1,2,3:
             //if G[x][y][y] == 1 : it's an edge

Also note, in your graph, the maximum branch factor (number of edges connected to a node) is 4. This means, maximum number of edges in the graph is |E| <= 4|V|.
Since your graph is not directed, finding all-to-all shortest path can be done more efficiently by doing a BFS from each node, it will take O(|V|*(|E|+|V|)) time, but since |E| <= 4|V|, this is O(|V|^2) - compared to Floyd-Warshall which runs in O(|V|^3).

amit
  • 175,853
  • 27
  • 231
  • 333