Added the function isFinite(), and replaced the calls to finite() with it.
This was necessary because finite() is not a standard function. Neither can
we use its standard counterpart isfinite(), because it was introduced only
in C99, and therefore it is not supplied by all C++ implementations.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 \page algorithms Algorithms
23 \section algo_bfs_dfs Bfs/Dfs
24 Both \ref lemon::Bfs "Bfs" and \ref lemon::Dfs "Dfs" are highly adaptable and efficient
25 implementations of the well known algorithms. The algorithms are placed most cases in
26 separated files named after the algorithm itself but lower case as all other header file names.
27 For example the next Bfs class is in the \c lemon/bfs.h.
30 The algorithm is implemented in the \ref lemon::Bfs "Bfs" template class - rather than as function.
31 The class has two template parameters: \b GR and \b TR.<br>
32 GR is the graph the algorithm runs on. It has \ref lemon::ListGraph "ListGraph" as default type.
33 TR is a Traits class commonly used to easy the parametrization of templates. In most cases you
34 wont need to modify the default type \ref lemon::BfsDefaultTraits "BfsDefaultTraits<GR>".
36 To use the class, declare it!
38 Bfs<ListUGraph> bfs(gr);
40 Note the lack of second template argument because of the default parameter.
42 It provides a simple but powerful interface to control the execution.
44 int dist = bfs.run(s,t);
46 It finds the shortest path from node \c s to node \c t and returns it, or zero
47 if there is no path from \c s to \c t.<br>
48 If you want the shortest path from a specified node to all other node, just write:
52 Now the distances and path information are stored in maps which you can access with
53 member functions like \ref lemon::Bfs::distMap "distMap()" or \ref lemon::Bfs::predMap "predMap()".<br>
54 Or more directly with other member functions like \ref lemon::Bfs::predNode "predNode()". Once the algorithm
55 is finished (or to be precise reached that node) \ref lemon::Bfs::dist "dist()" or \ref lemon::Bfs::predNode
56 "predNode()" can be called.
58 For an example let's say we want to print the shortest path of those nodes which
59 are in a certain distance.
63 for( ListUGraph::NodeIt n(gr); n != INVALID; ++n ) {
64 if( bfs.reached(n) && bfs.dist(n) <= max_dist ) {
65 std::cout << gr.id(n);
67 Node prev = bfs.prevNode(n);
68 while( prev != INVALID ) {
69 std::cout << "<-" << gr.id(prev);
70 prev = bfs.prevNode(n);
73 std::cout << std::endl;
78 \subsubsection bfs_adv_control Advanced control
79 In the previous code we only used \c run(). Now we introduce the way you can directly
80 control the execution of the algorithm.
82 First you have to initialize the variables with \ref lemon::Bfs::init "init()".
87 Then you add one or more source nodes to the queue. They will be processed, as they would
88 be reached by the algorithm before. And yes - you can add more sources during the execution.
90 bfs.addSource(node_1);
91 bfs.addSource(node_2);
95 And finally you can start the process with \ref lemon::Bfs::start "start()", or
96 you can write your own loop to process the nodes one-by-one.
98 \todo Demo for Bfs advanced control.
101 Since Dfs is very similar to Bfs with a few tiny differences we only see a bit more complex example
102 to demonstrate Dfs's capabilities.
104 We will see a program, which solves the problem of <b>topological ordering</b>.
105 We need to know in which order we should put on our clothes. The program will do the following:
107 <li>We run the dfs algorithm to all nodes.
108 <li>Put every node into a list when processed completely.
109 <li>Write out the list in reverse order.
112 \dontinclude topological_ordering.cc
113 First of all we will need an own \ref lemon::Dfs::ProcessedMap "ProcessedMap". The ordering
114 will be done through it.
117 The class meets the \ref concepts::WriteMap "WriteMap" concept. In it's \c set() method the only thing
118 we need to do is insert the key - that is the node whose processing just finished - into the beginning
120 Although we implemented this needed helper class ourselves it was not necessary.
121 The \ref lemon::FrontInserterBoolMap "FrontInserterBoolMap" class does exactly
122 what we needed. To be correct it's more general - and it's all in \c LEMON. But
123 we wanted to show you, how easy is to add additional functionality.
125 First we declare the needed data structures: the graph and a map to store the nodes' label.
129 Now we build a graph. But keep in mind that it must be DAG because cyclic graphs has no topological
136 Then add directed edges which represent the precedences between those items.
140 See how easy is to access the internal information of this algorithm trough maps.
141 We only need to set our own map as the class's \ref lemon::Dfs::ProcessedMap "ProcessedMap".
145 And now comes the third part. Write out the list in reverse order. But the list was
146 composed in reverse way (with \c push_front() instead of \c push_back() so we just iterate it.
150 The program is to be found in the \ref demo directory: \ref topological_ordering.cc
152 \todo Check the linking of the demo file, the code samples are missing.
154 More algorithms are described in the \ref algorithms2 "second part".