#include <iostream> #include <cstdlib> #include <cmath> #include <assert.h> #include <vector> #include <random> #include <fstream> #include <sstream> #include<algorithm> #include<ctime> #include <stdio.h> #include <stdlib.h> #define L 20 #define SIZE L*L //total nodes #define UP 0 //up nieghbor #define RIGHT 1 //right nieghbor #define LEFT 2 //left nieghbor #define DOWN 3 //down nieghbor #define steps 10*SIZE //steps of the RW #define perofniegh 0.001 //total percent of the external links #define iteration 1 //total iterations using namespace std; int *labels; int n_labels = 0; vector <vector<int>> get_neighbors(vector <vector<int>> matrix ,mt19937& gen, uniform_int_distribution<int>& ran_pos); //finding nieghbors void uf_done(void); void uf_initialize(int max_labels); int uf_make_set(void); int uf_union(int x, int y); int uf_find(int x); int HK( int matrix[SIZE]); //Hoshen-Kopelman algorithm Array sizeClusters; int main(void) { srand(time(0)); random_device rd; mt19937 gen(rd()); //Mersenne Twister RNG uniform_int_distribution<int> ran_pos(0, SIZE-1); //Get any random integer int ff = rand(); // int path[steps] = {0}; //RW path // clock_t start, endd; ofstream output;//outputt;//outputt; output.open("test" + to_string(L) + "-" + to_string(ff) + ".txt"); // outputt.open("delta" + to_string(L) + "-" + to_string(ff) + ".txt"); // ofstream niegh; // niegh.open("niegh" + to_string(L) + ".txt"); for (int k = 0; k < iteration; k++) { // printf("iter = %d \n", k); int i, j; int pos_i, p; float prob; // ofstream file; // file.open ("path"+ to_string(k) + ".txt"); int lat [SIZE] = {0}; //the lattice int coplat[SIZE] = {0}; vector <std::vector<int>> matrix(SIZE); //nieghbor list double s[steps] = {0}; //cluster size list vector<double> delta(steps - 1); //deltas list matrix = get_neighbors(matrix, gen, ran_pos); pos_i = ran_pos(gen); lat [pos_i] = 1; // path[0] = pos_i; for ( int i = 0; i < steps; i++) { int siize = matrix[pos_i].size(); uniform_int_distribution<int> probab(0, siize-1); p = probab(gen); pos_i = matrix[pos_i][p]; //cout << pos_i << endl; lat [pos_i] = 1; // path[i + 1] = pos_i; copy_n(lat,SIZE,coplat); s[i] = ((float)HK(coplat)); } for (int j = 0; j < steps-1; j++) { delta[j] = s[j + 1]-s[j]; // file<<path[j]<<endl; } // file.close(); double deltamax = float(*max_element(delta.begin(), delta.end())) / float(SIZE); //auto deltamax2 = minmax_element (delta.begin(), delta.end()); int index1 = distance(delta.begin(), max_element(delta.begin(), delta.end())); //int index2 = distance(delta.begin(), deltamax2.second); output<< deltamax << "\t" << float(s[index1]) / float(SIZE) << "\t" << float(s[index1 + 1]) / float(SIZE) << "\t" << ((float)index1) / ((float)SIZE) <<endl; // for(int w = 0 ; w<delta.size(); w++) // { // outputt<< float(delta[w]) / float(SIZE)<< ","; // } // outputt<< endl; // } // double endd = clock(); // // double duration_sec = double(endd-start)/CLOCKS_PER_SEC; // cout<< duration_sec; // free(clusters); } output.close(); // outputt.close(); } vector <vector<int>> get_neighbors(vector <vector<int>> matrix,mt19937& gen, uniform_int_distribution<int>& ran_pos) { int i, j, k, m; int u, d, r, l; int percent = ((float)perofniegh * SIZE); for (i=0; i < L; i++) { for (j=0; j < L; j++) { //Get the (x,y) with periodic boundaries r = j + 1 == L ? 0 : j + 1; l = j - 1 == -1 ? L - 1 : j - 1; u = i + 1 == L ? 0 : i + 1; d = i - 1 == -1 ? L - 1 : i - 1; //(x,y) to index notation and store in table matrix[i + j * L].resize(4); matrix[i + j * L][LEFT] = i+l*L; matrix[i + j * L][DOWN] = d+j*L; matrix[i + j * L][RIGHT] = i+r*L; matrix[i + j * L][UP] = u+j*L; } } k = 0; while (k < percent) { uniform_int_distribution<int> distr(0, SIZE-1); int ind1 = distr(gen); int ind2 = distr(gen); while (ind1 == ind2) { ind2 = distr(gen); } vector<int>::iterator it = find(matrix[ind2].begin(), matrix[ind2].end() , ind1); auto a = it - matrix[ind2].begin(); if(a == matrix[ind2].size()) { auto len = matrix[ind2].size(); matrix[ind2].resize(len+1); matrix[ind2][len] = ind1; auto lenn = matrix[ind1].size(); matrix[ind1].resize(lenn+1); matrix[ind1][lenn] = ind2; k++; } } // for(int f=0; f<L; f++) // { // for( int g=0; g<L; g++) // { // cout << matrix[f+g*L].size() <<" "; // } // cout<<endl; // } return matrix; } int uf_find(int x) { int y = x; while (labels[y] != y) y = labels[y]; while (labels[x] != x) { int z = labels[x]; labels[x] = y; x = z; } return y; } int uf_union(int x, int y) { return labels[uf_find(x)] = uf_find(y); } int uf_make_set(void) { labels[0] ++; assert(labels[0] < n_labels); labels[labels[0]] = labels[0]; return labels[0]; } void uf_initialize(int max_labels) { n_labels = max_labels; labels = static_cast<int*>(calloc(sizeof(int), n_labels)); labels[0] = 0; } void uf_done(void) { n_labels = 0; free(labels); labels = 0; } int HK( int matrix[SIZE]) { //double start = clock(); uf_initialize(L * L / 2); for (int i=0; i<L; i++) { for (int j=0; j<L; j++) { if (matrix[i + L * j]) { // if occupied ... int up = (i==0 ? 0 : matrix[(i - 1) + L * j]); // look up int left = (j==0 ? 0 : matrix[i + L * (j - 1)]); // look left switch (!!up + !!left) { case 0: matrix[i + L * j] = uf_make_set(); // a new cluster break; case 1: // part of an existing cluster matrix[i + L * j] = max(up,left); // whichever is nonzero is labelled break; case 2: // this site binds two clusters matrix[i + L * j] = uf_union(up, left); break; } } } } /* apply the relabeling to the matrix */ /* This is a little bit sneaky.. we create a mapping from the canonical labels determined by union/find into a new set of canonical labels, which are guaranteed to be sequential. */ int *new_labels = static_cast<int*>(calloc(sizeof(int), n_labels)); // allocate array, initialized to zero for (int i=0; i<L; i++) { for (int j=0; j<L; j++) { if (matrix[i+L*j]) { int x = uf_find(matrix[i + j * L]); if (new_labels[x] == 0) { new_labels[0]++; new_labels[x] = new_labels[0]; } matrix[i + j * L] = new_labels[x]; } } } int total_clusters = new_labels[0]; free(new_labels); uf_done(); // for(int f=0; f<L; f++) // { // for(int g=0; g<L; g++) // { // cout<<matrix[f+g*L]<<" "; // } // cout<<endl; // } // // cout<<endl; // double start = clock(); int *clusters = (int*)calloc(total_clusters+1, sizeof(int)); // clusters = static_cast<int*>(calloc(sizeof(int), total_clusters)); int bigclus = 0; for (int i = 0; i < SIZE; i++) { // if(matrix[i] == 0) continue; clusters[matrix[i] ]++; } for (int i = 1; i < total_clusters+1; i++) { bigclus = clusters[i] > bigclus ? clusters[i] : bigclus; } free(clusters); // double endd = clock(); // double duration_sec = double(endd-start)/CLOCKS_PER_SEC; // cout<< duration_sec; // cout<<"big cluster: "<<bigclus<<endl; return bigclus; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}