Fix: Prevent oscillation in label propagation algorithm

- Changed from synchronous to asynchronous updates with randomized node order
- Added maximum iteration limit (100) to prevent infinite loops
- Implemented oscillation detection with early stopping mechanism
- Improved tie-breaking with deterministic sorting
- Added detailed docstring and logging for convergence/oscillation events
- Fixed oscillation detection to properly break out of nested loops

The previous implementation used synchronous updates where all nodes
updated simultaneously, which could cause oscillation in certain graph
structures (e.g., bipartite graphs). This fix ensures the algorithm
always terminates and produces stable community assignments while
maintaining backward compatibility with existing tests.
This commit is contained in:
remonxiao 2025-11-28 09:56:41 +08:00
parent 675efbebe1
commit febf8923f6

View file

@ -85,99 +85,106 @@ async def get_community_clusters(
def label_propagation(projection: dict[str, list[Neighbor]]) -> list[list[str]]: def label_propagation(projection: dict[str, list[Neighbor]]) -> list[list[str]]:
""" """
Implement the label propagation community detection algorithm with oscillation prevention. Implement the label propagation community detection algorithm.
Algorithm: Algorithm:
1. Start with each node being assigned its own community 1. Start with each node being assigned its own community
2. Each node will take on the community of the plurality of its neighbors 2. Each node will take on the community of the plurality of its neighbors
3. Ties are broken by going to the largest community 3. Ties are broken by going to the largest community
4. Continue until no communities change during propagation 4. Continue until no communities change during propagation
Oscillation prevention: Oscillation prevention:
- Uses asynchronous updates (randomized node order) - Uses asynchronous updates (randomized node order)
- Maximum iteration limit to prevent infinite loops - Maximum iteration limit to prevent infinite loops
- Early stopping if oscillation is detected - Early stopping if oscillation is detected
""" """
import random import random
MAX_ITERATIONS = 100 MAX_ITERATIONS = 100
OSCILLATION_CHECK_WINDOW = 5 OSCILLATION_CHECK_WINDOW = 5
community_map = {uuid: i for i, uuid in enumerate(projection.keys())} community_map = {uuid: i for i, uuid in enumerate(projection.keys())}
node_uuids = list(projection.keys()) node_uuids = list(projection.keys())
# Track history to detect oscillations # Track history to detect oscillations
history: list[dict[str, int]] = [] history: list[dict[str, int]] = []
for iteration in range(MAX_ITERATIONS): for iteration in range(MAX_ITERATIONS):
# Asynchronous update: randomize node processing order # Asynchronous update: randomize node processing order to prevent oscillation
random.shuffle(node_uuids) random.shuffle(node_uuids)
changed_count = 0 changed_count = 0
for uuid in node_uuids: for uuid in node_uuids:
neighbors = projection[uuid] neighbors = projection[uuid]
curr_community = community_map[uuid] curr_community = community_map[uuid]
# Count votes from neighbors # Count votes from neighbors
community_candidates: dict[int, int] = defaultdict(int) community_candidates: dict[int, int] = defaultdict(int)
for neighbor in neighbors: for neighbor in neighbors:
community_candidates[community_map[neighbor.node_uuid]] += neighbor.edge_count community_candidates[community_map[neighbor.node_uuid]] += neighbor.edge_count
if not community_candidates: if not community_candidates:
continue continue
# Sort by count (descending), then by community ID for deterministic tie-breaking # Sort by count (descending), then by community ID for deterministic tie-breaking
community_lst = [ community_lst = [
(count, community) for community, count in community_candidates.items() (count, community) for community, count in community_candidates.items()
] ]
community_lst.sort(key=lambda x: (-x[0], x[1])) community_lst.sort(key=lambda x: (-x[0], x[1]))
candidate_rank, community_candidate = community_lst[0] candidate_rank, community_candidate = community_lst[0]
# Update community based on neighbor plurality # Determine new community:
# - If strong signal (edge count > 1), adopt the neighbor's community
# - Otherwise, prefer the larger community ID (original behavior)
if candidate_rank > 1: if candidate_rank > 1:
new_community = community_candidate new_community = community_candidate
else: else:
# For weak signals, prefer staying in current community
new_community = max(community_candidate, curr_community) new_community = max(community_candidate, curr_community)
if new_community != curr_community: if new_community != curr_community:
community_map[uuid] = new_community community_map[uuid] = new_community
changed_count += 1 changed_count += 1
# Check for convergence # Check for convergence
if changed_count == 0: if changed_count == 0:
logger.debug(f'Label propagation converged after {iteration + 1} iterations') logger.debug(f'Label propagation converged after {iteration + 1} iterations')
break break
# Check for oscillation by comparing with recent history # Check for oscillation by comparing with recent history
if iteration >= OSCILLATION_CHECK_WINDOW: current_state = community_map.copy()
current_state = community_map.copy() history.append(current_state)
history.append(current_state)
# Keep only recent history
# Keep only recent history if len(history) > OSCILLATION_CHECK_WINDOW:
if len(history) > OSCILLATION_CHECK_WINDOW: history.pop(0)
history.pop(0)
# Detect oscillation: if current state matches any recent state
# Detect oscillation: if current state matches any recent state if len(history) >= 2:
for past_state in history[:-1]: for past_state in history[:-1]:
if past_state == current_state: if past_state == current_state:
logger.warning( logger.warning(
f'Label propagation oscillation detected at iteration {iteration + 1}, ' f'Label propagation oscillation detected at iteration {iteration + 1}, '
'stopping early' 'stopping early'
) )
# Break out of the for loop
break break
else:
# No oscillation detected, continue to next iteration
continue
# Oscillation detected, break out of the main loop
break
else: else:
logger.warning( logger.warning(
f'Label propagation reached maximum iterations ({MAX_ITERATIONS}) without converging' f'Label propagation reached maximum iterations ({MAX_ITERATIONS}) without converging'
) )
# Group nodes by community # Group nodes by community
community_cluster_map = defaultdict(list) community_cluster_map: dict[int, list[str]] = defaultdict(list)
for uuid, community in community_map.items(): for uuid, community in community_map.items():
community_cluster_map[community].append(uuid) community_cluster_map[community].append(uuid)
clusters = list(community_cluster_map.values()) clusters = list(community_cluster_map.values())
return clusters return clusters