-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlouvain.py
54 lines (44 loc) · 1.52 KB
/
louvain.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
import sys
import community
import networkx as nx
import itertools
def read_dimacs_format(lines):
number_of_variables = None
number_of_clauses = None
clauses = []
for line in lines:
tokens = line.split()
if not len(tokens) or tokens[0] == 'c':
continue
elif tokens[0] == 'p' and tokens[1] == 'cnf':
number_of_variables = int(tokens[2])
number_of_clauses = int(tokens[3])
elif tokens[-1] == '0':
clause = [int(i) for i in tokens[:-1]]
clauses.append(clause)
return number_of_variables, number_of_clauses, tuple(clauses)
def build_graph(G, sat_instance):
'''
Construct Variable Incidence Graph
'''
for clause in sat_instance:
if len(clause) > 1:
weight = 1.0 / (len(clause) - 1)
for combination in itertools.combinations(clause, 2):
a=abs(combination[0])
b=abs(combination[1])
if G.has_edge(a, b):
G[a][b]['weight']+=weight
else:
G.add_edge(a, b, weight=weight)
if __name__ == '__main__':
filename = sys.argv[1]
content = []
with open(filename, "r") as f:
content = f.readlines()
number_of_variables, number_of_clauses, sat_instance = read_dimacs_format(content)
G = nx.Graph()
build_graph(G, sat_instance)
partition = community.best_partition(G)
for k in partition.keys():
print("{0} {1}".format(k - 1, partition[k]))