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