graphgen.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * graphgen.cpp
  3. * by Sam Kauffman - Univeristy of Virginia
  4. *
  5. * This program generates graphs of the format described in GraphFormat.txt
  6. * and SampleGraph.jpg for use with BFS (breadth-first search) in Rodinia.
  7. *
  8. * The graph is not guaranteed to be connected, are there may be multiple edges
  9. * and loops.
  10. *
  11. * Usage:
  12. * graphgen <num> [<filename_bit>]
  13. * num = number of nodes
  14. * Output filename is "graph<filename_bit>.txt". filename_bit defaults to num.
  15. *
  16. * This program uses the TR1 header <random>.
  17. *
  18. */
  19. #include <iostream>
  20. #include <fstream>
  21. #include <string>
  22. #include <vector>
  23. #include <random>
  24. #include <cstdlib>
  25. #include <ctime>
  26. #include <climits>
  27. // These names may vary by implementation
  28. //#define LINEAR_CONGRUENTIAL_ENGINE linear_congruential_engine
  29. #define LINEAR_CONGRUENTIAL_ENGINE linear_congruential
  30. //#define UNIFORM_INT_DISTRIBUTION uniform_int_distribution
  31. #define UNIFORM_INT_DISTRIBUTION uniform_int
  32. using namespace std;
  33. #define MIN_NODES 20
  34. #define MAX_NODES ULONG_MAX
  35. #define MIN_EDGES 2
  36. #define MAX_INIT_EDGES 4 // Nodes will have, on average, 2*MAX_INIT_EDGES edges
  37. #define MIN_WEIGHT 1
  38. #define MAX_WEIGHT 10
  39. typedef unsigned int uint;
  40. typedef unsigned long ulong;
  41. struct edge; // forward declaration
  42. typedef vector<edge> node;
  43. struct edge {
  44. ulong dest;
  45. uint weight;
  46. };
  47. int main( int argc, char ** argv )
  48. {
  49. // Parse command lined
  50. ulong numNodes;
  51. string s;
  52. if ( argc < 2 )
  53. {
  54. cerr << "Error: enter a number of nodes.\n";
  55. exit( 1 );
  56. }
  57. numNodes = strtoul( argv[1], NULL, 10 );
  58. if ( numNodes < MIN_NODES || numNodes > MAX_NODES || argv[1][0] == '-' )
  59. {
  60. cerr << "Error: Invalid argument: " << argv[1] << "\n";
  61. exit( 1 );
  62. }
  63. s = argc > 2 ? argv[2] : argv[1]; // filename bit
  64. string filename = "graph" + s + ".txt";
  65. cout << "Generating graph with " << numNodes << " nodes...\n";
  66. node * graph;
  67. graph = new node[numNodes];
  68. // Initialize random number generators
  69. // C RNG for numbers of edges and weights
  70. srand( time( NULL ) );
  71. // TR1 RNG for choosing edge destinations
  72. LINEAR_CONGRUENTIAL_ENGINE<ulong, 48271, 0, ULONG_MAX> gen( time( NULL ) );
  73. UNIFORM_INT_DISTRIBUTION<ulong> randNode( 0, numNodes - 1 );
  74. // Generate graph
  75. uint numEdges;
  76. ulong nodeID;
  77. uint weight;
  78. ulong i;
  79. uint j;
  80. for ( i = 0; i < numNodes; i++ )
  81. {
  82. numEdges = rand() % ( MAX_INIT_EDGES - MIN_EDGES + 1 ) + MIN_EDGES;
  83. for ( j = 0; j < numEdges; j++ )
  84. {
  85. nodeID = randNode( gen );
  86. weight = rand() % ( MAX_WEIGHT - MIN_WEIGHT + 1 ) + MIN_WEIGHT;
  87. graph[i].push_back( edge() );
  88. graph[i].back().dest = nodeID;
  89. graph[i].back().weight = weight;
  90. graph[nodeID].push_back( edge() );
  91. graph[nodeID].back().dest = i;
  92. graph[nodeID].back().weight = weight;
  93. }
  94. }
  95. // Output
  96. cout << "Writing to file \"" << filename << "\"...\n";
  97. ofstream outf( filename );
  98. outf << numNodes << "\n";
  99. ulong totalEdges = 0;
  100. for ( uint i = 0; i < numNodes; i++ )
  101. {
  102. numEdges = graph[i].size();
  103. outf << totalEdges << " " << numEdges << "\n";
  104. totalEdges += numEdges;
  105. }
  106. outf << "\n" << randNode( gen ) << "\n\n";
  107. outf << totalEdges << "\n";
  108. for ( ulong i = 0; i < numNodes; i++ )
  109. for ( uint j = 0; j < graph[i].size(); j++ )
  110. outf << graph[i][j].dest << " " << graph[i][j].weight << "\n";
  111. outf.close();
  112. delete[] graph;
  113. }