CosminVarlan

Generator graf orientat echilibrat ca si Componente Tare Conexe

Apr 28th, 2026
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <time.h>
  5.  
  6. #define OutputToFile 1
  7.  
  8. #define NrCTC 1000
  9. #define nodes 100000
  10.  
  11. using namespace std;
  12.  
  13. ofstream fout("data.out");
  14. ofstream dout("debug.out");
  15.  
  16. vector<int> ctc[NrCTC+1];
  17. int a[nodes+1];
  18.  
  19. struct edge{
  20.     int x, y;
  21. };
  22.  
  23. vector<edge> edges;
  24. vector<edge> edges_between_ctc;
  25.  
  26.  
  27. int main()
  28. {
  29.     srand(time(0));
  30.     for(int i=1; i<=nodes; i++)
  31.     {
  32.         int ctc_assign = rand() % NrCTC + 1;
  33.         ctc[ctc_assign].push_back(i); /// nu exista ctc[0]
  34.         a[i] = ctc_assign;
  35.     }
  36.  
  37.  
  38.     for(int i=1; i<=NrCTC; i++)
  39.     {
  40.         dout << "CTC "<< i << ": ";
  41.         for(auto nod:ctc[i])
  42.             dout << nod << ' ';
  43.         dout << endl;
  44.     }
  45.  
  46.     for(int i=1; i<=NrCTC; i++)
  47.     {
  48.         if (ctc[i].size()==0) continue;
  49.         edge e;
  50.         /// adding edges to make CTC (in a cricle):
  51.         for(int j=0; j<ctc[i].size()-1; j++)
  52.         {
  53.             e.x = ctc[i][j];
  54.             e.y = ctc[i][j+1];
  55.             edges.push_back(e);
  56.         }
  57.         e.x = ctc[i][ctc[i].size()-1]; // closing circle
  58.         e.y = ctc[i][0];
  59.         edges.push_back(e);
  60.  
  61.         /// probabilistically adding random edges in ith CTC:
  62.         while(rand() % 100 < 97)
  63.         {
  64.             //int nrr1 = rand()%(ctc[i].size()-1;
  65.             //int nrr2 = rand()%(ctc[i].size()-1;
  66.  
  67.             e.x = ctc[i][rand()%ctc[i].size()];
  68.             e.y = ctc[i][rand()%ctc[i].size()];
  69.             if (e.x==e.y) continue;
  70.             int alreadyAdded = 0;
  71.             for(auto ee:edges)
  72.                 if (ee.x==e.x && ee.y==e.y) alreadyAdded = 1;
  73.             if (!alreadyAdded) edges.push_back(e);
  74.         }
  75.  
  76.     }
  77.  
  78.     /// adding random edges and counting remembering how they connect the CTCs
  79.     /// not connecting CTC both ways:
  80.  
  81.     while(rand() % 100 < 98)
  82.     {
  83.         edge e;
  84.         e.x = rand()% nodes + 1;
  85.         e.y = rand()% nodes + 1;
  86.         if (e.x==e.y) continue; /// the same node
  87.         if (a[e.x]== a[e.y]) continue; /// they are already in same CTC
  88.         int ctc1 = a[e.x];
  89.         int ctc2 = a[e.y];
  90.         if (ctc1<ctc2)
  91.         {
  92.             swap(e.x, e.y);
  93.         }
  94.  
  95.         int alreadyAdded = 0;
  96.         for(auto ee:edges)
  97.         {
  98.             /// non of the edges between CTCs exists:
  99.             if (a[ee.x]==a[e.x] && a[ee.y]==a[e.y])
  100.                 alreadyAdded = 1;
  101.         }
  102.  
  103.         if (!alreadyAdded) edges.push_back(e);
  104.     }
  105.  
  106.     if (OutputToFile) fout << nodes << ' ' << edges.size() << endl << endl;
  107.                  else cout << nodes << ' ' << edges.size() << endl << endl;
  108.  
  109.     for(auto e:edges){
  110.         if (OutputToFile) fout << e.x << ' ' << e.y << '\n';
  111.         else cout << e.x << ' ' << e.y << '\n';
  112.     }
  113.  
  114.  
  115.  
  116.     fout<< '\n' << nodes << '\n';
  117.     for(int i=1; i<=nodes; i++) fout << i << ' ';
  118.  
  119.  
  120.     if (OutputToFile) fout.close();
  121.  
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment