-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.cpp
More file actions
269 lines (196 loc) · 9.04 KB
/
Copy pathDemo.cpp
File metadata and controls
269 lines (196 loc) · 9.04 KB
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
/*
Author : shlomi zecharia
ID : 315141242
Email : 2shlomiariel9@gmail.com
*/
#include "Graph.hpp"
#include "Algorithms.hpp"
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;
using ariel::Algorithms;
int main()
{
ariel::Graph g1;
ariel::Graph g2;
ariel::Graph g3;
vector<vector<int>> graph1 = {
{0, 1, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0}};
g1.loadGraph(graph1);
vector<vector<int>> graph2 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 5},
{0, 0, 0, 5, 0}};
g2.loadGraph(graph2);
vector<vector<int>> graph3 = {
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
{1, 0, 0, 0}};
g3.loadGraph(graph3);
// --------Before changes Graph g1-------
cout<<"g1 : \n"<< g1 <<endl;
cout<<"isConnected:"<<endl;
cout << Algorithms::isConnected(g1) << endl; // Should print: "0"
cout<<"shortestPath(0,2):"<<endl;
cout << Algorithms::shortestPath(g1, 0, 2) << endl; // Should print: "0->1->2"
cout<<"isContainsCycle:"<<endl;
cout << Algorithms::isContainsCycle(g1) << endl; // Should print: "The cycle is: 0 -> 1 -> 2 -> 0"
cout<<"isBipartite:"<<endl;
cout << Algorithms::isBipartite(g1) << endl; // Should print: "The graph is not bipartite (adjacent vertices have the same color)"
cout<<"negativeCycle:"<<endl;
cout << Algorithms::negativeCycle(g1) << endl; // Should print: "0"
// ---------- After Changes graph g1 ------------
cout<<"\ng1++ : \n"<< g1++ <<endl;
cout<<"++g1 : \n"<< ++g1 <<endl;
cout<<"isConnected:"<<endl;
cout << Algorithms::isConnected(g1) << endl; // Should print: "0"
cout<<"shortestPath(0,2):"<<endl;
cout << Algorithms::shortestPath(g1, 0, 2) << endl; // Should print: "0->2"
cout<<"isContainsCycle:"<<endl;
cout << Algorithms::isContainsCycle(g1) << endl; // Should print: "The cycle is: 0 -> 1 -> 2 -> 0"
cout<<"isBipartite:"<<endl;
cout << Algorithms::isBipartite(g1) << endl; // Should print: "The graph is not bipartite (adjacent vertices have the same color)"
cout<<"negativeCycle:"<<endl;
cout << Algorithms::negativeCycle(g1) << endl; // Should print: "0"
cout<<"g1-- : \n"<< g1-- <<endl;
cout<<"--g1 : \n"<< --g1 <<endl;
g1 *= -2;
cout<<"g1 * (-2) : \n"<<g1<<endl;
cout<<"isConnected:"<<endl;
cout << Algorithms::isConnected(g1) << endl; // Should print: "0"
cout<<"shortestPath(0,2):"<<endl;
cout << Algorithms::shortestPath(g1, 0, 2) << endl; // Should print: "Detect negative cycle, no shortest path"
cout<<"isContainsCycle:"<<endl;
cout << Algorithms::isContainsCycle(g1) << endl; // Should print: "0 -> 1 -> 2 -> 0"
cout<<"isBipartite:"<<endl;
cout << Algorithms::isBipartite(g1) << endl; // Should print: "The graph is not bipartite (adjacent vertices have the same color)"
cout<<"negativeCycle:"<<endl;
cout << Algorithms::negativeCycle(g1) << endl; // Should print: "1"
g1 /= -2;
cout<<"\ng1 / (-2) :\n"<<g1<<endl;
cout<<"isConnected:"<<endl;
cout << Algorithms::isConnected(g1) << endl; // Should print: "0"
cout<<"shortestPath(0,2):"<<endl;
cout << Algorithms::shortestPath(g1, 0, 2) << endl; // Should print: 0->2
cout<<"isContainsCycle:"<<endl;
cout << Algorithms::isContainsCycle(g1) << endl; // Should print: "0 -> 1 -> 2 -> 0"
cout<<"isBipartite:"<<endl;
cout << Algorithms::isBipartite(g1) << endl; // Should print: "The graph is not bipartite (adjacent vertices have the same color)"
cout<<"negativeCycle:"<<endl;
cout << Algorithms::negativeCycle(g1) << endl; // Should print: "0"
cout<<"g1 + (unary) :\n" <<+g1<<endl;
cout<<"g1 - (unary) :\n" << -g1<<endl;
// -------- Mul graphs g1 and g2 --------------
ariel::Graph g_mul;
g_mul = g1*g2;
cout<<"g1 : \n"<<g1<<endl;
cout<<"g2 : \n"<<g2<<endl;
cout<<"g1*g2 : \n"<<g_mul<<endl;
cout<<"isConnected:"<<endl;
cout << Algorithms::isConnected(g_mul) << endl; // Should print: "1"
cout<<"shortestPath(0,2):"<<endl;
cout << Algorithms::shortestPath(g_mul, 0, 2) << endl; // Should print: "Detect negative cycle, no shortest path"
cout<<"isContainsCycle:"<<endl;
cout << Algorithms::isContainsCycle(g_mul) << endl; // Should print: "The cycle is: 0 -> 0"
cout<<"isBipartite:"<<endl;
cout << Algorithms::isBipartite(g_mul) << endl; // Should print: "The graph is not bipartite (adjacent vertices have the same color)"
cout<<"negativeCycle:"<<endl;
cout << Algorithms::negativeCycle(g_mul) << endl; // Should print: "1"
// ----------- Comparison g1 , g2 , g3 -------------
bool ans = (g1==g2);
cout<<"g1 == g2 ? "<< ans <<endl; // Should print: "0"
ans = (g1<g2);
cout<<"g1 < g2 ? "<< ans <<endl; // Should print: "1"
cout<<"\ng3 : \n"<<g3<<endl;
ans = (g3<=g2); // Should print: "1"
cout<<"g3 <= g2 ?"<<ans<<endl;
// ----------- Extreme cases -------------
//Compare undirected graph with directed graph
vector<vector<int>> graph4 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 5},
{0, 0, 0, 5, 0}};
g1.loadGraph(graph4);
vector<vector<int>> graph5 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 5},
{0, 0, 1, 0, 0}};
g2.loadGraph(graph5);
ans = (g1==g2);
cout<<"\ng1 == g2 ? "<< ans <<endl; // Should print: "0"
ans = (g1<g2);
cout<<"g1 < g2 ? "<< ans <<endl; // Should print: "1"
//Compare by number of edges when, g1 size > g2 size
vector<vector<int>> graph6 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
g1.loadGraph(graph6);
vector<vector<int>> graph7 = {
{0, 1, 2, 0},
{1, 0, 3, 0},
{2, 3, 0, 4},
{0, 0, 4, 0}};
g2.loadGraph(graph7);
ans = (g1==g2);
cout<<"\ng1 == g2 ? "<< ans <<endl; // Should print: "0"
ans = (g1<g2);
cout<<"g1 < g2 ? "<< ans <<endl; // Should print: "0"
//Compare by size of the matrix, when g1 number of edges = g2 number of edges
vector<vector<int>> graph8 = {
{0, 1, 2, 0, 0},
{1, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 0},
{0, 0, 0, 0, 0}};
g1.loadGraph(graph8);
vector<vector<int>> graph9 = {
{0, 1, 2, 0},
{1, 0, 3, 0},
{2, 3, 0, 4},
{0, 0, 4, 0}};
g2.loadGraph(graph9);
ans = (g1==g2);
cout<<"\ng1 == g2 ? "<< ans <<endl; // Should print: "0"
ans = (g1<g2);
cout<<"g1 < g2 ? "<< ans <<endl; // Should print: "0"
//Compare by edges weight, when g1 number of edges = g2 number of edges && g1 size == g2 size
vector<vector<int>> graph10 = {
{0, 3, 2, 0, 0},
{3, 0, 3, 0, 0},
{2, 3, 0, 4, 0},
{0, 0, 4, 0, 7},
{0, 0, 0, 7, 0}};
g1.loadGraph(graph10);
vector<vector<int>> graph11 = {
{0, 1, 2, 0, 0},
{1, 0, 4, 0, 0},
{2, 4, 0, 5, 0},
{0, 0, 5, 0, 12},
{0, 0, 0, 12, 0}};
g2.loadGraph(graph11);
ans = (g1==g2);
cout<<"\ng1 == g2 ? "<< ans <<endl; // Should print: "1"
ans = (g1<=g2);
cout<<"g1 <= g2 ? "<< ans <<endl; // Should print: "1"
ans = (g1>=g2);
cout<<"g1 >= g2 ? "<< ans <<endl; // Should print: "1"
ans = (g1<g2);
cout<<"g1 < g2 ? "<< ans <<endl; // Should print: "0"
ans = (g1>g2);
cout<<"g1 > g2 ? "<< ans <<endl; // Should print: "0"
}