| Example Program All Pairs Shortest Path Computing all-pairs shortest paths. File "graph_algo_path_allpairs.cpp" This code example illustrates an all-pair shortest path algorithm
1 | #include <seqan/graph.h>
2 | #include <iostream>
3 |
4 | using namespace seqan;
5 |
6 | int main() {
7 | typedef Graph<Directed<> > TGraph;
8 | typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
9 | typedef EdgeDescriptor<TGraph>::Type TEdgeDescriptor;
10 | typedef Size<TGraph>::Type TSize;
Graph creation: 9 directed edges (0,1), (0,2), ...
11 | TSize numEdges = 9;
12 | TVertexDescriptor edges[] = {0,1, 0,2, 0,4, 1,3, 1,4, 2,1, 3,0, 3,2, 4,3};
13 | TGraph g;
14 | addEdges(g, edges, numEdges);
15 | std::cout << g << ::std::endl;
One external property map: Weight map
16 | int weights[] = {3, 8, -4, 1, 7, 4, 2, -5, 6};
17 | String<int> weightMap;
18 | resizeEdgeMap(g,weightMap, weights);
Out-parameters: Predecessor and distance matrices
19 | Matrix<int> distMat;
20 | Matrix<TVertexDescriptor> predMat;
All-Pairs shortest path
21 | all_pairs_shortest_path(g,weightMap, distMat, predMat);
Console Output
22 | unsigned int len = length(distMat, 0);
23 | for (TSize row=0;row < len;++row) {
24 | for (TSize col=0;col < len;++col) {
25 | std::cout << row << "," << col << " (Distance=" << getValue(distMat, row*len + col) << "): ";
26 | _print_all_pairs_shortest_path(g,predMat,row,col);
27 | std::cout << ::std::endl;
28 | }
29 | }
30 | return 0;
31 | }
See all_pairs_shortest_path, Graph SeqAn - Sequence Analysis Library - www.seqan.de