alpar@2
|
1 |
// -*-mode: c++; -*-
|
alpar@2
|
2 |
#ifndef _BFS_H_
|
alpar@2
|
3 |
#define _BFS_H_
|
alpar@2
|
4 |
|
alpar@2
|
5 |
#include <queue>
|
alpar@2
|
6 |
#include <graph.h>
|
alpar@2
|
7 |
|
alpar@2
|
8 |
namespace NEGRO
|
alpar@2
|
9 |
{
|
alpar@2
|
10 |
using namespace std;
|
alpar@2
|
11 |
|
alpar@2
|
12 |
// template <typename G,typename> class bfs_T
|
alpar@2
|
13 |
// {
|
alpar@2
|
14 |
// typedef G Graph;
|
alpar@2
|
15 |
// typedef Graph::OutEdgeIterator EdgeIterator; //Ez kell
|
alpar@2
|
16 |
// typedef Graph::NodeIterator NodeIterator;
|
alpar@2
|
17 |
|
alpar@2
|
18 |
// class bfs_node_D
|
alpar@2
|
19 |
// {
|
alpar@2
|
20 |
// EdgeIterator Tree;
|
alpar@2
|
21 |
// int Dist;
|
alpar@2
|
22 |
// int Priority;
|
alpar@2
|
23 |
// }
|
alpar@2
|
24 |
|
alpar@2
|
25 |
|
alpar@2
|
26 |
// // template <bfs_T<G>>
|
alpar@2
|
27 |
// // void bfs(bfs_T::Graph &G,const bfs_T::NodeIterator &start_node,const &maps)
|
alpar@2
|
28 |
|
alpar@2
|
29 |
|
alpar@2
|
30 |
|
alpar@2
|
31 |
// template <class G>
|
alpar@2
|
32 |
// class bfs_maps_T
|
alpar@2
|
33 |
// {
|
alpar@2
|
34 |
// typedef visited; //node->bool (RW)
|
alpar@2
|
35 |
// typedef tree; //node->EdgeIterator (W)
|
alpar@2
|
36 |
// typedef dist; //node->int (W)
|
alpar@2
|
37 |
// typedef priority; //node->int (W)
|
alpar@2
|
38 |
// };
|
alpar@2
|
39 |
|
alpar@2
|
40 |
|
alpar@2
|
41 |
// Nem jo! Osszeakad a masik Set-tel
|
alpar@2
|
42 |
// class do_nothing_map {};
|
alpar@2
|
43 |
// template <typename V,typename T>
|
alpar@2
|
44 |
// void Set(const do_nothing_map &p,const V &v,const T &t) {}
|
alpar@2
|
45 |
|
alpar@2
|
46 |
struct do_nothing_map {
|
alpar@2
|
47 |
template <typename V,typename T>
|
alpar@2
|
48 |
static void Set(V v,T t) {}
|
alpar@2
|
49 |
};
|
alpar@2
|
50 |
|
alpar@2
|
51 |
|
alpar@2
|
52 |
template <typename I,typename C, typename T, T C::*M>
|
alpar@2
|
53 |
class class_element_map {
|
alpar@2
|
54 |
public:
|
alpar@2
|
55 |
typedef T value_type;
|
alpar@2
|
56 |
static void Set(const I &i, const T &t) {(*i).*M=t;}
|
alpar@2
|
57 |
static T Get(const I i) {return (*i).*M;}
|
alpar@2
|
58 |
T &operator[](I i) {return (*i).*M;}
|
alpar@2
|
59 |
};
|
alpar@2
|
60 |
|
alpar@2
|
61 |
/*
|
alpar@2
|
62 |
template <typename C,typename I,typename T, T C::*M>
|
alpar@2
|
63 |
void Set(class_element_map<C,T,M> p,I i, T t)
|
alpar@2
|
64 |
{
|
alpar@2
|
65 |
i->*M=t;
|
alpar@2
|
66 |
};
|
alpar@2
|
67 |
*/
|
alpar@2
|
68 |
|
alpar@2
|
69 |
template <typename P,typename I,typename T>
|
alpar@2
|
70 |
inline void Set(P &p,const I &i, const T &t)
|
alpar@2
|
71 |
{
|
alpar@2
|
72 |
p.Set(i,t);
|
alpar@2
|
73 |
};
|
alpar@2
|
74 |
template <typename P,typename I>
|
alpar@2
|
75 |
inline typename P::value_type Get(const P &p,const I &i)
|
alpar@2
|
76 |
{
|
alpar@2
|
77 |
return p.Get(i);
|
alpar@2
|
78 |
};
|
alpar@2
|
79 |
|
alpar@2
|
80 |
/*
|
alpar@2
|
81 |
template <typename G,typename T, typename G::EdgeType::*M>
|
alpar@2
|
82 |
T Get(edge_element_map p,G::EdgeIterator i)
|
alpar@2
|
83 |
{
|
alpar@2
|
84 |
return i->*M;
|
alpar@2
|
85 |
};
|
alpar@2
|
86 |
*/
|
alpar@2
|
87 |
|
alpar@2
|
88 |
/*
|
alpar@2
|
89 |
template <class G>
|
alpar@2
|
90 |
class default_bfs_maps
|
alpar@2
|
91 |
{
|
alpar@2
|
92 |
public:
|
alpar@2
|
93 |
typedef typename G::NodeType NodeType;
|
alpar@2
|
94 |
|
alpar@2
|
95 |
class_element_map<typename G::NodeIterator,
|
alpar@2
|
96 |
typename G::NodeType,
|
alpar@2
|
97 |
bool,&NodeType::isVis> visited; //node->bool (RW)
|
alpar@2
|
98 |
do_nothing_map tree; //node->EdgeIterator (W)
|
alpar@2
|
99 |
do_nothing_map dist; //node->int (W)
|
alpar@2
|
100 |
do_nothing_map priority; //node->int (W)
|
alpar@2
|
101 |
};
|
alpar@2
|
102 |
*/
|
alpar@2
|
103 |
|
alpar@2
|
104 |
template<class G>
|
alpar@2
|
105 |
struct bfs_node_data
|
alpar@2
|
106 |
{
|
alpar@2
|
107 |
bool visited;
|
alpar@2
|
108 |
typename G::EdgeIterator tree;
|
alpar@2
|
109 |
int dist;
|
alpar@2
|
110 |
int priority;
|
alpar@2
|
111 |
};
|
alpar@2
|
112 |
|
alpar@2
|
113 |
template <class G>
|
alpar@2
|
114 |
class bfs_static_maps
|
alpar@2
|
115 |
{
|
alpar@2
|
116 |
public:
|
alpar@2
|
117 |
typedef typename G::NodeType NodeType;
|
alpar@2
|
118 |
|
alpar@2
|
119 |
/* class_element_map<typename G::NodeIterator,
|
alpar@2
|
120 |
typename G::NodeType,
|
alpar@2
|
121 |
bfs_node_data<G>,&NT::D> n_d; //node-> data
|
alpar@2
|
122 |
*/
|
alpar@2
|
123 |
class
|
alpar@2
|
124 |
{
|
alpar@2
|
125 |
public:
|
alpar@2
|
126 |
bfs_node_data<G> NodeType::*d;
|
alpar@2
|
127 |
typedef bool value_type;
|
alpar@2
|
128 |
void Set(typename G::NodeIterator &i, const value_type &t)
|
alpar@2
|
129 |
{((*i).*d).visited=t;}
|
alpar@2
|
130 |
value_type Get(const typename G::NodeIterator &i) const
|
alpar@2
|
131 |
{return ((*i).*d).visited;}
|
alpar@2
|
132 |
} visited;
|
alpar@2
|
133 |
|
alpar@2
|
134 |
class
|
alpar@2
|
135 |
{
|
alpar@2
|
136 |
public:
|
alpar@2
|
137 |
bfs_node_data<G> NodeType::*d;
|
alpar@2
|
138 |
typedef typename G::EdgeIterator value_type;
|
alpar@2
|
139 |
void Set(typename G::NodeIterator &i, const value_type &t)
|
alpar@2
|
140 |
{((*i).*d).tree=t;}
|
alpar@2
|
141 |
value_type Get(const typename G::NodeIterator &i) const
|
alpar@2
|
142 |
{return ((*i).*d).tree;}
|
alpar@2
|
143 |
} tree;
|
alpar@2
|
144 |
|
alpar@2
|
145 |
class
|
alpar@2
|
146 |
{
|
alpar@2
|
147 |
public:
|
alpar@2
|
148 |
bfs_node_data<G> NodeType::*d;
|
alpar@2
|
149 |
typedef int value_type;
|
alpar@2
|
150 |
void Set(typename G::NodeIterator &i, const value_type &t)
|
alpar@2
|
151 |
{((*i).*d).dist=t;}
|
alpar@2
|
152 |
value_type Get(const typename G::NodeIterator &i) const
|
alpar@2
|
153 |
{return ((*i).*d).dist;}
|
alpar@2
|
154 |
} dist;
|
alpar@2
|
155 |
|
alpar@2
|
156 |
class
|
alpar@2
|
157 |
{
|
alpar@2
|
158 |
public:
|
alpar@2
|
159 |
bfs_node_data<G> NodeType::*d;
|
alpar@2
|
160 |
typedef int value_type;
|
alpar@2
|
161 |
void Set(typename G::NodeIterator &i, const value_type &t)
|
alpar@2
|
162 |
{((*i).*d).priority=t;}
|
alpar@2
|
163 |
value_type Get(const typename G::NodeIterator &i) const
|
alpar@2
|
164 |
{return ((*i).*d).priority;}
|
alpar@2
|
165 |
} priority;
|
alpar@2
|
166 |
|
alpar@2
|
167 |
//do_nothing_map tree; //node->EdgeIterator (W)
|
alpar@2
|
168 |
// do_nothing_map dist; //node->int (W)
|
alpar@2
|
169 |
// do_nothing_map priority; //node->int (W)
|
alpar@2
|
170 |
|
alpar@2
|
171 |
void SetDataField(const bfs_node_data<G> NodeType::*dd)
|
alpar@2
|
172 |
{
|
alpar@2
|
173 |
tree.d=visited.d=dist.d=priority.d=dd;
|
alpar@2
|
174 |
}
|
alpar@2
|
175 |
|
alpar@2
|
176 |
bfs_static_maps(const bfs_node_data<G> NodeType::*dd)
|
alpar@2
|
177 |
{
|
alpar@2
|
178 |
SetDataField(dd);
|
alpar@2
|
179 |
}
|
alpar@2
|
180 |
|
alpar@2
|
181 |
bfs_static_maps(const bfs_static_maps<G> &B)
|
alpar@2
|
182 |
{
|
alpar@2
|
183 |
tree.d=B.tree.d;visited.d=B.visited.d;
|
alpar@2
|
184 |
dist.d=B.dist.d;priority.d=B.priority.d;
|
alpar@2
|
185 |
}
|
alpar@2
|
186 |
|
alpar@2
|
187 |
};
|
alpar@2
|
188 |
|
alpar@2
|
189 |
template<typename I>
|
alpar@2
|
190 |
struct BFS_Q
|
alpar@2
|
191 |
{
|
alpar@2
|
192 |
I n;
|
alpar@2
|
193 |
int dist;
|
alpar@2
|
194 |
// BFS_Q() {}
|
alpar@2
|
195 |
// BFS_Q(BFS_Q<I> &b) {n=b.n;dist=b.dist;}
|
alpar@2
|
196 |
};
|
alpar@2
|
197 |
|
alpar@2
|
198 |
template<class G,class M>
|
alpar@2
|
199 |
void bfs(G &Gr,const typename G::NodeIterator &start_node,M &maps)
|
alpar@2
|
200 |
{
|
alpar@2
|
201 |
using namespace std;
|
alpar@2
|
202 |
|
alpar@2
|
203 |
typedef BFS_Q<typename G::NodeIterator> Q_T;
|
alpar@2
|
204 |
|
alpar@2
|
205 |
Q_T q;
|
alpar@2
|
206 |
|
alpar@2
|
207 |
int pr=0;
|
alpar@2
|
208 |
typename G::NodeIterator n,m;
|
alpar@2
|
209 |
typename G::OutEdgeIterator e;
|
alpar@2
|
210 |
int d;
|
alpar@2
|
211 |
|
alpar@2
|
212 |
for(Gr.GetFirst(n);n.isValid();++n)
|
alpar@2
|
213 |
Set(maps.visited,n,false);
|
alpar@2
|
214 |
|
alpar@2
|
215 |
queue<Q_T> Q;
|
alpar@2
|
216 |
|
alpar@2
|
217 |
q.n=start_node;
|
alpar@2
|
218 |
q.dist=0;
|
alpar@2
|
219 |
Q.push(q);
|
alpar@2
|
220 |
Set(maps.visited,start_node,true);
|
alpar@2
|
221 |
// Set(maps::tree,start_node,?????);
|
alpar@2
|
222 |
Set(maps.dist,start_node,0);
|
alpar@2
|
223 |
Set(maps.priority,start_node,pr++);
|
alpar@2
|
224 |
|
alpar@2
|
225 |
do {
|
alpar@2
|
226 |
n=Q.front().n;d=Q.front().dist+1;
|
alpar@2
|
227 |
Q.pop();
|
alpar@2
|
228 |
for(Gr.GetFirst(e,n);e.isValid();++e)
|
alpar@2
|
229 |
if(!Get(maps.visited,(m=e.Bnode()))) {
|
alpar@2
|
230 |
q.n=m;
|
alpar@2
|
231 |
q.dist=d;
|
alpar@2
|
232 |
Q.push(q);
|
alpar@2
|
233 |
Set(maps.visited,m,true);
|
alpar@2
|
234 |
Set(maps.tree,m,e);
|
alpar@2
|
235 |
Set(maps.dist,m,d);
|
alpar@2
|
236 |
Set(maps.priority,m,pr++);
|
alpar@2
|
237 |
}
|
alpar@2
|
238 |
} while(!Q.empty());
|
alpar@2
|
239 |
};
|
alpar@2
|
240 |
}
|
alpar@2
|
241 |
#endif
|