算法练习(106): Erdős–Rényi model (1.5.21-1.5.26)

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。

算法(第4版)

知识点

  • Erdős–Rényi model

关于图论
In graph theory, the Erdős–Rényi model is either of two closely related models for generating random graphs. They are named after Paul Erdős and Alfréd Rényi, who first introduced one of the models in 1959;[1][2] the other model was introduced independently and contemporaneously by Edgar Gilbert.[3] In the model introduced by Erdős and Rényi, all graphs on a fixed vertex set with a fixed number of edges are equally likely; in the model introduced by Gilbert, each edge has a fixed probability of being present or absent, independently of the other edges. These models can be used in the probabilistic method to prove the existence of graphs satisfying various properties, or to provide a rigorous definition of what it means for a property to hold for almost all graphs.

题目

1.5.21 Erdös-Renyi 模型。使用练习 1.5.17 的用例验证这个猜想:得到单个连通分量所需生成的整数对数量为 ~1/2NlnN。


1.5.21 Erdös-Renyi model. Use your client from Exercise 1.5.17 to test the hypothesis that the number of pairs generated to get one component is ~ 1⁄2N ln N.

题目

1.5.22 Erdös-Renyi 的倍率实验。开发一个性能测试用例,从命令行接受一个 int 值 T 并进行 T 次以下实验:使用练习 1.5.17 的用例生成随机连接,和我们的开发用例一样使用 UnionFind 来检查触点的连通性,不断循环直到所有触点都相互连通。对于每个 N,打印出 N 值和平均所需的连接数以及前后两次运行时间的比值。使用你的程序验证正文中的猜想:quick-find 算法和 quick-union 算法的运行时间是平方级别的,加权 quick-union 算法则接近线性级别。


1.5.22 Doubling test for Erdös-Renyi model. Develop a performance-testing client that takes an int value T from the command line and performs T trials of the following ex- periment: Use your client from Exercise 1.5.17 to generate random connections, using UnionFind to determine connectivity as in our development client, looping until all sites are connected. For each N, print the value of N, the average number of connections processed, and the ratio of the running time to the previous. Use your program to vali- date the hypotheses in the text that the running times for quick-find and quick-union are quadratic and weighted quick-union is near-linear.

题目

*1.5.23 在 Erdös-Renyi 模型下比较 quick-find 算法和 quick-union 算法。开发一个性能测试用例,从命令行接受一个 int 值 T 并进行 T 次以下实验:使用练习 1.5.17 的用例生成随机连接。保存这些连接并和我们的开发用例一样分别用 quick-find 和 quick-union 算法检查触点的连通性,不断循环直到所有触点均相互连通。对于每个 N,打印出 N 值和两种算法的运行时间比值。


1.5.23 Compare quick-find with quick-union for Erdös-Renyi model. Develop a performance-testing client that takes an int value T from the command line and performs T trials of the following experiment: Use your client from Exercise 1.5.17 to generate random connections. Save the connections, so that you can use both quick-union and quick-find to determine connectivity as in our development client, looping until all sites are connected. For each N, print the value of N and the ratio of the two running times.

题目

1.5.24 适用于 Erdös-Renyi 模型的快速算法。在练习1.5.23 的测试中增加加权 quick-union 算法和使用路径压缩的加权 quick-union 算法。你能分辨出这两种算法的区别吗?


1.5.24 Fast algorithms for Erdös-Renyi model. Add weighted quick-union and weight- ed quick-union with path compression to your tests from Exercise 1.5.23 . Can you discern a difference between these two algorithms?

题目

1.5.25 随机网格的倍率测试。开发一个性能测试用例,从命令行接受一个 int 值 T 并进行 T 次以下实验:使用练习 1.5.18 的用例生成一个 N×N 的随机网格,所有连接的方向随机且排列随机。和我们的开发用例一样使用 UnionFind 来检查触点的连通性,不断循环直到所有触点均相互连通。对于每个 N,打印出 N 值和平均所需的连接数以及前后两次运行时间的比值。使用你的程序验证正文中的猜想:quick-find 和 quick-union 算法的运行时间是平方级别的,加权 quick-union 算法则接近线性级别。注意:随着 N 值加倍,网格中触点的数量会乘以 4,因此平方级别的算法运行时间会变为原来的 16 倍,线性级别的算法的运行时间则变为原来的 4 倍。
1.5.25 Doubling test for random grids. Develop a performance-testing client that takes an int value T from the command line and performs T trials of the following experiement: Use your client from Exercise 1.5.18 to generate the connections in an N-by-N square grid, randomly oriented and in random order, then use UnionFind to determine connectivity as in our development client, looping until all sites are connected. For each N, print the value of N, the average number of connections processed, and the ratio of the running time to the previous. Use your program to validate the hypotheses in the text that the running times for quick-find and quick-union are quadratic and weighted quick-union is near-linear. Note : As N doubles, the number of sites in the grid increases by a factor of 4, so expect a doubling factor of 16 for quadratic and 4 for linear.

分析

答案

你可能感兴趣的:(算法练习(106): Erdős–Rényi model (1.5.21-1.5.26))