1 |
foucault |
27 |
#ifndef GRAPHH
|
2 |
|
|
#define GRAPHH
|
3 |
|
|
|
4 |
|
|
#include <map>
|
5 |
|
|
#include <set>
|
6 |
|
|
#include <vector>
|
7 |
|
|
|
8 |
|
|
#include "HypergraphLib_GraphObject.h"
|
9 |
|
|
|
10 |
|
|
#define GRAPH_FOR_EACH_ARC_CONST(G,A) \
|
11 |
|
|
for (HypergraphLib::Graph::MapArcsById::const_iterator A = G->GetArcs().begin(); \
|
12 |
|
|
A != G->GetArcs().end(); \
|
13 |
|
|
A++)
|
14 |
|
|
|
15 |
|
|
#define GRAPH_FOR_EACH_ARC(G,A) \
|
16 |
|
|
for (HypergraphLib::Graph::MapArcsById::iterator A = G->GetArcs().begin(); \
|
17 |
|
|
A != G->GetArcs().end(); \
|
18 |
|
|
A++)
|
19 |
|
|
|
20 |
|
|
#define GRAPH_FOR_EACH_NODE_CONST(G,N) \
|
21 |
|
|
for (HypergraphLib::Graph::MapNodesById::const_iterator N = G->GetNodes().begin(); \
|
22 |
|
|
N != G->GetNodes().end(); \
|
23 |
|
|
N++)
|
24 |
|
|
|
25 |
|
|
#define GRAPH_FOR_EACH_NODE(G,N) \
|
26 |
|
|
for (HypergraphLib::Graph::MapNodesById::iterator N = G->GetNodes().begin(); \
|
27 |
|
|
N != G->GetNodes().end(); \
|
28 |
|
|
N++)
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
namespace HypergraphLib {
|
33 |
|
|
|
34 |
|
|
class Node;
|
35 |
|
|
class Arc;
|
36 |
|
|
|
37 |
|
|
class HYPERGRAPHLIB_ITEM Graph : public GraphObject {
|
38 |
|
|
public:
|
39 |
|
|
typedef std::map < int, Arc * > MapArcsById;
|
40 |
|
|
typedef std::map < int, Node * > MapNodesById;
|
41 |
|
|
|
42 |
|
|
~Graph();
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Constructor
|
46 |
|
|
*/
|
47 |
|
|
Graph(int __id=0);
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* copy constructor
|
51 |
|
|
*/
|
52 |
|
|
Graph(const Graph &__G, int __clone=1, int __reverse=0);
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* subgraph constructor
|
56 |
|
|
*/
|
57 |
|
|
Graph(const Graph &__G, const std::set <Node *> & );
|
58 |
|
|
Graph(const Graph &__G, const std::vector <Node*> & __nodes);
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Get one node by ID
|
62 |
|
|
*/
|
63 |
|
|
Node * GetNode (int) const;
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Merge two nodes
|
68 |
|
|
* if __keepNodes = true, then the 2 initial nodes are kept
|
69 |
|
|
*/
|
70 |
|
|
Node* MergeNodes(Node *__A, Node *__B, int __id, bool __keepNodes=false);
|
71 |
|
|
Node* MergeNodes(int __A,int __B, int __id, bool __keepNodes=false);
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Get one arc by ID
|
75 |
|
|
*/
|
76 |
|
|
Arc * GetArc (int) const;
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Remove one node, and all its references in its incident arcs
|
80 |
|
|
*/
|
81 |
|
|
int RemoveNode (int __id);
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Remove one node, and all its references in its incident arcs
|
85 |
|
|
*/
|
86 |
|
|
int RemoveNode (Node *);
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* Remove one Arc, and all its references in its nodes
|
90 |
|
|
*/
|
91 |
|
|
int RemoveArc (int __id);
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Remove one Arc, and all its references in its nodes
|
95 |
|
|
*/
|
96 |
|
|
int RemoveArc (Arc *);
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* Add one node
|
100 |
|
|
*/
|
101 |
|
|
Node * AddNode (int __id=0);
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* Add an arc
|
105 |
|
|
* returns 0 if successful
|
106 |
|
|
*/
|
107 |
|
|
Arc * AddArc (const std::vector<int> &, int __id);
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Add an arc that links __rank Nodes Together,
|
111 |
|
|
* and affects the ID __id to the new arc
|
112 |
|
|
* Example : AddArc(123, 3, 4, 54, 5);
|
113 |
|
|
* creates arc ID=123, Rank=3, Nodes=4;54;5
|
114 |
|
|
*/
|
115 |
|
|
Arc * AddArc(const int __id, int __rank, ...);
|
116 |
|
|
|
117 |
|
|
Arc * AddArc(const int __id, const std::multimap<int, Node*> & __nodes);
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Checks if the ID already exists in the Nodes of the graph,
|
121 |
|
|
* if it already exists, it get the first free one starting
|
122 |
|
|
* from the lowest ID of the nodes.
|
123 |
|
|
*
|
124 |
|
|
* return : the ID
|
125 |
|
|
*/
|
126 |
|
|
int CheckFreeNodeId(int __id) const;
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* Checks if the ID already exists in the Arcs of the graph,
|
130 |
|
|
* if it already exists, it get the first free one starting
|
131 |
|
|
* from the lowest ID of the arcs.
|
132 |
|
|
*
|
133 |
|
|
* return : the ID
|
134 |
|
|
*/
|
135 |
|
|
int CheckFreeArcId(int __id) const;
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Returns the maximum value of the arcs' IDs of this graph
|
139 |
|
|
*/
|
140 |
|
|
int GetMaxArcId();
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Returns the maximum value of the nodes' IDs of this graph
|
144 |
|
|
*/
|
145 |
|
|
int GetMaxNodeId();
|
146 |
|
|
|
147 |
|
|
/**
|
148 |
|
|
* Get the array of arcs by ID
|
149 |
|
|
*/
|
150 |
|
|
const MapArcsById & GetArcs()const {return _arcs; } ;
|
151 |
|
|
|
152 |
|
|
/**
|
153 |
|
|
* Get the array of nodes by ID
|
154 |
|
|
*/
|
155 |
|
|
const MapNodesById & GetNodes()const {return _nodes; } ;
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* DEBUGGING PURPOSES
|
159 |
|
|
* Verifies the integrity of the Graph :
|
160 |
|
|
* * each node of the arcs must exist in the graph
|
161 |
|
|
* * each incident arc of each node must exist in the graph
|
162 |
|
|
* * each incident arc of each node must reference this node
|
163 |
|
|
* * each node of each arc must reference this arc
|
164 |
|
|
*/
|
165 |
|
|
void CheckIntegrity()const;
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* Extract filaments of the graph
|
169 |
|
|
*/
|
170 |
|
|
void Filaments ( std::vector < std::vector < int > > & __filaments );
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
|
|
* Tests wether the graph is a cycle or not
|
174 |
|
|
*/
|
175 |
|
|
bool IsCycle () const;
|
176 |
|
|
bool IsCycle (const std::vector < Node * > & __nodes) const;
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* duplicate an arc, use the second argument to set
|
180 |
|
|
* the id of the new arc
|
181 |
|
|
*/
|
182 |
|
|
Arc * DuplicateArc(int __id, int __dupId);
|
183 |
|
|
Arc * DuplicateArc(Arc * __arc, int __dupId);
|
184 |
|
|
|
185 |
|
|
protected:
|
186 |
|
|
MapArcsById _arcs;
|
187 |
|
|
MapNodesById _nodes;
|
188 |
|
|
};
|
189 |
|
|
|
190 |
|
|
} // end namespace HypergraphLib
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
#endif // GRAPH_H
|