-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected_components.py
More file actions
73 lines (57 loc) · 1.76 KB
/
connected_components.py
File metadata and controls
73 lines (57 loc) · 1.76 KB
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
69
70
71
72
73
import sys
filename_graph = sys.argv[1]
if len(sys.argv) == 3:
sep = sys.argv[2]
else:
sep = " "
connected_components = {}
print "Loading edges..."
with open(filename_graph, 'r') as file:
for line in file:
line = line.replace("\n", "").split(sep)
if line[0] not in connected_components and line[1] not in connected_components:
connected_components[line[0]] = set()
id_comp = line[0]
elif line[0] in connected_components:
id_comp = line[0]
else:
id_comp = line[1]
connected_components[id_comp].add(line[0])
connected_components[id_comp].add(line[1])
print "Finding connected components..."
keys_id = connected_components.keys()
for id_comp1 in keys_id:
find = False
for id_comp2 in connected_components.keys():
if id_comp1 == id_comp2:
continue
for id_node in connected_components[id_comp1]:
if id_node in connected_components[id_comp2]:
find = True
break
if find:
for id_node in connected_components[id_comp2]:
connected_components[id_comp1].add(id_node)
del connected_components[id_comp2]
keys_id.remove(id_comp2)
find = False
connected_components = list(connected_components.values())
ordered_list_cc = sorted(connected_components, key=len, reverse=True)
print "Number of connected components : " + str(len(ordered_list_cc))
dist_size_cc = {}
for cc in ordered_list_cc:
if len(cc) not in dist_size_cc:
dist_size_cc[len(cc)] = 0
dist_size_cc[len(cc)] += 1
print dist_size_cc
print "Writing giant component..."
f = open(filename_graph + "_giant", 'w')
with open(filename_graph, 'r') as file:
for line in file:
line = line.replace("\n", "").split(sep)
if line[0] in ordered_list_cc[0]:
f.write(line[0] + " " + line[1])
if len(line) == 3:
f.write(" " + line[2])
f.write('\n')
f.close()