Skip to content

Commit 6f02ec3

Browse files
author
Ayşegül Sümeyye Kütük
authored
Add files via upload
CAST algorithm for clustering
1 parent 10322bb commit 6f02ec3

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

cast.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import networkx as nx
2+
3+
def read_graph(filename):
4+
GG = nx.Graph()
5+
f = open('dataset.txt', 'r')
6+
for line in f:
7+
proteinA = line.split()[0]
8+
proteinB = line.split()[1]
9+
GG.add_node(proteinA)
10+
GG.add_node(proteinB)
11+
GG.add_edge(proteinA,proteinB)
12+
f.close()
13+
14+
G = dict()
15+
for edge in GG.edges():
16+
x, y = (edge[0]), (edge[1])
17+
if x not in G: G[x] = set()
18+
if y not in G: G[y] = set()
19+
G[x].add(y)
20+
G[y].add(x)
21+
return G
22+
23+
24+
def biggest_degree(G):
25+
max_deg_node = ''
26+
max_deg = 0
27+
for i in G:
28+
if len(G[i]) > max_deg:
29+
max_deg_node = i
30+
max_deg = len(G[i])
31+
32+
return max_deg_node
33+
34+
35+
def distancer(clust, node):
36+
return 1.4
37+
38+
def how_many_edge_it_brings(clust, node):
39+
#print('hwnsdhwndhwndwh')
40+
#print(clust)
41+
counter = 0
42+
for i in clust:
43+
for j in G[i]:
44+
# print(str(i) + 'hehe boi '+str(j))
45+
if j == node:
46+
counter += 1
47+
return counter
48+
49+
def update_graph(G, clust):
50+
print(clust)
51+
for i in clust:
52+
for j in clust:
53+
for k in G[i]:
54+
if k == j:
55+
G[i][j] = ''
56+
57+
for i in clust:
58+
flag = True
59+
for j in G[i]:
60+
if j != '':
61+
flag = False
62+
if flag == True:
63+
print('za')
64+
del G[i]
65+
return G
66+
67+
def cast(G, n, e):
68+
all_clust = []
69+
while(n > 0):
70+
print('zazaza')
71+
cur_clust = []
72+
cur_clust_n = 0 #current cluster's number of nodes
73+
cur_clust_e = 0 #current cluster's number of edges
74+
v = biggest_degree(G)
75+
cur_clust.append(v)
76+
cur_clust_n += 1
77+
78+
print(G['Veli'])
79+
cur_clust1 = cur_clust
80+
for i in cur_clust1:
81+
print(i)
82+
print(1)
83+
print(len(cur_clust))
84+
for j in list(G[i]):
85+
86+
if distancer(i, j) <= 1.5:
87+
cur_clust.append(j)
88+
cur_clust_n += 1
89+
#print(cur_clust_n)
90+
cur_clust_e += how_many_edge_it_brings(cur_clust, j)
91+
92+
93+
print('qqqqq')
94+
all_clust.append(cur_clust)
95+
G = update_graph(G, cur_clust)
96+
n = len(G)
97+
print('-------------------------')
98+
99+
return all_clust
100+
101+
G = read_graph('wi.txt')
102+
print(cast(G, 8, 8))
103+

0 commit comments

Comments
 (0)