0
7
0
143
105
145
107
127
99
28
11
26
6
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 |
#include <lemon/path.h> |
|
31 | 32 |
|
32 | 33 |
namespace lemon { |
33 | 34 |
|
34 | 35 |
///Default traits class of Bfs class. |
35 | 36 |
|
36 | 37 |
///Default traits class of Bfs class. |
37 | 38 |
///\tparam GR Digraph type. |
38 | 39 |
template<class GR> |
39 | 40 |
struct BfsDefaultTraits |
40 | 41 |
{ |
41 | 42 |
///The type of the digraph the algorithm runs on. |
42 | 43 |
typedef GR Digraph; |
43 | 44 |
|
44 | 45 |
///\brief The type of the map that stores the predecessor |
45 | 46 |
///arcs of the shortest paths. |
46 | 47 |
/// |
47 | 48 |
///The type of the map that stores the predecessor |
48 | 49 |
///arcs of the shortest paths. |
49 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
50 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
51 | 52 |
///Instantiates a \ref PredMap. |
52 | 53 |
|
53 | 54 |
///This function instantiates a \ref PredMap. |
54 | 55 |
///\param g is the digraph, to which we would like to define the |
55 | 56 |
///\ref PredMap. |
56 | 57 |
static PredMap *createPredMap(const Digraph &g) |
57 | 58 |
{ |
58 | 59 |
return new PredMap(g); |
59 | 60 |
} |
60 | 61 |
|
61 | 62 |
///The type of the map that indicates which nodes are processed. |
62 | 63 |
|
... | ... |
@@ -84,65 +85,65 @@ |
84 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
85 | 86 |
///Instantiates a \ref ReachedMap. |
86 | 87 |
|
87 | 88 |
///This function instantiates a \ref ReachedMap. |
88 | 89 |
///\param g is the digraph, to which |
89 | 90 |
///we would like to define the \ref ReachedMap. |
90 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
91 | 92 |
{ |
92 | 93 |
return new ReachedMap(g); |
93 | 94 |
} |
94 | 95 |
|
95 | 96 |
///The type of the map that stores the distances of the nodes. |
96 | 97 |
|
97 | 98 |
///The type of the map that stores the distances of the nodes. |
98 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
99 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
100 | 101 |
///Instantiates a \ref DistMap. |
101 | 102 |
|
102 | 103 |
///This function instantiates a \ref DistMap. |
103 | 104 |
///\param g is the digraph, to which we would like to define the |
104 | 105 |
///\ref DistMap. |
105 | 106 |
static DistMap *createDistMap(const Digraph &g) |
106 | 107 |
{ |
107 | 108 |
return new DistMap(g); |
108 | 109 |
} |
109 | 110 |
}; |
110 | 111 |
|
111 | 112 |
///%BFS algorithm class. |
112 | 113 |
|
113 | 114 |
///\ingroup search |
114 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
115 | 116 |
/// |
116 |
///There is also a \ref bfs() "function |
|
117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
|
117 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
118 | 119 |
///used easier. |
119 | 120 |
/// |
120 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
121 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
122 | 123 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
123 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
124 | 125 |
///The default traits class is |
125 | 126 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
126 | 127 |
///See \ref BfsDefaultTraits for the documentation of |
127 | 128 |
///a Bfs traits class. |
128 | 129 |
#ifdef DOXYGEN |
129 | 130 |
template <typename GR, |
130 | 131 |
typename TR> |
131 | 132 |
#else |
132 | 133 |
template <typename GR=ListDigraph, |
133 | 134 |
typename TR=BfsDefaultTraits<GR> > |
134 | 135 |
#endif |
135 | 136 |
class Bfs { |
136 | 137 |
public: |
137 | 138 |
///\ref Exception for uninitialized parameters. |
138 | 139 |
|
139 | 140 |
///This error represents problems in the initialization of the |
140 | 141 |
///parameters of the algorithm. |
141 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
142 | 143 |
public: |
143 | 144 |
virtual const char* what() const throw() { |
144 | 145 |
return "lemon::Bfs::UninitializedParameter"; |
145 | 146 |
} |
146 | 147 |
}; |
147 | 148 |
|
148 | 149 |
///The type of the digraph the algorithm runs on. |
... | ... |
@@ -809,381 +810,418 @@ |
809 | 810 |
///arcs, which form the shortest path tree. |
810 | 811 |
/// |
811 | 812 |
///\pre Either \ref run() or \ref init() |
812 | 813 |
///must be called before using this function. |
813 | 814 |
const PredMap &predMap() const { return *_pred;} |
814 | 815 |
|
815 | 816 |
///Checks if a node is reachable from the root(s). |
816 | 817 |
|
817 | 818 |
///Returns \c true if \c v is reachable from the root(s). |
818 | 819 |
///\pre Either \ref run() or \ref start() |
819 | 820 |
///must be called before using this function. |
820 | 821 |
bool reached(Node v) const { return (*_reached)[v]; } |
821 | 822 |
|
822 | 823 |
///@} |
823 | 824 |
}; |
824 | 825 |
|
825 | 826 |
///Default traits class of bfs() function. |
826 | 827 |
|
827 | 828 |
///Default traits class of bfs() function. |
828 | 829 |
///\tparam GR Digraph type. |
829 | 830 |
template<class GR> |
830 | 831 |
struct BfsWizardDefaultTraits |
831 | 832 |
{ |
832 | 833 |
///The type of the digraph the algorithm runs on. |
833 | 834 |
typedef GR Digraph; |
834 | 835 |
|
835 | 836 |
///\brief The type of the map that stores the predecessor |
836 | 837 |
///arcs of the shortest paths. |
837 | 838 |
/// |
838 | 839 |
///The type of the map that stores the predecessor |
839 | 840 |
///arcs of the shortest paths. |
840 | 841 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
841 |
typedef |
|
842 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
842 | 843 |
///Instantiates a \ref PredMap. |
843 | 844 |
|
844 | 845 |
///This function instantiates a \ref PredMap. |
845 | 846 |
///\param g is the digraph, to which we would like to define the |
846 | 847 |
///\ref PredMap. |
847 |
#ifdef DOXYGEN |
|
848 | 848 |
static PredMap *createPredMap(const Digraph &g) |
849 |
#else |
|
850 |
static PredMap *createPredMap(const Digraph &) |
|
851 |
#endif |
|
852 | 849 |
{ |
853 |
return new PredMap(); |
|
850 |
return new PredMap(g); |
|
854 | 851 |
} |
855 | 852 |
|
856 | 853 |
///The type of the map that indicates which nodes are processed. |
857 | 854 |
|
858 | 855 |
///The type of the map that indicates which nodes are processed. |
859 | 856 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
857 |
///By default it is a NullMap. |
|
860 | 858 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
861 | 859 |
///Instantiates a \ref ProcessedMap. |
862 | 860 |
|
863 | 861 |
///This function instantiates a \ref ProcessedMap. |
864 | 862 |
///\param g is the digraph, to which |
865 | 863 |
///we would like to define the \ref ProcessedMap. |
866 | 864 |
#ifdef DOXYGEN |
867 | 865 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
868 | 866 |
#else |
869 | 867 |
static ProcessedMap *createProcessedMap(const Digraph &) |
870 | 868 |
#endif |
871 | 869 |
{ |
872 | 870 |
return new ProcessedMap(); |
873 | 871 |
} |
874 | 872 |
|
875 | 873 |
///The type of the map that indicates which nodes are reached. |
876 | 874 |
|
877 | 875 |
///The type of the map that indicates which nodes are reached. |
878 | 876 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
879 | 877 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
880 | 878 |
///Instantiates a \ref ReachedMap. |
881 | 879 |
|
882 | 880 |
///This function instantiates a \ref ReachedMap. |
883 | 881 |
///\param g is the digraph, to which |
884 | 882 |
///we would like to define the \ref ReachedMap. |
885 | 883 |
static ReachedMap *createReachedMap(const Digraph &g) |
886 | 884 |
{ |
887 | 885 |
return new ReachedMap(g); |
888 | 886 |
} |
889 | 887 |
|
890 | 888 |
///The type of the map that stores the distances of the nodes. |
891 | 889 |
|
892 | 890 |
///The type of the map that stores the distances of the nodes. |
893 | 891 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
894 |
/// |
|
895 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
|
892 |
typedef typename Digraph::template NodeMap<int> DistMap; |
|
896 | 893 |
///Instantiates a \ref DistMap. |
897 | 894 |
|
898 | 895 |
///This function instantiates a \ref DistMap. |
899 | 896 |
///\param g is the digraph, to which we would like to define |
900 | 897 |
///the \ref DistMap |
901 |
#ifdef DOXYGEN |
|
902 | 898 |
static DistMap *createDistMap(const Digraph &g) |
903 |
#else |
|
904 |
static DistMap *createDistMap(const Digraph &) |
|
905 |
#endif |
|
906 | 899 |
{ |
907 |
return new DistMap(); |
|
900 |
return new DistMap(g); |
|
908 | 901 |
} |
902 |
|
|
903 |
///The type of the shortest paths. |
|
904 |
|
|
905 |
///The type of the shortest paths. |
|
906 |
///It must meet the \ref concepts::Path "Path" concept. |
|
907 |
typedef lemon::Path<Digraph> Path; |
|
909 | 908 |
}; |
910 | 909 |
|
911 | 910 |
/// Default traits class used by \ref BfsWizard |
912 | 911 |
|
913 | 912 |
/// To make it easier to use Bfs algorithm |
914 | 913 |
/// we have created a wizard class. |
915 | 914 |
/// This \ref BfsWizard class needs default traits, |
916 | 915 |
/// as well as the \ref Bfs class. |
917 | 916 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
918 | 917 |
/// \ref BfsWizard class. |
919 | 918 |
template<class GR> |
920 | 919 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
921 | 920 |
{ |
922 | 921 |
|
923 | 922 |
typedef BfsWizardDefaultTraits<GR> Base; |
924 | 923 |
protected: |
925 | 924 |
//The type of the nodes in the digraph. |
926 | 925 |
typedef typename Base::Digraph::Node Node; |
927 | 926 |
|
928 | 927 |
//Pointer to the digraph the algorithm runs on. |
929 | 928 |
void *_g; |
930 | 929 |
//Pointer to the map of reached nodes. |
931 | 930 |
void *_reached; |
932 | 931 |
//Pointer to the map of processed nodes. |
933 | 932 |
void *_processed; |
934 | 933 |
//Pointer to the map of predecessors arcs. |
935 | 934 |
void *_pred; |
936 | 935 |
//Pointer to the map of distances. |
937 | 936 |
void *_dist; |
938 |
//Pointer to the source node. |
|
939 |
Node _source; |
|
937 |
//Pointer to the shortest path to the target node. |
|
938 |
void *_path; |
|
939 |
//Pointer to the distance of the target node. |
|
940 |
int *_di; |
|
940 | 941 |
|
941 | 942 |
public: |
942 | 943 |
/// Constructor. |
943 | 944 |
|
944 | 945 |
/// This constructor does not require parameters, therefore it initiates |
945 |
/// all of the attributes to |
|
946 |
/// all of the attributes to \c 0. |
|
946 | 947 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
947 |
_dist(0), |
|
948 |
_dist(0), _path(0), _di(0) {} |
|
948 | 949 |
|
949 | 950 |
/// Constructor. |
950 | 951 |
|
951 |
/// This constructor requires some parameters, |
|
952 |
/// listed in the parameters list. |
|
953 |
/// |
|
952 |
/// This constructor requires one parameter, |
|
953 |
/// others are initiated to \c 0. |
|
954 | 954 |
/// \param g The digraph the algorithm runs on. |
955 |
/// \param s The source node. |
|
956 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
|
955 |
BfsWizardBase(const GR &g) : |
|
957 | 956 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
958 |
_reached(0), _processed(0), _pred(0), _dist(0), |
|
957 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
|
959 | 958 |
|
960 | 959 |
}; |
961 | 960 |
|
962 |
/// Auxiliary class for the function |
|
961 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
|
963 | 962 |
|
964 |
/// This auxiliary class is created to implement the function type |
|
965 |
/// interface of \ref Bfs algorithm. It uses the functions and features |
|
966 |
/// of the plain \ref Bfs, but it is much simpler to use it. |
|
967 |
/// It should only be used through the \ref bfs() function, which makes |
|
968 |
/// |
|
963 |
/// This auxiliary class is created to implement the |
|
964 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
|
965 |
/// It does not have own \ref run() method, it uses the functions |
|
966 |
/// and features of the plain \ref Bfs. |
|
969 | 967 |
/// |
970 |
/// Simplicity means that the way to change the types defined |
|
971 |
/// in the traits class is based on functions that returns the new class |
|
972 |
/// and not on templatable built-in classes. |
|
973 |
/// When using the plain \ref Bfs |
|
974 |
/// the new class with the modified type comes from |
|
975 |
/// the original class by using the :: |
|
976 |
/// operator. In the case of \ref BfsWizard only |
|
977 |
/// a function have to be called, and it will |
|
978 |
/// return the needed class. |
|
979 |
/// |
|
980 |
/// It does not have own \ref run() method. When its \ref run() method |
|
981 |
/// is called, it initiates a plain \ref Bfs object, and calls the |
|
982 |
/// \ref |
|
968 |
/// This class should only be used through the \ref bfs() function, |
|
969 |
/// which makes it easier to use the algorithm. |
|
983 | 970 |
template<class TR> |
984 | 971 |
class BfsWizard : public TR |
985 | 972 |
{ |
986 | 973 |
typedef TR Base; |
987 | 974 |
|
988 | 975 |
///The type of the digraph the algorithm runs on. |
989 | 976 |
typedef typename TR::Digraph Digraph; |
990 | 977 |
|
991 | 978 |
typedef typename Digraph::Node Node; |
992 | 979 |
typedef typename Digraph::NodeIt NodeIt; |
993 | 980 |
typedef typename Digraph::Arc Arc; |
994 | 981 |
typedef typename Digraph::OutArcIt OutArcIt; |
995 | 982 |
|
996 | 983 |
///\brief The type of the map that stores the predecessor |
997 | 984 |
///arcs of the shortest paths. |
998 | 985 |
typedef typename TR::PredMap PredMap; |
999 | 986 |
///\brief The type of the map that stores the distances of the nodes. |
1000 | 987 |
typedef typename TR::DistMap DistMap; |
1001 | 988 |
///\brief The type of the map that indicates which nodes are reached. |
1002 | 989 |
typedef typename TR::ReachedMap ReachedMap; |
1003 | 990 |
///\brief The type of the map that indicates which nodes are processed. |
1004 | 991 |
typedef typename TR::ProcessedMap ProcessedMap; |
992 |
///The type of the shortest paths |
|
993 |
typedef typename TR::Path Path; |
|
1005 | 994 |
|
1006 | 995 |
public: |
1007 | 996 |
|
1008 | 997 |
/// Constructor. |
1009 | 998 |
BfsWizard() : TR() {} |
1010 | 999 |
|
1011 | 1000 |
/// Constructor that requires parameters. |
1012 | 1001 |
|
1013 | 1002 |
/// Constructor that requires parameters. |
1014 | 1003 |
/// These parameters will be the default values for the traits class. |
1015 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
|
1016 |
TR(g,s) {} |
|
1004 |
/// \param g The digraph the algorithm runs on. |
|
1005 |
BfsWizard(const Digraph &g) : |
|
1006 |
TR(g) {} |
|
1017 | 1007 |
|
1018 | 1008 |
///Copy constructor |
1019 | 1009 |
BfsWizard(const TR &b) : TR(b) {} |
1020 | 1010 |
|
1021 | 1011 |
~BfsWizard() {} |
1022 | 1012 |
|
1023 |
///Runs BFS algorithm from |
|
1013 |
///Runs BFS algorithm from the given source node. |
|
1024 | 1014 |
|
1025 |
///Runs BFS algorithm from a source node. |
|
1026 |
///The node can be given with the \ref source() function. |
|
1015 |
///This method runs BFS algorithm from node \c s |
|
1016 |
///in order to compute the shortest path to each node. |
|
1017 |
void run(Node s) |
|
1018 |
{ |
|
1019 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
1020 |
if (Base::_pred) |
|
1021 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1022 |
if (Base::_dist) |
|
1023 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1024 |
if (Base::_reached) |
|
1025 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
1026 |
if (Base::_processed) |
|
1027 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1028 |
if (s!=INVALID) |
|
1029 |
alg.run(s); |
|
1030 |
else |
|
1031 |
alg.run(); |
|
1032 |
} |
|
1033 |
|
|
1034 |
///Finds the shortest path between \c s and \c t. |
|
1035 |
|
|
1036 |
///This method runs BFS algorithm from node \c s |
|
1037 |
///in order to compute the shortest path to node \c t |
|
1038 |
///(it stops searching when \c t is processed). |
|
1039 |
/// |
|
1040 |
///\return \c true if \c t is reachable form \c s. |
|
1041 |
bool run(Node s, Node t) |
|
1042 |
{ |
|
1043 |
if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
|
1044 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
1045 |
if (Base::_pred) |
|
1046 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1047 |
if (Base::_dist) |
|
1048 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1049 |
if (Base::_reached) |
|
1050 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
1051 |
if (Base::_processed) |
|
1052 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1053 |
alg.run(s,t); |
|
1054 |
if (Base::_path) |
|
1055 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
|
1056 |
if (Base::_di) |
|
1057 |
*Base::_di = alg.dist(t); |
|
1058 |
return alg.reached(t); |
|
1059 |
} |
|
1060 |
|
|
1061 |
///Runs BFS algorithm to visit all nodes in the digraph. |
|
1062 |
|
|
1063 |
///This method runs BFS algorithm in order to compute |
|
1064 |
///the shortest path to each node. |
|
1027 | 1065 |
void run() |
1028 | 1066 |
{ |
1029 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
|
1030 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
1031 |
if(Base::_reached) |
|
1032 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
1033 |
if(Base::_processed) |
|
1034 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1035 |
if(Base::_pred) |
|
1036 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1037 |
if(Base::_dist) |
|
1038 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1039 |
alg.run(Base::_source); |
|
1040 |
} |
|
1041 |
|
|
1042 |
///Runs BFS algorithm from the given node. |
|
1043 |
|
|
1044 |
///Runs BFS algorithm from the given node. |
|
1045 |
///\param s is the given source. |
|
1046 |
void run(Node s) |
|
1047 |
{ |
|
1048 |
Base::_source=s; |
|
1049 |
run(); |
|
1050 |
} |
|
1051 |
|
|
1052 |
/// Sets the source node, from which the Bfs algorithm runs. |
|
1053 |
|
|
1054 |
/// Sets the source node, from which the Bfs algorithm runs. |
|
1055 |
/// \param s is the source node. |
|
1056 |
BfsWizard<TR> &source(Node s) |
|
1057 |
{ |
|
1058 |
Base::_source=s; |
|
1059 |
|
|
1067 |
run(INVALID); |
|
1060 | 1068 |
} |
1061 | 1069 |
|
1062 | 1070 |
template<class T> |
1063 | 1071 |
struct SetPredMapBase : public Base { |
1064 | 1072 |
typedef T PredMap; |
1065 | 1073 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1066 | 1074 |
SetPredMapBase(const TR &b) : TR(b) {} |
1067 | 1075 |
}; |
1068 |
///\brief \ref named- |
|
1076 |
///\brief \ref named-func-param "Named parameter" |
|
1069 | 1077 |
///for setting \ref PredMap object. |
1070 | 1078 |
/// |
1071 |
/// |
|
1079 |
///\ref named-func-param "Named parameter" |
|
1072 | 1080 |
///for setting \ref PredMap object. |
1073 | 1081 |
template<class T> |
1074 | 1082 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1075 | 1083 |
{ |
1076 | 1084 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1077 | 1085 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1078 | 1086 |
} |
1079 | 1087 |
|
1080 | 1088 |
template<class T> |
1081 | 1089 |
struct SetReachedMapBase : public Base { |
1082 | 1090 |
typedef T ReachedMap; |
1083 | 1091 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1084 | 1092 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1085 | 1093 |
}; |
1086 |
///\brief \ref named- |
|
1094 |
///\brief \ref named-func-param "Named parameter" |
|
1087 | 1095 |
///for setting \ref ReachedMap object. |
1088 | 1096 |
/// |
1089 |
/// \ref named- |
|
1097 |
/// \ref named-func-param "Named parameter" |
|
1090 | 1098 |
///for setting \ref ReachedMap object. |
1091 | 1099 |
template<class T> |
1092 | 1100 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1093 | 1101 |
{ |
1094 | 1102 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1095 | 1103 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1096 | 1104 |
} |
1097 | 1105 |
|
1098 | 1106 |
template<class T> |
1107 |
struct SetDistMapBase : public Base { |
|
1108 |
typedef T DistMap; |
|
1109 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1110 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1111 |
}; |
|
1112 |
///\brief \ref named-func-param "Named parameter" |
|
1113 |
///for setting \ref DistMap object. |
|
1114 |
/// |
|
1115 |
/// \ref named-func-param "Named parameter" |
|
1116 |
///for setting \ref DistMap object. |
|
1117 |
template<class T> |
|
1118 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
|
1119 |
{ |
|
1120 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1121 |
return BfsWizard<SetDistMapBase<T> >(*this); |
|
1122 |
} |
|
1123 |
|
|
1124 |
template<class T> |
|
1099 | 1125 |
struct SetProcessedMapBase : public Base { |
1100 | 1126 |
typedef T ProcessedMap; |
1101 | 1127 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1102 | 1128 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1103 | 1129 |
}; |
1104 |
///\brief \ref named- |
|
1130 |
///\brief \ref named-func-param "Named parameter" |
|
1105 | 1131 |
///for setting \ref ProcessedMap object. |
1106 | 1132 |
/// |
1107 |
/// \ref named- |
|
1133 |
/// \ref named-func-param "Named parameter" |
|
1108 | 1134 |
///for setting \ref ProcessedMap object. |
1109 | 1135 |
template<class T> |
1110 | 1136 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1111 | 1137 |
{ |
1112 | 1138 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1113 | 1139 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1114 | 1140 |
} |
1115 | 1141 |
|
1116 | 1142 |
template<class T> |
1117 |
struct SetDistMapBase : public Base { |
|
1118 |
typedef T DistMap; |
|
1119 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1120 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1143 |
struct SetPathBase : public Base { |
|
1144 |
typedef T Path; |
|
1145 |
SetPathBase(const TR &b) : TR(b) {} |
|
1121 | 1146 |
}; |
1122 |
///\brief \ref named-templ-param "Named parameter" |
|
1123 |
///for setting \ref DistMap object. |
|
1147 |
///\brief \ref named-func-param "Named parameter" |
|
1148 |
///for getting the shortest path to the target node. |
|
1124 | 1149 |
/// |
1125 |
/// \ref named-templ-param "Named parameter" |
|
1126 |
///for setting \ref DistMap object. |
|
1150 |
///\ref named-func-param "Named parameter" |
|
1151 |
///for getting the shortest path to the target node. |
|
1127 | 1152 |
template<class T> |
1128 |
BfsWizard< |
|
1153 |
BfsWizard<SetPathBase<T> > path(const T &t) |
|
1129 | 1154 |
{ |
1130 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1131 |
return BfsWizard<SetDistMapBase<T> >(*this); |
|
1155 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1156 |
return BfsWizard<SetPathBase<T> >(*this); |
|
1157 |
} |
|
1158 |
|
|
1159 |
///\brief \ref named-func-param "Named parameter" |
|
1160 |
///for getting the distance of the target node. |
|
1161 |
/// |
|
1162 |
///\ref named-func-param "Named parameter" |
|
1163 |
///for getting the distance of the target node. |
|
1164 |
BfsWizard dist(const int &d) |
|
1165 |
{ |
|
1166 |
Base::_di=const_cast<int*>(&d); |
|
1167 |
return *this; |
|
1132 | 1168 |
} |
1133 | 1169 |
|
1134 | 1170 |
}; |
1135 | 1171 |
|
1136 |
///Function |
|
1172 |
///Function-type interface for BFS algorithm. |
|
1137 | 1173 |
|
1138 | 1174 |
/// \ingroup search |
1139 |
///Function |
|
1175 |
///Function-type interface for BFS algorithm. |
|
1140 | 1176 |
/// |
1141 |
///This function also has several |
|
1142 |
///\ref named-templ-func-param "named parameters", |
|
1177 |
///This function also has several \ref named-func-param "named parameters", |
|
1143 | 1178 |
///they are declared as the members of class \ref BfsWizard. |
1144 |
///The following |
|
1145 |
///example shows how to use these parameters. |
|
1179 |
///The following examples show how to use these parameters. |
|
1146 | 1180 |
///\code |
1147 |
/// |
|
1181 |
/// // Compute shortest path from node s to each node |
|
1182 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
|
1183 |
/// |
|
1184 |
/// // Compute shortest path from s to t |
|
1185 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
|
1148 | 1186 |
///\endcode |
1149 | 1187 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1150 | 1188 |
///to the end of the parameter list. |
1151 | 1189 |
///\sa BfsWizard |
1152 | 1190 |
///\sa Bfs |
1153 | 1191 |
template<class GR> |
1154 | 1192 |
BfsWizard<BfsWizardBase<GR> > |
1155 |
bfs(const GR & |
|
1193 |
bfs(const GR &digraph) |
|
1156 | 1194 |
{ |
1157 |
return BfsWizard<BfsWizardBase<GR> >( |
|
1195 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
|
1158 | 1196 |
} |
1159 | 1197 |
|
1160 | 1198 |
#ifdef DOXYGEN |
1161 | 1199 |
/// \brief Visitor class for BFS. |
1162 | 1200 |
/// |
1163 | 1201 |
/// This class defines the interface of the BfsVisit events, and |
1164 | 1202 |
/// it could be the base of a real visitor class. |
1165 | 1203 |
template <typename _Digraph> |
1166 | 1204 |
struct BfsVisitor { |
1167 | 1205 |
typedef _Digraph Digraph; |
1168 | 1206 |
typedef typename Digraph::Arc Arc; |
1169 | 1207 |
typedef typename Digraph::Node Node; |
1170 | 1208 |
/// \brief Called for the source node(s) of the BFS. |
1171 | 1209 |
/// |
1172 | 1210 |
/// This function is called for the source node(s) of the BFS. |
1173 | 1211 |
void start(const Node& node) {} |
1174 | 1212 |
/// \brief Called when a node is reached first time. |
1175 | 1213 |
/// |
1176 | 1214 |
/// This function is called when a node is reached first time. |
1177 | 1215 |
void reach(const Node& node) {} |
1178 | 1216 |
/// \brief Called when a node is processed. |
1179 | 1217 |
/// |
1180 | 1218 |
/// This function is called when a node is processed. |
1181 | 1219 |
void process(const Node& node) {} |
1182 | 1220 |
/// \brief Called when an arc reaches a new node. |
1183 | 1221 |
/// |
1184 | 1222 |
/// This function is called when the BFS finds an arc whose target node |
1185 | 1223 |
/// is not reached yet. |
1186 | 1224 |
void discover(const Arc& arc) {} |
1187 | 1225 |
/// \brief Called when an arc is examined but its target node is |
1188 | 1226 |
/// already discovered. |
1189 | 1227 |
/// |
... | ... |
@@ -36,65 +36,68 @@ |
36 | 36 |
/// \brief A skeleton structure for representing directed paths in |
37 | 37 |
/// a digraph. |
38 | 38 |
/// |
39 | 39 |
/// A skeleton structure for representing directed paths in a |
40 | 40 |
/// digraph. |
41 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
42 | 42 |
/// |
43 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
44 | 44 |
/// lemon path type stores just this list. As a consequence it |
45 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
46 | 46 |
/// paths cannot store the source. |
47 | 47 |
/// |
48 | 48 |
template <typename _Digraph> |
49 | 49 |
class Path { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// Type of the underlying digraph. |
53 | 53 |
typedef _Digraph Digraph; |
54 | 54 |
/// Arc type of the underlying digraph. |
55 | 55 |
typedef typename Digraph::Arc Arc; |
56 | 56 |
|
57 | 57 |
class ArcIt; |
58 | 58 |
|
59 | 59 |
/// \brief Default constructor |
60 | 60 |
Path() {} |
61 | 61 |
|
62 | 62 |
/// \brief Template constructor |
63 | 63 |
template <typename CPath> |
64 | 64 |
Path(const CPath& cpath) {} |
65 | 65 |
|
66 | 66 |
/// \brief Template assigment |
67 | 67 |
template <typename CPath> |
68 |
Path& operator=(const CPath& cpath) { |
|
68 |
Path& operator=(const CPath& cpath) { |
|
69 |
ignore_unused_variable_warning(cpath); |
|
70 |
return *this; |
|
71 |
} |
|
69 | 72 |
|
70 | 73 |
/// Length of the path ie. the number of arcs in the path. |
71 | 74 |
int length() const { return 0;} |
72 | 75 |
|
73 | 76 |
/// Returns whether the path is empty. |
74 | 77 |
bool empty() const { return true;} |
75 | 78 |
|
76 | 79 |
/// Resets the path to an empty path. |
77 | 80 |
void clear() {} |
78 | 81 |
|
79 | 82 |
/// \brief LEMON style iterator for path arcs |
80 | 83 |
/// |
81 | 84 |
/// This class is used to iterate on the arcs of the paths. |
82 | 85 |
class ArcIt { |
83 | 86 |
public: |
84 | 87 |
/// Default constructor |
85 | 88 |
ArcIt() {} |
86 | 89 |
/// Invalid constructor |
87 | 90 |
ArcIt(Invalid) {} |
88 | 91 |
/// Constructor for first arc |
89 | 92 |
ArcIt(const Path &) {} |
90 | 93 |
|
91 | 94 |
/// Conversion to Arc |
92 | 95 |
operator Arc() const { return INVALID; } |
93 | 96 |
|
94 | 97 |
/// Next arc |
95 | 98 |
ArcIt& operator++() {return *this;} |
96 | 99 |
|
97 | 100 |
/// Comparison operator |
98 | 101 |
bool operator==(const ArcIt&) const {return true;} |
99 | 102 |
/// Comparison operator |
100 | 103 |
bool operator!=(const ArcIt&) const {return true;} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
#include <lemon/maps.h> |
32 |
#include <lemon/path.h> |
|
32 | 33 |
|
33 | 34 |
namespace lemon { |
34 | 35 |
|
35 | 36 |
///Default traits class of Dfs class. |
36 | 37 |
|
37 | 38 |
///Default traits class of Dfs class. |
38 | 39 |
///\tparam GR Digraph type. |
39 | 40 |
template<class GR> |
40 | 41 |
struct DfsDefaultTraits |
41 | 42 |
{ |
42 | 43 |
///The type of the digraph the algorithm runs on. |
43 | 44 |
typedef GR Digraph; |
44 | 45 |
|
45 | 46 |
///\brief The type of the map that stores the predecessor |
46 | 47 |
///arcs of the %DFS paths. |
47 | 48 |
/// |
48 | 49 |
///The type of the map that stores the predecessor |
49 | 50 |
///arcs of the %DFS paths. |
50 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 52 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 53 |
///Instantiates a \ref PredMap. |
53 | 54 |
|
54 | 55 |
///This function instantiates a \ref PredMap. |
55 | 56 |
///\param g is the digraph, to which we would like to define the |
56 | 57 |
///\ref PredMap. |
57 | 58 |
static PredMap *createPredMap(const Digraph &g) |
58 | 59 |
{ |
59 | 60 |
return new PredMap(g); |
60 | 61 |
} |
61 | 62 |
|
62 | 63 |
///The type of the map that indicates which nodes are processed. |
63 | 64 |
|
... | ... |
@@ -85,65 +86,65 @@ |
85 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 87 |
///Instantiates a \ref ReachedMap. |
87 | 88 |
|
88 | 89 |
///This function instantiates a \ref ReachedMap. |
89 | 90 |
///\param g is the digraph, to which |
90 | 91 |
///we would like to define the \ref ReachedMap. |
91 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 93 |
{ |
93 | 94 |
return new ReachedMap(g); |
94 | 95 |
} |
95 | 96 |
|
96 | 97 |
///The type of the map that stores the distances of the nodes. |
97 | 98 |
|
98 | 99 |
///The type of the map that stores the distances of the nodes. |
99 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 102 |
///Instantiates a \ref DistMap. |
102 | 103 |
|
103 | 104 |
///This function instantiates a \ref DistMap. |
104 | 105 |
///\param g is the digraph, to which we would like to define the |
105 | 106 |
///\ref DistMap. |
106 | 107 |
static DistMap *createDistMap(const Digraph &g) |
107 | 108 |
{ |
108 | 109 |
return new DistMap(g); |
109 | 110 |
} |
110 | 111 |
}; |
111 | 112 |
|
112 | 113 |
///%DFS algorithm class. |
113 | 114 |
|
114 | 115 |
///\ingroup search |
115 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 117 |
/// |
117 |
///There is also a \ref dfs() "function |
|
118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
|
118 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 120 |
///used easier. |
120 | 121 |
/// |
121 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 123 |
///The default value is \ref ListDigraph. The value of GR is not used |
123 | 124 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
124 | 125 |
///\tparam TR Traits class to set various data types used by the algorithm. |
125 | 126 |
///The default traits class is |
126 | 127 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
127 | 128 |
///See \ref DfsDefaultTraits for the documentation of |
128 | 129 |
///a Dfs traits class. |
129 | 130 |
#ifdef DOXYGEN |
130 | 131 |
template <typename GR, |
131 | 132 |
typename TR> |
132 | 133 |
#else |
133 | 134 |
template <typename GR=ListDigraph, |
134 | 135 |
typename TR=DfsDefaultTraits<GR> > |
135 | 136 |
#endif |
136 | 137 |
class Dfs { |
137 | 138 |
public: |
138 | 139 |
///\ref Exception for uninitialized parameters. |
139 | 140 |
|
140 | 141 |
///This error represents problems in the initialization of the |
141 | 142 |
///parameters of the algorithm. |
142 | 143 |
class UninitializedParameter : public lemon::UninitializedParameter { |
143 | 144 |
public: |
144 | 145 |
virtual const char* what() const throw() { |
145 | 146 |
return "lemon::Dfs::UninitializedParameter"; |
146 | 147 |
} |
147 | 148 |
}; |
148 | 149 |
|
149 | 150 |
///The type of the digraph the algorithm runs on. |
... | ... |
@@ -743,382 +744,419 @@ |
743 | 744 |
///arcs, which form the DFS tree. |
744 | 745 |
/// |
745 | 746 |
///\pre Either \ref run() or \ref init() |
746 | 747 |
///must be called before using this function. |
747 | 748 |
const PredMap &predMap() const { return *_pred;} |
748 | 749 |
|
749 | 750 |
///Checks if a node is reachable from the root(s). |
750 | 751 |
|
751 | 752 |
///Returns \c true if \c v is reachable from the root(s). |
752 | 753 |
///\pre Either \ref run() or \ref start() |
753 | 754 |
///must be called before using this function. |
754 | 755 |
bool reached(Node v) const { return (*_reached)[v]; } |
755 | 756 |
|
756 | 757 |
///@} |
757 | 758 |
}; |
758 | 759 |
|
759 | 760 |
///Default traits class of dfs() function. |
760 | 761 |
|
761 | 762 |
///Default traits class of dfs() function. |
762 | 763 |
///\tparam GR Digraph type. |
763 | 764 |
template<class GR> |
764 | 765 |
struct DfsWizardDefaultTraits |
765 | 766 |
{ |
766 | 767 |
///The type of the digraph the algorithm runs on. |
767 | 768 |
typedef GR Digraph; |
768 | 769 |
|
769 | 770 |
///\brief The type of the map that stores the predecessor |
770 | 771 |
///arcs of the %DFS paths. |
771 | 772 |
/// |
772 | 773 |
///The type of the map that stores the predecessor |
773 | 774 |
///arcs of the %DFS paths. |
774 | 775 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
775 |
/// |
|
776 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
|
776 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
777 | 777 |
///Instantiates a \ref PredMap. |
778 | 778 |
|
779 | 779 |
///This function instantiates a \ref PredMap. |
780 | 780 |
///\param g is the digraph, to which we would like to define the |
781 | 781 |
///\ref PredMap. |
782 |
#ifdef DOXYGEN |
|
783 | 782 |
static PredMap *createPredMap(const Digraph &g) |
784 |
#else |
|
785 |
static PredMap *createPredMap(const Digraph &) |
|
786 |
#endif |
|
787 | 783 |
{ |
788 |
return new PredMap(); |
|
784 |
return new PredMap(g); |
|
789 | 785 |
} |
790 | 786 |
|
791 | 787 |
///The type of the map that indicates which nodes are processed. |
792 | 788 |
|
793 | 789 |
///The type of the map that indicates which nodes are processed. |
794 | 790 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
791 |
///By default it is a NullMap. |
|
795 | 792 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
796 | 793 |
///Instantiates a \ref ProcessedMap. |
797 | 794 |
|
798 | 795 |
///This function instantiates a \ref ProcessedMap. |
799 | 796 |
///\param g is the digraph, to which |
800 | 797 |
///we would like to define the \ref ProcessedMap. |
801 | 798 |
#ifdef DOXYGEN |
802 | 799 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
803 | 800 |
#else |
804 | 801 |
static ProcessedMap *createProcessedMap(const Digraph &) |
805 | 802 |
#endif |
806 | 803 |
{ |
807 | 804 |
return new ProcessedMap(); |
808 | 805 |
} |
809 | 806 |
|
810 | 807 |
///The type of the map that indicates which nodes are reached. |
811 | 808 |
|
812 | 809 |
///The type of the map that indicates which nodes are reached. |
813 | 810 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
814 | 811 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
815 | 812 |
///Instantiates a \ref ReachedMap. |
816 | 813 |
|
817 | 814 |
///This function instantiates a \ref ReachedMap. |
818 | 815 |
///\param g is the digraph, to which |
819 | 816 |
///we would like to define the \ref ReachedMap. |
820 | 817 |
static ReachedMap *createReachedMap(const Digraph &g) |
821 | 818 |
{ |
822 | 819 |
return new ReachedMap(g); |
823 | 820 |
} |
824 | 821 |
|
825 | 822 |
///The type of the map that stores the distances of the nodes. |
826 | 823 |
|
827 | 824 |
///The type of the map that stores the distances of the nodes. |
828 | 825 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
829 |
/// |
|
830 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
|
826 |
typedef typename Digraph::template NodeMap<int> DistMap; |
|
831 | 827 |
///Instantiates a \ref DistMap. |
832 | 828 |
|
833 | 829 |
///This function instantiates a \ref DistMap. |
834 | 830 |
///\param g is the digraph, to which we would like to define |
835 | 831 |
///the \ref DistMap |
836 |
#ifdef DOXYGEN |
|
837 | 832 |
static DistMap *createDistMap(const Digraph &g) |
838 |
#else |
|
839 |
static DistMap *createDistMap(const Digraph &) |
|
840 |
#endif |
|
841 | 833 |
{ |
842 |
return new DistMap(); |
|
834 |
return new DistMap(g); |
|
843 | 835 |
} |
836 |
|
|
837 |
///The type of the DFS paths. |
|
838 |
|
|
839 |
///The type of the DFS paths. |
|
840 |
///It must meet the \ref concepts::Path "Path" concept. |
|
841 |
typedef lemon::Path<Digraph> Path; |
|
844 | 842 |
}; |
845 | 843 |
|
846 | 844 |
/// Default traits class used by \ref DfsWizard |
847 | 845 |
|
848 | 846 |
/// To make it easier to use Dfs algorithm |
849 | 847 |
/// we have created a wizard class. |
850 | 848 |
/// This \ref DfsWizard class needs default traits, |
851 | 849 |
/// as well as the \ref Dfs class. |
852 | 850 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
853 | 851 |
/// \ref DfsWizard class. |
854 | 852 |
template<class GR> |
855 | 853 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
856 | 854 |
{ |
857 | 855 |
|
858 | 856 |
typedef DfsWizardDefaultTraits<GR> Base; |
859 | 857 |
protected: |
860 | 858 |
//The type of the nodes in the digraph. |
861 | 859 |
typedef typename Base::Digraph::Node Node; |
862 | 860 |
|
863 | 861 |
//Pointer to the digraph the algorithm runs on. |
864 | 862 |
void *_g; |
865 | 863 |
//Pointer to the map of reached nodes. |
866 | 864 |
void *_reached; |
867 | 865 |
//Pointer to the map of processed nodes. |
868 | 866 |
void *_processed; |
869 | 867 |
//Pointer to the map of predecessors arcs. |
870 | 868 |
void *_pred; |
871 | 869 |
//Pointer to the map of distances. |
872 | 870 |
void *_dist; |
873 |
//Pointer to the source node. |
|
874 |
Node _source; |
|
871 |
//Pointer to the DFS path to the target node. |
|
872 |
void *_path; |
|
873 |
//Pointer to the distance of the target node. |
|
874 |
int *_di; |
|
875 | 875 |
|
876 | 876 |
public: |
877 | 877 |
/// Constructor. |
878 | 878 |
|
879 | 879 |
/// This constructor does not require parameters, therefore it initiates |
880 |
/// all of the attributes to |
|
880 |
/// all of the attributes to \c 0. |
|
881 | 881 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
882 |
_dist(0), |
|
882 |
_dist(0), _path(0), _di(0) {} |
|
883 | 883 |
|
884 | 884 |
/// Constructor. |
885 | 885 |
|
886 |
/// This constructor requires some parameters, |
|
887 |
/// listed in the parameters list. |
|
888 |
/// |
|
886 |
/// This constructor requires one parameter, |
|
887 |
/// others are initiated to \c 0. |
|
889 | 888 |
/// \param g The digraph the algorithm runs on. |
890 |
/// \param s The source node. |
|
891 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
|
889 |
DfsWizardBase(const GR &g) : |
|
892 | 890 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
893 |
_reached(0), _processed(0), _pred(0), _dist(0), |
|
891 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
|
894 | 892 |
|
895 | 893 |
}; |
896 | 894 |
|
897 |
/// Auxiliary class for the function |
|
895 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
|
898 | 896 |
|
899 |
/// This auxiliary class is created to implement the function type |
|
900 |
/// interface of \ref Dfs algorithm. It uses the functions and features |
|
901 |
/// of the plain \ref Dfs, but it is much simpler to use it. |
|
902 |
/// It should only be used through the \ref dfs() function, which makes |
|
903 |
/// |
|
897 |
/// This auxiliary class is created to implement the |
|
898 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
|
899 |
/// It does not have own \ref run() method, it uses the functions |
|
900 |
/// and features of the plain \ref Dfs. |
|
904 | 901 |
/// |
905 |
/// Simplicity means that the way to change the types defined |
|
906 |
/// in the traits class is based on functions that returns the new class |
|
907 |
/// and not on templatable built-in classes. |
|
908 |
/// When using the plain \ref Dfs |
|
909 |
/// the new class with the modified type comes from |
|
910 |
/// the original class by using the :: |
|
911 |
/// operator. In the case of \ref DfsWizard only |
|
912 |
/// a function have to be called, and it will |
|
913 |
/// return the needed class. |
|
914 |
/// |
|
915 |
/// It does not have own \ref run() method. When its \ref run() method |
|
916 |
/// is called, it initiates a plain \ref Dfs object, and calls the |
|
917 |
/// \ref |
|
902 |
/// This class should only be used through the \ref dfs() function, |
|
903 |
/// which makes it easier to use the algorithm. |
|
918 | 904 |
template<class TR> |
919 | 905 |
class DfsWizard : public TR |
920 | 906 |
{ |
921 | 907 |
typedef TR Base; |
922 | 908 |
|
923 | 909 |
///The type of the digraph the algorithm runs on. |
924 | 910 |
typedef typename TR::Digraph Digraph; |
925 | 911 |
|
926 | 912 |
typedef typename Digraph::Node Node; |
927 | 913 |
typedef typename Digraph::NodeIt NodeIt; |
928 | 914 |
typedef typename Digraph::Arc Arc; |
929 | 915 |
typedef typename Digraph::OutArcIt OutArcIt; |
930 | 916 |
|
931 | 917 |
///\brief The type of the map that stores the predecessor |
932 |
///arcs of the |
|
918 |
///arcs of the DFS paths. |
|
933 | 919 |
typedef typename TR::PredMap PredMap; |
934 | 920 |
///\brief The type of the map that stores the distances of the nodes. |
935 | 921 |
typedef typename TR::DistMap DistMap; |
936 | 922 |
///\brief The type of the map that indicates which nodes are reached. |
937 | 923 |
typedef typename TR::ReachedMap ReachedMap; |
938 | 924 |
///\brief The type of the map that indicates which nodes are processed. |
939 | 925 |
typedef typename TR::ProcessedMap ProcessedMap; |
926 |
///The type of the DFS paths |
|
927 |
typedef typename TR::Path Path; |
|
940 | 928 |
|
941 | 929 |
public: |
942 | 930 |
|
943 | 931 |
/// Constructor. |
944 | 932 |
DfsWizard() : TR() {} |
945 | 933 |
|
946 | 934 |
/// Constructor that requires parameters. |
947 | 935 |
|
948 | 936 |
/// Constructor that requires parameters. |
949 | 937 |
/// These parameters will be the default values for the traits class. |
950 |
DfsWizard(const Digraph &g, Node s=INVALID) : |
|
951 |
TR(g,s) {} |
|
938 |
/// \param g The digraph the algorithm runs on. |
|
939 |
DfsWizard(const Digraph &g) : |
|
940 |
TR(g) {} |
|
952 | 941 |
|
953 | 942 |
///Copy constructor |
954 | 943 |
DfsWizard(const TR &b) : TR(b) {} |
955 | 944 |
|
956 | 945 |
~DfsWizard() {} |
957 | 946 |
|
958 |
///Runs DFS algorithm from |
|
947 |
///Runs DFS algorithm from the given source node. |
|
959 | 948 |
|
960 |
///Runs DFS algorithm from a source node. |
|
961 |
///The node can be given with the \ref source() function. |
|
949 |
///This method runs DFS algorithm from node \c s |
|
950 |
///in order to compute the DFS path to each node. |
|
951 |
void run(Node s) |
|
952 |
{ |
|
953 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
954 |
if (Base::_pred) |
|
955 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
956 |
if (Base::_dist) |
|
957 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
958 |
if (Base::_reached) |
|
959 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
960 |
if (Base::_processed) |
|
961 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
962 |
if (s!=INVALID) |
|
963 |
alg.run(s); |
|
964 |
else |
|
965 |
alg.run(); |
|
966 |
} |
|
967 |
|
|
968 |
///Finds the DFS path between \c s and \c t. |
|
969 |
|
|
970 |
///This method runs DFS algorithm from node \c s |
|
971 |
///in order to compute the DFS path to node \c t |
|
972 |
///(it stops searching when \c t is processed). |
|
973 |
/// |
|
974 |
///\return \c true if \c t is reachable form \c s. |
|
975 |
bool run(Node s, Node t) |
|
976 |
{ |
|
977 |
if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
|
978 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
979 |
if (Base::_pred) |
|
980 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
981 |
if (Base::_dist) |
|
982 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
983 |
if (Base::_reached) |
|
984 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
985 |
if (Base::_processed) |
|
986 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
987 |
alg.run(s,t); |
|
988 |
if (Base::_path) |
|
989 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
|
990 |
if (Base::_di) |
|
991 |
*Base::_di = alg.dist(t); |
|
992 |
return alg.reached(t); |
|
993 |
} |
|
994 |
|
|
995 |
///Runs DFS algorithm to visit all nodes in the digraph. |
|
996 |
|
|
997 |
///This method runs DFS algorithm in order to compute |
|
998 |
///the DFS path to each node. |
|
962 | 999 |
void run() |
963 | 1000 |
{ |
964 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
|
965 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
|
966 |
if(Base::_reached) |
|
967 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
|
968 |
if(Base::_processed) |
|
969 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
970 |
if(Base::_pred) |
|
971 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
972 |
if(Base::_dist) |
|
973 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
974 |
alg.run(Base::_source); |
|
975 |
} |
|
976 |
|
|
977 |
///Runs DFS algorithm from the given node. |
|
978 |
|
|
979 |
///Runs DFS algorithm from the given node. |
|
980 |
///\param s is the given source. |
|
981 |
void run(Node s) |
|
982 |
{ |
|
983 |
Base::_source=s; |
|
984 |
run(); |
|
985 |
} |
|
986 |
|
|
987 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
988 |
|
|
989 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
990 |
/// \param s is the source node. |
|
991 |
DfsWizard<TR> &source(Node s) |
|
992 |
{ |
|
993 |
Base::_source=s; |
|
994 |
|
|
1001 |
run(INVALID); |
|
995 | 1002 |
} |
996 | 1003 |
|
997 | 1004 |
template<class T> |
998 | 1005 |
struct SetPredMapBase : public Base { |
999 | 1006 |
typedef T PredMap; |
1000 | 1007 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1001 | 1008 |
SetPredMapBase(const TR &b) : TR(b) {} |
1002 | 1009 |
}; |
1003 |
///\brief \ref named- |
|
1010 |
///\brief \ref named-func-param "Named parameter" |
|
1004 | 1011 |
///for setting \ref PredMap object. |
1005 | 1012 |
/// |
1006 |
///\ref named- |
|
1013 |
///\ref named-func-param "Named parameter" |
|
1007 | 1014 |
///for setting \ref PredMap object. |
1008 | 1015 |
template<class T> |
1009 | 1016 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1010 | 1017 |
{ |
1011 | 1018 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1012 | 1019 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1013 | 1020 |
} |
1014 | 1021 |
|
1015 | 1022 |
template<class T> |
1016 | 1023 |
struct SetReachedMapBase : public Base { |
1017 | 1024 |
typedef T ReachedMap; |
1018 | 1025 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1019 | 1026 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1020 | 1027 |
}; |
1021 |
///\brief \ref named- |
|
1028 |
///\brief \ref named-func-param "Named parameter" |
|
1022 | 1029 |
///for setting \ref ReachedMap object. |
1023 | 1030 |
/// |
1024 |
/// \ref named- |
|
1031 |
/// \ref named-func-param "Named parameter" |
|
1025 | 1032 |
///for setting \ref ReachedMap object. |
1026 | 1033 |
template<class T> |
1027 | 1034 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1028 | 1035 |
{ |
1029 | 1036 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1030 | 1037 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1031 | 1038 |
} |
1032 | 1039 |
|
1033 | 1040 |
template<class T> |
1041 |
struct SetDistMapBase : public Base { |
|
1042 |
typedef T DistMap; |
|
1043 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1044 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1045 |
}; |
|
1046 |
///\brief \ref named-func-param "Named parameter" |
|
1047 |
///for setting \ref DistMap object. |
|
1048 |
/// |
|
1049 |
/// \ref named-func-param "Named parameter" |
|
1050 |
///for setting \ref DistMap object. |
|
1051 |
template<class T> |
|
1052 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
|
1053 |
{ |
|
1054 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1055 |
return DfsWizard<SetDistMapBase<T> >(*this); |
|
1056 |
} |
|
1057 |
|
|
1058 |
template<class T> |
|
1034 | 1059 |
struct SetProcessedMapBase : public Base { |
1035 | 1060 |
typedef T ProcessedMap; |
1036 | 1061 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1037 | 1062 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1038 | 1063 |
}; |
1039 |
///\brief \ref named- |
|
1064 |
///\brief \ref named-func-param "Named parameter" |
|
1040 | 1065 |
///for setting \ref ProcessedMap object. |
1041 | 1066 |
/// |
1042 |
/// \ref named- |
|
1067 |
/// \ref named-func-param "Named parameter" |
|
1043 | 1068 |
///for setting \ref ProcessedMap object. |
1044 | 1069 |
template<class T> |
1045 | 1070 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1046 | 1071 |
{ |
1047 | 1072 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1048 | 1073 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1049 | 1074 |
} |
1050 | 1075 |
|
1051 | 1076 |
template<class T> |
1052 |
struct SetDistMapBase : public Base { |
|
1053 |
typedef T DistMap; |
|
1054 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1055 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1077 |
struct SetPathBase : public Base { |
|
1078 |
typedef T Path; |
|
1079 |
SetPathBase(const TR &b) : TR(b) {} |
|
1056 | 1080 |
}; |
1057 |
///\brief \ref named-templ-param "Named parameter" |
|
1058 |
///for setting \ref DistMap object. |
|
1081 |
///\brief \ref named-func-param "Named parameter" |
|
1082 |
///for getting the DFS path to the target node. |
|
1059 | 1083 |
/// |
1060 |
///\ref named-templ-param "Named parameter" |
|
1061 |
///for setting \ref DistMap object. |
|
1084 |
///\ref named-func-param "Named parameter" |
|
1085 |
///for getting the DFS path to the target node. |
|
1062 | 1086 |
template<class T> |
1063 |
DfsWizard< |
|
1087 |
DfsWizard<SetPathBase<T> > path(const T &t) |
|
1064 | 1088 |
{ |
1065 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1066 |
return DfsWizard<SetDistMapBase<T> >(*this); |
|
1089 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1090 |
return DfsWizard<SetPathBase<T> >(*this); |
|
1091 |
} |
|
1092 |
|
|
1093 |
///\brief \ref named-func-param "Named parameter" |
|
1094 |
///for getting the distance of the target node. |
|
1095 |
/// |
|
1096 |
///\ref named-func-param "Named parameter" |
|
1097 |
///for getting the distance of the target node. |
|
1098 |
DfsWizard dist(const int &d) |
|
1099 |
{ |
|
1100 |
Base::_di=const_cast<int*>(&d); |
|
1101 |
return *this; |
|
1067 | 1102 |
} |
1068 | 1103 |
|
1069 | 1104 |
}; |
1070 | 1105 |
|
1071 |
///Function |
|
1106 |
///Function-type interface for DFS algorithm. |
|
1072 | 1107 |
|
1073 | 1108 |
///\ingroup search |
1074 |
///Function |
|
1109 |
///Function-type interface for DFS algorithm. |
|
1075 | 1110 |
/// |
1076 |
///This function also has several |
|
1077 |
///\ref named-templ-func-param "named parameters", |
|
1111 |
///This function also has several \ref named-func-param "named parameters", |
|
1078 | 1112 |
///they are declared as the members of class \ref DfsWizard. |
1079 |
///The following |
|
1080 |
///example shows how to use these parameters. |
|
1113 |
///The following examples show how to use these parameters. |
|
1081 | 1114 |
///\code |
1082 |
/// |
|
1115 |
/// // Compute the DFS tree |
|
1116 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
|
1117 |
/// |
|
1118 |
/// // Compute the DFS path from s to t |
|
1119 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
|
1083 | 1120 |
///\endcode |
1121 |
|
|
1084 | 1122 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1085 | 1123 |
///to the end of the parameter list. |
1086 | 1124 |
///\sa DfsWizard |
1087 | 1125 |
///\sa Dfs |
1088 | 1126 |
template<class GR> |
1089 | 1127 |
DfsWizard<DfsWizardBase<GR> > |
1090 |
dfs(const GR & |
|
1128 |
dfs(const GR &digraph) |
|
1091 | 1129 |
{ |
1092 |
return DfsWizard<DfsWizardBase<GR> >( |
|
1130 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
|
1093 | 1131 |
} |
1094 | 1132 |
|
1095 | 1133 |
#ifdef DOXYGEN |
1096 | 1134 |
/// \brief Visitor class for DFS. |
1097 | 1135 |
/// |
1098 | 1136 |
/// This class defines the interface of the DfsVisit events, and |
1099 | 1137 |
/// it could be the base of a real visitor class. |
1100 | 1138 |
template <typename _Digraph> |
1101 | 1139 |
struct DfsVisitor { |
1102 | 1140 |
typedef _Digraph Digraph; |
1103 | 1141 |
typedef typename Digraph::Arc Arc; |
1104 | 1142 |
typedef typename Digraph::Node Node; |
1105 | 1143 |
/// \brief Called for the source node of the DFS. |
1106 | 1144 |
/// |
1107 | 1145 |
/// This function is called for the source node of the DFS. |
1108 | 1146 |
void start(const Node& node) {} |
1109 | 1147 |
/// \brief Called when the source node is leaved. |
1110 | 1148 |
/// |
1111 | 1149 |
/// This function is called when the source node is leaved. |
1112 | 1150 |
void stop(const Node& node) {} |
1113 | 1151 |
/// \brief Called when a node is reached first time. |
1114 | 1152 |
/// |
1115 | 1153 |
/// This function is called when a node is reached first time. |
1116 | 1154 |
void reach(const Node& node) {} |
1117 | 1155 |
/// \brief Called when an arc reaches a new node. |
1118 | 1156 |
/// |
1119 | 1157 |
/// This function is called when the DFS finds an arc whose target node |
1120 | 1158 |
/// is not reached yet. |
1121 | 1159 |
void discover(const Arc& arc) {} |
1122 | 1160 |
/// \brief Called when an arc is examined but its target node is |
1123 | 1161 |
/// already discovered. |
1124 | 1162 |
/// |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 |
#include <lemon/path.h> |
|
33 | 34 |
|
34 | 35 |
namespace lemon { |
35 | 36 |
|
36 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
37 | 38 |
/// |
38 | 39 |
/// This operation traits class defines all computational operations and |
39 | 40 |
/// constants which are used in the Dijkstra algorithm. |
40 | 41 |
template <typename Value> |
41 | 42 |
struct DijkstraDefaultOperationTraits { |
42 | 43 |
/// \brief Gives back the zero value of the type. |
43 | 44 |
static Value zero() { |
44 | 45 |
return static_cast<Value>(0); |
45 | 46 |
} |
46 | 47 |
/// \brief Gives back the sum of the given two elements. |
47 | 48 |
static Value plus(const Value& left, const Value& right) { |
48 | 49 |
return left + right; |
49 | 50 |
} |
50 | 51 |
/// \brief Gives back true only if the first value is less than the second. |
51 | 52 |
static bool less(const Value& left, const Value& right) { |
52 | 53 |
return left < right; |
53 | 54 |
} |
54 | 55 |
}; |
55 | 56 |
|
56 | 57 |
/// \brief Widest path operation traits for the Dijkstra algorithm class. |
57 | 58 |
/// |
58 | 59 |
/// This operation traits class defines all computational operations and |
59 | 60 |
/// constants which are used in the Dijkstra algorithm for widest path |
60 | 61 |
/// computation. |
61 | 62 |
/// |
62 | 63 |
/// \see DijkstraDefaultOperationTraits |
63 | 64 |
template <typename Value> |
64 | 65 |
struct DijkstraWidestPathOperationTraits { |
... | ... |
@@ -167,65 +168,65 @@ |
167 | 168 |
{ |
168 | 169 |
return new ProcessedMap(); |
169 | 170 |
} |
170 | 171 |
|
171 | 172 |
///The type of the map that stores the distances of the nodes. |
172 | 173 |
|
173 | 174 |
///The type of the map that stores the distances of the nodes. |
174 | 175 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
175 | 176 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
176 | 177 |
///Instantiates a \ref DistMap. |
177 | 178 |
|
178 | 179 |
///This function instantiates a \ref DistMap. |
179 | 180 |
///\param g is the digraph, to which we would like to define |
180 | 181 |
///the \ref DistMap |
181 | 182 |
static DistMap *createDistMap(const Digraph &g) |
182 | 183 |
{ |
183 | 184 |
return new DistMap(g); |
184 | 185 |
} |
185 | 186 |
}; |
186 | 187 |
|
187 | 188 |
///%Dijkstra algorithm class. |
188 | 189 |
|
189 | 190 |
/// \ingroup shortest_path |
190 | 191 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
191 | 192 |
/// |
192 | 193 |
///The arc lengths are passed to the algorithm using a |
193 | 194 |
///\ref concepts::ReadMap "ReadMap", |
194 | 195 |
///so it is easy to change it to any kind of length. |
195 | 196 |
///The type of the length is determined by the |
196 | 197 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
197 | 198 |
///It is also possible to change the underlying priority heap. |
198 | 199 |
/// |
199 |
///There is also a \ref dijkstra() "function |
|
200 |
///There is also a \ref dijkstra() "function-type interface" for the |
|
200 | 201 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
201 | 202 |
///it can be used easier. |
202 | 203 |
/// |
203 | 204 |
///\tparam GR The type of the digraph the algorithm runs on. |
204 | 205 |
///The default value is \ref ListDigraph. |
205 | 206 |
///The value of GR is not used directly by \ref Dijkstra, it is only |
206 | 207 |
///passed to \ref DijkstraDefaultTraits. |
207 | 208 |
///\tparam LM A readable arc map that determines the lengths of the |
208 | 209 |
///arcs. It is read once for each arc, so the map may involve in |
209 | 210 |
///relatively time consuming process to compute the arc lengths if |
210 | 211 |
///it is necessary. The default map type is \ref |
211 | 212 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
212 | 213 |
///The value of LM is not used directly by \ref Dijkstra, it is only |
213 | 214 |
///passed to \ref DijkstraDefaultTraits. |
214 | 215 |
///\tparam TR Traits class to set various data types used by the algorithm. |
215 | 216 |
///The default traits class is \ref DijkstraDefaultTraits |
216 | 217 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
217 | 218 |
///for the documentation of a Dijkstra traits class. |
218 | 219 |
#ifdef DOXYGEN |
219 | 220 |
template <typename GR, typename LM, typename TR> |
220 | 221 |
#else |
221 | 222 |
template <typename GR=ListDigraph, |
222 | 223 |
typename LM=typename GR::template ArcMap<int>, |
223 | 224 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
224 | 225 |
#endif |
225 | 226 |
class Dijkstra { |
226 | 227 |
public: |
227 | 228 |
///\ref Exception for uninitialized parameters. |
228 | 229 |
|
229 | 230 |
///This error represents problems in the initialization of the |
230 | 231 |
///parameters of the algorithm. |
231 | 232 |
class UninitializedParameter : public lemon::UninitializedParameter { |
... | ... |
@@ -953,326 +954,353 @@ |
953 | 954 |
///This function instantiates a \ref HeapCrossRef. |
954 | 955 |
/// \param g is the digraph, to which we would like to define the |
955 | 956 |
/// HeapCrossRef. |
956 | 957 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
957 | 958 |
{ |
958 | 959 |
return new HeapCrossRef(g); |
959 | 960 |
} |
960 | 961 |
|
961 | 962 |
///The heap type used by the Dijkstra algorithm. |
962 | 963 |
|
963 | 964 |
///The heap type used by the Dijkstra algorithm. |
964 | 965 |
/// |
965 | 966 |
///\sa BinHeap |
966 | 967 |
///\sa Dijkstra |
967 | 968 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
968 | 969 |
std::less<Value> > Heap; |
969 | 970 |
|
970 | 971 |
///Instantiates a \ref Heap. |
971 | 972 |
|
972 | 973 |
///This function instantiates a \ref Heap. |
973 | 974 |
/// \param r is the HeapCrossRef which is used. |
974 | 975 |
static Heap *createHeap(HeapCrossRef& r) |
975 | 976 |
{ |
976 | 977 |
return new Heap(r); |
977 | 978 |
} |
978 | 979 |
|
979 | 980 |
///\brief The type of the map that stores the predecessor |
980 | 981 |
///arcs of the shortest paths. |
981 | 982 |
/// |
982 | 983 |
///The type of the map that stores the predecessor |
983 | 984 |
///arcs of the shortest paths. |
984 | 985 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
985 |
typedef |
|
986 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
986 | 987 |
///Instantiates a \ref PredMap. |
987 | 988 |
|
988 | 989 |
///This function instantiates a \ref PredMap. |
989 | 990 |
///\param g is the digraph, to which we would like to define the |
990 | 991 |
///\ref PredMap. |
991 |
#ifdef DOXYGEN |
|
992 | 992 |
static PredMap *createPredMap(const Digraph &g) |
993 |
#else |
|
994 |
static PredMap *createPredMap(const Digraph &) |
|
995 |
#endif |
|
996 | 993 |
{ |
997 |
return new PredMap(); |
|
994 |
return new PredMap(g); |
|
998 | 995 |
} |
999 | 996 |
|
1000 | 997 |
///The type of the map that indicates which nodes are processed. |
1001 | 998 |
|
1002 | 999 |
///The type of the map that indicates which nodes are processed. |
1003 | 1000 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1004 | 1001 |
///By default it is a NullMap. |
1005 | 1002 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1006 | 1003 |
///Instantiates a \ref ProcessedMap. |
1007 | 1004 |
|
1008 | 1005 |
///This function instantiates a \ref ProcessedMap. |
1009 | 1006 |
///\param g is the digraph, to which |
1010 | 1007 |
///we would like to define the \ref ProcessedMap. |
1011 | 1008 |
#ifdef DOXYGEN |
1012 | 1009 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1013 | 1010 |
#else |
1014 | 1011 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1015 | 1012 |
#endif |
1016 | 1013 |
{ |
1017 | 1014 |
return new ProcessedMap(); |
1018 | 1015 |
} |
1019 | 1016 |
|
1020 | 1017 |
///The type of the map that stores the distances of the nodes. |
1021 | 1018 |
|
1022 | 1019 |
///The type of the map that stores the distances of the nodes. |
1023 | 1020 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1024 |
typedef |
|
1021 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
|
1025 | 1022 |
///Instantiates a \ref DistMap. |
1026 | 1023 |
|
1027 | 1024 |
///This function instantiates a \ref DistMap. |
1028 | 1025 |
///\param g is the digraph, to which we would like to define |
1029 | 1026 |
///the \ref DistMap |
1030 |
#ifdef DOXYGEN |
|
1031 | 1027 |
static DistMap *createDistMap(const Digraph &g) |
1032 |
#else |
|
1033 |
static DistMap *createDistMap(const Digraph &) |
|
1034 |
#endif |
|
1035 | 1028 |
{ |
1036 |
return new DistMap(); |
|
1029 |
return new DistMap(g); |
|
1037 | 1030 |
} |
1031 |
|
|
1032 |
///The type of the shortest paths. |
|
1033 |
|
|
1034 |
///The type of the shortest paths. |
|
1035 |
///It must meet the \ref concepts::Path "Path" concept. |
|
1036 |
typedef lemon::Path<Digraph> Path; |
|
1038 | 1037 |
}; |
1039 | 1038 |
|
1040 | 1039 |
/// Default traits class used by \ref DijkstraWizard |
1041 | 1040 |
|
1042 | 1041 |
/// To make it easier to use Dijkstra algorithm |
1043 | 1042 |
/// we have created a wizard class. |
1044 | 1043 |
/// This \ref DijkstraWizard class needs default traits, |
1045 | 1044 |
/// as well as the \ref Dijkstra class. |
1046 | 1045 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1047 | 1046 |
/// \ref DijkstraWizard class. |
1048 | 1047 |
template<class GR,class LM> |
1049 | 1048 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1050 | 1049 |
{ |
1051 | 1050 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1052 | 1051 |
protected: |
1053 | 1052 |
//The type of the nodes in the digraph. |
1054 | 1053 |
typedef typename Base::Digraph::Node Node; |
1055 | 1054 |
|
1056 | 1055 |
//Pointer to the digraph the algorithm runs on. |
1057 | 1056 |
void *_g; |
1058 |
//Pointer to the length map |
|
1057 |
//Pointer to the length map. |
|
1059 | 1058 |
void *_length; |
1060 | 1059 |
//Pointer to the map of processed nodes. |
1061 | 1060 |
void *_processed; |
1062 | 1061 |
//Pointer to the map of predecessors arcs. |
1063 | 1062 |
void *_pred; |
1064 | 1063 |
//Pointer to the map of distances. |
1065 | 1064 |
void *_dist; |
1066 |
//Pointer to the source node. |
|
1067 |
Node _source; |
|
1065 |
//Pointer to the shortest path to the target node. |
|
1066 |
void *_path; |
|
1067 |
//Pointer to the distance of the target node. |
|
1068 |
void *_di; |
|
1068 | 1069 |
|
1069 | 1070 |
public: |
1070 | 1071 |
/// Constructor. |
1071 | 1072 |
|
1072 | 1073 |
/// This constructor does not require parameters, therefore it initiates |
1073 |
/// all of the attributes to |
|
1074 |
/// all of the attributes to \c 0. |
|
1074 | 1075 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1075 |
_dist(0), |
|
1076 |
_dist(0), _path(0), _di(0) {} |
|
1076 | 1077 |
|
1077 | 1078 |
/// Constructor. |
1078 | 1079 |
|
1079 |
/// This constructor requires some parameters, |
|
1080 |
/// listed in the parameters list. |
|
1081 |
/// |
|
1080 |
/// This constructor requires two parameters, |
|
1081 |
/// others are initiated to \c 0. |
|
1082 | 1082 |
/// \param g The digraph the algorithm runs on. |
1083 | 1083 |
/// \param l The length map. |
1084 |
/// \param s The source node. |
|
1085 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
|
1084 |
DijkstraWizardBase(const GR &g,const LM &l) : |
|
1086 | 1085 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1087 | 1086 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1088 |
_processed(0), _pred(0), _dist(0), |
|
1087 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
|
1089 | 1088 |
|
1090 | 1089 |
}; |
1091 | 1090 |
|
1092 |
/// Auxiliary class for the function |
|
1091 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
|
1093 | 1092 |
|
1094 |
/// This auxiliary class is created to implement the function type |
|
1095 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features |
|
1096 |
/// of the plain \ref Dijkstra, but it is much simpler to use it. |
|
1097 |
/// It should only be used through the \ref dijkstra() function, which makes |
|
1098 |
/// |
|
1093 |
/// This auxiliary class is created to implement the |
|
1094 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
|
1095 |
/// It does not have own \ref run() method, it uses the functions |
|
1096 |
/// and features of the plain \ref Dijkstra. |
|
1099 | 1097 |
/// |
1100 |
/// Simplicity means that the way to change the types defined |
|
1101 |
/// in the traits class is based on functions that returns the new class |
|
1102 |
/// and not on templatable built-in classes. |
|
1103 |
/// When using the plain \ref Dijkstra |
|
1104 |
/// the new class with the modified type comes from |
|
1105 |
/// the original class by using the :: |
|
1106 |
/// operator. In the case of \ref DijkstraWizard only |
|
1107 |
/// a function have to be called, and it will |
|
1108 |
/// return the needed class. |
|
1109 |
/// |
|
1110 |
/// It does not have own \ref run() method. When its \ref run() method |
|
1111 |
/// is called, it initiates a plain \ref Dijkstra object, and calls the |
|
1112 |
/// \ref |
|
1098 |
/// This class should only be used through the \ref dijkstra() function, |
|
1099 |
/// which makes it easier to use the algorithm. |
|
1113 | 1100 |
template<class TR> |
1114 | 1101 |
class DijkstraWizard : public TR |
1115 | 1102 |
{ |
1116 | 1103 |
typedef TR Base; |
1117 | 1104 |
|
1118 | 1105 |
///The type of the digraph the algorithm runs on. |
1119 | 1106 |
typedef typename TR::Digraph Digraph; |
1120 | 1107 |
|
1121 | 1108 |
typedef typename Digraph::Node Node; |
1122 | 1109 |
typedef typename Digraph::NodeIt NodeIt; |
1123 | 1110 |
typedef typename Digraph::Arc Arc; |
1124 | 1111 |
typedef typename Digraph::OutArcIt OutArcIt; |
1125 | 1112 |
|
1126 | 1113 |
///The type of the map that stores the arc lengths. |
1127 | 1114 |
typedef typename TR::LengthMap LengthMap; |
1128 | 1115 |
///The type of the length of the arcs. |
1129 | 1116 |
typedef typename LengthMap::Value Value; |
1130 | 1117 |
///\brief The type of the map that stores the predecessor |
1131 | 1118 |
///arcs of the shortest paths. |
1132 | 1119 |
typedef typename TR::PredMap PredMap; |
1133 | 1120 |
///The type of the map that stores the distances of the nodes. |
1134 | 1121 |
typedef typename TR::DistMap DistMap; |
1135 | 1122 |
///The type of the map that indicates which nodes are processed. |
1136 | 1123 |
typedef typename TR::ProcessedMap ProcessedMap; |
1124 |
///The type of the shortest paths |
|
1125 |
typedef typename TR::Path Path; |
|
1137 | 1126 |
///The heap type used by the dijkstra algorithm. |
1138 | 1127 |
typedef typename TR::Heap Heap; |
1139 | 1128 |
|
1140 | 1129 |
public: |
1141 | 1130 |
|
1142 | 1131 |
/// Constructor. |
1143 | 1132 |
DijkstraWizard() : TR() {} |
1144 | 1133 |
|
1145 | 1134 |
/// Constructor that requires parameters. |
1146 | 1135 |
|
1147 | 1136 |
/// Constructor that requires parameters. |
1148 | 1137 |
/// These parameters will be the default values for the traits class. |
1149 |
DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
|
1150 |
TR(g,l,s) {} |
|
1138 |
/// \param g The digraph the algorithm runs on. |
|
1139 |
/// \param l The length map. |
|
1140 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
|
1141 |
TR(g,l) {} |
|
1151 | 1142 |
|
1152 | 1143 |
///Copy constructor |
1153 | 1144 |
DijkstraWizard(const TR &b) : TR(b) {} |
1154 | 1145 |
|
1155 | 1146 |
~DijkstraWizard() {} |
1156 | 1147 |
|
1157 |
///Runs Dijkstra algorithm from |
|
1148 |
///Runs Dijkstra algorithm from the given source node. |
|
1158 | 1149 |
|
1159 |
///Runs Dijkstra algorithm from a source node. |
|
1160 |
///The node can be given with the \ref source() function. |
|
1161 |
|
|
1150 |
///This method runs %Dijkstra algorithm from the given source node |
|
1151 |
///in order to compute the shortest path to each node. |
|
1152 |
void run(Node s) |
|
1162 | 1153 |
{ |
1163 |
if |
|
1154 |
if (s==INVALID) throw UninitializedParameter(); |
|
1164 | 1155 |
Dijkstra<Digraph,LengthMap,TR> |
1165 |
dij(*reinterpret_cast<const Digraph*>(Base::_g), |
|
1166 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1167 |
if(Base::_processed) |
|
1168 |
dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1169 |
if(Base::_pred) |
|
1170 |
dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1171 |
if(Base::_dist) |
|
1172 |
dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1173 |
|
|
1156 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
|
1157 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1158 |
if (Base::_pred) |
|
1159 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1160 |
if (Base::_dist) |
|
1161 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1162 |
if (Base::_processed) |
|
1163 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1164 |
dijk.run(s); |
|
1174 | 1165 |
} |
1175 | 1166 |
|
1176 |
/// |
|
1167 |
///Finds the shortest path between \c s and \c t. |
|
1177 | 1168 |
|
1178 |
///Runs Dijkstra algorithm from the given node. |
|
1179 |
///\param s is the given source. |
|
1180 |
|
|
1169 |
///This method runs the %Dijkstra algorithm from node \c s |
|
1170 |
///in order to compute the shortest path to node \c t |
|
1171 |
///(it stops searching when \c t is processed). |
|
1172 |
/// |
|
1173 |
///\return \c true if \c t is reachable form \c s. |
|
1174 |
bool run(Node s, Node t) |
|
1181 | 1175 |
{ |
1182 |
Base::_source=s; |
|
1183 |
run(); |
|
1184 |
} |
|
1185 |
|
|
1186 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1187 |
|
|
1188 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1189 |
/// \param s is the source node. |
|
1190 |
DijkstraWizard<TR> &source(Node s) |
|
1191 |
{ |
|
1192 |
Base::_source=s; |
|
1193 |
return *this; |
|
1176 |
if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
|
1177 |
Dijkstra<Digraph,LengthMap,TR> |
|
1178 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
|
1179 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1180 |
if (Base::_pred) |
|
1181 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1182 |
if (Base::_dist) |
|
1183 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1184 |
if (Base::_processed) |
|
1185 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
|
1186 |
dijk.run(s,t); |
|
1187 |
if (Base::_path) |
|
1188 |
*reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
|
1189 |
if (Base::_di) |
|
1190 |
*reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
|
1191 |
return dijk.reached(t); |
|
1194 | 1192 |
} |
1195 | 1193 |
|
1196 | 1194 |
template<class T> |
1197 | 1195 |
struct SetPredMapBase : public Base { |
1198 | 1196 |
typedef T PredMap; |
1199 | 1197 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1200 | 1198 |
SetPredMapBase(const TR &b) : TR(b) {} |
1201 | 1199 |
}; |
1202 |
///\brief \ref named- |
|
1200 |
///\brief \ref named-func-param "Named parameter" |
|
1203 | 1201 |
///for setting \ref PredMap object. |
1204 | 1202 |
/// |
1205 |
///\ref named- |
|
1203 |
///\ref named-func-param "Named parameter" |
|
1206 | 1204 |
///for setting \ref PredMap object. |
1207 | 1205 |
template<class T> |
1208 | 1206 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1209 | 1207 |
{ |
1210 | 1208 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1211 | 1209 |
return DijkstraWizard<SetPredMapBase<T> >(*this); |
1212 | 1210 |
} |
1213 | 1211 |
|
1214 | 1212 |
template<class T> |
1213 |
struct SetDistMapBase : public Base { |
|
1214 |
typedef T DistMap; |
|
1215 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1216 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1217 |
}; |
|
1218 |
///\brief \ref named-func-param "Named parameter" |
|
1219 |
///for setting \ref DistMap object. |
|
1220 |
/// |
|
1221 |
///\ref named-func-param "Named parameter" |
|
1222 |
///for setting \ref DistMap object. |
|
1223 |
template<class T> |
|
1224 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
|
1225 |
{ |
|
1226 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1227 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
|
1228 |
} |
|
1229 |
|
|
1230 |
template<class T> |
|
1215 | 1231 |
struct SetProcessedMapBase : public Base { |
1216 | 1232 |
typedef T ProcessedMap; |
1217 | 1233 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1218 | 1234 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1219 | 1235 |
}; |
1220 |
///\brief \ref named- |
|
1236 |
///\brief \ref named-func-param "Named parameter" |
|
1221 | 1237 |
///for setting \ref ProcessedMap object. |
1222 | 1238 |
/// |
1223 |
/// \ref named- |
|
1239 |
/// \ref named-func-param "Named parameter" |
|
1224 | 1240 |
///for setting \ref ProcessedMap object. |
1225 | 1241 |
template<class T> |
1226 | 1242 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1227 | 1243 |
{ |
1228 | 1244 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1229 | 1245 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1230 | 1246 |
} |
1231 | 1247 |
|
1232 | 1248 |
template<class T> |
1233 |
struct SetDistMapBase : public Base { |
|
1234 |
typedef T DistMap; |
|
1235 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1236 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1249 |
struct SetPathBase : public Base { |
|
1250 |
typedef T Path; |
|
1251 |
SetPathBase(const TR &b) : TR(b) {} |
|
1237 | 1252 |
}; |
1238 |
///\brief \ref named-templ-param "Named parameter" |
|
1239 |
///for setting \ref DistMap object. |
|
1253 |
///\brief \ref named-func-param "Named parameter" |
|
1254 |
///for getting the shortest path to the target node. |
|
1240 | 1255 |
/// |
1241 |
///\ref named-templ-param "Named parameter" |
|
1242 |
///for setting \ref DistMap object. |
|
1256 |
///\ref named-func-param "Named parameter" |
|
1257 |
///for getting the shortest path to the target node. |
|
1243 | 1258 |
template<class T> |
1244 |
DijkstraWizard< |
|
1259 |
DijkstraWizard<SetPathBase<T> > path(const T &t) |
|
1245 | 1260 |
{ |
1246 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1247 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
|
1261 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1262 |
return DijkstraWizard<SetPathBase<T> >(*this); |
|
1263 |
} |
|
1264 |
|
|
1265 |
///\brief \ref named-func-param "Named parameter" |
|
1266 |
///for getting the distance of the target node. |
|
1267 |
/// |
|
1268 |
///\ref named-func-param "Named parameter" |
|
1269 |
///for getting the distance of the target node. |
|
1270 |
DijkstraWizard dist(const Value &d) |
|
1271 |
{ |
|
1272 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
|
1273 |
return *this; |
|
1248 | 1274 |
} |
1249 | 1275 |
|
1250 | 1276 |
}; |
1251 | 1277 |
|
1252 |
///Function |
|
1278 |
///Function-type interface for Dijkstra algorithm. |
|
1253 | 1279 |
|
1254 | 1280 |
/// \ingroup shortest_path |
1255 |
///Function |
|
1281 |
///Function-type interface for Dijkstra algorithm. |
|
1256 | 1282 |
/// |
1257 |
///This function also has several |
|
1258 |
///\ref named-templ-func-param "named parameters", |
|
1283 |
///This function also has several \ref named-func-param "named parameters", |
|
1259 | 1284 |
///they are declared as the members of class \ref DijkstraWizard. |
1260 |
///The following |
|
1261 |
///example shows how to use these parameters. |
|
1285 |
///The following examples show how to use these parameters. |
|
1262 | 1286 |
///\code |
1263 |
/// |
|
1287 |
/// // Compute shortest path from node s to each node |
|
1288 |
/// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
|
1289 |
/// |
|
1290 |
/// // Compute shortest path from s to t |
|
1291 |
/// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
|
1264 | 1292 |
///\endcode |
1265 | 1293 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1266 | 1294 |
///to the end of the parameter list. |
1267 | 1295 |
///\sa DijkstraWizard |
1268 | 1296 |
///\sa Dijkstra |
1269 | 1297 |
template<class GR, class LM> |
1270 | 1298 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1271 |
dijkstra(const GR & |
|
1299 |
dijkstra(const GR &digraph, const LM &length) |
|
1272 | 1300 |
{ |
1273 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >( |
|
1301 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length); |
|
1274 | 1302 |
} |
1275 | 1303 |
|
1276 | 1304 |
} //END OF NAMESPACE LEMON |
1277 | 1305 |
|
1278 | 1306 |
#endif |
... | ... |
@@ -33,123 +33,140 @@ |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"@arcs\n" |
41 | 41 |
" label\n" |
42 | 42 |
"0 1 0\n" |
43 | 43 |
"1 2 1\n" |
44 | 44 |
"2 3 2\n" |
45 | 45 |
"3 4 3\n" |
46 | 46 |
"0 3 4\n" |
47 | 47 |
"0 3 5\n" |
48 | 48 |
"5 2 6\n" |
49 | 49 |
"@attributes\n" |
50 | 50 |
"source 0\n" |
51 | 51 |
"target 4\n"; |
52 | 52 |
|
53 | 53 |
void checkBfsCompile() |
54 | 54 |
{ |
55 | 55 |
typedef concepts::Digraph Digraph; |
56 | 56 |
typedef Bfs<Digraph> BType; |
57 | 57 |
|
58 | 58 |
Digraph G; |
59 | 59 |
Digraph::Node n; |
60 | 60 |
Digraph::Arc e; |
61 | 61 |
int l; |
62 | 62 |
bool b; |
63 | 63 |
BType::DistMap d(G); |
64 | 64 |
BType::PredMap p(G); |
65 |
// BType::PredNodeMap pn(G); |
|
66 | 65 |
|
67 | 66 |
BType bfs_test(G); |
68 | 67 |
|
69 | 68 |
bfs_test.run(n); |
70 | 69 |
|
71 | 70 |
l = bfs_test.dist(n); |
72 | 71 |
e = bfs_test.predArc(n); |
73 | 72 |
n = bfs_test.predNode(n); |
74 | 73 |
d = bfs_test.distMap(); |
75 |
|
|
76 | 74 |
p = bfs_test.predMap(); |
77 |
// pn = bfs_test.predNodeMap(); |
|
78 | 75 |
b = bfs_test.reached(n); |
79 | 76 |
|
80 | 77 |
Path<Digraph> pp = bfs_test.path(n); |
81 | 78 |
} |
82 | 79 |
|
83 | 80 |
void checkBfsFunctionCompile() |
84 | 81 |
{ |
85 | 82 |
typedef int VType; |
86 | 83 |
typedef concepts::Digraph Digraph; |
87 | 84 |
typedef Digraph::Arc Arc; |
88 | 85 |
typedef Digraph::Node Node; |
89 | 86 |
|
90 | 87 |
Digraph g; |
91 |
bfs(g,Node()).run(); |
|
92 |
bfs(g).source(Node()).run(); |
|
88 |
bool b; |
|
89 |
bfs(g).run(Node()); |
|
90 |
b=bfs(g).run(Node(),Node()); |
|
91 |
bfs(g).run(); |
|
93 | 92 |
bfs(g) |
94 |
.predMap(concepts::WriteMap<Node,Arc>()) |
|
95 |
.distMap(concepts::WriteMap<Node,VType>()) |
|
93 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
94 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
96 | 95 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
97 | 96 |
.processedMap(concepts::WriteMap<Node,bool>()) |
98 | 97 |
.run(Node()); |
98 |
b=bfs(g) |
|
99 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
100 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
101 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
102 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
103 |
.path(concepts::Path<Digraph>()) |
|
104 |
.dist(VType()) |
|
105 |
.run(Node(),Node()); |
|
106 |
bfs(g) |
|
107 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
108 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
109 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
110 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
111 |
.run(); |
|
99 | 112 |
} |
100 | 113 |
|
101 | 114 |
template <class Digraph> |
102 | 115 |
void checkBfs() { |
103 | 116 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
104 | 117 |
|
105 | 118 |
Digraph G; |
106 | 119 |
Node s, t; |
107 | 120 |
|
108 | 121 |
std::istringstream input(test_lgf); |
109 | 122 |
digraphReader(input, G). |
110 | 123 |
node("source", s). |
111 | 124 |
node("target", t). |
112 | 125 |
run(); |
113 | 126 |
|
114 | 127 |
Bfs<Digraph> bfs_test(G); |
115 | 128 |
bfs_test.run(s); |
116 | 129 |
|
117 |
check(bfs_test.dist(t)==2,"Bfs found a wrong path." |
|
130 |
check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
|
118 | 131 |
|
119 | 132 |
Path<Digraph> p = bfs_test.path(t); |
120 | 133 |
check(p.length()==2,"path() found a wrong path."); |
121 | 134 |
check(checkPath(G, p),"path() found a wrong path."); |
122 | 135 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
123 | 136 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
124 | 137 |
|
125 | 138 |
|
126 | 139 |
for(ArcIt a(G); a!=INVALID; ++a) { |
127 | 140 |
Node u=G.source(a); |
128 | 141 |
Node v=G.target(a); |
129 | 142 |
check( !bfs_test.reached(u) || |
130 | 143 |
(bfs_test.dist(v) <= bfs_test.dist(u)+1), |
131 |
"Wrong output." << G.id(v) |
|
144 |
"Wrong output. " << G.id(u) << "->" << G.id(v)); |
|
132 | 145 |
} |
133 | 146 |
|
134 | 147 |
for(NodeIt v(G); v!=INVALID; ++v) { |
135 | 148 |
if (bfs_test.reached(v)) { |
136 | 149 |
check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); |
137 | 150 |
if (bfs_test.predArc(v)!=INVALID ) { |
138 | 151 |
Arc a=bfs_test.predArc(v); |
139 | 152 |
Node u=G.source(a); |
140 | 153 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
141 | 154 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
142 | 155 |
"Wrong distance. Difference: " |
143 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) |
|
144 |
- 1)); |
|
156 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
|
145 | 157 |
} |
146 | 158 |
} |
147 | 159 |
} |
160 |
|
|
161 |
{ |
|
162 |
NullMap<Node,Arc> myPredMap; |
|
163 |
bfs(G).predMap(myPredMap).run(s); |
|
164 |
} |
|
148 | 165 |
} |
149 | 166 |
|
150 | 167 |
int main() |
151 | 168 |
{ |
152 | 169 |
checkBfs<ListDigraph>(); |
153 | 170 |
checkBfs<SmartDigraph>(); |
154 | 171 |
return 0; |
155 | 172 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 |
|
|
24 | 23 |
#include <lemon/dfs.h> |
25 | 24 |
#include <lemon/path.h> |
26 | 25 |
|
27 | 26 |
#include "graph_test.h" |
28 | 27 |
#include "test_tools.h" |
29 | 28 |
|
30 | 29 |
using namespace lemon; |
31 | 30 |
|
32 | 31 |
char test_lgf[] = |
33 | 32 |
"@nodes\n" |
34 | 33 |
"label\n" |
35 | 34 |
"0\n" |
36 | 35 |
"1\n" |
37 | 36 |
"2\n" |
38 | 37 |
"3\n" |
39 | 38 |
"4\n" |
40 | 39 |
"5\n" |
41 | 40 |
"6\n" |
42 | 41 |
"@arcs\n" |
43 | 42 |
" label\n" |
44 | 43 |
"0 1 0\n" |
45 | 44 |
"1 2 1\n" |
46 | 45 |
"2 3 2\n" |
47 | 46 |
"1 4 3\n" |
48 | 47 |
"4 2 4\n" |
49 | 48 |
"4 5 5\n" |
50 | 49 |
"5 0 6\n" |
51 | 50 |
"6 3 7\n" |
52 | 51 |
"@attributes\n" |
53 | 52 |
"source 0\n" |
54 | 53 |
"target 5\n"; |
55 | 54 |
|
... | ... |
@@ -59,85 +58,106 @@ |
59 | 58 |
typedef Dfs<Digraph> DType; |
60 | 59 |
|
61 | 60 |
Digraph G; |
62 | 61 |
Digraph::Node n; |
63 | 62 |
Digraph::Arc e; |
64 | 63 |
int l; |
65 | 64 |
bool b; |
66 | 65 |
DType::DistMap d(G); |
67 | 66 |
DType::PredMap p(G); |
68 | 67 |
|
69 | 68 |
DType dfs_test(G); |
70 | 69 |
|
71 | 70 |
dfs_test.run(n); |
72 | 71 |
|
73 | 72 |
l = dfs_test.dist(n); |
74 | 73 |
e = dfs_test.predArc(n); |
75 | 74 |
n = dfs_test.predNode(n); |
76 | 75 |
d = dfs_test.distMap(); |
77 | 76 |
p = dfs_test.predMap(); |
78 | 77 |
b = dfs_test.reached(n); |
79 | 78 |
|
80 | 79 |
Path<Digraph> pp = dfs_test.path(n); |
81 | 80 |
} |
82 | 81 |
|
83 | 82 |
void checkDfsFunctionCompile() |
84 | 83 |
{ |
85 | 84 |
typedef int VType; |
86 | 85 |
typedef concepts::Digraph Digraph; |
87 | 86 |
typedef Digraph::Arc Arc; |
88 | 87 |
typedef Digraph::Node Node; |
89 | 88 |
|
90 | 89 |
Digraph g; |
91 |
dfs(g,Node()).run(); |
|
92 |
dfs(g).source(Node()).run(); |
|
90 |
bool b; |
|
91 |
dfs(g).run(Node()); |
|
92 |
b=dfs(g).run(Node(),Node()); |
|
93 |
dfs(g).run(); |
|
93 | 94 |
dfs(g) |
94 |
.predMap(concepts::WriteMap<Node,Arc>()) |
|
95 |
.distMap(concepts::WriteMap<Node,VType>()) |
|
95 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
96 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
96 | 97 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
97 | 98 |
.processedMap(concepts::WriteMap<Node,bool>()) |
98 | 99 |
.run(Node()); |
100 |
b=dfs(g) |
|
101 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
102 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
103 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
104 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
105 |
.path(concepts::Path<Digraph>()) |
|
106 |
.dist(VType()) |
|
107 |
.run(Node(),Node()); |
|
108 |
dfs(g) |
|
109 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
110 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
111 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
112 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
113 |
.run(); |
|
99 | 114 |
} |
100 | 115 |
|
101 | 116 |
template <class Digraph> |
102 | 117 |
void checkDfs() { |
103 | 118 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
104 | 119 |
|
105 | 120 |
Digraph G; |
106 | 121 |
Node s, t; |
107 | 122 |
|
108 | 123 |
std::istringstream input(test_lgf); |
109 | 124 |
digraphReader(input, G). |
110 | 125 |
node("source", s). |
111 | 126 |
node("target", t). |
112 | 127 |
run(); |
113 | 128 |
|
114 | 129 |
Dfs<Digraph> dfs_test(G); |
115 | 130 |
dfs_test.run(s); |
116 | 131 |
|
117 | 132 |
Path<Digraph> p = dfs_test.path(t); |
118 | 133 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
119 | 134 |
check(checkPath(G, p),"path() found a wrong path."); |
120 | 135 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
121 | 136 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
122 | 137 |
|
123 | 138 |
for(NodeIt v(G); v!=INVALID; ++v) { |
124 | 139 |
if (dfs_test.reached(v)) { |
125 | 140 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
126 | 141 |
if (dfs_test.predArc(v)!=INVALID ) { |
127 | 142 |
Arc e=dfs_test.predArc(v); |
128 | 143 |
Node u=G.source(e); |
129 | 144 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
130 | 145 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
131 | 146 |
"Wrong distance. (" << dfs_test.dist(u) << "->" |
132 |
<<dfs_test.dist(v) << |
|
147 |
<< dfs_test.dist(v) << ")"); |
|
133 | 148 |
} |
134 | 149 |
} |
135 | 150 |
} |
151 |
|
|
152 |
{ |
|
153 |
NullMap<Node,Arc> myPredMap; |
|
154 |
dfs(G).predMap(myPredMap).run(s); |
|
155 |
} |
|
136 | 156 |
} |
137 | 157 |
|
138 | 158 |
int main() |
139 | 159 |
{ |
140 | 160 |
checkDfs<ListDigraph>(); |
141 | 161 |
checkDfs<SmartDigraph>(); |
142 | 162 |
return 0; |
143 | 163 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 |
|
|
24 | 23 |
#include <lemon/dijkstra.h> |
25 | 24 |
#include <lemon/path.h> |
26 | 25 |
|
27 | 26 |
#include "graph_test.h" |
28 | 27 |
#include "test_tools.h" |
29 | 28 |
|
30 | 29 |
using namespace lemon; |
31 | 30 |
|
32 | 31 |
char test_lgf[] = |
33 | 32 |
"@nodes\n" |
34 | 33 |
"label\n" |
35 | 34 |
"0\n" |
36 | 35 |
"1\n" |
37 | 36 |
"2\n" |
38 | 37 |
"3\n" |
39 | 38 |
"4\n" |
40 | 39 |
"@arcs\n" |
41 | 40 |
" label length\n" |
42 | 41 |
"0 1 0 1\n" |
43 | 42 |
"1 2 1 1\n" |
44 | 43 |
"2 3 2 1\n" |
45 | 44 |
"0 3 4 5\n" |
46 | 45 |
"0 3 5 10\n" |
47 | 46 |
"0 3 6 7\n" |
48 | 47 |
"4 2 7 1\n" |
49 | 48 |
"@attributes\n" |
50 | 49 |
"source 0\n" |
51 | 50 |
"target 3\n"; |
52 | 51 |
|
53 | 52 |
void checkDijkstraCompile() |
54 | 53 |
{ |
55 | 54 |
typedef int VType; |
56 | 55 |
typedef concepts::Digraph Digraph; |
57 | 56 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
58 | 57 |
typedef Dijkstra<Digraph, LengthMap> DType; |
59 | 58 |
|
60 | 59 |
Digraph G; |
61 | 60 |
Digraph::Node n; |
62 | 61 |
Digraph::Arc e; |
63 | 62 |
VType l; |
64 | 63 |
bool b; |
65 | 64 |
DType::DistMap d(G); |
66 | 65 |
DType::PredMap p(G); |
67 |
// DType::PredNodeMap pn(G); |
|
68 | 66 |
LengthMap length; |
69 | 67 |
|
70 | 68 |
DType dijkstra_test(G,length); |
71 | 69 |
|
72 | 70 |
dijkstra_test.run(n); |
73 | 71 |
|
74 | 72 |
l = dijkstra_test.dist(n); |
75 | 73 |
e = dijkstra_test.predArc(n); |
76 | 74 |
n = dijkstra_test.predNode(n); |
77 | 75 |
d = dijkstra_test.distMap(); |
78 | 76 |
p = dijkstra_test.predMap(); |
79 |
// pn = dijkstra_test.predNodeMap(); |
|
80 | 77 |
b = dijkstra_test.reached(n); |
81 | 78 |
|
82 | 79 |
Path<Digraph> pp = dijkstra_test.path(n); |
83 | 80 |
} |
84 | 81 |
|
85 | 82 |
void checkDijkstraFunctionCompile() |
86 | 83 |
{ |
87 | 84 |
typedef int VType; |
88 | 85 |
typedef concepts::Digraph Digraph; |
89 | 86 |
typedef Digraph::Arc Arc; |
90 | 87 |
typedef Digraph::Node Node; |
91 | 88 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
92 | 89 |
|
93 | 90 |
Digraph g; |
94 |
dijkstra(g,LengthMap(),Node()).run(); |
|
95 |
dijkstra(g,LengthMap()).source(Node()).run(); |
|
91 |
bool b; |
|
92 |
dijkstra(g,LengthMap()).run(Node()); |
|
93 |
b=dijkstra(g,LengthMap()).run(Node(),Node()); |
|
96 | 94 |
dijkstra(g,LengthMap()) |
97 |
.predMap(concepts::WriteMap<Node,Arc>()) |
|
98 |
.distMap(concepts::WriteMap<Node,VType>()) |
|
95 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
96 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
97 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
99 | 98 |
.run(Node()); |
99 |
b=dijkstra(g,LengthMap()) |
|
100 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
|
101 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
|
102 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
103 |
.path(concepts::Path<Digraph>()) |
|
104 |
.dist(VType()) |
|
105 |
.run(Node(),Node()); |
|
100 | 106 |
} |
101 | 107 |
|
102 | 108 |
template <class Digraph> |
103 | 109 |
void checkDijkstra() { |
104 | 110 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
105 | 111 |
typedef typename Digraph::template ArcMap<int> LengthMap; |
106 | 112 |
|
107 | 113 |
Digraph G; |
108 | 114 |
Node s, t; |
109 | 115 |
LengthMap length(G); |
110 | 116 |
|
111 | 117 |
std::istringstream input(test_lgf); |
112 | 118 |
digraphReader(input, G). |
113 | 119 |
arcMap("length", length). |
114 | 120 |
node("source", s). |
115 | 121 |
node("target", t). |
116 | 122 |
run(); |
117 | 123 |
|
118 | 124 |
Dijkstra<Digraph, LengthMap> |
119 | 125 |
dijkstra_test(G, length); |
120 | 126 |
dijkstra_test.run(s); |
121 | 127 |
|
122 | 128 |
check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path."); |
123 | 129 |
|
124 | 130 |
Path<Digraph> p = dijkstra_test.path(t); |
125 |
check(p.length()==3," |
|
131 |
check(p.length()==3,"path() found a wrong path."); |
|
126 | 132 |
check(checkPath(G, p),"path() found a wrong path."); |
127 | 133 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
128 | 134 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
129 | 135 |
|
130 | 136 |
for(ArcIt e(G); e!=INVALID; ++e) { |
131 | 137 |
Node u=G.source(e); |
132 | 138 |
Node v=G.target(e); |
133 | 139 |
check( !dijkstra_test.reached(u) || |
134 | 140 |
(dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), |
135 |
"dist(target)-dist(source)-arc_length= |
|
141 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
|
136 | 142 |
dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); |
137 | 143 |
} |
138 | 144 |
|
139 | 145 |
for(NodeIt v(G); v!=INVALID; ++v) { |
140 | 146 |
if (dijkstra_test.reached(v)) { |
141 | 147 |
check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree."); |
142 | 148 |
if (dijkstra_test.predArc(v)!=INVALID ) { |
143 | 149 |
Arc e=dijkstra_test.predArc(v); |
144 | 150 |
Node u=G.source(e); |
145 | 151 |
check(u==dijkstra_test.predNode(v),"Wrong tree."); |
146 | 152 |
check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e], |
147 | 153 |
"Wrong distance! Difference: " << |
148 | 154 |
std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e])); |
149 | 155 |
} |
150 | 156 |
} |
151 | 157 |
} |
152 | 158 |
|
153 | 159 |
{ |
154 | 160 |
NullMap<Node,Arc> myPredMap; |
155 | 161 |
dijkstra(G,length).predMap(myPredMap).run(s); |
156 | 162 |
} |
157 | 163 |
} |
158 | 164 |
|
159 | 165 |
int main() { |
160 | 166 |
checkDijkstra<ListDigraph>(); |
161 | 167 |
checkDijkstra<SmartDigraph>(); |
162 | 168 |
return 0; |
163 | 169 |
} |
0 comments (0 inline)