-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUndirectedGraph.h
263 lines (229 loc) · 6.8 KB
/
UndirectedGraph.h
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
#pragma once
#include<vector>
#include<set>
#include<unordered_set>
#include<iterator>
#include<iostream>
#include<algorithm>
using namespace std;
class UndirectedGraph
{
public:
static int count;
int id;
int numVertices;
int numEdges;
vector <set <int>> graph;
UndirectedGraph(int V);
void addEdge(int src, int dest);
void removeEdge(int src, int dest);
int containsCycle();
void displayGraph() const;
int isEdge(int src, int dest);
static int getNextID();
static UndirectedGraph Union(UndirectedGraph graphA, UndirectedGraph graphB);
static UndirectedGraph Intersection(UndirectedGraph graphA, UndirectedGraph graphB);
static UndirectedGraph Subtraction(UndirectedGraph graphA, UndirectedGraph graphB);
static UndirectedGraph RingSum(UndirectedGraph graphA, UndirectedGraph graphB);
UndirectedGraph Complement();
bool isPlanner();
void CyclicExchange();
bool operator<(const UndirectedGraph& t) const
{
return id < t.id;
}
};
int UndirectedGraph::count{ 0 };
int UndirectedGraph::getNextID() {
return ++count;
}
UndirectedGraph::UndirectedGraph(int V) {
id = getNextID();
numVertices = V;
numEdges = 0;
for (int i = 0; i < numVertices; i++)
{
graph.push_back(set<int> {});
}
}
void UndirectedGraph::displayGraph() const {
int node = 0;
for (auto i = graph.begin(); i != graph.end(); i++)
{
cout << (node++) << " -> ";
for (auto j = (*i).begin(); j != (*i).end(); j++)
{
cout << *j << " -> ";
}
cout << endl;
}
}
void UndirectedGraph::addEdge(int src, int dest) {
auto it = find (graph[src].begin(), graph[src].end(), dest);
if (it == graph[src].end()) numEdges++;
graph[src].insert(dest);
graph[dest].insert(src);
}
void UndirectedGraph::removeEdge(int src, int dest) {
auto it = find (graph[src].begin(), graph[src].end(), dest);
if (it != graph[src].end()) numEdges--;
graph[src].erase(dest);
graph[dest].erase(src);
}
int UndirectedGraph::containsCycle() {
int* visited = new int[numVertices]();
visited[0] = 1;
for (int i = 0; i < numVertices; i++)
{
for (auto&& j : graph[i])
{
if (j > i && visited[j]) return 1;
else visited[j] = 1;
}
}
delete[] visited;
return 0;
}
int UndirectedGraph::isEdge(int src, int dest) {
return (graph[src].find(dest) != graph[src].end());
}
UndirectedGraph UndirectedGraph::Union(UndirectedGraph graphA, UndirectedGraph graphB) {
int V = max(graphA.numVertices, graphB.numVertices);
UndirectedGraph union_graph(V);
for (int i = 0; i < V; i++)
{
set_union(
graphA.graph[i].begin(),
graphA.graph[i].end(),
graphB.graph[i].begin(),
graphB.graph[i].end(),
inserter(union_graph.graph[i], union_graph.graph[i].begin())
);
}
return union_graph;
}
UndirectedGraph UndirectedGraph::Intersection(UndirectedGraph graphA, UndirectedGraph graphB) {
int V = max(graphA.numVertices, graphB.numVertices);
UndirectedGraph intersection_graph(V);
for (int i = 0; i < V; i++)
{
set_intersection(
graphA.graph[i].begin(),
graphA.graph[i].end(),
graphB.graph[i].begin(),
graphB.graph[i].end(),
inserter(intersection_graph.graph[i], intersection_graph.graph[i].begin())
);
}
return intersection_graph;
}
UndirectedGraph UndirectedGraph::Subtraction(UndirectedGraph graphA, UndirectedGraph graphB) {
int V = max(graphA.numVertices, graphB.numVertices);
UndirectedGraph subtracted_graph(V);
for (int i = 0; i < V; i++)
{
set_difference(
graphA.graph[i].begin(),
graphA.graph[i].end(),
graphB.graph[i].begin(),
graphB.graph[i].end(),
inserter(subtracted_graph.graph[i], subtracted_graph.graph[i].begin())
);
}
return subtracted_graph;
}
UndirectedGraph UndirectedGraph::RingSum(UndirectedGraph graphA, UndirectedGraph graphB) {
return Subtraction(
Union(graphA, graphB),
Intersection(graphA, graphB)
);
}
UndirectedGraph UndirectedGraph::Complement() {
UndirectedGraph complement_graph(numVertices);
set <int> allVer;
for (int i = 0; i < numVertices; i++)
{
allVer.insert(i);
}
for (int i = 0; i < numVertices; i++)
{
allVer.erase(i);
set_difference(
allVer.begin(),
allVer.end(),
graph[i].begin(),
graph[i].end(),
inserter(complement_graph.graph[i], complement_graph.graph[i].begin())
);
allVer.insert(i);
}
return complement_graph;
}
void UndirectedGraph::CyclicExchange() {
using namespace std;
// creating a tree
UndirectedGraph tree(numVertices);
int n = 0;
while (n < numVertices - 1)
{
for (int i = 0; i < numVertices; i++)
{
for (auto&& j : graph[i])
{
tree.addEdge(i, j);
if (tree.containsCycle()) tree.removeEdge(i, j);
else if (j > i) n++;
}
}
}
int c = 1;
UndirectedGraph temp = tree;
cout << "Tree: " << c << endl;
cout << "================" << endl;
tree.displayGraph();
for (int src = 0; src < numVertices; src++)
{
for (auto&& dest : graph[src])
{
if (this->isEdge(src, dest) && !temp.isEdge(src, dest)) {
temp.addEdge(src, dest);
for (int treesrc = 0; treesrc < numVertices; treesrc++)
{
for (auto&& treedest : tree.graph[treesrc])
{
int ti = treedest;
temp.removeEdge(treesrc, ti);
if (!temp.containsCycle()) {
++c;
cout << "Tree: " << c << endl;
cout << "================" << endl;
temp.displayGraph();
}
temp.addEdge(treesrc, ti);
}
}
temp.removeEdge(src, dest);
}
}
}
}
bool UndirectedGraph::isPlanner(){
if(containsCycle()){
cout << "Here" << endl;
if(numEdges<=(3*numVertices-6)){
return true;
}
else{
return false;
}
}
else{
cout << "There" << endl;
if(numEdges<=(2*numVertices-4)){
return true;
}
else{
return false;
}
}
}