/* FILE: randGraph.cpp last change: 30-Sep-2013 author: Romeo Rizzi * randomly generates a weighted simple and connected graph. * Usage syntax: * > randGraph n m max_w seed */ #include #include #include #include using namespace std; const int MAX_N = 10000; int n; const int MAX_M = 1000000; int m; const int MAX_W = 1000000; int max_w; int RandNumber(int min, int max) { /* returns an integer in [min, max] * see Stroustrup "The c++ Programming Language" 3th edition pg. 685 * for comments on the following manipulation choice. * In particular, considerations on the bad quality of low bits come into account. */ return min + (int) ( (max-min +1) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } int main(int argc, char** argv) { n = atoi(argv[1]); m = atoi(argv[2]); max_w = atoi(argv[3]); assert(n <= MAX_N); assert(m <= MAX_M); assert( max_w <= MAX_W ); assert(n >= 1); assert(m <= MAX_M); assert( max_w <= MAX_W ); assert( m >= n-1); assert( m <= (n*(n-1))/2 ); srand(time(NULL)); if(argc > 4) srand( atoi(argv[4]) ); cout << n << " " << m << endl; int edges = 0; int jump = 1; int i = 1; while( edges < m ) if( i + jump <= n) { cout << i << " " << i+jump << " " << RandNumber(1, max_w) << endl; edges++; i++; } else { i = 1; jump++; } return 0; }