Community detection in social networks using brute-force method - GeeksforGeeks

05 Sep, 2020
Prerequisite- Python Basics, NetworkX Basics 
We are going to divide the nodes of the graph into two or more communities using the brute force method. The brute force method means we will try every division of nodes into communities and check whether the communities are correctly divided or not. We will use a brute force method for this task.
Algorithm:
Create a graph of N nodes and its edges or take an inbuilt graph like a barbell graph.Now take two lists as FirstCommunity and SecondCommunity.Now start putting nodes into communities like put 1st node in FirstCommunity and rest N-1 nodes to SecondCommunity and check its inter and intra edges.Now we will make combinations using itertools.Repeat steps 3 and 4 for every combination.Now check which division is best by taking the ratio of intra/number of inter-community edges.Now find the value of FirstCommunity and SecondCommunity with maximum ratio and print that value.
Create a graph of N nodes and its edges or take an inbuilt graph like a barbell graph.
Now take two lists as FirstCommunity and SecondCommunity.
Now start putting nodes into communities like put 1st node in FirstCommunity and rest N-1 nodes to SecondCommunity and check its inter and intra edges.
Now we will make combinations using itertools.
Repeat steps 3 and 4 for every combination.
Now check which division is best by taking the ratio of intra/number of inter-community edges.
Now find the value of FirstCommunity and SecondCommunity with maximum ratio and print that value.
Below is the implementation.
Python3
import networkx as nximport itertools    def communities_using_brute(gfg):  nodes = gfg.nodes()  n = gfg.number_of_nodes()  first_community = []      for i in range(1, n//2 + 1):    c = [list(a) for a in itertools.combinations(nodes, i)]    first_community.extend(c)    second_community = []    for i in range(len(first_community)):    b = list(set(nodes)-set(first_community[i]))    second_community.append(b)    # Which division is best...  intra_edges1 = []  intra_edges2 = []  inter_edges = []        # ratio of number of intra/number of inter  # community edges  ratio = []      for i in range(len(first_community)):    intra_edges1.append(gfg.subgraph(first_community[i]).number_of_edges())    for i in range(len(second_community)):    intra_edges2.append(gfg.subgraph(second_community[i]).number_of_edges())    e = gfg.number_of_edges()    for i in range(len(first_community)):    inter_edges.append(e-intra_edges1[i]-intra_edges2[i])    # Calculate the Ratio    for i in range(len(first_community)):    ratio.append((float(intra_edges1[i]+intra_edges2[i]))/inter_edges[i])    maxV=max(ratio)  mindex=ratio.index(maxV)    print('[ ', first_community[mindex], ' ] , [ ', second_community[mindex], ' ]')  # Example graphgfg=nx.barbell_graph(5, 0)communities_using_brute(gfg)
Output:
[ [0,1,2,3,4] ] , [ [8,9,5,6,7] ]

python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Selecting rows in pandas DataFrame based on conditions
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Python | Get unique values from a list
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()