fn() floydWarshallAlgorithm
Finds shortest paths between all pairs of vertices in a graph.

Defined in <seqan/graph_algorithms.h>
Signature void floydWarshallAlgorithm(g, weight, distance, predecessor);

Parameters

predecessor A matrix with predecessors. Entry (i,j) in this matrix indicates the predecessor of j on a shortest path from vertex i to vertex j. You can use _printAllPairsShortestPath(g, predecessor, i, j) to print the shortest path from i to j. Types: Matrix
distance A matrix with distances.Entry (i,j) in this matrix indicates the distance from vertex i to vertex j. Types: Matrix
weight A weight map. A property map with edge weights. Edge weights may be negative.
g A directed graph. Types: Directed Graph

Detailed Description

The graph must be free of negative-weight cycles.

Example

#include <iostream>
#include <seqan/graph_algorithms.h>

using namespace seqan;

int main()
{
    typedef Graph<Directed<> > TGraph;
    typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
    typedef Size<TGraph>::Type TSize;

    // Create graph with 9 directed edges (0,1), (0,2)
    TSize numEdges = 9;
    TVertexDescriptor edges[] = {0, 1, 0, 2, 0, 4, 1, 3, 1, 4, 2, 1, 3, 0, 3, 2, 4, 3};
    TGraph g;
    addEdges(g, edges, numEdges);
    // Print graph.
    std::cout << g << "\n";

    // Fill external property map with edge weights and assign to graph.
    int weights[] = {3, 8, -4, 1, 7, 4, 2, -5, 6};
    String<int> weightMap;
    assignEdgeMap(weightMap, g, weights);

    // Run Floyd-Warshall algorithm.
    String<int> distMat;
    String<TVertexDescriptor> predMat;
    floydWarshallAlgorithm(distMat, predMat, g, weightMap);

    // Print result to stdout.
    unsigned int len = static_cast<unsigned>(std::sqrt((double)length(distMat)));
    for (TSize row = 0; row < len; ++row)
        for (TSize col = 0; col < len; ++col)
        {
            std::cout << row << "," << col << " (Distance="
                      << getValue(distMat, row * len + col) << "): ";
            _printAllPairsShortestPath(g, predMat, row, col);
            std::cout << std::endl;
        }

    return 0;
}
Adjacency list:
0 -> 4,2,1,
1 -> 4,3,
2 -> 1,
3 -> 2,0,
4 -> 3,
Edge list:
Source: 0,Target: 4 (Id: 2)
Source: 0,Target: 2 (Id: 1)
Source: 0,Target: 1 (Id: 0)
Source: 1,Target: 4 (Id: 4)
Source: 1,Target: 3 (Id: 3)
Source: 2,Target: 1 (Id: 5)
Source: 3,Target: 2 (Id: 7)
Source: 3,Target: 0 (Id: 6)
Source: 4,Target: 3 (Id: 8)

0,0 (Distance=0): 0
0,1 (Distance=1): 0,4,3,2,1
0,2 (Distance=-3): 0,4,3,2
0,3 (Distance=2): 0,4,3
0,4 (Distance=-4): 0,4
1,0 (Distance=3): 1,3,0
1,1 (Distance=0): 1
1,2 (Distance=-4): 1,3,2
1,3 (Distance=1): 1,3
1,4 (Distance=-1): 1,3,0,4
2,0 (Distance=7): 2,1,3,0
2,1 (Distance=4): 2,1
2,2 (Distance=0): 2
2,3 (Distance=5): 2,1,3
2,4 (Distance=3): 2,1,3,0,4
3,0 (Distance=2): 3,0
3,1 (Distance=-1): 3,2,1
3,2 (Distance=-5): 3,2
3,3 (Distance=0): 3
3,4 (Distance=-2): 3,0,4
4,0 (Distance=8): 4,3,0
4,1 (Distance=5): 4,3,2,1
4,2 (Distance=1): 4,3,2
4,3 (Distance=6): 4,3
4,4 (Distance=0): 4

Data Races

Thread safety unknown!

See Also