-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDAGgen.cpp
390 lines (325 loc) · 9.23 KB
/
DAGgen.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// A C++ Program to generate test cases for
// an unweighted directed graph
#include <bits/stdc++.h>
using namespace std;
// Define the number of runs for the test data
// generated
#define RUN 1
// Define the maximum number of vertices of the graph
#define MAX_VERTICES 15
// Define the maximum number of edges
#define MAX_EDGES 30
#define MAX_WEIGHT 100
class Graph
{
public:
int V; // No. of vertices
// Pointer to an array containing adjacency lists
list<int> *adj;
Graph(int); // Constructor
void addEdge(int, int);
vector<int> BFS(int, int, int[]);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V + 1];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v); // Add w to v’s list.
// adj[v].push_back(u); // Add v to w’s list.
}
vector<int> Graph::BFS(int componentNum, int src, int visited[])
{
// Mark all the vertices as not visited
// Create a queue for BFS
queue<int> queue;
queue.push(src);
// Assign Component Number
visited[src] = componentNum;
// Vector to store all the reachable nodes from 'src'
vector<int> reachableNodes;
while (!queue.empty())
{
// Dequeue a vertex from queue
int u = queue.front();
queue.pop();
reachableNodes.push_back(u);
// Get all adjacent vertices of the dequeued
// vertex u. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (auto itr = adj[u].begin();
itr != adj[u].end(); itr++)
{
if (!visited[*itr])
{
// Assign Component Number to all the
// reachable nodes
visited[*itr] = componentNum;
queue.push(*itr);
}
}
}
return reachableNodes;
}
// Display all the Reachable Nodes from a node 'n'
void displayReachableNodes(int n, unordered_map<int, vector<int>> m)
{
vector<int> temp = m[n];
for (int i = 0; i < temp.size(); i++)
cout << temp[i] << " ";
cout << endl;
}
// Find all the reachable nodes for every element in the arr
bool findReachableNodes(Graph g, int a, int b)
{
// Get the number of nodes in the graph
int V = g.V;
int flag = 0;
// Take a integer visited array and initialize
// all the elements with 0
int visited[V + 1];
memset(visited, 0, sizeof(visited));
// Map to store list of reachable Nodes for a
// given node.
unordered_map<int, vector<int>> m;
// Initialize component Number with 0
int componentNum = 0;
// For each node in arr[] find reachable
// Nodes
// Visit all the nodes of the component
if (!visited[b])
{
componentNum++;
// Store the reachable Nodes corresponding to
// the node 'i'
m[visited[b]] = g.BFS(componentNum, b, visited);
}
// At this point, we have all reachable nodes
// from b, print them by doing a look up in map m.
// cout << "Reachable Nodes from " << b << " are\n";
// displayReachableNodes(visited[b], m);
// vector<int> temp = m[visited[b]];
for (int i = 0; i < m[visited[b]].size(); i++)
{
if (m[visited[b]][i] == a)
{
flag = 1;
}
}
return flag;
}
void printpath(vector<int> &path)
{
int size = path.size();
for (int i = 0; i < size; i++)
cout << path[i] << " ";
cout << endl;
}
// utility function to check if current
// vertex is already present in path
int isNotVisited(int x, vector<int> &path)
{
int size = path.size();
for (int i = 0; i < size; i++)
if (path[i] == x)
return 0;
return 1;
}
// utility function for finding paths in graph
// from source to destination
void findpaths(Graph &g, map<pair<int, int>, int> &w, int src, int dst)
{
// create a queue which stores
// the paths
queue<vector<int>> q;
int count = 0;
// path vector to store the current path
vector<int> path;
path.push_back(src);
q.push(path);
while (!q.empty() )
{
path = q.front();
q.pop();
int last = path[path.size() - 1];
// if last vertex is the desired destination
// then print the path
if (last == dst)
{
int flag = 1;
// printpath(path);
for (auto it : w)
{
if (it.second == 0)
{
flag = 0;
break;
}
}
if (flag == 0)
{
int wt = 1 + rand() % MAX_WEIGHT;
for (int i = 0; i < path.size() - 1; i++)
{
w[{path[i], path[i + 1]}] += wt;
}
// cout << "\t" << wt << endl;
}
for (int i = 0; i < path.size() - 1; i++)
{
path.pop_back();
}
}
// traverse to all the nodes connected to
// current vertex and push new path to queue
for (auto it : g.adj[last])
{
if (isNotVisited(it, path))
{
if (w[{last, it}] == 0)
{
vector<int> newpath(path);
newpath.push_back(it);
q.push(newpath);
}
}
}
count++;
}
}
void BFS(Graph &g, int s, vector<int> &bfs)
{
// Mark all the vertices as not visited
vector<bool> visited;
visited.resize(g.V, false);
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
while (!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
// cout << "\n"<< s;
bfs.push_back(s);
queue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (auto adjecent : g.adj[s])
{
if (!visited[adjecent])
{
visited[adjecent] = true;
queue.push_back(adjecent);
}
}
}
}
void print_weights(map<pair<int, int>, int> w, int n)
{
cout << "\t";
for (int i = 0; i < n; i++)
cout << i << "\t";
cout << endl;
for (int i = 0; i < n; i++)
{
cout << i << "\t";
for (int j = 0; j < n; j++)
{
cout << w[pair<int, int>(i, j)] << "\t";
}
cout << endl;
}
cout << endl;
}
int main()
{
set<pair<int, int>> container;
set<pair<int, int>>::iterator it;
std::ofstream ofs;
ofs.open("DAGraph.txt", std::ofstream::out | std::ofstream::trunc);
ofs.close();
// Uncomment the below line to store
// the test data in a file
freopen("DAGraph.txt", "w", stdout);
// For random values every time
srand(time(NULL));
int NUM; // Number of Vertices
int NUMEDGE; // Number of Edges
// NUM = 1 + rand() % MAX_VERTICES;
NUM = MAX_VERTICES;
NUMEDGE = MAX_EDGES;
vector<bool> sources(NUM, true);
vector<bool> sink(NUM, true);
map<pair<int, int>, int> w;
vector<int> path;
Graph g(NUM);
// Define the maximum number of edges of the graph
// Since the most dense graph can have N*(N-1)/2 edges
// where N = number of vertices in the graph
// First print the number of vertices and edges
// Then print the edges of the form (a b)
// where 'a' is connected to 'b'
for (int j = 1; j <= NUMEDGE; j++)
{
int a = 1 + rand() % (NUM - 2);
int b = 1 + rand() % (NUM - 2);
pair<int, int> p = make_pair(a, b);
// cout << a << " " << b << endl;
// Search for a random "new" edge everytime
// Note - In a tree the edge (a, b) is same
// as the edge (b, a)
while (container.find(p) != container.end() || findReachableNodes(g, a, b) || a == b)
{
a = 1 + rand() % (NUM - 2);
b = 1 + rand() % (NUM - 2);
p = make_pair(a, b);
}
container.insert(p);
g.addEdge(a, b);
w[{a, b}] = 0;
}
// now check for all sinks and all sources
for (it = container.begin(); it != container.end(); it++)
{
sources[it->second] = false;
sink[it->first] = false;
}
// cout << "\nSources\n";
for (int i = 1; i < NUM - 1; i++)
{
if (sources[i])
{
// printf("\n%d %d", 0, i);
pair<int, int> p = make_pair(0, i);
g.addEdge(0, i);
container.insert(p);
}
}
// cout << "\nSink\n";
for (int i = 1; i < NUM - 1; i++)
{
if (sink[i])
{
// printf("\n%d %d", i, NUM - 1);
pair<int, int> p = make_pair(i, NUM - 1);
g.addEdge(i, NUM - 1);
container.insert(p);
}
}
printf("%d %d", NUM, container.size());
findpaths(g, w, 0, NUM - 1);
for (it = container.begin(); it != container.end(); ++it)
{
printf("\n%d %d %d", it->first, it->second, w[{it->first, it->second}]);
}
// Uncomment the below line to store
// the test data in a file
container.clear();
fclose(stdout);
return (0);
}