[英]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: 令
N
为正整数, p
为0到1之间的数字。 (N, p)
随机图是通过以下过程生成的图:
Draw N
vertices, denoted by 1, 2, . . . , N
绘制
N
个顶点,分别用1, 2, . . . , N
1, 2, . . . , N
1, 2, . . . , N
respectively; 1, 2, . . . , N
分别为1, 2, . . . , N
; for every pair (u, v)
of different vertices, with probability p
, connect the two vertices with an edge. 对于每对
(u, v)
个不同的顶点,概率为p
,用一条边连接两个顶点。 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}
. 我们将
N
固定为500,000
但让p在{0.05, 0.10, 0.15, ..., 0.95}
。 For each value of p, you need to create 100 (N, p)
random graphs. 对于每个p值,您需要创建100个
(N, p)
随机图。 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
. 然后,对于p的每个值,您需要计算连接的随机图的数量
M
,并研究M
(反映了连接随机图的概率)与p
。
A random graph is a graph that is randomly created: 随机图是随机创建的图:
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. 参数
p
只是两个给定顶点连接的概率。 bernoulli_distributed_with_param_p
is actually pretty simple to implement in C: bernoulli_distributed_with_param_p
实际上非常容易在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
. 不要忘记,您需要使用
srand
初始化随机数生成器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.