簡體   English   中英

生成隨機頂點

[英]Generating Random Vertices

當用戶指定所需的頂點數時,如何生成范圍0 - 100隨機數。 我實現了下面的邊,但是頂點需要類似的東西。 它如何適應下面的現有結構?

int main(int argc, char *argv[]) {
// input number of vertices and edges
  int V, E;
  cin >> V >> E;

// check for maximum number of possible edges (disallow anti-parallel arcs)
  if (E > V*(V-1)/2) {
      E = V*(V-1)/2;  
  }

// construct random weighted graph
vector<vector<int> > G(V, vector<int>(V, 0));
for (int i = 0; i < E; i++)
   while (true) {
      const int u = rand() % V,
                v = rand() % V;

   // no self loops
   if (u != v)
       // disallow anti-parallel edges and overwriting existing arcs
       if (0 == G[v][u] && 0 == G[u][v]) {
           G[u][v] = rand() % 100;
           break;
       }
    }


 // union-find sets are implemented inside the arrays
 vector<int> parent(V, 0), depth(V, 0);

 // at the start, all nodes point to themselves
 for (int i = 0; i < V; i++) {
    parent[i] = i;
    depth[i] = 0;
 }

 // load all edges into priority queue, smallest weight on top of max-heap
 priority_queue<PairPQ<int, int> > PQ;
 for (int u = 0; u < V; u++)
    for (int v = 0; v < V; v++)
       if (G[u][v])
          PQ.push(PairPQ<int, int>(u, v, -G[u][v]));

 // keep track of MST edges
 set<int> MSTedges;

 // loop over all edges in ascending order of weight
 while (!PQ.empty()) {
    // no need for lazy decrease key idiom here!
    const int u = PQ.top().item().first,
              v = PQ.top().item().second;
   // (don't care about rank/weight, note too that direction does not matter)
   PQ.pop();

   // check for edge between disjoint sets
   if (find_set(u, parent) != find_set(v, parent)) {
      MSTedges.insert(u * V + v);
      union_set(u, v, parent, depth);
   }
  }

需要隨機頂點的地方:

  // printing MST graph in GraphViz format
  *cout << endl 
       << "digraph G {" << endl
       << "node [shape=circle]" << endl;
 for (int i = 0; i < V; i++)
    for (int j = i+1;j < V; j++) {
       const int w = max(G[i][j], G[j][i]);
       if (w) {
            cout << "  " << j <<" -> " << i 
                 << " [label=\"" << w << "\"";
       if (MSTedges.count(i * V + j) || MSTedges.count(j * V + i))
           cout << ",color=red";
           cout << "];" << endl;
      }

    }
   cout << "}" << endl;
   exit(0);*
 }

要生成的隨機數是節點 要生成的隨機數是節點。

這段代碼應該大致遵循您需要的格式,我同時提供了使用隨機標頭C ++ 11解決方案和帶有實時示例的 uniform_int_distribution ,如果可能的話,應優先使用:

#include <iostream>
#include <random>
#include <cstdlib>

int main()
{
    std::random_device rd;

    std::mt19937 e2(rd());

    int V = 10 ;

    std::uniform_int_distribution<int> dist1(0, V);


    for (int i = 0; i < V; ++i)
    {
        for (int k = i+1; k < V; ++k)
        {
            int index1 = dist1(e2) ;
            int index2 = dist1(e2) ;
            std::cout << "( " <<  index1 << " , " << index2 << ") " ;
        }
        std::cout << std::endl ;
    }
    std::cout << std::endl ;
}

我還提供了使用rand代碼,該代碼的公式基於如何獲得一定范圍內的隨機整數? C FAQ 實時示例中的條目:

#include <iostream>
#include <cstdlib>

int main()
{
    int V = 10 ;

    for (int i = 0; i < V; ++i)
    {
        for (int k = i+1; k < V; ++k)
        {
            int index1 = rand() / (RAND_MAX / V  + 1) + 1 ;
            int index2 = rand() / (RAND_MAX / V + 1) + 1;
            std::cout << "( " <<  index1 << " , " << index2 << ") " ;
        }
        std::cout << std::endl ;
    }
    std::cout << std::endl ;
}

好吧,這只是使用std::uniform_real_distribution (假設您要在0-100范圍內進行均勻分布):

int n; // ... populate from input
std::vector<int> numbers(n);

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<int> dis(0, 100);

for (int i = 0; i < n; ++i)
    numbers.push_back(dis(gen));

for (int i : numbers)
    // output i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM