-
Notifications
You must be signed in to change notification settings - Fork 0
/
undirected_weighted_graph.cpp
63 lines (53 loc) · 1.88 KB
/
undirected_weighted_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
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
if (argc < 4) {
cout << "Not enough arguments\n";
return 1;
}
const int NODES_COUNT = stoi(argv[1]);
const int EDGES_COUNT = stoi(argv[2]);
const int MAX_EDGE_WEIGHT = stoi(argv[3]);
if (EDGES_COUNT > NODES_COUNT * (NODES_COUNT - 1) / 2) {
cout << "Invalid edges count "
<< EDGES_COUNT
<< " for "
<< NODES_COUNT
<< " nodes. Maximum count of edges for "
<< NODES_COUNT
<< " nodes can be "
<< (NODES_COUNT * (NODES_COUNT - 1) / 2)
<< ".\n";
return 1;
}
set<pair<int, int> > container;
// Uncomment the below line to store the test data in a file
// freopen("input.txt", "w", stdout);
srand(time(NULL));
cout << NODES_COUNT << ' ' << EDGES_COUNT << '\n';
// Print the edges of the form (a b) where 'a' is connected to 'b'
for (int j = 1; j <= EDGES_COUNT; j++) {
int a = rand() % NODES_COUNT;
int b = rand() % NODES_COUNT;
pair<int, int> p = { a, b };
pair<int, int> reverse_p = { b, a };
// 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() || container.find(reverse_p) != container.end()) {
a = rand() % NODES_COUNT;
b = rand() % NODES_COUNT;
p = { a, b };
reverse_p = { b, a };
}
container.insert(p);
}
for (auto it = container.begin(); it != container.end(); ++it) {
int wt = 1 + rand() % MAX_EDGE_WEIGHT;
cout << (it->first + 1) << ' ' << (it->second + 1) << ' ' << wt << '\n';
}
// Uncomment the below line to store the test data in a file
// fclose(stdout);
}