This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (41 loc) · 1.63 KB
/
main.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
"""
Author: Hsuan-Hau Liu
Date: June, 23rd, 2019
Description: Find the optimal Yelp user community clustering using GN algorithm.
"""
from community_detection import gn, util
# PySpark import
from pyspark import SparkContext
def main():
""" Main """
context = SparkContext('local', 'GN')
context.setLogLevel('OFF')
# Read inputs
threshold, input_name, b_output_name, c_output_name = util.parse_inputs()
# Parse data
data = util.parse_csv(context.textFile(input_name), header=True)
user_set = util.build_user_set(data)
graph, edges = util.build_social_graph(user_set, threshold)
degree_table = util.build_degree_table(graph)
adjacency_matrix = util.build_adjacency_matrix(edges)
num_of_edges = len(edges)
# calculate betweenness of the initial graph
gn.calculate_betweenness(graph, edges)
util.output_betweenness(b_output_name, util.convert_to_list(edges))
# keep removing the edges with highest betweenness
highest_modularity = -1.0
optimal_communities = []
while edges:
communities = gn.find_communities(graph)
modularity = gn.calculate_modularity(communities, degree_table, adjacency_matrix, num_of_edges)
if modularity > highest_modularity:
highest_modularity = modularity
optimal_communities = communities
util.remove_highest_edges(graph, edges)
util.reset_betweenness(edges)
if edges:
gn.calculate_betweenness(graph, edges)
util.sort_communities(optimal_communities)
util.output_communities(c_output_name, optimal_communities)
if __name__ == '__main__':
main()