File tree Expand file tree Collapse file tree 7 files changed +24
-41
lines changed Expand file tree Collapse file tree 7 files changed +24
-41
lines changed Original file line number Diff line number Diff line change @@ -20,20 +20,20 @@ static void Main(string[] args)
20
20
21
21
// 证明:
22
22
// 一次 Union 操作只可能发生如下两种情况。
23
- // 1. 两棵树的高度相同,这样合并后的新树的高度等于较大那棵树的高度 +1。
24
- // 2. 两棵树的高度不同,这样合并后的新树高度等于较大那棵树的高度。
25
- //
26
- // 现在证明通过加权 quick-union 算法构造的高度为 h 的树至少包含 2^ h 个结点。
27
- // 基础情况,高度 h= 0, 结点数 k= 1。
28
- // 为了使高度增加,必须用一棵高度相同的树合并,而 h= 0 时结点数一定是 1,则:
29
- // h= 1, k= 2
23
+ // 1.两棵树的高度相同,这样合并后的新树的高度等于较大那棵树的高度 + 1。
24
+ // 2.两棵树的高度不同,这样合并后的新树高度等于较大那棵树的高度。
25
+
26
+ // 现在证明通过加权 quick-union 算法构造的高度为 h 的树至少包含 2 ^ h 个结点。
27
+ // 基础情况,高度 h = 0, 结点数 k = 1。
28
+ // 为了使高度增加,必须用一棵高度相同的树合并,而 h = 0 时结点数一定是 1,则:
29
+ // h = 1, k = 2
30
30
// 由于两棵大小不同的树合并,最大高度不会增加,只会增加结点数。
31
31
// 因此,每次都使用相同高度的最小树进行合并,有:
32
- // h= 2, k= 4
33
- // h= 3, k= 8
34
- // h= 4, k= 16
32
+ // h = 2, k = 4
33
+ // h = 3, k = 8
34
+ // h = 4, k = 16
35
35
// ......
36
- // 递推即可得到结论,k >= 2^ h
36
+ // 递推即可得到结论,k >= 2 ^ h
37
37
// 因此 h <= lgk
38
38
}
39
39
}
Original file line number Diff line number Diff line change 5
5
namespace _1 . _5 . _18
6
6
{
7
7
/// <summary>
8
- /// 随即背包 。
8
+ /// 随机背包 。
9
9
/// </summary>
10
10
/// <typeparam name="Item">背包中要存放的元素。</typeparam>
11
11
public class RandomBag < Item > : IEnumerable < Item >
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class Program
19
19
{
20
20
static void Main ( string [ ] args )
21
21
{
22
- int n = 1000 ;
22
+ int n = 2000 ;
23
23
for ( int t = 0 ; t < 5 ; ++ t )
24
24
{
25
25
Connection [ ] input = ErdosRenyi . Generate ( n ) ;
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ class Program
16
16
{
17
17
static void Main ( string [ ] args )
18
18
{
19
- int n = 5000 ;
19
+ int n = 10000 ;
20
20
for ( int t = 0 ; t < 5 ; ++ t )
21
21
{
22
22
var input = ErdosRenyi . Generate ( n ) ;
Original file line number Diff line number Diff line change @@ -28,8 +28,8 @@ class Program
28
28
{
29
29
static void Main ( string [ ] args )
30
30
{
31
- int n = 20 ;
32
- int t = 5 ;
31
+ int n = 40 ;
32
+ int t = 4 ;
33
33
34
34
// quick-find
35
35
Console . WriteLine ( "Quick-Find" ) ;
@@ -56,7 +56,7 @@ static void Main(string[] args)
56
56
57
57
// quick-union
58
58
Console . WriteLine ( "Quick-Union" ) ;
59
- n = 20 ;
59
+ n = 40 ;
60
60
for ( int i = 0 ; i < t ; ++ i , n *= 2 )
61
61
{
62
62
Console . WriteLine ( "N:" + n * n ) ;
@@ -77,8 +77,8 @@ static void Main(string[] args)
77
77
}
78
78
79
79
// 加权 quick-union
80
- Console . WriteLine ( "Quick-Union" ) ;
81
- n = 20 ;
80
+ Console . WriteLine ( "Weighted Quick-Union" ) ;
81
+ n = 40 ;
82
82
for ( int i = 0 ; i < t ; ++ i , n *= 2 )
83
83
{
84
84
Console . WriteLine ( "N:" + n * n ) ;
@@ -108,7 +108,7 @@ static void Main(string[] args)
108
108
static long RunTest ( UF uf , Connection [ ] connections )
109
109
{
110
110
Stopwatch timer = new Stopwatch ( ) ;
111
- long repeatTime = 5 ;
111
+ long repeatTime = 3 ;
112
112
timer . Start ( ) ;
113
113
for ( int i = 0 ; i < repeatTime ; ++ i )
114
114
{
Original file line number Diff line number Diff line change @@ -27,30 +27,10 @@ static void Main(string[] args)
27
27
int [ ] parent = weightedQuickUnion . GetParent ( ) ;
28
28
for ( int i = 0 ; i < parent . Length ; ++ i )
29
29
{
30
- if ( parent [ i ] == i )
31
- {
32
- Console . WriteLine ( "|---- " + i ) ;
33
- DFS ( parent , i , 1 ) ;
34
- }
30
+ Console . Write ( parent [ i ] + " " ) ;
35
31
}
36
32
Console . WriteLine ( "数组访问:" + weightedQuickUnion . ArrayParentVisitCount ) ;
37
33
}
38
34
}
39
-
40
- static void DFS ( int [ ] parent , int root , int level )
41
- {
42
- for ( int i = 0 ; i < parent . Length ; ++ i )
43
- {
44
- if ( parent [ i ] == root && i != root )
45
- {
46
- for ( int j = 0 ; j < level ; ++ j )
47
- {
48
- Console . Write ( " " ) ;
49
- }
50
- Console . WriteLine ( "|---- " + i ) ;
51
- DFS ( parent , i , level + 1 ) ;
52
- }
53
- }
54
- }
55
35
}
56
36
}
Original file line number Diff line number Diff line change 3
3
4
4
namespace UnionFind
5
5
{
6
+ /// <summary>
7
+ /// 提供一系列对并查集进行随机测试的静态方法。
8
+ /// </summary>
6
9
public class ErdosRenyi
7
10
{
8
11
/// <summary>
You can’t perform that action at this time.
0 commit comments