| ... | ... |
@@ -207,192 +207,197 @@ |
| 207 | 207 |
return new Heap(crossref); |
| 208 | 208 |
} |
| 209 | 209 |
}; |
| 210 | 210 |
|
| 211 | 211 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 212 | 212 |
/// heap and cross reference type with automatic allocation |
| 213 | 213 |
/// |
| 214 | 214 |
/// \ref named-templ-param "Named parameter" for setting heap and |
| 215 | 215 |
/// cross reference type with automatic allocation. They should |
| 216 | 216 |
/// have standard constructor interfaces to be able to |
| 217 | 217 |
/// automatically created by the algorithm (i.e. the graph should |
| 218 | 218 |
/// be passed to the constructor of the cross reference and the |
| 219 | 219 |
/// cross reference should be passed to the constructor of the |
| 220 | 220 |
/// heap). However, external heap and cross reference objects |
| 221 | 221 |
/// could also be passed to the algorithm using the \ref heap() |
| 222 | 222 |
/// function before calling \ref run() or \ref init(). The heap |
| 223 | 223 |
/// has to maximize the priorities. |
| 224 | 224 |
/// \sa SetHeap |
| 225 | 225 |
template <class H, class CR = RangeMap<int> > |
| 226 | 226 |
struct SetStandardHeap |
| 227 | 227 |
: public NagamochiIbaraki<Graph, CapacityMap, |
| 228 | 228 |
SetStandardHeapTraits<H, CR> > {
|
| 229 | 229 |
typedef NagamochiIbaraki<Graph, CapacityMap, |
| 230 | 230 |
SetStandardHeapTraits<H, CR> > Create; |
| 231 | 231 |
}; |
| 232 | 232 |
|
| 233 | 233 |
///@} |
| 234 | 234 |
|
| 235 | 235 |
|
| 236 | 236 |
private: |
| 237 | 237 |
|
| 238 | 238 |
const Graph &_graph; |
| 239 | 239 |
const CapacityMap *_capacity; |
| 240 | 240 |
bool _local_capacity; // unit capacity |
| 241 | 241 |
|
| 242 | 242 |
struct ArcData {
|
| 243 | 243 |
typename Graph::Node target; |
| 244 | 244 |
int prev, next; |
| 245 | 245 |
}; |
| 246 | 246 |
struct EdgeData {
|
| 247 | 247 |
Value capacity; |
| 248 | 248 |
Value cut; |
| 249 | 249 |
}; |
| 250 | 250 |
|
| 251 | 251 |
struct NodeData {
|
| 252 | 252 |
int first_arc; |
| 253 | 253 |
typename Graph::Node prev, next; |
| 254 | 254 |
int curr_arc; |
| 255 | 255 |
typename Graph::Node last_rep; |
| 256 | 256 |
Value sum; |
| 257 | 257 |
}; |
| 258 | 258 |
|
| 259 | 259 |
typename Graph::template NodeMap<NodeData> *_nodes; |
| 260 | 260 |
std::vector<ArcData> _arcs; |
| 261 | 261 |
std::vector<EdgeData> _edges; |
| 262 | 262 |
|
| 263 | 263 |
typename Graph::Node _first_node; |
| 264 | 264 |
int _node_num; |
| 265 | 265 |
|
| 266 | 266 |
Value _min_cut; |
| 267 | 267 |
|
| 268 | 268 |
HeapCrossRef *_heap_cross_ref; |
| 269 | 269 |
bool _local_heap_cross_ref; |
| 270 | 270 |
Heap *_heap; |
| 271 | 271 |
bool _local_heap; |
| 272 | 272 |
|
| 273 | 273 |
typedef typename Graph::template NodeMap<typename Graph::Node> NodeList; |
| 274 | 274 |
NodeList *_next_rep; |
| 275 | 275 |
|
| 276 | 276 |
typedef typename Graph::template NodeMap<bool> MinCutMap; |
| 277 | 277 |
MinCutMap *_cut_map; |
| 278 | 278 |
|
| 279 | 279 |
void createStructures() {
|
| 280 | 280 |
if (!_nodes) {
|
| 281 | 281 |
_nodes = new (typename Graph::template NodeMap<NodeData>)(_graph); |
| 282 | 282 |
} |
| 283 | 283 |
if (!_capacity) {
|
| 284 | 284 |
_local_capacity = true; |
| 285 | 285 |
_capacity = Traits::createCapacityMap(_graph); |
| 286 | 286 |
} |
| 287 | 287 |
if (!_heap_cross_ref) {
|
| 288 | 288 |
_local_heap_cross_ref = true; |
| 289 | 289 |
_heap_cross_ref = Traits::createHeapCrossRef(_graph); |
| 290 | 290 |
} |
| 291 | 291 |
if (!_heap) {
|
| 292 | 292 |
_local_heap = true; |
| 293 | 293 |
_heap = Traits::createHeap(*_heap_cross_ref); |
| 294 | 294 |
} |
| 295 | 295 |
if (!_next_rep) {
|
| 296 | 296 |
_next_rep = new NodeList(_graph); |
| 297 | 297 |
} |
| 298 | 298 |
if (!_cut_map) {
|
| 299 | 299 |
_cut_map = new MinCutMap(_graph); |
| 300 | 300 |
} |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 |
protected: |
|
| 304 |
//This is here to avoid a gcc-3.3 compilation error. |
|
| 305 |
//It should never be called. |
|
| 306 |
NagamochiIbaraki() {}
|
|
| 307 |
|
|
| 303 | 308 |
public : |
| 304 | 309 |
|
| 305 | 310 |
typedef NagamochiIbaraki Create; |
| 306 | 311 |
|
| 307 | 312 |
|
| 308 | 313 |
/// \brief Constructor. |
| 309 | 314 |
/// |
| 310 | 315 |
/// \param graph The graph the algorithm runs on. |
| 311 | 316 |
/// \param capacity The capacity map used by the algorithm. |
| 312 | 317 |
NagamochiIbaraki(const Graph& graph, const CapacityMap& capacity) |
| 313 | 318 |
: _graph(graph), _capacity(&capacity), _local_capacity(false), |
| 314 | 319 |
_nodes(0), _arcs(), _edges(), _min_cut(), |
| 315 | 320 |
_heap_cross_ref(0), _local_heap_cross_ref(false), |
| 316 | 321 |
_heap(0), _local_heap(false), |
| 317 | 322 |
_next_rep(0), _cut_map(0) {}
|
| 318 | 323 |
|
| 319 | 324 |
/// \brief Constructor. |
| 320 | 325 |
/// |
| 321 | 326 |
/// This constructor can be used only when the Traits class |
| 322 | 327 |
/// defines how can the local capacity map be instantiated. |
| 323 | 328 |
/// If the SetUnitCapacity used the algorithm automatically |
| 324 | 329 |
/// constructs the capacity map. |
| 325 | 330 |
/// |
| 326 | 331 |
///\param graph The graph the algorithm runs on. |
| 327 | 332 |
NagamochiIbaraki(const Graph& graph) |
| 328 | 333 |
: _graph(graph), _capacity(0), _local_capacity(false), |
| 329 | 334 |
_nodes(0), _arcs(), _edges(), _min_cut(), |
| 330 | 335 |
_heap_cross_ref(0), _local_heap_cross_ref(false), |
| 331 | 336 |
_heap(0), _local_heap(false), |
| 332 | 337 |
_next_rep(0), _cut_map(0) {}
|
| 333 | 338 |
|
| 334 | 339 |
/// \brief Destructor. |
| 335 | 340 |
/// |
| 336 | 341 |
/// Destructor. |
| 337 | 342 |
~NagamochiIbaraki() {
|
| 338 | 343 |
if (_local_capacity) delete _capacity; |
| 339 | 344 |
if (_nodes) delete _nodes; |
| 340 | 345 |
if (_local_heap) delete _heap; |
| 341 | 346 |
if (_local_heap_cross_ref) delete _heap_cross_ref; |
| 342 | 347 |
if (_next_rep) delete _next_rep; |
| 343 | 348 |
if (_cut_map) delete _cut_map; |
| 344 | 349 |
} |
| 345 | 350 |
|
| 346 | 351 |
/// \brief Sets the heap and the cross reference used by algorithm. |
| 347 | 352 |
/// |
| 348 | 353 |
/// Sets the heap and the cross reference used by algorithm. |
| 349 | 354 |
/// If you don't use this function before calling \ref run(), |
| 350 | 355 |
/// it will allocate one. The destuctor deallocates this |
| 351 | 356 |
/// automatically allocated heap and cross reference, of course. |
| 352 | 357 |
/// \return <tt> (*this) </tt> |
| 353 | 358 |
NagamochiIbaraki &heap(Heap& hp, HeapCrossRef &cr) |
| 354 | 359 |
{
|
| 355 | 360 |
if (_local_heap_cross_ref) {
|
| 356 | 361 |
delete _heap_cross_ref; |
| 357 | 362 |
_local_heap_cross_ref = false; |
| 358 | 363 |
} |
| 359 | 364 |
_heap_cross_ref = &cr; |
| 360 | 365 |
if (_local_heap) {
|
| 361 | 366 |
delete _heap; |
| 362 | 367 |
_local_heap = false; |
| 363 | 368 |
} |
| 364 | 369 |
_heap = &hp; |
| 365 | 370 |
return *this; |
| 366 | 371 |
} |
| 367 | 372 |
|
| 368 | 373 |
/// \name Execution control |
| 369 | 374 |
/// The simplest way to execute the algorithm is to use |
| 370 | 375 |
/// one of the member functions called \c run(). |
| 371 | 376 |
/// \n |
| 372 | 377 |
/// If you need more control on the execution, |
| 373 | 378 |
/// first you must call \ref init() and then call the start() |
| 374 | 379 |
/// or proper times the processNextPhase() member functions. |
| 375 | 380 |
|
| 376 | 381 |
///@{
|
| 377 | 382 |
|
| 378 | 383 |
/// \brief Initializes the internal data structures. |
| 379 | 384 |
/// |
| 380 | 385 |
/// Initializes the internal data structures. |
| 381 | 386 |
void init() {
|
| 382 | 387 |
createStructures(); |
| 383 | 388 |
|
| 384 | 389 |
int edge_num = countEdges(_graph); |
| 385 | 390 |
_edges.resize(edge_num); |
| 386 | 391 |
_arcs.resize(2 * edge_num); |
| 387 | 392 |
|
| 388 | 393 |
typename Graph::Node prev = INVALID; |
| 389 | 394 |
_node_num = 0; |
| 390 | 395 |
for (typename Graph::NodeIt n(_graph); n != INVALID; ++n) {
|
| 391 | 396 |
(*_cut_map)[n] = false; |
| 392 | 397 |
(*_next_rep)[n] = INVALID; |
| 393 | 398 |
(*_nodes)[n].last_rep = n; |
| 394 | 399 |
(*_nodes)[n].first_arc = -1; |
| 395 | 400 |
(*_nodes)[n].curr_arc = -1; |
| 396 | 401 |
(*_nodes)[n].prev = prev; |
| 397 | 402 |
if (prev != INVALID) {
|
| 398 | 403 |
(*_nodes)[prev].next = n; |
0 comments (0 inline)