gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Better return type for cycleLength() functions (#179) in the min mean cycle algorithms. The original Value type is used instead of the LargeValue type, which is introduced for internal computations.
0 3 0
default
3 files changed with 6 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -312,194 +312,194 @@
312 312
    /// \ref findMinMean().
313 313

	
314 314
    /// @{
315 315

	
316 316
    /// \brief Run the algorithm.
317 317
    ///
318 318
    /// This function runs the algorithm.
319 319
    /// It can be called more than once (e.g. if the underlying digraph
320 320
    /// and/or the arc lengths have been modified).
321 321
    ///
322 322
    /// \return \c true if a directed cycle exists in the digraph.
323 323
    ///
324 324
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
325 325
    /// \code
326 326
    ///   return mmc.findMinMean() && mmc.findCycle();
327 327
    /// \endcode
328 328
    bool run() {
329 329
      return findMinMean() && findCycle();
330 330
    }
331 331

	
332 332
    /// \brief Find the minimum cycle mean.
333 333
    ///
334 334
    /// This function finds the minimum mean length of the directed
335 335
    /// cycles in the digraph.
336 336
    ///
337 337
    /// \return \c true if a directed cycle exists in the digraph.
338 338
    bool findMinMean() {
339 339
      // Initialization and find strongly connected components
340 340
      init();
341 341
      findComponents();
342 342
      
343 343
      // Find the minimum cycle mean in the components
344 344
      for (int comp = 0; comp < _comp_num; ++comp) {
345 345
        if (!initComponent(comp)) continue;
346 346
        processRounds();
347 347
        
348 348
        // Update the best cycle (global minimum mean cycle)
349 349
        if ( _curr_found && (!_best_found || 
350 350
             _curr_length * _best_size < _best_length * _curr_size) ) {
351 351
          _best_found = true;
352 352
          _best_length = _curr_length;
353 353
          _best_size = _curr_size;
354 354
          _best_node = _curr_node;
355 355
          _best_level = _curr_level;
356 356
        }
357 357
      }
358 358
      return _best_found;
359 359
    }
360 360

	
361 361
    /// \brief Find a minimum mean directed cycle.
362 362
    ///
363 363
    /// This function finds a directed cycle of minimum mean length
364 364
    /// in the digraph using the data computed by findMinMean().
365 365
    ///
366 366
    /// \return \c true if a directed cycle exists in the digraph.
367 367
    ///
368 368
    /// \pre \ref findMinMean() must be called before using this function.
369 369
    bool findCycle() {
370 370
      if (!_best_found) return false;
371 371
      IntNodeMap reached(_gr, -1);
372 372
      int r = _best_level + 1;
373 373
      Node u = _best_node;
374 374
      while (reached[u] < 0) {
375 375
        reached[u] = --r;
376 376
        u = _gr.source(_data[u][r].pred);
377 377
      }
378 378
      r = reached[u];
379 379
      Arc e = _data[u][r].pred;
380 380
      _cycle_path->addFront(e);
381 381
      _best_length = _length[e];
382 382
      _best_size = 1;
383 383
      Node v;
384 384
      while ((v = _gr.source(e)) != u) {
385 385
        e = _data[v][--r].pred;
386 386
        _cycle_path->addFront(e);
387 387
        _best_length += _length[e];
388 388
        ++_best_size;
389 389
      }
390 390
      return true;
391 391
    }
392 392

	
393 393
    /// @}
394 394

	
395 395
    /// \name Query Functions
396 396
    /// The results of the algorithm can be obtained using these
397 397
    /// functions.\n
398 398
    /// The algorithm should be executed before using them.
399 399

	
400 400
    /// @{
401 401

	
402 402
    /// \brief Return the total length of the found cycle.
403 403
    ///
404 404
    /// This function returns the total length of the found cycle.
405 405
    ///
406 406
    /// \pre \ref run() or \ref findMinMean() must be called before
407 407
    /// using this function.
408
    LargeValue cycleLength() const {
409
      return _best_length;
408
    Value cycleLength() const {
409
      return static_cast<Value>(_best_length);
410 410
    }
411 411

	
412 412
    /// \brief Return the number of arcs on the found cycle.
413 413
    ///
414 414
    /// This function returns the number of arcs on the found cycle.
415 415
    ///
416 416
    /// \pre \ref run() or \ref findMinMean() must be called before
417 417
    /// using this function.
418 418
    int cycleArcNum() const {
419 419
      return _best_size;
420 420
    }
421 421

	
422 422
    /// \brief Return the mean length of the found cycle.
423 423
    ///
424 424
    /// This function returns the mean length of the found cycle.
425 425
    ///
426 426
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
427 427
    /// following code.
428 428
    /// \code
429 429
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
430 430
    /// \endcode
431 431
    ///
432 432
    /// \pre \ref run() or \ref findMinMean() must be called before
433 433
    /// using this function.
434 434
    double cycleMean() const {
435 435
      return static_cast<double>(_best_length) / _best_size;
436 436
    }
437 437

	
438 438
    /// \brief Return the found cycle.
439 439
    ///
440 440
    /// This function returns a const reference to the path structure
441 441
    /// storing the found cycle.
442 442
    ///
443 443
    /// \pre \ref run() or \ref findCycle() must be called before using
444 444
    /// this function.
445 445
    const Path& cycle() const {
446 446
      return *_cycle_path;
447 447
    }
448 448

	
449 449
    ///@}
450 450

	
451 451
  private:
452 452

	
453 453
    // Initialization
454 454
    void init() {
455 455
      if (!_cycle_path) {
456 456
        _local_path = true;
457 457
        _cycle_path = new Path;
458 458
      }
459 459
      _cycle_path->clear();
460 460
      _best_found = false;
461 461
      _best_length = 0;
462 462
      _best_size = 1;
463 463
      _cycle_path->clear();
464 464
      for (NodeIt u(_gr); u != INVALID; ++u)
465 465
        _data[u].clear();
466 466
    }
467 467

	
468 468
    // Find strongly connected components and initialize _comp_nodes
469 469
    // and _out_arcs
470 470
    void findComponents() {
471 471
      _comp_num = stronglyConnectedComponents(_gr, _comp);
472 472
      _comp_nodes.resize(_comp_num);
473 473
      if (_comp_num == 1) {
474 474
        _comp_nodes[0].clear();
475 475
        for (NodeIt n(_gr); n != INVALID; ++n) {
476 476
          _comp_nodes[0].push_back(n);
477 477
          _out_arcs[n].clear();
478 478
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
479 479
            _out_arcs[n].push_back(a);
480 480
          }
481 481
        }
482 482
      } else {
483 483
        for (int i = 0; i < _comp_num; ++i)
484 484
          _comp_nodes[i].clear();
485 485
        for (NodeIt n(_gr); n != INVALID; ++n) {
486 486
          int k = _comp[n];
487 487
          _comp_nodes[k].push_back(n);
488 488
          _out_arcs[n].clear();
489 489
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
490 490
            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
491 491
          }
492 492
        }
493 493
      }
494 494
    }
495 495

	
496 496
    // Initialize path data for the current component
497 497
    bool initComponent(int comp) {
498 498
      _nodes = &(_comp_nodes[comp]);
499 499
      int n = _nodes->size();
500 500
      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
501 501
        return false;
502 502
      }      
503 503
      for (int i = 0; i < n; ++i) {
504 504
        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
505 505
      }
Ignore white space 6 line context
... ...
@@ -291,194 +291,194 @@
291 291
    /// \brief Return a const reference to the tolerance.
292 292
    ///
293 293
    /// This function returns a const reference to the tolerance object
294 294
    /// used by the algorithm.
295 295
    const Tolerance& tolerance() const {
296 296
      return _tolerance;
297 297
    }
298 298

	
299 299
    /// \name Execution control
300 300
    /// The simplest way to execute the algorithm is to call the \ref run()
301 301
    /// function.\n
302 302
    /// If you only need the minimum mean length, you may call
303 303
    /// \ref findMinMean().
304 304

	
305 305
    /// @{
306 306

	
307 307
    /// \brief Run the algorithm.
308 308
    ///
309 309
    /// This function runs the algorithm.
310 310
    /// It can be called more than once (e.g. if the underlying digraph
311 311
    /// and/or the arc lengths have been modified).
312 312
    ///
313 313
    /// \return \c true if a directed cycle exists in the digraph.
314 314
    ///
315 315
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
316 316
    /// \code
317 317
    ///   return mmc.findMinMean() && mmc.findCycle();
318 318
    /// \endcode
319 319
    bool run() {
320 320
      return findMinMean() && findCycle();
321 321
    }
322 322

	
323 323
    /// \brief Find the minimum cycle mean.
324 324
    ///
325 325
    /// This function finds the minimum mean length of the directed
326 326
    /// cycles in the digraph.
327 327
    ///
328 328
    /// \return \c true if a directed cycle exists in the digraph.
329 329
    bool findMinMean() {
330 330
      // Initialize and find strongly connected components
331 331
      init();
332 332
      findComponents();
333 333
      
334 334
      // Find the minimum cycle mean in the components
335 335
      for (int comp = 0; comp < _comp_num; ++comp) {
336 336
        // Find the minimum mean cycle in the current component
337 337
        if (!buildPolicyGraph(comp)) continue;
338 338
        while (true) {
339 339
          findPolicyCycle();
340 340
          if (!computeNodeDistances()) break;
341 341
        }
342 342
        // Update the best cycle (global minimum mean cycle)
343 343
        if ( _curr_found && (!_best_found ||
344 344
             _curr_length * _best_size < _best_length * _curr_size) ) {
345 345
          _best_found = true;
346 346
          _best_length = _curr_length;
347 347
          _best_size = _curr_size;
348 348
          _best_node = _curr_node;
349 349
        }
350 350
      }
351 351
      return _best_found;
352 352
    }
353 353

	
354 354
    /// \brief Find a minimum mean directed cycle.
355 355
    ///
356 356
    /// This function finds a directed cycle of minimum mean length
357 357
    /// in the digraph using the data computed by findMinMean().
358 358
    ///
359 359
    /// \return \c true if a directed cycle exists in the digraph.
360 360
    ///
361 361
    /// \pre \ref findMinMean() must be called before using this function.
362 362
    bool findCycle() {
363 363
      if (!_best_found) return false;
364 364
      _cycle_path->addBack(_policy[_best_node]);
365 365
      for ( Node v = _best_node;
366 366
            (v = _gr.target(_policy[v])) != _best_node; ) {
367 367
        _cycle_path->addBack(_policy[v]);
368 368
      }
369 369
      return true;
370 370
    }
371 371

	
372 372
    /// @}
373 373

	
374 374
    /// \name Query Functions
375 375
    /// The results of the algorithm can be obtained using these
376 376
    /// functions.\n
377 377
    /// The algorithm should be executed before using them.
378 378

	
379 379
    /// @{
380 380

	
381 381
    /// \brief Return the total length of the found cycle.
382 382
    ///
383 383
    /// This function returns the total length of the found cycle.
384 384
    ///
385 385
    /// \pre \ref run() or \ref findMinMean() must be called before
386 386
    /// using this function.
387
    LargeValue cycleLength() const {
388
      return _best_length;
387
    Value cycleLength() const {
388
      return static_cast<Value>(_best_length);
389 389
    }
390 390

	
391 391
    /// \brief Return the number of arcs on the found cycle.
392 392
    ///
393 393
    /// This function returns the number of arcs on the found cycle.
394 394
    ///
395 395
    /// \pre \ref run() or \ref findMinMean() must be called before
396 396
    /// using this function.
397 397
    int cycleArcNum() const {
398 398
      return _best_size;
399 399
    }
400 400

	
401 401
    /// \brief Return the mean length of the found cycle.
402 402
    ///
403 403
    /// This function returns the mean length of the found cycle.
404 404
    ///
405 405
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
406 406
    /// following code.
407 407
    /// \code
408 408
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
409 409
    /// \endcode
410 410
    ///
411 411
    /// \pre \ref run() or \ref findMinMean() must be called before
412 412
    /// using this function.
413 413
    double cycleMean() const {
414 414
      return static_cast<double>(_best_length) / _best_size;
415 415
    }
416 416

	
417 417
    /// \brief Return the found cycle.
418 418
    ///
419 419
    /// This function returns a const reference to the path structure
420 420
    /// storing the found cycle.
421 421
    ///
422 422
    /// \pre \ref run() or \ref findCycle() must be called before using
423 423
    /// this function.
424 424
    const Path& cycle() const {
425 425
      return *_cycle_path;
426 426
    }
427 427

	
428 428
    ///@}
429 429

	
430 430
  private:
431 431

	
432 432
    // Initialize
433 433
    void init() {
434 434
      if (!_cycle_path) {
435 435
        _local_path = true;
436 436
        _cycle_path = new Path;
437 437
      }
438 438
      _queue.resize(countNodes(_gr));
439 439
      _best_found = false;
440 440
      _best_length = 0;
441 441
      _best_size = 1;
442 442
      _cycle_path->clear();
443 443
    }
444 444
    
445 445
    // Find strongly connected components and initialize _comp_nodes
446 446
    // and _in_arcs
447 447
    void findComponents() {
448 448
      _comp_num = stronglyConnectedComponents(_gr, _comp);
449 449
      _comp_nodes.resize(_comp_num);
450 450
      if (_comp_num == 1) {
451 451
        _comp_nodes[0].clear();
452 452
        for (NodeIt n(_gr); n != INVALID; ++n) {
453 453
          _comp_nodes[0].push_back(n);
454 454
          _in_arcs[n].clear();
455 455
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
456 456
            _in_arcs[n].push_back(a);
457 457
          }
458 458
        }
459 459
      } else {
460 460
        for (int i = 0; i < _comp_num; ++i)
461 461
          _comp_nodes[i].clear();
462 462
        for (NodeIt n(_gr); n != INVALID; ++n) {
463 463
          int k = _comp[n];
464 464
          _comp_nodes[k].push_back(n);
465 465
          _in_arcs[n].clear();
466 466
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
467 467
            if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
468 468
          }
469 469
        }
470 470
      }
471 471
    }
472 472

	
473 473
    // Build the policy graph in the given strongly connected component
474 474
    // (the out-degree of every node is 1)
475 475
    bool buildPolicyGraph(int comp) {
476 476
      _nodes = &(_comp_nodes[comp]);
477 477
      if (_nodes->size() < 1 ||
478 478
          (_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
479 479
        return false;
480 480
      }
481 481
      for (int i = 0; i < int(_nodes->size()); ++i) {
482 482
        _dist[(*_nodes)[i]] = INF;
483 483
      }
484 484
      Node u, v;
Ignore white space 192 line context
... ...
@@ -299,194 +299,194 @@
299 299
    /// used by the algorithm.
300 300
    const Tolerance& tolerance() const {
301 301
      return _tolerance;
302 302
    }
303 303

	
304 304
    /// \name Execution control
305 305
    /// The simplest way to execute the algorithm is to call the \ref run()
306 306
    /// function.\n
307 307
    /// If you only need the minimum mean length, you may call
308 308
    /// \ref findMinMean().
309 309

	
310 310
    /// @{
311 311

	
312 312
    /// \brief Run the algorithm.
313 313
    ///
314 314
    /// This function runs the algorithm.
315 315
    /// It can be called more than once (e.g. if the underlying digraph
316 316
    /// and/or the arc lengths have been modified).
317 317
    ///
318 318
    /// \return \c true if a directed cycle exists in the digraph.
319 319
    ///
320 320
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
321 321
    /// \code
322 322
    ///   return mmc.findMinMean() && mmc.findCycle();
323 323
    /// \endcode
324 324
    bool run() {
325 325
      return findMinMean() && findCycle();
326 326
    }
327 327

	
328 328
    /// \brief Find the minimum cycle mean.
329 329
    ///
330 330
    /// This function finds the minimum mean length of the directed
331 331
    /// cycles in the digraph.
332 332
    ///
333 333
    /// \return \c true if a directed cycle exists in the digraph.
334 334
    bool findMinMean() {
335 335
      // Initialization and find strongly connected components
336 336
      init();
337 337
      findComponents();
338 338
      
339 339
      // Find the minimum cycle mean in the components
340 340
      for (int comp = 0; comp < _comp_num; ++comp) {
341 341
        if (!initComponent(comp)) continue;
342 342
        processRounds();
343 343
        updateMinMean();
344 344
      }
345 345
      return (_cycle_node != INVALID);
346 346
    }
347 347

	
348 348
    /// \brief Find a minimum mean directed cycle.
349 349
    ///
350 350
    /// This function finds a directed cycle of minimum mean length
351 351
    /// in the digraph using the data computed by findMinMean().
352 352
    ///
353 353
    /// \return \c true if a directed cycle exists in the digraph.
354 354
    ///
355 355
    /// \pre \ref findMinMean() must be called before using this function.
356 356
    bool findCycle() {
357 357
      if (_cycle_node == INVALID) return false;
358 358
      IntNodeMap reached(_gr, -1);
359 359
      int r = _data[_cycle_node].size();
360 360
      Node u = _cycle_node;
361 361
      while (reached[u] < 0) {
362 362
        reached[u] = --r;
363 363
        u = _gr.source(_data[u][r].pred);
364 364
      }
365 365
      r = reached[u];
366 366
      Arc e = _data[u][r].pred;
367 367
      _cycle_path->addFront(e);
368 368
      _cycle_length = _length[e];
369 369
      _cycle_size = 1;
370 370
      Node v;
371 371
      while ((v = _gr.source(e)) != u) {
372 372
        e = _data[v][--r].pred;
373 373
        _cycle_path->addFront(e);
374 374
        _cycle_length += _length[e];
375 375
        ++_cycle_size;
376 376
      }
377 377
      return true;
378 378
    }
379 379

	
380 380
    /// @}
381 381

	
382 382
    /// \name Query Functions
383 383
    /// The results of the algorithm can be obtained using these
384 384
    /// functions.\n
385 385
    /// The algorithm should be executed before using them.
386 386

	
387 387
    /// @{
388 388

	
389 389
    /// \brief Return the total length of the found cycle.
390 390
    ///
391 391
    /// This function returns the total length of the found cycle.
392 392
    ///
393 393
    /// \pre \ref run() or \ref findMinMean() must be called before
394 394
    /// using this function.
395
    LargeValue cycleLength() const {
396
      return _cycle_length;
395
    Value cycleLength() const {
396
      return static_cast<Value>(_cycle_length);
397 397
    }
398 398

	
399 399
    /// \brief Return the number of arcs on the found cycle.
400 400
    ///
401 401
    /// This function returns the number of arcs on the found cycle.
402 402
    ///
403 403
    /// \pre \ref run() or \ref findMinMean() must be called before
404 404
    /// using this function.
405 405
    int cycleArcNum() const {
406 406
      return _cycle_size;
407 407
    }
408 408

	
409 409
    /// \brief Return the mean length of the found cycle.
410 410
    ///
411 411
    /// This function returns the mean length of the found cycle.
412 412
    ///
413 413
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
414 414
    /// following code.
415 415
    /// \code
416 416
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
417 417
    /// \endcode
418 418
    ///
419 419
    /// \pre \ref run() or \ref findMinMean() must be called before
420 420
    /// using this function.
421 421
    double cycleMean() const {
422 422
      return static_cast<double>(_cycle_length) / _cycle_size;
423 423
    }
424 424

	
425 425
    /// \brief Return the found cycle.
426 426
    ///
427 427
    /// This function returns a const reference to the path structure
428 428
    /// storing the found cycle.
429 429
    ///
430 430
    /// \pre \ref run() or \ref findCycle() must be called before using
431 431
    /// this function.
432 432
    const Path& cycle() const {
433 433
      return *_cycle_path;
434 434
    }
435 435

	
436 436
    ///@}
437 437

	
438 438
  private:
439 439

	
440 440
    // Initialization
441 441
    void init() {
442 442
      if (!_cycle_path) {
443 443
        _local_path = true;
444 444
        _cycle_path = new Path;
445 445
      }
446 446
      _cycle_path->clear();
447 447
      _cycle_length = 0;
448 448
      _cycle_size = 1;
449 449
      _cycle_node = INVALID;
450 450
      for (NodeIt u(_gr); u != INVALID; ++u)
451 451
        _data[u].clear();
452 452
    }
453 453

	
454 454
    // Find strongly connected components and initialize _comp_nodes
455 455
    // and _out_arcs
456 456
    void findComponents() {
457 457
      _comp_num = stronglyConnectedComponents(_gr, _comp);
458 458
      _comp_nodes.resize(_comp_num);
459 459
      if (_comp_num == 1) {
460 460
        _comp_nodes[0].clear();
461 461
        for (NodeIt n(_gr); n != INVALID; ++n) {
462 462
          _comp_nodes[0].push_back(n);
463 463
          _out_arcs[n].clear();
464 464
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
465 465
            _out_arcs[n].push_back(a);
466 466
          }
467 467
        }
468 468
      } else {
469 469
        for (int i = 0; i < _comp_num; ++i)
470 470
          _comp_nodes[i].clear();
471 471
        for (NodeIt n(_gr); n != INVALID; ++n) {
472 472
          int k = _comp[n];
473 473
          _comp_nodes[k].push_back(n);
474 474
          _out_arcs[n].clear();
475 475
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
476 476
            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
477 477
          }
478 478
        }
479 479
      }
480 480
    }
481 481

	
482 482
    // Initialize path data for the current component
483 483
    bool initComponent(int comp) {
484 484
      _nodes = &(_comp_nodes[comp]);
485 485
      int n = _nodes->size();
486 486
      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
487 487
        return false;
488 488
      }      
489 489
      for (int i = 0; i < n; ++i) {
490 490
        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
491 491
      }
492 492
      return true;
0 comments (0 inline)