-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_graph.cpp
307 lines (267 loc) · 7.52 KB
/
test_graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
@file test_graph.cpp
@author Ayoub Ouarrak, [email protected]
@version 1.0
*/
#include "Graph.hh"
#include <iostream>
#include <string>
#include <ctime>
#include <list>
using namespace GraphLib;
void testRandomGraphGenerator() {
/** random Graph with(max) 10 nodes */
std::cout << "***** test 1: generate random graph" << std::endl;
Graph G1 = Graph::generateRandomGraph(10);
std::cout << G1 << std::endl;
std::cout << "**** test 1.1 rank of a Node" << std::endl;
std::cout << G1.rank("3") << std::endl;
}
void testGraphInterval() {
/** circular Graph with nodes{a,b,c,d} */
std::cout << "***** test 2: interval:<a-d> using Graph::circular " << std::endl;
Graph G2("a-d", Graph::circular);
std::cout << "***** test 2.1: setWeight" << std::endl;
/** manual setting of weight in the edge */
G2.setWeight("c", "d", 6);
std::cout << G2 << std::endl;
/** nodes{1,2,3,4} with random edges to connect the nodes */
std::cout << "***** test 3: interval:<1-4> using Graph::random " << std::endl;
Graph G3("1-4", Graph::random);
std::cout << G3 << std::endl;
}
void testUndirectedGraph() {
std::cout << "***** test 4: undirected graph" << std::endl;
/** undirected Graph */
Graph G4(Graph::undirected);
std::cout << "***** test 4.1: addNode" << std::endl;
G4.addNode("h");
G4.addNode("y");
G4.addNode("j");
std::cout << "***** test 4.2: addEdge" << std::endl;
G4.addEdge("h", "j"); // default weight is 1
G4.addEdge("j", "y", 5); // 5: weight of edge <j, y>
G4.addEdge("y", "h");
std::cout << G4 << std::endl;
}
void testNodeAdjacent() {
std::cout << "**** test 6: Node adjacent to 2 (in G5)" << std::endl;
Graph G5 = Graph::generateRandomGraph(6);
std::cout << G5 << std::endl;
std::list<std::string> adj = G5.adjacent("2");
std::list<std::string>::const_iterator adjIt;
for(adjIt = adj.begin(); adjIt != adj.end(); ++adjIt) {
std::cout << *adjIt << " ";
}
std::cout << std::endl;
}
void testRemoveEdge() {
std::cout << "**** test 8: remove edge" << std::endl;
Graph G8("a-f", Graph::circular);
std::cout << G8 << std::endl;
G8.removeEdge("b", "c");
G8.removeEdge("d", "e");
std::cout << G8 << std::endl;
}
void testDraw() {
std::cout << "**** test 11: draw graph using dracula javascript library" << std::endl;
Graph G11;
G11.addNode("v1");
G11.addNode("v2");
G11.addNode("v3");
G11.addNode("v4");
G11.addEdge("v1", "v3", 5);
G11.addEdge("v2", "v4", 3);
G11.addEdge("v1", "v2", 6);
G11.addEdge("v3", "v2", 10);
std::cout << G11 << std::endl;
G11.draw();
}
void testRemoveNode() {
std::cout << "**** test 12: remove A" << std::endl;
Graph G(Graph::undirected);
G.addEdge("A", "B");
G.addEdge("B", "C");
G.addEdge("D", "A");
G.addEdge("A", "F");
G.addEdge("C", "A");
std::cout << G;
G.removeNode("A");
std::cout << G;
}
void testIsCyclic() {
std::cout << "**** test 13: cyclic Graph" << std::endl;
Graph G("1-4", Graph::random);
std::cout << G << std::endl;
if(G.isCyclic())
std::cout << "Graph contains cycle" << std::endl;
else
std::cout << "Graph doesn't contain cycle" << std::endl;
}
void testTranspose() {
std::cout << "**** test 14: graph transpose" << std::endl;
Graph g(Graph::directed);
g.addEdge("1", "0");
g.addEdge("0", "2");
g.addEdge("2", "1");
g.addEdge("0", "3");
g.addEdge("3", "4");
std::cout << g << std::endl;
std::cout << g.transpose() << std::endl;
}
void testColoring() {
std::cout << "**** test 15: coloring graph" << std::endl;
Graph g2(Graph::undirected);
g2.addEdge("0", "1");
g2.addEdge("0", "2");
g2.addEdge("1", "2");
g2.addEdge("1", "4");
g2.addEdge("2", "4");
g2.addEdge("4", "3");
std::cout << "Coloring of Graph 1 \n";
g2.coloring();
}
void testEulerian() {
std::cout << "**** test 16: eulerian cycle/path" << std::endl;
Graph g1;
g1.addNode("0");
g1.addNode("1");
g1.addNode("2");
g1.addNode("3");
g1.addNode("4");
g1.addEdge("1", "0");
g1.addEdge("0", "2");
g1.addEdge("2", "1");
g1.addEdge("0", "3");
g1.addEdge("3", "1");
std::cout << g1 << std::endl;
int res = g1.isEulerian();
if (res == 0)
std::cout << "Graph is not Eulerian \n";
else if (res == 1)
std::cout << "Graph has a Euler path \n";
else
std::cout << "Graph has a Euler cycle \n";
Graph g2;
g2.addNode("0");
g2.addNode("1");
g2.addNode("2");
g2.addNode("3");
g2.addNode("4");
g2.addEdge("1", "0");
g2.addEdge("0", "2");
g2.addEdge("2", "1");
g2.addEdge("0", "3");
g2.addEdge("3", "4");
g2.addEdge("4", "0");
std::cout << g2 << std::endl;
res = g2.isEulerian();
if (res == 0)
std::cout << "Graph is not Eulerian \n";
else if (res == 1)
std::cout << "Graph has a Euler path \n";
else
std::cout << "Graph has a Euler cycle \n";
Graph g3;
g3.addNode("0");
g3.addNode("1");
g3.addNode("2");
g3.addNode("3");
g3.addNode("4");
g3.addEdge("1", "0");
g3.addEdge("0", "2");
g3.addEdge("2", "1");
g3.addEdge("0", "3");
g3.addEdge("3", "4");
g3.addEdge("1", "3");
std::cout << g3 << std::endl;
res = g3.isEulerian();
if (res == 0)
std::cout << "Graph is not Eulerian \n";
else if (res == 1)
std::cout << "Graph has a Euler path \n";
else
std::cout << "Graph has a Euler cycle \n";
// Let us create a graph with 3 vertices
// connected in the form of cycle
Graph g4;
g4.addNode("0");
g4.addNode("1");
g4.addNode("2");
g4.addEdge("0", "1");
g4.addEdge("1", "2");
g4.addEdge("2", "0");
std::cout << g4 << std::endl;
res = g4.isEulerian();
if (res == 0)
std::cout << "Graph is not Eulerian \n";
else if (res == 1)
std::cout << "Graph has a Euler path \n";
else
std::cout << "Graph has a Euler cycle \n";
}
void testBFS() {
std::cout << "**** test 17: BFS" << std::endl;
Graph g;
g.addEdge("0", "1");
g.addEdge("0", "2");
g.addEdge("1", "2");
g.addEdge("2", "0");
g.addEdge("2", "3");
g.addEdge("3", "3");
std::cout << std::endl;
std::cout << "Following is Breadth First Traversal (starting from vertex 2) \n";
std::cout << g << std::endl;
std::cout << std::endl;
g.BFS("2");
std::cout << std::endl;
}
void testDFS() {
std::cout << "**** test 18: DFS" << std::endl;
Graph g;
g.addEdge("0", "1");
g.addEdge("0", "2");
g.addEdge("1", "2");
g.addEdge("2", "0");
g.addEdge("2", "3");
g.addEdge("3", "3");
std::cout << std::endl;
std::cout << "Following is Depth First Traversal (starting from vertex 2) \n";
std::cout << g << std::endl;
std::cout << std::endl;
g.DFS("2");
std::cout << std::endl;
}
void testFloydWarshell() {
std::cout << "**** test 19: Floyd Warshall algorithm" << std::endl;
Graph g;
g.addNode("0");
g.addNode("1");
g.addNode("2");
g.addNode("3");
g.addEdge("0", "3", 9);
g.addEdge("0", "1", 5);
g.addEdge("1", "2", 3);
g.addEdge("2", "3", 1);
double** wMatrix = new double*[g.nodes()];
for(unsigned i = 0; i < g.nodes(); ++i)
wMatrix[i] = new double[g.nodes()];
std::cout << std::endl;
std::cout << g << std::endl;
g.floydWarshell(g.transpose().weightMatrix());
}
int main() {
testRandomGraphGenerator();
testGraphInterval();
testUndirectedGraph();
testNodeAdjacent();
testRemoveEdge();
testRemoveNode();
testIsCyclic();
testTranspose();
testColoring();
testEulerian();
testBFS();
testDFS();
testFloydWarshell();
}