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)
2020
2121 // 证明:
2222 // 一次 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
3030 // 由于两棵大小不同的树合并,最大高度不会增加,只会增加结点数。
3131 // 因此,每次都使用相同高度的最小树进行合并,有:
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
3535 // ......
36- // 递推即可得到结论,k >= 2^ h
36+ // 递推即可得到结论,k >= 2 ^ h
3737 // 因此 h <= lgk
3838 }
3939 }
Original file line number Diff line number Diff line change 55namespace _1 . _5 . _18
66{
77 /// <summary>
8- /// 随即背包 。
8+ /// 随机背包 。
99 /// </summary>
1010 /// <typeparam name="Item">背包中要存放的元素。</typeparam>
1111 public class RandomBag < Item > : IEnumerable < Item >
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class Program
1919 {
2020 static void Main ( string [ ] args )
2121 {
22- int n = 1000 ;
22+ int n = 2000 ;
2323 for ( int t = 0 ; t < 5 ; ++ t )
2424 {
2525 Connection [ ] input = ErdosRenyi . Generate ( n ) ;
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ class Program
1616 {
1717 static void Main ( string [ ] args )
1818 {
19- int n = 5000 ;
19+ int n = 10000 ;
2020 for ( int t = 0 ; t < 5 ; ++ t )
2121 {
2222 var input = ErdosRenyi . Generate ( n ) ;
Original file line number Diff line number Diff line change @@ -28,8 +28,8 @@ class Program
2828 {
2929 static void Main ( string [ ] args )
3030 {
31- int n = 20 ;
32- int t = 5 ;
31+ int n = 40 ;
32+ int t = 4 ;
3333
3434 // quick-find
3535 Console . WriteLine ( "Quick-Find" ) ;
@@ -56,7 +56,7 @@ static void Main(string[] args)
5656
5757 // quick-union
5858 Console . WriteLine ( "Quick-Union" ) ;
59- n = 20 ;
59+ n = 40 ;
6060 for ( int i = 0 ; i < t ; ++ i , n *= 2 )
6161 {
6262 Console . WriteLine ( "N:" + n * n ) ;
@@ -77,8 +77,8 @@ static void Main(string[] args)
7777 }
7878
7979 // 加权 quick-union
80- Console . WriteLine ( "Quick-Union" ) ;
81- n = 20 ;
80+ Console . WriteLine ( "Weighted Quick-Union" ) ;
81+ n = 40 ;
8282 for ( int i = 0 ; i < t ; ++ i , n *= 2 )
8383 {
8484 Console . WriteLine ( "N:" + n * n ) ;
@@ -108,7 +108,7 @@ static void Main(string[] args)
108108 static long RunTest ( UF uf , Connection [ ] connections )
109109 {
110110 Stopwatch timer = new Stopwatch ( ) ;
111- long repeatTime = 5 ;
111+ long repeatTime = 3 ;
112112 timer . Start ( ) ;
113113 for ( int i = 0 ; i < repeatTime ; ++ i )
114114 {
Original file line number Diff line number Diff line change @@ -27,30 +27,10 @@ static void Main(string[] args)
2727 int [ ] parent = weightedQuickUnion . GetParent ( ) ;
2828 for ( int i = 0 ; i < parent . Length ; ++ i )
2929 {
30- if ( parent [ i ] == i )
31- {
32- Console . WriteLine ( "|---- " + i ) ;
33- DFS ( parent , i , 1 ) ;
34- }
30+ Console . Write ( parent [ i ] + " " ) ;
3531 }
3632 Console . WriteLine ( "数组访问:" + weightedQuickUnion . ArrayParentVisitCount ) ;
3733 }
3834 }
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- }
5535 }
5636}
Original file line number Diff line number Diff line change 33
44namespace UnionFind
55{
6+ /// <summary>
7+ /// 提供一系列对并查集进行随机测试的静态方法。
8+ /// </summary>
69 public class ErdosRenyi
710 {
811 /// <summary>
You can’t perform that action at this time.
0 commit comments