| Example Program Topological Sort Topological sort of a graph. File "graph_algo_topsort.cpp" This code example illustrates the topological sort algorithm
1 | #include <seqan/graph.h>
2 | #include <iostream>
3 |
4 | using namespace seqan;
5 |
6 |
7 | int main() {
8 | typedef Graph<Directed<> > TGraph;
9 | typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
10 | typedef EdgeDescriptor<TGraph>::Type TEdgeDescriptor;
11 | typedef Size<TGraph>::Type TSize;
Graph creation: 9 directed edges (0,3), (0,1), ...
12 | TSize numEdges = 9;
13 | TVertexDescriptor edges[] = {0,3, 0,1, 1,2, 3,2, 5,7, 5,6, 6,7, 6,3, 8,7};
14 | TGraph g;
15 | addEdges(g, edges, numEdges);
16 | std::cout << g << ::std::endl;
One external property map: Vertex names
17 | String<std::string> nameMap;
18 | std::string names[] = {"shirt", "tie", "jacket", "belt", "watch", "undershorts", "pants", "shoes", "socks"};
19 | resizeVertexMap(g,nameMap, names);
Out-parameter: Order of vertices
20 | String<TVertexDescriptor> order;
Topological sort
21 | topological_sort(g, order);
Console output
22 | std::cout << "Topological sort: " << ::std::endl;
23 | typedef Iterator<String<TVertexDescriptor> >::Type TStringIterator;
24 | TStringIterator it = begin(order);
25 | TStringIterator itEnd = end(order);
26 | while(it != itEnd) {
27 | std::cout << getProperty(nameMap, getValue(it)) << ",";
28 | goNext(it);
29 | }
30 | std::cout << ::std::endl;
31 | return 0;
32 | }
See Graph, topological_sort SeqAn - Sequence Analysis Library - www.seqan.de