Skip to content

Commit 92ac953

Browse files
committed
bug fixed in UF 6 test.
1 parent 108a12e commit 92ac953

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

06-Union-Find/Course Code (C++)/Optional-01-Same-Cases-Test-for-UF/UnionFind5.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
using namespace std;
1111

12-
// 我们的第五版Union-Find
12+
// 我们的第五版Union-Find, 其中路径压缩使用迭代的方式
1313
namespace UF5{
1414

1515
class UnionFind{
@@ -52,11 +52,6 @@ namespace UF5{
5252
p = parent[p];
5353
}
5454
return p;
55-
56-
// path compression 2, 递归算法
57-
// if( p != parent[p] )
58-
// parent[p] = find( parent[p] );
59-
// return parent[p];
6055
}
6156

6257
// 查看元素p和元素q是否所属一个集合

06-Union-Find/Course Code (C++)/Optional-01-Same-Cases-Test-for-UF/UnionFind6.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using namespace std;
1111

1212
// 我们的第六版Union-Find, 路径压缩使用递归的方式
13-
namespace UF5{
13+
namespace UF6{
1414

1515
class UnionFind{
1616

06-Union-Find/Course Code (C++)/Optional-01-Same-Cases-Test-for-UF/UnionFindTestHelper.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "UnionFind3.h"
1313
#include "UnionFind4.h"
1414
#include "UnionFind5.h"
15+
#include "UnionFind6.h"
1516

1617
using namespace std;
1718

@@ -138,6 +139,30 @@ namespace UnionFindTestHelper{
138139
cout<<"UF5, "<<2*n<<" ops, "<<double(endTime-startTime)/CLOCKS_PER_SEC<<" s"<<endl;
139140
}
140141

142+
// 测试第六版本的并查集, 测试元素个数为n
143+
void testUF6( int n ){
144+
145+
srand( time(NULL) );
146+
UF6::UnionFind uf = UF6::UnionFind(n);
147+
148+
time_t startTime = clock();
149+
150+
for( int i = 0 ; i < n ; i ++ ){
151+
int a = rand()%n;
152+
int b = rand()%n;
153+
uf.unionElements(a,b);
154+
}
155+
for(int i = 0 ; i < n ; i ++ ){
156+
int a = rand()%n;
157+
int b = rand()%n;
158+
uf.isConnected(a,b);
159+
}
160+
time_t endTime = clock();
161+
162+
cout<<"UF6, "<<2*n<<" ops, "<<double(endTime-startTime)/CLOCKS_PER_SEC<<" s"<<endl;
163+
}
164+
165+
141166
// 使用相同的测试数据测试UF的执行效率
142167
template<typename UF>
143168
void testUF(const string &ufName, UF &uf, const vector<pair<int,int>> &unionTest, const vector<pair<int,int>> &connectTest){

06-Union-Find/Course Code (C++)/Optional-01-Same-Cases-Test-for-UF/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using namespace std;
66

7-
// 对比UF1, UF2, UF3, UF4和UF5的时间性能
7+
// 对比UF1, UF2, UF3, UF4, UF5和UF6的时间性能
88
// 在这里, 我们对于不同的UnionFind的实现, 使用相同的测试用例, 让测试结果更加准确
99
int main() {
1010

@@ -29,7 +29,7 @@ int main() {
2929
connectTest.push_back(make_pair(a,b));
3030
}
3131

32-
// 测试我们的UF1 ~ UF5
32+
// 测试我们的UF1 ~ UF6
3333

3434
// 100万数据对于UF1和UF2来说太慢了, 不再测试
3535
// UF1::UnionFind uf1 = UF1::UnionFind(n);
@@ -47,7 +47,7 @@ int main() {
4747
UF5::UnionFind uf5 = UF5::UnionFind(n);
4848
UnionFindTestHelper::testUF("UF5", uf5, unionTest, connectTest);
4949

50-
UF5::UnionFind uf6 = UF5::UnionFind(n);
50+
UF6::UnionFind uf6 = UF6::UnionFind(n);
5151
UnionFindTestHelper::testUF("UF6", uf6, unionTest, connectTest);
5252

5353
return 0;

0 commit comments

Comments
 (0)