-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectedComponents.py
64 lines (54 loc) · 1.64 KB
/
connectedComponents.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
from Util import scatter_plot
import time
from collections import deque
def getComponentArr(file):
cluster_dic = {}
f = open(file,"r")
lines = f.readlines()
for l in lines:
a,b = l.strip().split()
a,b = int(a),int(b)
edge = str(a)+","+str(b)
if a in cluster_dic:
cluster_dic[a].add(b)
else:
cluster_dic[a] = set()
cluster_dic[a].add(b)
if b in cluster_dic:
cluster_dic[b].add(a)
else:
cluster_dic[b] = set()
cluster_dic[b].add(a)
nodeList = list(cluster_dic.keys())
nodeSet = set()
q = deque()
componentDis = []
for node in nodeList:
if node not in nodeSet:
q.append(node)
num = 1
nodeSet.add(node)
while len(q)!=0:
tem = q.popleft()
for x in cluster_dic[tem]:
if x not in nodeSet:
nodeSet.add(x)
q.append(x)
num += 1
componentDis.append(num)
return componentDis
def getComponentPic(file,dataName):
componentDis = getComponentArr(file)
print(sum(componentDis))
numDic = {}
for x in componentDis:
numDic[x] = numDic.get(x,0)+1
x = list(numDic.keys())
y = list(numDic.values())
print(numDic)
scatter_plot(x, y, "component size", "Count", "connected Component-" + dataName, "result/connected Component-" + dataName + ".png")
if __name__ == "__main__":
start = time.time()
getComponentPic("data/kronecker.txt","kronecker")
end = time.time()
print(end - start)