summaryrefslogtreecommitdiff
path: root/grown_networks.cpp
blob: ea4deaef8595f0d76f66c714953a308d5c968b55 (plain)
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

#include <fstream>
#include <random>
#include <cinttypes>
#include <vector>
#include <string>

#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"

using Rng = randutils::random_generator<pcg32>;

class ClusterTree {
private:
  std::vector<signed> p;
  std::vector<unsigned> o;

  unsigned findroot(unsigned i) {
    if (p[i] < 0)
      return i;
    else
      return p[i] = this->findroot(p[i]);
  }

  void join(unsigned i, unsigned j) {
    if (o[i] < o[j]) {
      p[i] = j;
      o[j] += o[i];
    } else {
      p[j] = i;
      o[i] += o[j];
    }
  }

public:
  ClusterTree(unsigned n) : p(n, -1), o(n, 1) {}

  void add_bond(unsigned v0, unsigned v1) {
    unsigned i = this->findroot(v0);
    unsigned j = this->findroot(v1);

    if (i != j) {
      this->join(i, j);
    }
  }

  bool same_component(unsigned v0, unsigned v1) {
    unsigned i = this->findroot(v0);
    unsigned j = this->findroot(v1);

    return i == j;
  }

  signed pointer(unsigned i) const {
    return p[i];
  }

  unsigned size(unsigned i) const {
    return o[i];
  }
};

int main(int argc, char* argv[]) {
  unsigned N = 10;
  unsigned T = 1000;
  double δ = 1.0 / 8;

  int opt;

  while ((opt = getopt(argc, argv, "N:T:d:")) != -1) {
    switch (opt) {
      case 'N':
        N = (unsigned)atof(optarg);
        break;
      case 'T':
        T = (unsigned)atof(optarg);
        break;
      case 'd':
        δ = atof(optarg);
        break;
      default:
        exit(1);
    }
  }

  Rng rng;

  std::uniform_real_distribution<double> dr(0.0, 1.0);

  std::vector<uint64_t> ns(T);
  std::vector<uint64_t> nS(T);

  for (unsigned i = 0; i < N; i++) {
    ClusterTree G(T);

    for (unsigned t = 0; t < T; t++) {
      if (rng.uniform(0.0, 1.0) < δ) {
        G.add_bond(rng.uniform<unsigned>(0, t), rng.uniform<unsigned>(0, t));
      }
    }

    unsigned maxSize = 0;

    for (unsigned j = 0; j < T; j++) {
      if (G.pointer(j) < 0) {
        ns[G.size(j) - 1]++;

        if (G.size(j) > maxSize) {
          maxSize= G.size(j);
        }
      }
    }

    nS[maxSize - 1]++;
  }

  std::string filename = "ns_" + std::to_string(δ) + "_" + std::to_string(T) + ".dat";

  std::ifstream outfile(filename);

  std::vector<uint64_t> ns_old(T, 0);

  if (outfile.is_open()) {
    for (unsigned i = 0; i < T; i++) {
      uint64_t num;
      outfile >> num;
      ns_old[i] = num;
    }

    outfile.close();
  }

  std::ofstream file_out(filename);

  for (unsigned i = 0; i < T; i++) {
    file_out <<std::fixed<< ns_old[i] + ns[i] << " ";
  }

  std::string filename_S = "nS_" + std::to_string(δ) + "_" + std::to_string(T) + ".dat";

  std::ifstream outfile_S(filename_S);

  std::vector<uint64_t> nS_old(T, 0);

  if (outfile_S.is_open()) {
    for (unsigned i = 0; i < T; i++) {
      uint64_t num;
      outfile_S >> num;
      nS_old[i] = num;
    }

    outfile_S.close();
  }

  std::ofstream file_out_S(filename_S);

  for (unsigned i = 0; i < T; i++) {
    file_out_S <<std::fixed<< nS_old[i] + nS[i] << " ";
  }

  return 0;
}