简体   繁体   中英

Random Graphs in C

I have a homework about random graphs. I can't understand the question. Can anyone please clarify to me what I am supposed to do?

Let N be a positive integer and p be a number between 0 and 1. An (N, p) random graph is a graph generated by the following procedure:

Draw N vertices, denoted by 1, 2, . . . , N 1, 2, . . . , N 1, 2, . . . , N respectively; for every pair (u, v) of different vertices, with probability p , connect the two vertices with an edge. A graph is said to be connected if there is a path between any two vertices.

In this lab, you will write code to generate large random graphs and investigate the connectedness of such graphs.

We will fix N to be 500,000 but let p vary in {0.05, 0.10, 0.15, ..., 0.95} . For each value of p, you need to create 100 (N, p) random graphs. You need to develop a method (and of course implement it in your program) to determine if a graph is connected. Then for each value of p, you need to count the number M of random graphs that are connected, and investigate the relationship between M (which reflects the probability that a random graph is connected) and p .

A random graph is a graph that is randomly created:

Pseudo-code

for i := 1 to N
   for j := i+1 to N
       if bernoulli_distributed_with_param_p is true
           add undirected edge {i,j} to graph

The parameter p is simply the probability that two given vertices are connected. bernoulli_distributed_with_param_p is actually pretty simple to implement in C:

// Returns 0 with probability (1-p)
int bernoulli_distributed(double p){
    return (p > ((double)rand())/RAND_MAX);
}

Don't forget that you need to initialize the random generator with srand .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM