0
4
0
| ... | ... |
@@ -548,274 +548,274 @@ |
| 548 | 548 |
if (arc._id == -2) arc._id = -1; |
| 549 | 549 |
} |
| 550 | 550 |
|
| 551 | 551 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 552 | 552 |
int de = nodes[v._id].first_out; |
| 553 | 553 |
if (de != -1) {
|
| 554 | 554 |
arc._id = de / 2; |
| 555 | 555 |
d = ((de & 1) == 1); |
| 556 | 556 |
} else {
|
| 557 | 557 |
arc._id = -1; |
| 558 | 558 |
d = true; |
| 559 | 559 |
} |
| 560 | 560 |
} |
| 561 | 561 |
void nextInc(Edge &arc, bool& d) const {
|
| 562 | 562 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 563 | 563 |
if (de != -1) {
|
| 564 | 564 |
arc._id = de / 2; |
| 565 | 565 |
d = ((de & 1) == 1); |
| 566 | 566 |
} else {
|
| 567 | 567 |
arc._id = -1; |
| 568 | 568 |
d = true; |
| 569 | 569 |
} |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 | 572 |
static int id(Node v) { return v._id; }
|
| 573 | 573 |
static int id(Arc e) { return e._id; }
|
| 574 | 574 |
static int id(Edge e) { return e._id; }
|
| 575 | 575 |
|
| 576 | 576 |
static Node nodeFromId(int id) { return Node(id);}
|
| 577 | 577 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 578 | 578 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 579 | 579 |
|
| 580 | 580 |
bool valid(Node n) const {
|
| 581 | 581 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 582 | 582 |
} |
| 583 | 583 |
bool valid(Arc a) const {
|
| 584 | 584 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 585 | 585 |
} |
| 586 | 586 |
bool valid(Edge e) const {
|
| 587 | 587 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
| 588 | 588 |
} |
| 589 | 589 |
|
| 590 | 590 |
Node addNode() {
|
| 591 | 591 |
int n = nodes.size(); |
| 592 | 592 |
nodes.push_back(NodeT()); |
| 593 | 593 |
nodes[n].first_out = -1; |
| 594 | 594 |
|
| 595 | 595 |
return Node(n); |
| 596 | 596 |
} |
| 597 | 597 |
|
| 598 | 598 |
Edge addEdge(Node u, Node v) {
|
| 599 | 599 |
int n = arcs.size(); |
| 600 | 600 |
arcs.push_back(ArcT()); |
| 601 | 601 |
arcs.push_back(ArcT()); |
| 602 | 602 |
|
| 603 | 603 |
arcs[n].target = u._id; |
| 604 | 604 |
arcs[n | 1].target = v._id; |
| 605 | 605 |
|
| 606 | 606 |
arcs[n].next_out = nodes[v._id].first_out; |
| 607 | 607 |
nodes[v._id].first_out = n; |
| 608 | 608 |
|
| 609 | 609 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
| 610 | 610 |
nodes[u._id].first_out = (n | 1); |
| 611 | 611 |
|
| 612 | 612 |
return Edge(n / 2); |
| 613 | 613 |
} |
| 614 | 614 |
|
| 615 | 615 |
void clear() {
|
| 616 | 616 |
arcs.clear(); |
| 617 | 617 |
nodes.clear(); |
| 618 | 618 |
} |
| 619 | 619 |
|
| 620 | 620 |
}; |
| 621 | 621 |
|
| 622 | 622 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
| 623 | 623 |
|
| 624 | 624 |
/// \ingroup graphs |
| 625 | 625 |
/// |
| 626 | 626 |
/// \brief A smart undirected graph class. |
| 627 | 627 |
/// |
| 628 | 628 |
/// This is a simple and fast graph implementation. |
| 629 | 629 |
/// It is also quite memory efficient, but at the price |
| 630 | 630 |
/// that <b> it does support only limited (only stack-like) |
| 631 | 631 |
/// node and arc deletions</b>. |
| 632 | 632 |
/// Except from this it conforms to |
| 633 | 633 |
/// the \ref concepts::Graph "Graph concept". |
| 634 | 634 |
/// |
| 635 | 635 |
/// It also has an |
| 636 | 636 |
/// important extra feature that |
| 637 | 637 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
| 638 | 638 |
/// |
| 639 | 639 |
/// \sa concepts::Graph. |
| 640 | 640 |
/// |
| 641 | 641 |
class SmartGraph : public ExtendedSmartGraphBase {
|
| 642 | 642 |
private: |
| 643 | 643 |
|
| 644 | 644 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 645 | 645 |
|
| 646 | 646 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 647 | 647 |
/// |
| 648 | 648 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
|
| 649 | 649 |
|
| 650 | 650 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
| 651 | 651 |
///Use GraphCopy() instead. |
| 652 | 652 |
|
| 653 | 653 |
///Assignment of SmartGraph to another one is \e not allowed. |
| 654 | 654 |
///Use GraphCopy() instead. |
| 655 | 655 |
void operator=(const SmartGraph &) {}
|
| 656 | 656 |
|
| 657 | 657 |
public: |
| 658 | 658 |
|
| 659 | 659 |
typedef ExtendedSmartGraphBase Parent; |
| 660 | 660 |
|
| 661 | 661 |
/// Constructor |
| 662 | 662 |
|
| 663 | 663 |
/// Constructor. |
| 664 | 664 |
/// |
| 665 | 665 |
SmartGraph() {}
|
| 666 | 666 |
|
| 667 | 667 |
///Add a new node to the graph. |
| 668 | 668 |
|
| 669 | 669 |
/// \return the new node. |
| 670 | 670 |
/// |
| 671 | 671 |
Node addNode() { return Parent::addNode(); }
|
| 672 | 672 |
|
| 673 | 673 |
///Add a new edge to the graph. |
| 674 | 674 |
|
| 675 | 675 |
///Add a new edge to the graph with node \c s |
| 676 | 676 |
///and \c t. |
| 677 | 677 |
///\return the new edge. |
| 678 | 678 |
Edge addEdge(const Node& s, const Node& t) {
|
| 679 | 679 |
return Parent::addEdge(s, t); |
| 680 | 680 |
} |
| 681 | 681 |
|
| 682 | 682 |
/// \brief Node validity check |
| 683 | 683 |
/// |
| 684 | 684 |
/// This function gives back true if the given node is valid, |
| 685 | 685 |
/// ie. it is a real node of the graph. |
| 686 | 686 |
/// |
| 687 | 687 |
/// \warning A removed node (using Snapshot) could become valid again |
| 688 | 688 |
/// when new nodes are added to the graph. |
| 689 | 689 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 690 | 690 |
|
| 691 | 691 |
/// \brief Arc validity check |
| 692 | 692 |
/// |
| 693 | 693 |
/// This function gives back true if the given arc is valid, |
| 694 | 694 |
/// ie. it is a real arc of the graph. |
| 695 | 695 |
/// |
| 696 | 696 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 697 | 697 |
/// when new edges are added to the graph. |
| 698 | 698 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 699 | 699 |
|
| 700 | 700 |
/// \brief Edge validity check |
| 701 | 701 |
/// |
| 702 | 702 |
/// This function gives back true if the given edge is valid, |
| 703 | 703 |
/// ie. it is a real edge of the graph. |
| 704 | 704 |
/// |
| 705 | 705 |
/// \warning A removed edge (using Snapshot) could become valid again |
| 706 | 706 |
/// when new edges are added to the graph. |
| 707 | 707 |
bool valid(Edge e) const { return Parent::valid(e); }
|
| 708 | 708 |
|
| 709 | 709 |
///Clear the graph. |
| 710 | 710 |
|
| 711 | 711 |
///Erase all the nodes and edges from the graph. |
| 712 | 712 |
/// |
| 713 | 713 |
void clear() {
|
| 714 | 714 |
Parent::clear(); |
| 715 | 715 |
} |
| 716 | 716 |
|
| 717 | 717 |
public: |
| 718 | 718 |
|
| 719 | 719 |
class Snapshot; |
| 720 | 720 |
|
| 721 | 721 |
protected: |
| 722 | 722 |
|
| 723 | 723 |
void saveSnapshot(Snapshot &s) |
| 724 | 724 |
{
|
| 725 | 725 |
s._graph = this; |
| 726 | 726 |
s.node_num = nodes.size(); |
| 727 | 727 |
s.arc_num = arcs.size(); |
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
void restoreSnapshot(const Snapshot &s) |
| 731 | 731 |
{
|
| 732 | 732 |
while(s.arc_num<arcs.size()) {
|
| 733 | 733 |
int n=arcs.size()-1; |
| 734 | 734 |
Edge arc=edgeFromId(n/2); |
| 735 | 735 |
Parent::notifier(Edge()).erase(arc); |
| 736 | 736 |
std::vector<Arc> dir; |
| 737 | 737 |
dir.push_back(arcFromId(n)); |
| 738 | 738 |
dir.push_back(arcFromId(n-1)); |
| 739 | 739 |
Parent::notifier(Arc()).erase(dir); |
| 740 |
nodes[arcs[n].target].first_out=arcs[n].next_out; |
|
| 741 |
nodes[arcs[n-1].target].first_out=arcs[n-1].next_out; |
|
| 740 |
nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
|
| 741 |
nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
|
| 742 | 742 |
arcs.pop_back(); |
| 743 | 743 |
arcs.pop_back(); |
| 744 | 744 |
} |
| 745 | 745 |
while(s.node_num<nodes.size()) {
|
| 746 | 746 |
int n=nodes.size()-1; |
| 747 | 747 |
Node node = nodeFromId(n); |
| 748 | 748 |
Parent::notifier(Node()).erase(node); |
| 749 | 749 |
nodes.pop_back(); |
| 750 | 750 |
} |
| 751 | 751 |
} |
| 752 | 752 |
|
| 753 | 753 |
public: |
| 754 | 754 |
|
| 755 | 755 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 756 | 756 |
|
| 757 | 757 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 758 | 758 |
/// |
| 759 | 759 |
///The newly added nodes and arcs can be removed using the |
| 760 | 760 |
///restore() function. |
| 761 | 761 |
/// |
| 762 | 762 |
///\note After you restore a state, you cannot restore |
| 763 | 763 |
///a later state, in other word you cannot add again the arcs deleted |
| 764 | 764 |
///by restore() using another one Snapshot instance. |
| 765 | 765 |
/// |
| 766 | 766 |
///\warning If you do not use correctly the snapshot that can cause |
| 767 | 767 |
///either broken program, invalid state of the digraph, valid but |
| 768 | 768 |
///not the restored digraph or no change. Because the runtime performance |
| 769 | 769 |
///the validity of the snapshot is not stored. |
| 770 | 770 |
class Snapshot |
| 771 | 771 |
{
|
| 772 | 772 |
SmartGraph *_graph; |
| 773 | 773 |
protected: |
| 774 | 774 |
friend class SmartGraph; |
| 775 | 775 |
unsigned int node_num; |
| 776 | 776 |
unsigned int arc_num; |
| 777 | 777 |
public: |
| 778 | 778 |
///Default constructor. |
| 779 | 779 |
|
| 780 | 780 |
///Default constructor. |
| 781 | 781 |
///To actually make a snapshot you must call save(). |
| 782 | 782 |
/// |
| 783 | 783 |
Snapshot() : _graph(0) {}
|
| 784 | 784 |
///Constructor that immediately makes a snapshot |
| 785 | 785 |
|
| 786 | 786 |
///This constructor immediately makes a snapshot of the digraph. |
| 787 | 787 |
///\param graph The digraph we make a snapshot of. |
| 788 | 788 |
Snapshot(SmartGraph &graph) {
|
| 789 | 789 |
graph.saveSnapshot(*this); |
| 790 | 790 |
} |
| 791 | 791 |
|
| 792 | 792 |
///Make a snapshot. |
| 793 | 793 |
|
| 794 | 794 |
///Make a snapshot of the graph. |
| 795 | 795 |
/// |
| 796 | 796 |
///This function can be called more than once. In case of a repeated |
| 797 | 797 |
///call, the previous snapshot gets lost. |
| 798 | 798 |
///\param graph The digraph we make the snapshot of. |
| 799 | 799 |
void save(SmartGraph &graph) |
| 800 | 800 |
{
|
| 801 | 801 |
graph.saveSnapshot(*this); |
| 802 | 802 |
} |
| 803 | 803 |
|
| 804 | 804 |
///Undo the changes until a snapshot. |
| 805 | 805 |
|
| 806 | 806 |
///Undo the changes until a snapshot created by save(). |
| 807 | 807 |
/// |
| 808 | 808 |
///\note After you restored a state, you cannot restore |
| 809 | 809 |
///a later state, in other word you cannot add again the arcs deleted |
| 810 | 810 |
///by restore(). |
| 811 | 811 |
void restore() |
| 812 | 812 |
{
|
| 813 | 813 |
_graph->restoreSnapshot(*this); |
| 814 | 814 |
} |
| 815 | 815 |
}; |
| 816 | 816 |
}; |
| 817 | 817 |
|
| 818 | 818 |
} //namespace lemon |
| 819 | 819 |
|
| 820 | 820 |
|
| 821 | 821 |
#endif //LEMON_SMART_GRAPH_H |
| 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/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/full_graph.h> |
| 23 | 23 |
|
| 24 | 24 |
#include "test_tools.h" |
| 25 | 25 |
#include "graph_test.h" |
| 26 | 26 |
|
| 27 | 27 |
using namespace lemon; |
| 28 | 28 |
using namespace lemon::concepts; |
| 29 | 29 |
|
| 30 | 30 |
template <class Digraph> |
| 31 |
void |
|
| 31 |
void checkDigraphBuild() {
|
|
| 32 | 32 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 33 | 33 |
Digraph G; |
| 34 | 34 |
|
| 35 | 35 |
checkGraphNodeList(G, 0); |
| 36 | 36 |
checkGraphArcList(G, 0); |
| 37 | 37 |
|
| 38 | 38 |
Node |
| 39 | 39 |
n1 = G.addNode(), |
| 40 | 40 |
n2 = G.addNode(), |
| 41 | 41 |
n3 = G.addNode(); |
| 42 | 42 |
checkGraphNodeList(G, 3); |
| 43 | 43 |
checkGraphArcList(G, 0); |
| 44 | 44 |
|
| 45 | 45 |
Arc a1 = G.addArc(n1, n2); |
| 46 | 46 |
check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc"); |
| 47 | 47 |
checkGraphNodeList(G, 3); |
| 48 | 48 |
checkGraphArcList(G, 1); |
| 49 | 49 |
|
| 50 | 50 |
checkGraphOutArcList(G, n1, 1); |
| 51 | 51 |
checkGraphOutArcList(G, n2, 0); |
| 52 | 52 |
checkGraphOutArcList(G, n3, 0); |
| 53 | 53 |
|
| 54 | 54 |
checkGraphInArcList(G, n1, 0); |
| 55 | 55 |
checkGraphInArcList(G, n2, 1); |
| 56 | 56 |
checkGraphInArcList(G, n3, 0); |
| 57 | 57 |
|
| 58 | 58 |
checkGraphConArcList(G, 1); |
| 59 | 59 |
|
| 60 |
Arc a2 = G.addArc(n2, n1), |
|
| 60 |
Arc a2 = G.addArc(n2, n1), |
|
| 61 |
a3 = G.addArc(n2, n3), |
|
| 62 |
a4 = G.addArc(n2, n3); |
|
| 63 |
|
|
| 64 |
checkGraphNodeList(G, 3); |
|
| 65 |
checkGraphArcList(G, 4); |
|
| 66 |
|
|
| 67 |
checkGraphOutArcList(G, n1, 1); |
|
| 68 |
checkGraphOutArcList(G, n2, 3); |
|
| 69 |
checkGraphOutArcList(G, n3, 0); |
|
| 70 |
|
|
| 71 |
checkGraphInArcList(G, n1, 1); |
|
| 72 |
checkGraphInArcList(G, n2, 1); |
|
| 73 |
checkGraphInArcList(G, n3, 2); |
|
| 74 |
|
|
| 75 |
checkGraphConArcList(G, 4); |
|
| 76 |
|
|
| 77 |
checkNodeIds(G); |
|
| 78 |
checkArcIds(G); |
|
| 79 |
checkGraphNodeMap(G); |
|
| 80 |
checkGraphArcMap(G); |
|
| 81 |
} |
|
| 82 |
|
|
| 83 |
template <class Digraph> |
|
| 84 |
void checkDigraphSplit() {
|
|
| 85 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
|
| 86 |
|
|
| 87 |
Digraph G; |
|
| 88 |
Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); |
|
| 89 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), |
|
| 90 |
a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); |
|
| 91 |
|
|
| 92 |
Node n4 = G.split(n2); |
|
| 93 |
|
|
| 94 |
check(G.target(OutArcIt(G, n2)) == n4 && |
|
| 95 |
G.source(InArcIt(G, n4)) == n2, |
|
| 96 |
"Wrong split."); |
|
| 97 |
|
|
| 98 |
checkGraphNodeList(G, 4); |
|
| 99 |
checkGraphArcList(G, 5); |
|
| 100 |
|
|
| 101 |
checkGraphOutArcList(G, n1, 1); |
|
| 102 |
checkGraphOutArcList(G, n2, 1); |
|
| 103 |
checkGraphOutArcList(G, n3, 0); |
|
| 104 |
checkGraphOutArcList(G, n4, 3); |
|
| 105 |
|
|
| 106 |
checkGraphInArcList(G, n1, 1); |
|
| 107 |
checkGraphInArcList(G, n2, 1); |
|
| 108 |
checkGraphInArcList(G, n3, 2); |
|
| 109 |
checkGraphInArcList(G, n4, 1); |
|
| 110 |
|
|
| 111 |
checkGraphConArcList(G, 5); |
|
| 112 |
} |
|
| 113 |
|
|
| 114 |
template <class Digraph> |
|
| 115 |
void checkDigraphAlter() {
|
|
| 116 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
|
| 117 |
|
|
| 118 |
Digraph G; |
|
| 119 |
Node n1 = G.addNode(), n2 = G.addNode(), |
|
| 120 |
n3 = G.addNode(), n4 = G.addNode(); |
|
| 121 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), |
|
| 122 |
a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3), |
|
| 123 |
a5 = G.addArc(n2, n4); |
|
| 124 |
|
|
| 125 |
checkGraphNodeList(G, 4); |
|
| 126 |
checkGraphArcList(G, 5); |
|
| 127 |
|
|
| 128 |
// Check changeSource() and changeTarget() |
|
| 129 |
G.changeTarget(a4, n1); |
|
| 130 |
|
|
| 131 |
checkGraphNodeList(G, 4); |
|
| 132 |
checkGraphArcList(G, 5); |
|
| 133 |
|
|
| 134 |
checkGraphOutArcList(G, n1, 1); |
|
| 135 |
checkGraphOutArcList(G, n2, 1); |
|
| 136 |
checkGraphOutArcList(G, n3, 0); |
|
| 137 |
checkGraphOutArcList(G, n4, 3); |
|
| 138 |
|
|
| 139 |
checkGraphInArcList(G, n1, 2); |
|
| 140 |
checkGraphInArcList(G, n2, 1); |
|
| 141 |
checkGraphInArcList(G, n3, 1); |
|
| 142 |
checkGraphInArcList(G, n4, 1); |
|
| 143 |
|
|
| 144 |
checkGraphConArcList(G, 5); |
|
| 145 |
|
|
| 146 |
G.changeSource(a4, n3); |
|
| 147 |
|
|
| 148 |
checkGraphNodeList(G, 4); |
|
| 149 |
checkGraphArcList(G, 5); |
|
| 150 |
|
|
| 151 |
checkGraphOutArcList(G, n1, 1); |
|
| 152 |
checkGraphOutArcList(G, n2, 1); |
|
| 153 |
checkGraphOutArcList(G, n3, 1); |
|
| 154 |
checkGraphOutArcList(G, n4, 2); |
|
| 155 |
|
|
| 156 |
checkGraphInArcList(G, n1, 2); |
|
| 157 |
checkGraphInArcList(G, n2, 1); |
|
| 158 |
checkGraphInArcList(G, n3, 1); |
|
| 159 |
checkGraphInArcList(G, n4, 1); |
|
| 160 |
|
|
| 161 |
checkGraphConArcList(G, 5); |
|
| 162 |
|
|
| 163 |
// Check contract() |
|
| 164 |
G.contract(n2, n4, false); |
|
| 165 |
|
|
| 166 |
checkGraphNodeList(G, 3); |
|
| 167 |
checkGraphArcList(G, 5); |
|
| 168 |
|
|
| 169 |
checkGraphOutArcList(G, n1, 1); |
|
| 170 |
checkGraphOutArcList(G, n2, 3); |
|
| 171 |
checkGraphOutArcList(G, n3, 1); |
|
| 172 |
|
|
| 173 |
checkGraphInArcList(G, n1, 2); |
|
| 174 |
checkGraphInArcList(G, n2, 2); |
|
| 175 |
checkGraphInArcList(G, n3, 1); |
|
| 176 |
|
|
| 177 |
checkGraphConArcList(G, 5); |
|
| 178 |
|
|
| 179 |
G.contract(n2, n1); |
|
| 180 |
|
|
| 181 |
checkGraphNodeList(G, 2); |
|
| 182 |
checkGraphArcList(G, 3); |
|
| 183 |
|
|
| 184 |
checkGraphOutArcList(G, n2, 2); |
|
| 185 |
checkGraphOutArcList(G, n3, 1); |
|
| 186 |
|
|
| 187 |
checkGraphInArcList(G, n2, 2); |
|
| 188 |
checkGraphInArcList(G, n3, 1); |
|
| 189 |
|
|
| 190 |
checkGraphConArcList(G, 3); |
|
| 191 |
} |
|
| 192 |
|
|
| 193 |
template <class Digraph> |
|
| 194 |
void checkDigraphErase() {
|
|
| 195 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
|
| 196 |
|
|
| 197 |
Digraph G; |
|
| 198 |
Node n1 = G.addNode(), n2 = G.addNode(), |
|
| 199 |
n3 = G.addNode(), n4 = G.addNode(); |
|
| 200 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), |
|
| 201 |
a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1), |
|
| 202 |
a5 = G.addArc(n2, n4); |
|
| 203 |
|
|
| 204 |
// Check arc deletion |
|
| 205 |
G.erase(a1); |
|
| 206 |
|
|
| 207 |
checkGraphNodeList(G, 4); |
|
| 208 |
checkGraphArcList(G, 4); |
|
| 209 |
|
|
| 210 |
checkGraphOutArcList(G, n1, 0); |
|
| 211 |
checkGraphOutArcList(G, n2, 1); |
|
| 212 |
checkGraphOutArcList(G, n3, 1); |
|
| 213 |
checkGraphOutArcList(G, n4, 2); |
|
| 214 |
|
|
| 215 |
checkGraphInArcList(G, n1, 2); |
|
| 216 |
checkGraphInArcList(G, n2, 0); |
|
| 217 |
checkGraphInArcList(G, n3, 1); |
|
| 218 |
checkGraphInArcList(G, n4, 1); |
|
| 219 |
|
|
| 220 |
checkGraphConArcList(G, 4); |
|
| 221 |
|
|
| 222 |
// Check node deletion |
|
| 223 |
G.erase(n4); |
|
| 224 |
|
|
| 225 |
checkGraphNodeList(G, 3); |
|
| 226 |
checkGraphArcList(G, 1); |
|
| 227 |
|
|
| 228 |
checkGraphOutArcList(G, n1, 0); |
|
| 229 |
checkGraphOutArcList(G, n2, 0); |
|
| 230 |
checkGraphOutArcList(G, n3, 1); |
|
| 231 |
checkGraphOutArcList(G, n4, 0); |
|
| 232 |
|
|
| 233 |
checkGraphInArcList(G, n1, 1); |
|
| 234 |
checkGraphInArcList(G, n2, 0); |
|
| 235 |
checkGraphInArcList(G, n3, 0); |
|
| 236 |
checkGraphInArcList(G, n4, 0); |
|
| 237 |
|
|
| 238 |
checkGraphConArcList(G, 1); |
|
| 239 |
} |
|
| 240 |
|
|
| 241 |
|
|
| 242 |
template <class Digraph> |
|
| 243 |
void checkDigraphSnapshot() {
|
|
| 244 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
|
| 245 |
|
|
| 246 |
Digraph G; |
|
| 247 |
Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); |
|
| 248 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), |
|
| 249 |
a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); |
|
| 250 |
|
|
| 251 |
typename Digraph::Snapshot snapshot(G); |
|
| 252 |
|
|
| 253 |
Node n = G.addNode(); |
|
| 254 |
G.addArc(n3, n); |
|
| 255 |
G.addArc(n, n3); |
|
| 256 |
|
|
| 257 |
checkGraphNodeList(G, 4); |
|
| 258 |
checkGraphArcList(G, 6); |
|
| 259 |
|
|
| 260 |
snapshot.restore(); |
|
| 261 |
|
|
| 61 | 262 |
checkGraphNodeList(G, 3); |
| 62 | 263 |
checkGraphArcList(G, 4); |
| 63 | 264 |
|
| 64 | 265 |
checkGraphOutArcList(G, n1, 1); |
| 65 | 266 |
checkGraphOutArcList(G, n2, 3); |
| 66 | 267 |
checkGraphOutArcList(G, n3, 0); |
| 67 | 268 |
|
| 68 | 269 |
checkGraphInArcList(G, n1, 1); |
| 69 | 270 |
checkGraphInArcList(G, n2, 1); |
| 70 | 271 |
checkGraphInArcList(G, n3, 2); |
| 71 | 272 |
|
| 72 | 273 |
checkGraphConArcList(G, 4); |
| 73 | 274 |
|
| 74 | 275 |
checkNodeIds(G); |
| 75 | 276 |
checkArcIds(G); |
| 76 | 277 |
checkGraphNodeMap(G); |
| 77 | 278 |
checkGraphArcMap(G); |
| 78 | 279 |
|
| 79 |
|
|
| 280 |
G.addNode(); |
|
| 281 |
snapshot.save(G); |
|
| 80 | 282 |
|
| 81 |
void checkFullDigraph(int num) {
|
|
| 82 |
typedef FullDigraph Digraph; |
|
| 83 |
DIGRAPH_TYPEDEFS(Digraph); |
|
| 84 |
Digraph G(num); |
|
| 283 |
G.addArc(G.addNode(), G.addNode()); |
|
| 85 | 284 |
|
| 86 |
checkGraphNodeList(G, num); |
|
| 87 |
checkGraphArcList(G, num * num); |
|
| 285 |
snapshot.restore(); |
|
| 88 | 286 |
|
| 89 |
for (NodeIt n(G); n != INVALID; ++n) {
|
|
| 90 |
checkGraphOutArcList(G, n, num); |
|
| 91 |
checkGraphInArcList(G, n, num); |
|
| 92 |
} |
|
| 93 |
|
|
| 94 |
checkGraphConArcList(G, num * num); |
|
| 95 |
|
|
| 96 |
checkNodeIds(G); |
|
| 97 |
checkArcIds(G); |
|
| 98 |
checkGraphNodeMap(G); |
|
| 99 |
checkGraphArcMap(G); |
|
| 100 |
|
|
| 101 |
for (int i = 0; i < G.nodeNum(); ++i) {
|
|
| 102 |
check(G.index(G(i)) == i, "Wrong index"); |
|
| 103 |
} |
|
| 104 |
|
|
| 105 |
for (NodeIt s(G); s != INVALID; ++s) {
|
|
| 106 |
for (NodeIt t(G); t != INVALID; ++t) {
|
|
| 107 |
Arc a = G.arc(s, t); |
|
| 108 |
check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup"); |
|
| 109 |
} |
|
| 110 |
} |
|
| 111 |
|
|
| 287 |
checkGraphNodeList(G, 4); |
|
| 288 |
checkGraphArcList(G, 4); |
|
| 112 | 289 |
} |
| 113 | 290 |
|
| 114 | 291 |
void checkConcepts() {
|
| 115 | 292 |
{ // Checking digraph components
|
| 116 | 293 |
checkConcept<BaseDigraphComponent, BaseDigraphComponent >(); |
| 117 | 294 |
|
| 118 | 295 |
checkConcept<IDableDigraphComponent<>, |
| 119 | 296 |
IDableDigraphComponent<> >(); |
| 120 | 297 |
|
| 121 | 298 |
checkConcept<IterableDigraphComponent<>, |
| 122 | 299 |
IterableDigraphComponent<> >(); |
| 123 | 300 |
|
| 124 | 301 |
checkConcept<MappableDigraphComponent<>, |
| 125 | 302 |
MappableDigraphComponent<> >(); |
| 126 | 303 |
} |
| 127 | 304 |
{ // Checking skeleton digraph
|
| 128 | 305 |
checkConcept<Digraph, Digraph>(); |
| 129 | 306 |
} |
| 130 | 307 |
{ // Checking ListDigraph
|
| 131 | 308 |
checkConcept<Digraph, ListDigraph>(); |
| 132 | 309 |
checkConcept<AlterableDigraphComponent<>, ListDigraph>(); |
| 133 | 310 |
checkConcept<ExtendableDigraphComponent<>, ListDigraph>(); |
| 134 | 311 |
checkConcept<ClearableDigraphComponent<>, ListDigraph>(); |
| 135 | 312 |
checkConcept<ErasableDigraphComponent<>, ListDigraph>(); |
| 136 | 313 |
} |
| 137 | 314 |
{ // Checking SmartDigraph
|
| 138 | 315 |
checkConcept<Digraph, SmartDigraph>(); |
| 139 | 316 |
checkConcept<AlterableDigraphComponent<>, SmartDigraph>(); |
| 140 | 317 |
checkConcept<ExtendableDigraphComponent<>, SmartDigraph>(); |
| 141 | 318 |
checkConcept<ClearableDigraphComponent<>, SmartDigraph>(); |
| 142 | 319 |
} |
| 143 | 320 |
{ // Checking FullDigraph
|
| 144 | 321 |
checkConcept<Digraph, FullDigraph>(); |
| 145 | 322 |
} |
| 146 | 323 |
} |
| 147 | 324 |
|
| 148 | 325 |
template <typename Digraph> |
| 149 | 326 |
void checkDigraphValidity() {
|
| 150 | 327 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 151 | 328 |
Digraph g; |
| 152 | 329 |
|
| 153 | 330 |
Node |
| 154 | 331 |
n1 = g.addNode(), |
| 155 | 332 |
n2 = g.addNode(), |
| 156 | 333 |
n3 = g.addNode(); |
| 157 | 334 |
|
| 158 | 335 |
Arc |
| 159 | 336 |
e1 = g.addArc(n1, n2), |
| 160 | 337 |
e2 = g.addArc(n2, n3); |
| 161 | 338 |
|
| 162 | 339 |
check(g.valid(n1), "Wrong validity check"); |
| 163 | 340 |
check(g.valid(e1), "Wrong validity check"); |
| 164 | 341 |
|
| 165 | 342 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 166 | 343 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 167 | 344 |
} |
| 168 | 345 |
|
| 169 | 346 |
template <typename Digraph> |
| 170 | 347 |
void checkDigraphValidityErase() {
|
| 171 | 348 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 172 | 349 |
Digraph g; |
| 173 | 350 |
|
| 174 | 351 |
Node |
| 175 | 352 |
n1 = g.addNode(), |
| 176 | 353 |
n2 = g.addNode(), |
| 177 | 354 |
n3 = g.addNode(); |
| 178 | 355 |
|
| 179 | 356 |
Arc |
| 180 | 357 |
e1 = g.addArc(n1, n2), |
| 181 | 358 |
e2 = g.addArc(n2, n3); |
| 182 | 359 |
|
| 183 | 360 |
check(g.valid(n1), "Wrong validity check"); |
| 184 | 361 |
check(g.valid(e1), "Wrong validity check"); |
| 185 | 362 |
|
| 186 | 363 |
g.erase(n1); |
| 187 | 364 |
|
| 188 | 365 |
check(!g.valid(n1), "Wrong validity check"); |
| 189 | 366 |
check(g.valid(n2), "Wrong validity check"); |
| 190 | 367 |
check(g.valid(n3), "Wrong validity check"); |
| 191 | 368 |
check(!g.valid(e1), "Wrong validity check"); |
| 192 | 369 |
check(g.valid(e2), "Wrong validity check"); |
| 193 | 370 |
|
| 194 | 371 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 195 | 372 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 196 | 373 |
} |
| 197 | 374 |
|
| 375 |
void checkFullDigraph(int num) {
|
|
| 376 |
typedef FullDigraph Digraph; |
|
| 377 |
DIGRAPH_TYPEDEFS(Digraph); |
|
| 378 |
Digraph G(num); |
|
| 379 |
|
|
| 380 |
checkGraphNodeList(G, num); |
|
| 381 |
checkGraphArcList(G, num * num); |
|
| 382 |
|
|
| 383 |
for (NodeIt n(G); n != INVALID; ++n) {
|
|
| 384 |
checkGraphOutArcList(G, n, num); |
|
| 385 |
checkGraphInArcList(G, n, num); |
|
| 386 |
} |
|
| 387 |
|
|
| 388 |
checkGraphConArcList(G, num * num); |
|
| 389 |
|
|
| 390 |
checkNodeIds(G); |
|
| 391 |
checkArcIds(G); |
|
| 392 |
checkGraphNodeMap(G); |
|
| 393 |
checkGraphArcMap(G); |
|
| 394 |
|
|
| 395 |
for (int i = 0; i < G.nodeNum(); ++i) {
|
|
| 396 |
check(G.index(G(i)) == i, "Wrong index"); |
|
| 397 |
} |
|
| 398 |
|
|
| 399 |
for (NodeIt s(G); s != INVALID; ++s) {
|
|
| 400 |
for (NodeIt t(G); t != INVALID; ++t) {
|
|
| 401 |
Arc a = G.arc(s, t); |
|
| 402 |
check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup"); |
|
| 403 |
} |
|
| 404 |
} |
|
| 405 |
} |
|
| 406 |
|
|
| 198 | 407 |
void checkDigraphs() {
|
| 199 | 408 |
{ // Checking ListDigraph
|
| 200 |
|
|
| 409 |
checkDigraphBuild<ListDigraph>(); |
|
| 410 |
checkDigraphSplit<ListDigraph>(); |
|
| 411 |
checkDigraphAlter<ListDigraph>(); |
|
| 412 |
checkDigraphErase<ListDigraph>(); |
|
| 413 |
checkDigraphSnapshot<ListDigraph>(); |
|
| 201 | 414 |
checkDigraphValidityErase<ListDigraph>(); |
| 202 | 415 |
} |
| 203 | 416 |
{ // Checking SmartDigraph
|
| 204 |
|
|
| 417 |
checkDigraphBuild<SmartDigraph>(); |
|
| 418 |
checkDigraphSplit<SmartDigraph>(); |
|
| 419 |
checkDigraphSnapshot<SmartDigraph>(); |
|
| 205 | 420 |
checkDigraphValidity<SmartDigraph>(); |
| 206 | 421 |
} |
| 207 | 422 |
{ // Checking FullDigraph
|
| 208 | 423 |
checkFullDigraph(8); |
| 209 | 424 |
} |
| 210 | 425 |
} |
| 211 | 426 |
|
| 212 | 427 |
int main() {
|
| 213 | 428 |
checkDigraphs(); |
| 214 | 429 |
checkConcepts(); |
| 215 | 430 |
return 0; |
| 216 | 431 |
} |
| 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/graph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/full_graph.h> |
| 23 | 23 |
#include <lemon/grid_graph.h> |
| 24 | 24 |
#include <lemon/hypercube_graph.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
using namespace lemon::concepts; |
| 31 | 31 |
|
| 32 | 32 |
template <class Graph> |
| 33 |
void |
|
| 33 |
void checkGraphBuild() {
|
|
| 34 | 34 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 35 | 35 |
|
| 36 | 36 |
Graph G; |
| 37 | 37 |
checkGraphNodeList(G, 0); |
| 38 | 38 |
checkGraphEdgeList(G, 0); |
| 39 |
checkGraphArcList(G, 0); |
|
| 39 | 40 |
|
| 40 | 41 |
Node |
| 41 | 42 |
n1 = G.addNode(), |
| 42 | 43 |
n2 = G.addNode(), |
| 43 | 44 |
n3 = G.addNode(); |
| 44 | 45 |
checkGraphNodeList(G, 3); |
| 45 | 46 |
checkGraphEdgeList(G, 0); |
| 47 |
checkGraphArcList(G, 0); |
|
| 46 | 48 |
|
| 47 | 49 |
Edge e1 = G.addEdge(n1, n2); |
| 48 | 50 |
check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1), |
| 49 | 51 |
"Wrong edge"); |
| 52 |
|
|
| 50 | 53 |
checkGraphNodeList(G, 3); |
| 54 |
checkGraphEdgeList(G, 1); |
|
| 51 | 55 |
checkGraphArcList(G, 2); |
| 52 |
checkGraphEdgeList(G, 1); |
|
| 53 | 56 |
|
| 54 |
checkGraphOutArcList(G, n1, 1); |
|
| 55 |
checkGraphOutArcList(G, n2, 1); |
|
| 56 |
|
|
| 57 |
checkGraphIncEdgeArcLists(G, n1, 1); |
|
| 58 |
checkGraphIncEdgeArcLists(G, n2, 1); |
|
| 59 |
checkGraphIncEdgeArcLists(G, n3, 0); |
|
| 57 | 60 |
|
| 58 |
checkGraphInArcList(G, n1, 1); |
|
| 59 |
checkGraphInArcList(G, n2, 1); |
|
| 60 |
|
|
| 61 |
checkGraphConEdgeList(G, 1); |
|
| 62 |
checkGraphConArcList(G, 2); |
|
| 61 | 63 |
|
| 62 |
checkGraphIncEdgeList(G, n1, 1); |
|
| 63 |
checkGraphIncEdgeList(G, n2, 1); |
|
| 64 |
|
|
| 64 |
Edge e2 = G.addEdge(n2, n1), |
|
| 65 |
e3 = G.addEdge(n2, n3); |
|
| 65 | 66 |
|
| 66 |
checkGraphConArcList(G, 2); |
|
| 67 |
checkGraphConEdgeList(G, 1); |
|
| 67 |
checkGraphNodeList(G, 3); |
|
| 68 |
checkGraphEdgeList(G, 3); |
|
| 69 |
checkGraphArcList(G, 6); |
|
| 68 | 70 |
|
| 69 |
Edge e2 = G.addEdge(n2, n1), e3 = G.addEdge(n2, n3); |
|
| 70 |
checkGraphNodeList(G, 3); |
|
| 71 |
checkGraphArcList(G, 6); |
|
| 72 |
checkGraphEdgeList(G, 3); |
|
| 71 |
checkGraphIncEdgeArcLists(G, n1, 2); |
|
| 72 |
checkGraphIncEdgeArcLists(G, n2, 3); |
|
| 73 |
checkGraphIncEdgeArcLists(G, n3, 1); |
|
| 73 | 74 |
|
| 74 |
checkGraphOutArcList(G, n1, 2); |
|
| 75 |
checkGraphOutArcList(G, n2, 3); |
|
| 76 |
checkGraphOutArcList(G, n3, 1); |
|
| 77 |
|
|
| 78 |
checkGraphInArcList(G, n1, 2); |
|
| 79 |
checkGraphInArcList(G, n2, 3); |
|
| 80 |
checkGraphInArcList(G, n3, 1); |
|
| 81 |
|
|
| 82 |
checkGraphIncEdgeList(G, n1, 2); |
|
| 83 |
checkGraphIncEdgeList(G, n2, 3); |
|
| 84 |
checkGraphIncEdgeList(G, n3, 1); |
|
| 85 |
|
|
| 75 |
checkGraphConEdgeList(G, 3); |
|
| 86 | 76 |
checkGraphConArcList(G, 6); |
| 87 |
checkGraphConEdgeList(G, 3); |
|
| 88 | 77 |
|
| 89 | 78 |
checkArcDirections(G); |
| 90 | 79 |
|
| 91 | 80 |
checkNodeIds(G); |
| 92 | 81 |
checkArcIds(G); |
| 93 | 82 |
checkEdgeIds(G); |
| 94 | 83 |
checkGraphNodeMap(G); |
| 95 | 84 |
checkGraphArcMap(G); |
| 96 | 85 |
checkGraphEdgeMap(G); |
| 97 | 86 |
} |
| 98 | 87 |
|
| 88 |
template <class Graph> |
|
| 89 |
void checkGraphAlter() {
|
|
| 90 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
|
| 91 |
|
|
| 92 |
Graph G; |
|
| 93 |
Node n1 = G.addNode(), n2 = G.addNode(), |
|
| 94 |
n3 = G.addNode(), n4 = G.addNode(); |
|
| 95 |
Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1), |
|
| 96 |
e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4), |
|
| 97 |
e5 = G.addEdge(n4, n3); |
|
| 98 |
|
|
| 99 |
checkGraphNodeList(G, 4); |
|
| 100 |
checkGraphEdgeList(G, 5); |
|
| 101 |
checkGraphArcList(G, 10); |
|
| 102 |
|
|
| 103 |
// Check changeU() and changeV() |
|
| 104 |
if (G.u(e2) == n2) {
|
|
| 105 |
G.changeU(e2, n3); |
|
| 106 |
} else {
|
|
| 107 |
G.changeV(e2, n3); |
|
| 108 |
} |
|
| 109 |
|
|
| 110 |
checkGraphNodeList(G, 4); |
|
| 111 |
checkGraphEdgeList(G, 5); |
|
| 112 |
checkGraphArcList(G, 10); |
|
| 113 |
|
|
| 114 |
checkGraphIncEdgeArcLists(G, n1, 3); |
|
| 115 |
checkGraphIncEdgeArcLists(G, n2, 2); |
|
| 116 |
checkGraphIncEdgeArcLists(G, n3, 3); |
|
| 117 |
checkGraphIncEdgeArcLists(G, n4, 2); |
|
| 118 |
|
|
| 119 |
checkGraphConEdgeList(G, 5); |
|
| 120 |
checkGraphConArcList(G, 10); |
|
| 121 |
|
|
| 122 |
if (G.u(e2) == n1) {
|
|
| 123 |
G.changeU(e2, n2); |
|
| 124 |
} else {
|
|
| 125 |
G.changeV(e2, n2); |
|
| 126 |
} |
|
| 127 |
|
|
| 128 |
checkGraphNodeList(G, 4); |
|
| 129 |
checkGraphEdgeList(G, 5); |
|
| 130 |
checkGraphArcList(G, 10); |
|
| 131 |
|
|
| 132 |
checkGraphIncEdgeArcLists(G, n1, 2); |
|
| 133 |
checkGraphIncEdgeArcLists(G, n2, 3); |
|
| 134 |
checkGraphIncEdgeArcLists(G, n3, 3); |
|
| 135 |
checkGraphIncEdgeArcLists(G, n4, 2); |
|
| 136 |
|
|
| 137 |
checkGraphConEdgeList(G, 5); |
|
| 138 |
checkGraphConArcList(G, 10); |
|
| 139 |
|
|
| 140 |
// Check contract() |
|
| 141 |
G.contract(n1, n4, false); |
|
| 142 |
|
|
| 143 |
checkGraphNodeList(G, 3); |
|
| 144 |
checkGraphEdgeList(G, 5); |
|
| 145 |
checkGraphArcList(G, 10); |
|
| 146 |
|
|
| 147 |
checkGraphIncEdgeArcLists(G, n1, 4); |
|
| 148 |
checkGraphIncEdgeArcLists(G, n2, 3); |
|
| 149 |
checkGraphIncEdgeArcLists(G, n3, 3); |
|
| 150 |
|
|
| 151 |
checkGraphConEdgeList(G, 5); |
|
| 152 |
checkGraphConArcList(G, 10); |
|
| 153 |
|
|
| 154 |
G.contract(n2, n3); |
|
| 155 |
|
|
| 156 |
checkGraphNodeList(G, 2); |
|
| 157 |
checkGraphEdgeList(G, 3); |
|
| 158 |
checkGraphArcList(G, 6); |
|
| 159 |
|
|
| 160 |
checkGraphIncEdgeArcLists(G, n1, 4); |
|
| 161 |
checkGraphIncEdgeArcLists(G, n2, 2); |
|
| 162 |
|
|
| 163 |
checkGraphConEdgeList(G, 3); |
|
| 164 |
checkGraphConArcList(G, 6); |
|
| 165 |
} |
|
| 166 |
|
|
| 167 |
template <class Graph> |
|
| 168 |
void checkGraphErase() {
|
|
| 169 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
|
| 170 |
|
|
| 171 |
Graph G; |
|
| 172 |
Node n1 = G.addNode(), n2 = G.addNode(), |
|
| 173 |
n3 = G.addNode(), n4 = G.addNode(); |
|
| 174 |
Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1), |
|
| 175 |
e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4), |
|
| 176 |
e5 = G.addEdge(n4, n3); |
|
| 177 |
|
|
| 178 |
// Check edge deletion |
|
| 179 |
G.erase(e2); |
|
| 180 |
|
|
| 181 |
checkGraphNodeList(G, 4); |
|
| 182 |
checkGraphEdgeList(G, 4); |
|
| 183 |
checkGraphArcList(G, 8); |
|
| 184 |
|
|
| 185 |
checkGraphIncEdgeArcLists(G, n1, 2); |
|
| 186 |
checkGraphIncEdgeArcLists(G, n2, 2); |
|
| 187 |
checkGraphIncEdgeArcLists(G, n3, 2); |
|
| 188 |
checkGraphIncEdgeArcLists(G, n4, 2); |
|
| 189 |
|
|
| 190 |
checkGraphConEdgeList(G, 4); |
|
| 191 |
checkGraphConArcList(G, 8); |
|
| 192 |
|
|
| 193 |
// Check node deletion |
|
| 194 |
G.erase(n3); |
|
| 195 |
|
|
| 196 |
checkGraphNodeList(G, 3); |
|
| 197 |
checkGraphEdgeList(G, 2); |
|
| 198 |
checkGraphArcList(G, 4); |
|
| 199 |
|
|
| 200 |
checkGraphIncEdgeArcLists(G, n1, 2); |
|
| 201 |
checkGraphIncEdgeArcLists(G, n2, 1); |
|
| 202 |
checkGraphIncEdgeArcLists(G, n4, 1); |
|
| 203 |
|
|
| 204 |
checkGraphConEdgeList(G, 2); |
|
| 205 |
checkGraphConArcList(G, 4); |
|
| 206 |
} |
|
| 207 |
|
|
| 208 |
|
|
| 209 |
template <class Graph> |
|
| 210 |
void checkGraphSnapshot() {
|
|
| 211 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
|
| 212 |
|
|
| 213 |
Graph G; |
|
| 214 |
Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); |
|
| 215 |
Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1), |
|
| 216 |
e3 = G.addEdge(n2, n3); |
|
| 217 |
|
|
| 218 |
checkGraphNodeList(G, 3); |
|
| 219 |
checkGraphEdgeList(G, 3); |
|
| 220 |
checkGraphArcList(G, 6); |
|
| 221 |
|
|
| 222 |
typename Graph::Snapshot snapshot(G); |
|
| 223 |
|
|
| 224 |
Node n = G.addNode(); |
|
| 225 |
G.addEdge(n3, n); |
|
| 226 |
G.addEdge(n, n3); |
|
| 227 |
G.addEdge(n3, n2); |
|
| 228 |
|
|
| 229 |
checkGraphNodeList(G, 4); |
|
| 230 |
checkGraphEdgeList(G, 6); |
|
| 231 |
checkGraphArcList(G, 12); |
|
| 232 |
|
|
| 233 |
snapshot.restore(); |
|
| 234 |
|
|
| 235 |
checkGraphNodeList(G, 3); |
|
| 236 |
checkGraphEdgeList(G, 3); |
|
| 237 |
checkGraphArcList(G, 6); |
|
| 238 |
|
|
| 239 |
checkGraphIncEdgeArcLists(G, n1, 2); |
|
| 240 |
checkGraphIncEdgeArcLists(G, n2, 3); |
|
| 241 |
checkGraphIncEdgeArcLists(G, n3, 1); |
|
| 242 |
|
|
| 243 |
checkGraphConEdgeList(G, 3); |
|
| 244 |
checkGraphConArcList(G, 6); |
|
| 245 |
|
|
| 246 |
checkNodeIds(G); |
|
| 247 |
checkEdgeIds(G); |
|
| 248 |
checkArcIds(G); |
|
| 249 |
checkGraphNodeMap(G); |
|
| 250 |
checkGraphEdgeMap(G); |
|
| 251 |
checkGraphArcMap(G); |
|
| 252 |
|
|
| 253 |
G.addNode(); |
|
| 254 |
snapshot.save(G); |
|
| 255 |
|
|
| 256 |
G.addEdge(G.addNode(), G.addNode()); |
|
| 257 |
|
|
| 258 |
snapshot.restore(); |
|
| 259 |
|
|
| 260 |
checkGraphNodeList(G, 4); |
|
| 261 |
checkGraphEdgeList(G, 3); |
|
| 262 |
checkGraphArcList(G, 6); |
|
| 263 |
} |
|
| 264 |
|
|
| 99 | 265 |
void checkFullGraph(int num) {
|
| 100 | 266 |
typedef FullGraph Graph; |
| 101 | 267 |
GRAPH_TYPEDEFS(Graph); |
| 102 | 268 |
|
| 103 | 269 |
Graph G(num); |
| 104 | 270 |
checkGraphNodeList(G, num); |
| 105 | 271 |
checkGraphEdgeList(G, num * (num - 1) / 2); |
| 106 | 272 |
|
| 107 | 273 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 108 | 274 |
checkGraphOutArcList(G, n, num - 1); |
| 109 | 275 |
checkGraphInArcList(G, n, num - 1); |
| 110 | 276 |
checkGraphIncEdgeList(G, n, num - 1); |
| 111 | 277 |
} |
| 112 | 278 |
|
| 113 | 279 |
checkGraphConArcList(G, num * (num - 1)); |
| 114 | 280 |
checkGraphConEdgeList(G, num * (num - 1) / 2); |
| 115 | 281 |
|
| 116 | 282 |
checkArcDirections(G); |
| 117 | 283 |
|
| 118 | 284 |
checkNodeIds(G); |
| 119 | 285 |
checkArcIds(G); |
| 120 | 286 |
checkEdgeIds(G); |
| 121 | 287 |
checkGraphNodeMap(G); |
| 122 | 288 |
checkGraphArcMap(G); |
| 123 | 289 |
checkGraphEdgeMap(G); |
| 124 | 290 |
|
| 125 | 291 |
|
| 126 | 292 |
for (int i = 0; i < G.nodeNum(); ++i) {
|
| 127 | 293 |
check(G.index(G(i)) == i, "Wrong index"); |
| 128 | 294 |
} |
| 129 | 295 |
|
| 130 | 296 |
for (NodeIt u(G); u != INVALID; ++u) {
|
| 131 | 297 |
for (NodeIt v(G); v != INVALID; ++v) {
|
| 132 | 298 |
Edge e = G.edge(u, v); |
| 133 | 299 |
Arc a = G.arc(u, v); |
| 134 | 300 |
if (u == v) {
|
| 135 | 301 |
check(e == INVALID, "Wrong edge lookup"); |
| 136 | 302 |
check(a == INVALID, "Wrong arc lookup"); |
| 137 | 303 |
} else {
|
| 138 | 304 |
check((G.u(e) == u && G.v(e) == v) || |
| 139 | 305 |
(G.u(e) == v && G.v(e) == u), "Wrong edge lookup"); |
| 140 | 306 |
check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup"); |
| 141 | 307 |
} |
| 142 | 308 |
} |
| 143 | 309 |
} |
| 144 | 310 |
} |
| 145 | 311 |
|
| 146 | 312 |
void checkConcepts() {
|
| 147 | 313 |
{ // Checking graph components
|
| 148 | 314 |
checkConcept<BaseGraphComponent, BaseGraphComponent >(); |
| 149 | 315 |
|
| 150 | 316 |
checkConcept<IDableGraphComponent<>, |
| 151 | 317 |
IDableGraphComponent<> >(); |
| 152 | 318 |
|
| 153 | 319 |
checkConcept<IterableGraphComponent<>, |
| 154 | 320 |
IterableGraphComponent<> >(); |
| 155 | 321 |
|
| 156 | 322 |
checkConcept<MappableGraphComponent<>, |
| 157 | 323 |
MappableGraphComponent<> >(); |
| 158 | 324 |
} |
| 159 | 325 |
{ // Checking skeleton graph
|
| 160 | 326 |
checkConcept<Graph, Graph>(); |
| 161 | 327 |
} |
| 162 | 328 |
{ // Checking ListGraph
|
| 163 | 329 |
checkConcept<Graph, ListGraph>(); |
| 164 | 330 |
checkConcept<AlterableGraphComponent<>, ListGraph>(); |
| 165 | 331 |
checkConcept<ExtendableGraphComponent<>, ListGraph>(); |
| 166 | 332 |
checkConcept<ClearableGraphComponent<>, ListGraph>(); |
| 167 | 333 |
checkConcept<ErasableGraphComponent<>, ListGraph>(); |
| 168 | 334 |
} |
| 169 | 335 |
{ // Checking SmartGraph
|
| 170 | 336 |
checkConcept<Graph, SmartGraph>(); |
| 171 | 337 |
checkConcept<AlterableGraphComponent<>, SmartGraph>(); |
| 172 | 338 |
checkConcept<ExtendableGraphComponent<>, SmartGraph>(); |
| 173 | 339 |
checkConcept<ClearableGraphComponent<>, SmartGraph>(); |
| 174 | 340 |
} |
| 175 | 341 |
{ // Checking FullGraph
|
| 176 | 342 |
checkConcept<Graph, FullGraph>(); |
| 177 | 343 |
} |
| 178 | 344 |
{ // Checking GridGraph
|
| 179 | 345 |
checkConcept<Graph, GridGraph>(); |
| 180 | 346 |
} |
| 181 | 347 |
{ // Checking HypercubeGraph
|
| 182 | 348 |
checkConcept<Graph, HypercubeGraph>(); |
| 183 | 349 |
} |
| 184 | 350 |
} |
| 185 | 351 |
|
| 186 | 352 |
template <typename Graph> |
| 187 | 353 |
void checkGraphValidity() {
|
| 188 | 354 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 189 | 355 |
Graph g; |
| 190 | 356 |
|
| 191 | 357 |
Node |
| 192 | 358 |
n1 = g.addNode(), |
| 193 | 359 |
n2 = g.addNode(), |
| 194 | 360 |
n3 = g.addNode(); |
| 195 | 361 |
|
| 196 | 362 |
Edge |
| 197 | 363 |
e1 = g.addEdge(n1, n2), |
| 198 | 364 |
e2 = g.addEdge(n2, n3); |
| 199 | 365 |
|
| 200 | 366 |
check(g.valid(n1), "Wrong validity check"); |
| 201 | 367 |
check(g.valid(e1), "Wrong validity check"); |
| 202 | 368 |
check(g.valid(g.direct(e1, true)), "Wrong validity check"); |
| 203 | 369 |
|
| 204 | 370 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 205 | 371 |
check(!g.valid(g.edgeFromId(-1)), "Wrong validity check"); |
| 206 | 372 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 207 | 373 |
} |
| 208 | 374 |
|
| 209 | 375 |
template <typename Graph> |
| 210 | 376 |
void checkGraphValidityErase() {
|
| 211 | 377 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 212 | 378 |
Graph g; |
| 213 | 379 |
|
| 214 | 380 |
Node |
| 215 | 381 |
n1 = g.addNode(), |
| 216 | 382 |
n2 = g.addNode(), |
| 217 | 383 |
n3 = g.addNode(); |
| 218 | 384 |
|
| 219 | 385 |
Edge |
| 220 | 386 |
e1 = g.addEdge(n1, n2), |
| 221 | 387 |
e2 = g.addEdge(n2, n3); |
| 222 | 388 |
|
| 223 | 389 |
check(g.valid(n1), "Wrong validity check"); |
| 224 | 390 |
check(g.valid(e1), "Wrong validity check"); |
| 225 | 391 |
check(g.valid(g.direct(e1, true)), "Wrong validity check"); |
| 226 | 392 |
|
| 227 | 393 |
g.erase(n1); |
| 228 | 394 |
|
| 229 | 395 |
check(!g.valid(n1), "Wrong validity check"); |
| 230 | 396 |
check(g.valid(n2), "Wrong validity check"); |
| 231 | 397 |
check(g.valid(n3), "Wrong validity check"); |
| 232 | 398 |
check(!g.valid(e1), "Wrong validity check"); |
| 233 | 399 |
check(g.valid(e2), "Wrong validity check"); |
| 234 | 400 |
|
| 235 | 401 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 236 | 402 |
check(!g.valid(g.edgeFromId(-1)), "Wrong validity check"); |
| 237 | 403 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 238 | 404 |
} |
| 239 | 405 |
|
| 240 | 406 |
void checkGridGraph(int width, int height) {
|
| 241 | 407 |
typedef GridGraph Graph; |
| 242 | 408 |
GRAPH_TYPEDEFS(Graph); |
| 243 | 409 |
Graph G(width, height); |
| 244 | 410 |
|
| 245 | 411 |
check(G.width() == width, "Wrong column number"); |
| 246 | 412 |
check(G.height() == height, "Wrong row number"); |
| 247 | 413 |
|
| 248 | 414 |
for (int i = 0; i < width; ++i) {
|
| 249 | 415 |
for (int j = 0; j < height; ++j) {
|
| 250 | 416 |
check(G.col(G(i, j)) == i, "Wrong column"); |
| 251 | 417 |
check(G.row(G(i, j)) == j, "Wrong row"); |
| 252 | 418 |
check(G.pos(G(i, j)).x == i, "Wrong column"); |
| 253 | 419 |
check(G.pos(G(i, j)).y == j, "Wrong row"); |
| 254 | 420 |
} |
| 255 | 421 |
} |
| 256 | 422 |
|
| 257 | 423 |
for (int j = 0; j < height; ++j) {
|
| 258 | 424 |
for (int i = 0; i < width - 1; ++i) {
|
| 259 | 425 |
check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right"); |
| 260 | 426 |
check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right"); |
| 261 | 427 |
} |
| 262 | 428 |
check(G.right(G(width - 1, j)) == INVALID, "Wrong right"); |
| 263 | 429 |
} |
| 264 | 430 |
|
| 265 | 431 |
for (int j = 0; j < height; ++j) {
|
| 266 | 432 |
for (int i = 1; i < width; ++i) {
|
| 267 | 433 |
check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left"); |
| 268 | 434 |
check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left"); |
| 269 | 435 |
} |
| 270 | 436 |
check(G.left(G(0, j)) == INVALID, "Wrong left"); |
| 271 | 437 |
} |
| 272 | 438 |
|
| 273 | 439 |
for (int i = 0; i < width; ++i) {
|
| 274 | 440 |
for (int j = 0; j < height - 1; ++j) {
|
| 275 | 441 |
check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up"); |
| 276 | 442 |
check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up"); |
| 277 | 443 |
} |
| 278 | 444 |
check(G.up(G(i, height - 1)) == INVALID, "Wrong up"); |
| 279 | 445 |
} |
| 280 | 446 |
|
| 281 | 447 |
for (int i = 0; i < width; ++i) {
|
| 282 | 448 |
for (int j = 1; j < height; ++j) {
|
| 283 | 449 |
check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down"); |
| 284 | 450 |
check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down"); |
| 285 | 451 |
} |
| 286 | 452 |
check(G.down(G(i, 0)) == INVALID, "Wrong down"); |
| 287 | 453 |
} |
| 288 | 454 |
|
| 289 | 455 |
checkGraphNodeList(G, width * height); |
| 290 | 456 |
checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height); |
| 291 | 457 |
checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height)); |
| 292 | 458 |
|
| 293 | 459 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 294 | 460 |
int nb = 4; |
| 295 | 461 |
if (G.col(n) == 0) --nb; |
| 296 | 462 |
if (G.col(n) == width - 1) --nb; |
| 297 | 463 |
if (G.row(n) == 0) --nb; |
| 298 | 464 |
if (G.row(n) == height - 1) --nb; |
| 299 | 465 |
|
| 300 | 466 |
checkGraphOutArcList(G, n, nb); |
| 301 | 467 |
checkGraphInArcList(G, n, nb); |
| 302 | 468 |
checkGraphIncEdgeList(G, n, nb); |
| 303 | 469 |
} |
| 304 | 470 |
|
| 305 | 471 |
checkArcDirections(G); |
| 306 | 472 |
|
| 307 | 473 |
checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height)); |
| 308 | 474 |
checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height); |
| 309 | 475 |
|
| 310 | 476 |
checkNodeIds(G); |
| 311 | 477 |
checkArcIds(G); |
| 312 | 478 |
checkEdgeIds(G); |
| 313 | 479 |
checkGraphNodeMap(G); |
| 314 | 480 |
checkGraphArcMap(G); |
| 315 | 481 |
checkGraphEdgeMap(G); |
| 316 | 482 |
|
| 317 | 483 |
} |
| 318 | 484 |
|
| 319 | 485 |
void checkHypercubeGraph(int dim) {
|
| 320 | 486 |
GRAPH_TYPEDEFS(HypercubeGraph); |
| 321 | 487 |
|
| 322 | 488 |
HypercubeGraph G(dim); |
| 323 | 489 |
checkGraphNodeList(G, 1 << dim); |
| 324 | 490 |
checkGraphEdgeList(G, dim * (1 << (dim-1))); |
| 325 | 491 |
checkGraphArcList(G, dim * (1 << dim)); |
| 326 | 492 |
|
| 327 | 493 |
Node n = G.nodeFromId(dim); |
| 328 | 494 |
|
| 329 | 495 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 330 | 496 |
checkGraphIncEdgeList(G, n, dim); |
| 331 | 497 |
for (IncEdgeIt e(G, n); e != INVALID; ++e) {
|
| 332 | 498 |
check( (G.u(e) == n && |
| 333 | 499 |
G.id(G.v(e)) == (G.id(n) ^ (1 << G.dimension(e)))) || |
| 334 | 500 |
(G.v(e) == n && |
| 335 | 501 |
G.id(G.u(e)) == (G.id(n) ^ (1 << G.dimension(e)))), |
| 336 | 502 |
"Wrong edge or wrong dimension"); |
| 337 | 503 |
} |
| 338 | 504 |
|
| 339 | 505 |
checkGraphOutArcList(G, n, dim); |
| 340 | 506 |
for (OutArcIt a(G, n); a != INVALID; ++a) {
|
| 341 | 507 |
check(G.source(a) == n && |
| 342 | 508 |
G.id(G.target(a)) == (G.id(n) ^ (1 << G.dimension(a))), |
| 343 | 509 |
"Wrong arc or wrong dimension"); |
| 344 | 510 |
} |
| 345 | 511 |
|
| 346 | 512 |
checkGraphInArcList(G, n, dim); |
| 347 | 513 |
for (InArcIt a(G, n); a != INVALID; ++a) {
|
| 348 | 514 |
check(G.target(a) == n && |
| 349 | 515 |
G.id(G.source(a)) == (G.id(n) ^ (1 << G.dimension(a))), |
| 350 | 516 |
"Wrong arc or wrong dimension"); |
| 351 | 517 |
} |
| 352 | 518 |
} |
| 353 | 519 |
|
| 354 | 520 |
checkGraphConArcList(G, (1 << dim) * dim); |
| 355 | 521 |
checkGraphConEdgeList(G, dim * (1 << (dim-1))); |
| 356 | 522 |
|
| 357 | 523 |
checkArcDirections(G); |
| 358 | 524 |
|
| 359 | 525 |
checkNodeIds(G); |
| 360 | 526 |
checkArcIds(G); |
| 361 | 527 |
checkEdgeIds(G); |
| 362 | 528 |
checkGraphNodeMap(G); |
| 363 | 529 |
checkGraphArcMap(G); |
| 364 | 530 |
checkGraphEdgeMap(G); |
| 365 | 531 |
} |
| 366 | 532 |
|
| 367 | 533 |
void checkGraphs() {
|
| 368 | 534 |
{ // Checking ListGraph
|
| 369 |
|
|
| 535 |
checkGraphBuild<ListGraph>(); |
|
| 536 |
checkGraphAlter<ListGraph>(); |
|
| 537 |
checkGraphErase<ListGraph>(); |
|
| 538 |
checkGraphSnapshot<ListGraph>(); |
|
| 370 | 539 |
checkGraphValidityErase<ListGraph>(); |
| 371 | 540 |
} |
| 372 | 541 |
{ // Checking SmartGraph
|
| 373 |
|
|
| 542 |
checkGraphBuild<SmartGraph>(); |
|
| 543 |
checkGraphSnapshot<SmartGraph>(); |
|
| 374 | 544 |
checkGraphValidity<SmartGraph>(); |
| 375 | 545 |
} |
| 376 | 546 |
{ // Checking FullGraph
|
| 377 | 547 |
checkFullGraph(7); |
| 378 | 548 |
checkFullGraph(8); |
| 379 | 549 |
} |
| 380 | 550 |
{ // Checking GridGraph
|
| 381 | 551 |
checkGridGraph(5, 8); |
| 382 | 552 |
checkGridGraph(8, 5); |
| 383 | 553 |
checkGridGraph(5, 5); |
| 384 | 554 |
checkGridGraph(0, 0); |
| 385 | 555 |
checkGridGraph(1, 1); |
| 386 | 556 |
} |
| 387 | 557 |
{ // Checking HypercubeGraph
|
| 388 | 558 |
checkHypercubeGraph(1); |
| 389 | 559 |
checkHypercubeGraph(2); |
| 390 | 560 |
checkHypercubeGraph(3); |
| 391 | 561 |
checkHypercubeGraph(4); |
| 392 | 562 |
} |
| 393 | 563 |
} |
| 394 | 564 |
|
| 395 | 565 |
int main() {
|
| 396 | 566 |
checkConcepts(); |
| 397 | 567 |
checkGraphs(); |
| 398 | 568 |
return 0; |
| 399 | 569 |
} |
| 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_TEST_GRAPH_TEST_H |
| 20 | 20 |
#define LEMON_TEST_GRAPH_TEST_H |
| 21 | 21 |
|
| 22 | 22 |
#include <set> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/core.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
template<class Graph> |
| 32 | 32 |
void checkGraphNodeList(const Graph &G, int cnt) |
| 33 | 33 |
{
|
| 34 | 34 |
typename Graph::NodeIt n(G); |
| 35 | 35 |
for(int i=0;i<cnt;i++) {
|
| 36 | 36 |
check(n!=INVALID,"Wrong Node list linking."); |
| 37 | 37 |
++n; |
| 38 | 38 |
} |
| 39 | 39 |
check(n==INVALID,"Wrong Node list linking."); |
| 40 | 40 |
check(countNodes(G)==cnt,"Wrong Node number."); |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 | 43 |
template<class Graph> |
| 44 | 44 |
void checkGraphArcList(const Graph &G, int cnt) |
| 45 | 45 |
{
|
| 46 | 46 |
typename Graph::ArcIt e(G); |
| 47 | 47 |
for(int i=0;i<cnt;i++) {
|
| 48 | 48 |
check(e!=INVALID,"Wrong Arc list linking."); |
| 49 | 49 |
check(G.oppositeNode(G.source(e), e) == G.target(e), |
| 50 | 50 |
"Wrong opposite node"); |
| 51 | 51 |
check(G.oppositeNode(G.target(e), e) == G.source(e), |
| 52 | 52 |
"Wrong opposite node"); |
| 53 | 53 |
++e; |
| 54 | 54 |
} |
| 55 | 55 |
check(e==INVALID,"Wrong Arc list linking."); |
| 56 | 56 |
check(countArcs(G)==cnt,"Wrong Arc number."); |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 | 59 |
template<class Graph> |
| 60 | 60 |
void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt) |
| 61 | 61 |
{
|
| 62 | 62 |
typename Graph::OutArcIt e(G,n); |
| 63 | 63 |
for(int i=0;i<cnt;i++) {
|
| 64 | 64 |
check(e!=INVALID,"Wrong OutArc list linking."); |
| 65 | 65 |
check(n==G.source(e),"Wrong OutArc list linking."); |
| 66 | 66 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 67 | 67 |
check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking."); |
| 68 | 68 |
++e; |
| 69 | 69 |
} |
| 70 | 70 |
check(e==INVALID,"Wrong OutArc list linking."); |
| 71 | 71 |
check(countOutArcs(G,n)==cnt,"Wrong OutArc number."); |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 | 74 |
template<class Graph> |
| 75 | 75 |
void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt) |
| 76 | 76 |
{
|
| 77 | 77 |
typename Graph::InArcIt e(G,n); |
| 78 | 78 |
for(int i=0;i<cnt;i++) {
|
| 79 | 79 |
check(e!=INVALID,"Wrong InArc list linking."); |
| 80 | 80 |
check(n==G.target(e),"Wrong InArc list linking."); |
| 81 | 81 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 82 | 82 |
check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking."); |
| 83 | 83 |
++e; |
| 84 | 84 |
} |
| 85 | 85 |
check(e==INVALID,"Wrong InArc list linking."); |
| 86 | 86 |
check(countInArcs(G,n)==cnt,"Wrong InArc number."); |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
template<class Graph> |
| 90 | 90 |
void checkGraphEdgeList(const Graph &G, int cnt) |
| 91 | 91 |
{
|
| 92 | 92 |
typename Graph::EdgeIt e(G); |
| 93 | 93 |
for(int i=0;i<cnt;i++) {
|
| 94 | 94 |
check(e!=INVALID,"Wrong Edge list linking."); |
| 95 | 95 |
check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node"); |
| 96 | 96 |
check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node"); |
| 97 | 97 |
++e; |
| 98 | 98 |
} |
| 99 | 99 |
check(e==INVALID,"Wrong Edge list linking."); |
| 100 | 100 |
check(countEdges(G)==cnt,"Wrong Edge number."); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
template<class Graph> |
| 104 | 104 |
void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt) |
| 105 | 105 |
{
|
| 106 | 106 |
typename Graph::IncEdgeIt e(G,n); |
| 107 | 107 |
for(int i=0;i<cnt;i++) {
|
| 108 | 108 |
check(e!=INVALID,"Wrong IncEdge list linking."); |
| 109 | 109 |
check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking."); |
| 110 | 110 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 111 | 111 |
check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e), |
| 112 | 112 |
"Wrong OutArc list linking."); |
| 113 | 113 |
++e; |
| 114 | 114 |
} |
| 115 | 115 |
check(e==INVALID,"Wrong IncEdge list linking."); |
| 116 | 116 |
check(countIncEdges(G,n)==cnt,"Wrong IncEdge number."); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
template <class Graph> |
| 120 |
void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n, |
|
| 121 |
int cnt) |
|
| 122 |
{
|
|
| 123 |
checkGraphIncEdgeList(G, n, cnt); |
|
| 124 |
checkGraphOutArcList(G, n, cnt); |
|
| 125 |
checkGraphInArcList(G, n, cnt); |
|
| 126 |
} |
|
| 127 |
|
|
| 128 |
template <class Graph> |
|
| 120 | 129 |
void checkGraphConArcList(const Graph &G, int cnt) {
|
| 121 | 130 |
int i = 0; |
| 122 | 131 |
for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
|
| 123 | 132 |
for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
|
| 124 | 133 |
for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
|
| 125 | 134 |
check(G.source(a) == u, "Wrong iterator."); |
| 126 | 135 |
check(G.target(a) == v, "Wrong iterator."); |
| 127 | 136 |
++i; |
| 128 | 137 |
} |
| 129 | 138 |
} |
| 130 | 139 |
} |
| 131 | 140 |
check(cnt == i, "Wrong iterator."); |
| 132 | 141 |
} |
| 133 | 142 |
|
| 134 | 143 |
template <class Graph> |
| 135 | 144 |
void checkGraphConEdgeList(const Graph &G, int cnt) {
|
| 136 | 145 |
int i = 0; |
| 137 | 146 |
for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
|
| 138 | 147 |
for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
|
| 139 | 148 |
for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
|
| 140 | 149 |
check((G.u(e) == u && G.v(e) == v) || |
| 141 | 150 |
(G.u(e) == v && G.v(e) == u), "Wrong iterator."); |
| 142 | 151 |
i += u == v ? 2 : 1; |
| 143 | 152 |
} |
| 144 | 153 |
} |
| 145 | 154 |
} |
| 146 | 155 |
check(2 * cnt == i, "Wrong iterator."); |
| 147 | 156 |
} |
| 148 | 157 |
|
| 149 | 158 |
template <typename Graph> |
| 150 | 159 |
void checkArcDirections(const Graph& G) {
|
| 151 | 160 |
for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
|
| 152 | 161 |
check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction"); |
| 153 | 162 |
check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction"); |
| 154 | 163 |
check(G.direct(a, G.direction(a)) == a, "Wrong direction"); |
| 155 | 164 |
} |
| 156 | 165 |
} |
| 157 | 166 |
|
| 158 | 167 |
template <typename Graph> |
| 159 | 168 |
void checkNodeIds(const Graph& G) {
|
| 160 | 169 |
std::set<int> values; |
| 161 | 170 |
for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
|
| 162 | 171 |
check(G.nodeFromId(G.id(n)) == n, "Wrong id"); |
| 163 | 172 |
check(values.find(G.id(n)) == values.end(), "Wrong id"); |
| 164 | 173 |
check(G.id(n) <= G.maxNodeId(), "Wrong maximum id"); |
| 165 | 174 |
values.insert(G.id(n)); |
| 166 | 175 |
} |
| 167 | 176 |
} |
| 168 | 177 |
|
| 169 | 178 |
template <typename Graph> |
| 170 | 179 |
void checkArcIds(const Graph& G) {
|
| 171 | 180 |
std::set<int> values; |
| 172 | 181 |
for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
|
| 173 | 182 |
check(G.arcFromId(G.id(a)) == a, "Wrong id"); |
| 174 | 183 |
check(values.find(G.id(a)) == values.end(), "Wrong id"); |
| 175 | 184 |
check(G.id(a) <= G.maxArcId(), "Wrong maximum id"); |
| 176 | 185 |
values.insert(G.id(a)); |
| 177 | 186 |
} |
| 178 | 187 |
} |
| 179 | 188 |
|
| 180 | 189 |
template <typename Graph> |
| 181 | 190 |
void checkEdgeIds(const Graph& G) {
|
| 182 | 191 |
std::set<int> values; |
| 183 | 192 |
for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
|
| 184 | 193 |
check(G.edgeFromId(G.id(e)) == e, "Wrong id"); |
| 185 | 194 |
check(values.find(G.id(e)) == values.end(), "Wrong id"); |
| 186 | 195 |
check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id"); |
| 187 | 196 |
values.insert(G.id(e)); |
| 188 | 197 |
} |
| 189 | 198 |
} |
| 190 | 199 |
|
| 191 | 200 |
template <typename Graph> |
| 192 | 201 |
void checkGraphNodeMap(const Graph& G) {
|
| 193 | 202 |
typedef typename Graph::Node Node; |
| 194 | 203 |
typedef typename Graph::NodeIt NodeIt; |
| 195 | 204 |
|
| 196 | 205 |
typedef typename Graph::template NodeMap<int> IntNodeMap; |
| 197 | 206 |
IntNodeMap map(G, 42); |
| 198 | 207 |
for (NodeIt it(G); it != INVALID; ++it) {
|
| 199 | 208 |
check(map[it] == 42, "Wrong map constructor."); |
| 200 | 209 |
} |
| 201 | 210 |
int s = 0; |
| 202 | 211 |
for (NodeIt it(G); it != INVALID; ++it) {
|
| 203 | 212 |
map[it] = 0; |
| 204 | 213 |
check(map[it] == 0, "Wrong operator[]."); |
| 205 | 214 |
map.set(it, s); |
| 206 | 215 |
check(map[it] == s, "Wrong set."); |
| 207 | 216 |
++s; |
| 208 | 217 |
} |
| 209 | 218 |
s = s * (s - 1) / 2; |
| 210 | 219 |
for (NodeIt it(G); it != INVALID; ++it) {
|
| 211 | 220 |
s -= map[it]; |
| 212 | 221 |
} |
| 213 | 222 |
check(s == 0, "Wrong sum."); |
| 214 | 223 |
|
| 215 | 224 |
// map = constMap<Node>(12); |
| 216 | 225 |
// for (NodeIt it(G); it != INVALID; ++it) {
|
| 217 | 226 |
// check(map[it] == 12, "Wrong operator[]."); |
| 218 | 227 |
// } |
| 219 | 228 |
} |
| 220 | 229 |
|
| 221 | 230 |
template <typename Graph> |
| 222 | 231 |
void checkGraphArcMap(const Graph& G) {
|
| 223 | 232 |
typedef typename Graph::Arc Arc; |
| 224 | 233 |
typedef typename Graph::ArcIt ArcIt; |
| 225 | 234 |
|
| 226 | 235 |
typedef typename Graph::template ArcMap<int> IntArcMap; |
| 227 | 236 |
IntArcMap map(G, 42); |
| 228 | 237 |
for (ArcIt it(G); it != INVALID; ++it) {
|
| 229 | 238 |
check(map[it] == 42, "Wrong map constructor."); |
| 230 | 239 |
} |
| 231 | 240 |
int s = 0; |
| 232 | 241 |
for (ArcIt it(G); it != INVALID; ++it) {
|
| 233 | 242 |
map[it] = 0; |
| 234 | 243 |
check(map[it] == 0, "Wrong operator[]."); |
| 235 | 244 |
map.set(it, s); |
| 236 | 245 |
check(map[it] == s, "Wrong set."); |
| 237 | 246 |
++s; |
| 238 | 247 |
} |
| 239 | 248 |
s = s * (s - 1) / 2; |
| 240 | 249 |
for (ArcIt it(G); it != INVALID; ++it) {
|
| 241 | 250 |
s -= map[it]; |
| 242 | 251 |
} |
| 243 | 252 |
check(s == 0, "Wrong sum."); |
| 244 | 253 |
|
| 245 | 254 |
// map = constMap<Arc>(12); |
| 246 | 255 |
// for (ArcIt it(G); it != INVALID; ++it) {
|
| 247 | 256 |
// check(map[it] == 12, "Wrong operator[]."); |
| 248 | 257 |
// } |
| 249 | 258 |
} |
| 250 | 259 |
|
| 251 | 260 |
template <typename Graph> |
| 252 | 261 |
void checkGraphEdgeMap(const Graph& G) {
|
| 253 | 262 |
typedef typename Graph::Edge Edge; |
| 254 | 263 |
typedef typename Graph::EdgeIt EdgeIt; |
| 255 | 264 |
|
| 256 | 265 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; |
| 257 | 266 |
IntEdgeMap map(G, 42); |
| 258 | 267 |
for (EdgeIt it(G); it != INVALID; ++it) {
|
| 259 | 268 |
check(map[it] == 42, "Wrong map constructor."); |
| 260 | 269 |
} |
| 261 | 270 |
int s = 0; |
| 262 | 271 |
for (EdgeIt it(G); it != INVALID; ++it) {
|
| 263 | 272 |
map[it] = 0; |
| 264 | 273 |
check(map[it] == 0, "Wrong operator[]."); |
| 265 | 274 |
map.set(it, s); |
| 266 | 275 |
check(map[it] == s, "Wrong set."); |
| 267 | 276 |
++s; |
| 268 | 277 |
} |
| 269 | 278 |
s = s * (s - 1) / 2; |
| 270 | 279 |
for (EdgeIt it(G); it != INVALID; ++it) {
|
| 271 | 280 |
s -= map[it]; |
| 272 | 281 |
} |
| 273 | 282 |
check(s == 0, "Wrong sum."); |
| 274 | 283 |
|
| 275 | 284 |
// map = constMap<Edge>(12); |
| 276 | 285 |
// for (EdgeIt it(G); it != INVALID; ++it) {
|
| 277 | 286 |
// check(map[it] == 12, "Wrong operator[]."); |
| 278 | 287 |
// } |
| 279 | 288 |
} |
| 280 | 289 |
|
| 281 | 290 |
|
| 282 | 291 |
} //namespace lemon |
| 283 | 292 |
|
| 284 | 293 |
#endif |
0 comments (0 inline)