This class implements Karp's algorithm for finding a directed cycle of minimum mean cost in a digraph [AMO93], [DG98]. It runs in time O(ne) and uses space O(n2+e).
GR | The type of the digraph the algorithm runs on. |
CM | The type of the cost map. The default map type is GR::ArcMap<int>. |
TR | The traits class that defines various types used by the algorithm. By default, it is KarpMmcDefaultTraits<GR, CM>. In most cases, this parameter should not be set directly, consider to use the named template parameters instead. |
#include <lemon/karp_mmc.h>
Classes | |
struct | SetLargeCost |
Named parameter for setting LargeCost type. More... | |
struct | SetPath |
Named parameter for setting Path type. More... | |
Public Types | |
typedef TR::Digraph | Digraph |
The type of the digraph. | |
typedef TR::CostMap | CostMap |
The type of the cost map. | |
typedef TR::Cost | Cost |
The type of the arc costs. | |
typedef TR::LargeCost | LargeCost |
The large cost type. | |
typedef TR::Tolerance | Tolerance |
The tolerance type. | |
typedef TR::Path | Path |
The path type of the found cycles. | |
typedef TR | Traits |
The traits class of the algorithm. | |
Public Member Functions | |
KarpMmc (const Digraph &digraph, const CostMap &cost) | |
Constructor. | |
~KarpMmc () | |
Destructor. | |
KarpMmc & | cycle (Path &path) |
Set the path structure for storing the found cycle. | |
KarpMmc & | tolerance (const Tolerance &tolerance) |
Set the tolerance used by the algorithm. | |
const Tolerance & | tolerance () const |
Return a const reference to the tolerance. | |
Execution control | |
The simplest way to execute the algorithm is to call the run() function. | |
bool | run () |
Run the algorithm. | |
bool | findCycleMean () |
Find the minimum cycle mean. | |
bool | findCycle () |
Find a minimum mean directed cycle. | |
Query Functions | |
The results of the algorithm can be obtained using these functions. | |
Cost | cycleCost () const |
Return the total cost of the found cycle. | |
int | cycleSize () const |
Return the number of arcs on the found cycle. | |
double | cycleMean () const |
Return the mean cost of the found cycle. | |
const Path & | cycle () const |
Return the found cycle. |
typedef TR::LargeCost LargeCost |
The large cost type used for internal computations. By default, it is long
long
if the Cost
type is integer, otherwise it is double
.
typedef TR::Path Path |
The path type of the found cycles. Using the default traits class, it is Path<Digraph>.
The constructor of the class.
digraph | The digraph the algorithm runs on. |
cost | The costs of the arcs. |
This function sets an external path structure for storing the found cycle.
If you don't call this function before calling run() or findCycleMean(), it will allocate a local path structure. The destuctor deallocates this automatically allocated object, of course.
(*this)
This function sets the tolerance object used by the algorithm.
(*this)
const Tolerance& tolerance | ( | ) | const [inline] |
This function returns a const reference to the tolerance object used by the algorithm.
bool run | ( | ) | [inline] |
This function runs the algorithm. It can be called more than once (e.g. if the underlying digraph and/or the arc costs have been modified).
true
if a directed cycle exists in the digraph.mmc.run()
is just a shortcut of the following code. return mmc.findCycleMean() && mmc.findCycle();
bool findCycleMean | ( | ) | [inline] |
This function finds the minimum mean cost of the directed cycles in the digraph.
true
if a directed cycle exists in the digraph. bool findCycle | ( | ) | [inline] |
This function finds a directed cycle of minimum mean cost in the digraph using the data computed by findCycleMean().
true
if a directed cycle exists in the digraph.Cost cycleCost | ( | ) | const [inline] |
This function returns the total cost of the found cycle.
int cycleSize | ( | ) | const [inline] |
This function returns the number of arcs on the found cycle.
double cycleMean | ( | ) | const [inline] |
This function returns the mean cost of the found cycle.
alg.cycleMean()
is just a shortcut of the following code. return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
const Path& cycle | ( | ) | const [inline] |
This function returns a const reference to the path structure storing the found cycle.