-
Notifications
You must be signed in to change notification settings - Fork 0
/
girvan.py
48 lines (40 loc) · 1.37 KB
/
girvan.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
import networkx as nx
import matplotlib.pyplot as plt
import intro2
def edge_to_remove(G):
dict1 = nx.edge_betweenness_centrality(G) # gives the betweenness of all edgess
# Betweenness is the value showing for each edge, how many shortest paths between any 2 nodes, does it lies.
list_of_tuples = list(dict1.items())
list_of_tuples.sort(
key=lambda x: x[1], reverse=True
) # Method to sort a dictionary on the basis of 2nd value of the tuple in decreasing order.
return list_of_tuples[0][0] # (a,b)
def girvan(G):
c = nx.connected_component_subgraphs(
G
) # Gives a graph object(generator object in new networkx version)
l = len(list(c))
print("The number of connected components are:", l)
while l == 1:
edge_removed = edge_to_remove(G)
print("The edge removed is:", edge_removed)
G.remove_edge(*edge_removed) # ((a,b)) -> (a,b)
c = nx.connected_component_subgraphs(G)
l = len(c)
print("The number of connected components are:", l)
return c
# G = intro2.create_network((15))
# c = girvan(G)
# # print(c)
# # nx.draw(c)
# # plt.show()
# for i in c:
# print(i.nodes())
# print ("-----------------------------")
# for item in girvan(G):
# print(item)
G = nx.karate_club_graph()
c = girvan(G)
for i in c:
print(i.nodes())
print("-----------------------------")