-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect1.py
68 lines (57 loc) · 1.28 KB
/
connect1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
def findX(x):
return dSets[x]
def mergeSet(p,q):
lis=hashSet[q]
for i in lis:
dSets[i]=p
hashSet[p].append(i)
del hashSet[q]
n=1000000
dSets =[]
for i in range(0,n):
dSets.append(i)
hashSet={}
for i in range(0,n):
hashSet[i]=[i]
edgeSet=[]
fh= open("Graph.txt","r")
for k in fh:
a,b=k.split(',')
a=int(a)
b=int(b)
#print(a,b)
c,d=findX(a),findX(b)
if c!=d:
p=min(c,d)
q=max(c,d)
mergeSet(p,q)
#by cal
clusters = {}
individuals = []
for x in dSets:
if x not in clusters:
clusters[x] = 1
else:
clusters[x] += 1
for key in list(clusters):
if clusters[key] == 1:
individuals.append(clusters[key])
del clusters[key]
print("Number of clusters: ", len(clusters))
print("Number of individuals: ", len(individuals))
clusterSizes = []
for key in clusters:
clusterSizes.append(clusters[key])
clusterSizes=sorted(clusterSizes, key=int, reverse=True)
print("Sizes of 5 largest clusters: ", clusterSizes[0:5])
print(findX(825289)==findX(891950))
print(findX(1)==findX(267721))
print(findX(785775)==findX(891950))
print(findX(733252)==findX(891950))
print(findX(250429)==findX(2))
#for i in dSets:
#print(i)
#for k,v in hashSet.items():
#print(k,v)
fh.close()