| Example Program Depth-First Search Depth-first search through a graph. File "graph_algo_dfs.cpp" A tutorial about depth-first search.
1 | #include <iostream>
2 | #include <seqan/graph_algorithms.h>
3 |
4 | using namespace seqan;
5 |
6 |
7 | int main()
8 | {
9 | typedef Graph<Directed<> > TGraph;
10 | typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
11 | typedef EdgeDescriptor<TGraph>::Type TEdgeDescriptor;
12 | typedef Size<TGraph>::Type TSize;
Graph creation: 8 directed edges (0,3), (0,1), ...
13 | TSize numEdges = 8;
14 | TVertexDescriptor edges[] = {0,3, 0,1, 1,4, 2,4, 2,5, 3,1, 4,3, 5,5};
15 | TGraph g;
16 | addEdges(g, edges, numEdges);
17 | ::std::cout << g << ::std::endl;
One external property map: Vertex names
18 | char names[] = {'u', 'v', 'w', 'x', 'y', 'z'};
19 | String<char> nameMap;
20 | resizeVertexMap(g,nameMap, names);
Out-parameters: Predecessor and discovery maps
21 | String<unsigned int> predMap;
22 | String<unsigned int> discoveryTimeMap;
23 | String<unsigned int> finishingTimeMap;
Depth-frist search
24 | depth_first_search(g, predMap, discoveryTimeMap, finishingTimeMap);
Console output
25 | ::std::cout << "Depth-First search: " << ::std::endl;
26 | typedef Iterator<Graph<>, VertexIterator>::Type TVertexIterator;
27 | TVertexIterator it(g);
28 | while(!atEnd(it)) {
29 | ::std::cout << "Vertex " << getProperty(nameMap, getValue(it)) << ": ";
30 | ::std::cout << "Discovery time = " << getProperty(discoveryTimeMap, getValue(it)) << ",";
31 | ::std::cout << "Finishing time = " << getProperty(finishingTimeMap, getValue(it)) << ",";
32 | typedef Value<String<unsigned int> >::Type TPredVal;
33 | TPredVal pre = getProperty(predMap, getValue(it));
34 | if (pre != getNil<TVertexDescriptor>()) {
35 | ::std::cout << "Predecessor = " << getProperty(nameMap, pre) << ::std::endl;
36 | } else {
37 | ::std::cout << "Predecessor = nil" << ::std::endl;
38 | }
39 | goNext(it);
40 | }
41 | return 0;
42 | }
See depth_first_search, Graph SeqAn - Sequence Analysis Library - www.seqan.de