Skip to content

Commit

Permalink
FIX: Fixed edge ratio metric
Browse files Browse the repository at this point in the history
  • Loading branch information
RienkF committed May 31, 2022
1 parent 95f26e0 commit 636aa02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
22 changes: 10 additions & 12 deletions algorithm/edge_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@

# TODO: Type partition
def global_edge_ratio(G: networkx.DiGraph, partitions):
score_sum = 0
for partition in partitions:
internal_edges = len(G.subgraph(partition).edges)
out_edges = len(list(edge_boundary(G, partition, G.nodes - partition)))
denom = internal_edges + out_edges
if denom == 0:
continue
score_sum += internal_edges / denom
return score_sum
return sum(map(lambda partition: edge_boundary_ratio(G, partition), partitions))


def local_edge_ratio(
G: networkx.DiGraph, u, neighbour, node_to_community, inner_partition, partition, original_graph: networkx.DiGraph
G: networkx.DiGraph,
u,
neighbour,
node_to_community,
inner_partition,
partition,
original_graph: networkx.DiGraph,
):
"""
Calculates the change in score if u is moved to the community of the given neighbour.
Expand All @@ -30,7 +28,7 @@ def local_edge_ratio(
:param partition: Total partition of the complete graph.
:return: Change in local score
"""
nodes_in_u = original_graph.nodes[u].get("nodes", {u})
nodes_in_u = G.nodes[u].get("nodes", {u})

old_score_u = edge_boundary_ratio(original_graph, nodes_in_u)

Expand All @@ -57,7 +55,7 @@ def edge_boundary_ratio(G: networkx.DiGraph, partition):
else:
in_edge_size += 1
in_edge_size = in_edge_size / 2
denom = (edge_boundary_size + in_edge_size)
denom = edge_boundary_size + in_edge_size
if denom == 0:
return 0
return in_edge_size / denom
13 changes: 9 additions & 4 deletions louvain.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def louvain_partitions(
print("Executing iteration {}".format(counter))
counter += 1
yield partition
new_community_score = global_community_measure(graph, partition)
print(f"Calculated modularity: {new_community_score}")
new_community_score = global_community_measure(G, partition)
print(f"Calculated global measure score: {new_community_score}")
if new_community_score - comm_score <= threshold:
return
comm_score = new_community_score
Expand Down Expand Up @@ -119,11 +119,16 @@ def _one_level(
best_community_score = 0
best_com = node_to_community[u]

# TODO - Compute for each neighbour, the increase in score.
# We pass the node_to_community dict, as well as the current node, and its neighbours
for neighbour in G.neighbors(u):
new_score = local_community_measure(
G, u, neighbour, node_to_community, inner_partition, partition, original_graph
G,
u,
neighbour,
node_to_community,
inner_partition,
partition,
original_graph,
)
if new_score > best_community_score:
best_community_score = new_score
Expand Down
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ def load_network(
def main():
# G = load_network()
# Barabasi-Albert graph
G = nx.barabasi_albert_graph(500, 5)
G = nx.barabasi_albert_graph(5000, 5)
# Add edge weight of 1 to each edge
for u, v, d in G.edges(data=True):
d["weight"] = 1
G = nx.to_directed(G)
print(f"Loaded {len(G.nodes)} nodes and {len(G.edges)} edges")


communities = louvain_communities(G, global_edge_ratio, local_edge_ratio)
print(len(communities))

Expand Down

0 comments on commit 636aa02

Please sign in to comment.