COIN-OR::LEMON - Graph Library

source: lemon-main/test/graph_test.cc @ 368:a7e8ad460d66

Last change on this file since 368:a7e8ad460d66 was 365:a12eef1f82b2, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Rework hypercube graph implementation to be undirected (#57)

File size: 10.4 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#include <lemon/concepts/graph.h>
20#include <lemon/list_graph.h>
21#include <lemon/smart_graph.h>
22#include <lemon/full_graph.h>
23#include <lemon/grid_graph.h>
24#include <lemon/hypercube_graph.h>
25
26#include "test_tools.h"
27#include "graph_test.h"
28
29using namespace lemon;
30using namespace lemon::concepts;
31
32template <class Graph>
33void checkGraph() {
34  TEMPLATE_GRAPH_TYPEDEFS(Graph);
35
36  Graph G;
37  checkGraphNodeList(G, 0);
38  checkGraphEdgeList(G, 0);
39
40  Node
41    n1 = G.addNode(),
42    n2 = G.addNode(),
43    n3 = G.addNode();
44  checkGraphNodeList(G, 3);
45  checkGraphEdgeList(G, 0);
46
47  Edge e1 = G.addEdge(n1, n2);
48  check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1),
49        "Wrong edge");
50  checkGraphNodeList(G, 3);
51  checkGraphArcList(G, 2);
52  checkGraphEdgeList(G, 1);
53
54  checkGraphOutArcList(G, n1, 1);
55  checkGraphOutArcList(G, n2, 1);
56  checkGraphOutArcList(G, n3, 0);
57
58  checkGraphInArcList(G, n1, 1);
59  checkGraphInArcList(G, n2, 1);
60  checkGraphInArcList(G, n3, 0);
61
62  checkGraphIncEdgeList(G, n1, 1);
63  checkGraphIncEdgeList(G, n2, 1);
64  checkGraphIncEdgeList(G, n3, 0);
65
66  checkGraphConArcList(G, 2);
67  checkGraphConEdgeList(G, 1);
68
69  Edge e2 = G.addEdge(n2, n1), e3 = G.addEdge(n2, n3);
70  checkGraphNodeList(G, 3);
71  checkGraphArcList(G, 6);
72  checkGraphEdgeList(G, 3);
73
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
86  checkGraphConArcList(G, 6);
87  checkGraphConEdgeList(G, 3);
88
89  checkArcDirections(G);
90
91  checkNodeIds(G);
92  checkArcIds(G);
93  checkEdgeIds(G);
94  checkGraphNodeMap(G);
95  checkGraphArcMap(G);
96  checkGraphEdgeMap(G);
97}
98
99void checkFullGraph(int num) {
100  typedef FullGraph Graph;
101  GRAPH_TYPEDEFS(Graph);
102
103  Graph G(num);
104  checkGraphNodeList(G, num);
105  checkGraphEdgeList(G, num * (num - 1) / 2);
106
107  for (NodeIt n(G); n != INVALID; ++n) {
108    checkGraphOutArcList(G, n, num - 1);
109    checkGraphInArcList(G, n, num - 1);
110    checkGraphIncEdgeList(G, n, num - 1);
111  }
112
113  checkGraphConArcList(G, num * (num - 1));
114  checkGraphConEdgeList(G, num * (num - 1) / 2);
115
116  checkArcDirections(G);
117
118  checkNodeIds(G);
119  checkArcIds(G);
120  checkEdgeIds(G);
121  checkGraphNodeMap(G);
122  checkGraphArcMap(G);
123  checkGraphEdgeMap(G);
124
125
126  for (int i = 0; i < G.nodeNum(); ++i) {
127    check(G.index(G(i)) == i, "Wrong index");
128  }
129
130  for (NodeIt u(G); u != INVALID; ++u) {
131    for (NodeIt v(G); v != INVALID; ++v) {
132      Edge e = G.edge(u, v);
133      Arc a = G.arc(u, v);
134      if (u == v) {
135        check(e == INVALID, "Wrong edge lookup");
136        check(a == INVALID, "Wrong arc lookup");
137      } else {
138        check((G.u(e) == u && G.v(e) == v) ||
139              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
140        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
141      }
142    }
143  }
144}
145
146void checkConcepts() {
147  { // Checking graph components
148    checkConcept<BaseGraphComponent, BaseGraphComponent >();
149
150    checkConcept<IDableGraphComponent<>,
151      IDableGraphComponent<> >();
152
153    checkConcept<IterableGraphComponent<>,
154      IterableGraphComponent<> >();
155
156    checkConcept<MappableGraphComponent<>,
157      MappableGraphComponent<> >();
158  }
159  { // Checking skeleton graph
160    checkConcept<Graph, Graph>();
161  }
162  { // Checking ListGraph
163    checkConcept<Graph, ListGraph>();
164    checkConcept<AlterableGraphComponent<>, ListGraph>();
165    checkConcept<ExtendableGraphComponent<>, ListGraph>();
166    checkConcept<ClearableGraphComponent<>, ListGraph>();
167    checkConcept<ErasableGraphComponent<>, ListGraph>();
168  }
169  { // Checking SmartGraph
170    checkConcept<Graph, SmartGraph>();
171    checkConcept<AlterableGraphComponent<>, SmartGraph>();
172    checkConcept<ExtendableGraphComponent<>, SmartGraph>();
173    checkConcept<ClearableGraphComponent<>, SmartGraph>();
174  }
175  { // Checking FullGraph
176    checkConcept<Graph, FullGraph>();
177  }
178  { // Checking GridGraph
179    checkConcept<Graph, GridGraph>();
180  }
181  { // Checking HypercubeGraph
182    checkConcept<Graph, HypercubeGraph>();
183  }
184}
185
186template <typename Graph>
187void checkGraphValidity() {
188  TEMPLATE_GRAPH_TYPEDEFS(Graph);
189  Graph g;
190
191  Node
192    n1 = g.addNode(),
193    n2 = g.addNode(),
194    n3 = g.addNode();
195
196  Edge
197    e1 = g.addEdge(n1, n2),
198    e2 = g.addEdge(n2, n3);
199
200  check(g.valid(n1), "Wrong validity check");
201  check(g.valid(e1), "Wrong validity check");
202  check(g.valid(g.direct(e1, true)), "Wrong validity check");
203
204  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
205  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
206  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
207}
208
209template <typename Graph>
210void checkGraphValidityErase() {
211  TEMPLATE_GRAPH_TYPEDEFS(Graph);
212  Graph g;
213
214  Node
215    n1 = g.addNode(),
216    n2 = g.addNode(),
217    n3 = g.addNode();
218
219  Edge
220    e1 = g.addEdge(n1, n2),
221    e2 = g.addEdge(n2, n3);
222
223  check(g.valid(n1), "Wrong validity check");
224  check(g.valid(e1), "Wrong validity check");
225  check(g.valid(g.direct(e1, true)), "Wrong validity check");
226
227  g.erase(n1);
228
229  check(!g.valid(n1), "Wrong validity check");
230  check(g.valid(n2), "Wrong validity check");
231  check(g.valid(n3), "Wrong validity check");
232  check(!g.valid(e1), "Wrong validity check");
233  check(g.valid(e2), "Wrong validity check");
234
235  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
236  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
237  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
238}
239
240void checkGridGraph(int width, int height) {
241  typedef GridGraph Graph;
242  GRAPH_TYPEDEFS(Graph);
243  Graph G(width, height);
244
245  check(G.width() == width, "Wrong column number");
246  check(G.height() == height, "Wrong row number");
247
248  for (int i = 0; i < width; ++i) {
249    for (int j = 0; j < height; ++j) {
250      check(G.col(G(i, j)) == i, "Wrong column");
251      check(G.row(G(i, j)) == j, "Wrong row");
252      check(G.pos(G(i, j)).x == i, "Wrong column");
253      check(G.pos(G(i, j)).y == j, "Wrong row");
254    }
255  }
256
257  for (int j = 0; j < height; ++j) {
258    for (int i = 0; i < width - 1; ++i) {
259      check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right");
260      check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right");
261    }
262    check(G.right(G(width - 1, j)) == INVALID, "Wrong right");
263  }
264
265  for (int j = 0; j < height; ++j) {
266    for (int i = 1; i < width; ++i) {
267      check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left");
268      check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left");
269    }
270    check(G.left(G(0, j)) == INVALID, "Wrong left");
271  }
272
273  for (int i = 0; i < width; ++i) {
274    for (int j = 0; j < height - 1; ++j) {
275      check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up");
276      check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up");
277    }
278    check(G.up(G(i, height - 1)) == INVALID, "Wrong up");
279  }
280
281  for (int i = 0; i < width; ++i) {
282    for (int j = 1; j < height; ++j) {
283      check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down");
284      check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down");
285    }
286    check(G.down(G(i, 0)) == INVALID, "Wrong down");
287  }
288
289  checkGraphNodeList(G, width * height);
290  checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height);
291  checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
292
293  for (NodeIt n(G); n != INVALID; ++n) {
294    int nb = 4;
295    if (G.col(n) == 0) --nb;
296    if (G.col(n) == width - 1) --nb;
297    if (G.row(n) == 0) --nb;
298    if (G.row(n) == height - 1) --nb;
299
300    checkGraphOutArcList(G, n, nb);
301    checkGraphInArcList(G, n, nb);
302    checkGraphIncEdgeList(G, n, nb);
303  }
304
305  checkArcDirections(G);
306
307  checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height));
308  checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height);
309
310  checkNodeIds(G);
311  checkArcIds(G);
312  checkEdgeIds(G);
313  checkGraphNodeMap(G);
314  checkGraphArcMap(G);
315  checkGraphEdgeMap(G);
316
317}
318
319void checkHypercubeGraph(int dim) {
320  GRAPH_TYPEDEFS(HypercubeGraph);
321
322  HypercubeGraph G(dim);
323  checkGraphNodeList(G, 1 << dim);
324  checkGraphEdgeList(G, dim * (1 << dim-1));
325  checkGraphArcList(G, dim * (1 << dim));
326
327  Node n = G.nodeFromId(dim);
328
329  for (NodeIt n(G); n != INVALID; ++n) {
330    checkGraphIncEdgeList(G, n, dim);
331    for (IncEdgeIt e(G, n); e != INVALID; ++e) {
332      check( (G.u(e) == n &&
333              G.id(G.v(e)) == G.id(n) ^ (1 << G.dimension(e))) ||
334             (G.v(e) == n &&
335              G.id(G.u(e)) == G.id(n) ^ (1 << G.dimension(e))),
336             "Wrong edge or wrong dimension");
337    }
338
339    checkGraphOutArcList(G, n, dim);
340    for (OutArcIt a(G, n); a != INVALID; ++a) {
341      check(G.source(a) == n &&
342            G.id(G.target(a)) == G.id(n) ^ (1 << G.dimension(a)),
343            "Wrong arc or wrong dimension");
344    }
345
346    checkGraphInArcList(G, n, dim);
347    for (InArcIt a(G, n); a != INVALID; ++a) {
348      check(G.target(a) == n &&
349            G.id(G.source(a)) == G.id(n) ^ (1 << G.dimension(a)),
350            "Wrong arc or wrong dimension");
351    }
352  }
353
354  checkGraphConArcList(G, (1 << dim) * dim);
355  checkGraphConEdgeList(G, dim * (1 << dim-1));
356
357  checkArcDirections(G);
358
359  checkNodeIds(G);
360  checkArcIds(G);
361  checkEdgeIds(G);
362  checkGraphNodeMap(G);
363  checkGraphArcMap(G);
364  checkGraphEdgeMap(G);
365}
366
367void checkGraphs() {
368  { // Checking ListGraph
369    checkGraph<ListGraph>();
370    checkGraphValidityErase<ListGraph>();
371  }
372  { // Checking SmartGraph
373    checkGraph<SmartGraph>();
374    checkGraphValidity<SmartGraph>();
375  }
376  { // Checking FullGraph
377    checkFullGraph(7);
378    checkFullGraph(8);
379  }
380  { // Checking GridGraph
381    checkGridGraph(5, 8);
382    checkGridGraph(8, 5);
383    checkGridGraph(5, 5);
384    checkGridGraph(0, 0);
385    checkGridGraph(1, 1);
386  }
387  { // Checking HypercubeGraph
388    checkHypercubeGraph(1);
389    checkHypercubeGraph(2);
390    checkHypercubeGraph(3);
391    checkHypercubeGraph(4);
392  }
393}
394
395int main() {
396  checkConcepts();
397  checkGraphs();
398  return 0;
399}
Note: See TracBrowser for help on using the repository browser.