graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Code consultation --- About the label propagation algorithm

Open FuJiaJie123 opened this issue 9 months ago • 2 comments

I am not sure if I might be misunderstanding something, but I believe there could be an issue with line 110 in community_operations.py:

new_community = max(community_candidate, curr_community)

The core idea of this algorithm is to update community_candidate based on the majority vote from its neighbors. However, if community_candidate has a higher weight but a smaller ID, wouldn’t this prevent the necessary update from being applied?

It seems to me that this line should be: new_community = community_candidate == -1 ? curr_community : community_candidate Since the tie-breaking logic has already been handled through sorting in line 107, using max(community_candidate, curr_community) may cause cases where an update should occur but does not.

I would appreciate your thoughts on this. Thank you for your time!

FuJiaJie123 avatar Mar 21 '25 09:03 FuJiaJie123

You are correct in that the max() line of logic is incorrect. However, the tie-breaking isn't completely handled by line 107 since a node could have a community shared by none of its neighbors (in particular this will during the first iteration of the algorithm).

I believe that the conditions are as follows:

  1. If community_candidate == -1, we should keep curr_community
  2. Else if the rank of community_candidate > 1 then we should use the community_candidate. This is because the only way for curr_community to not be in the candidate list is for the node to not share a community with any of its neighbors.
  3. Else if the rank of community_candidate == 1, then we should use max(community_candidate, current_community).

I believe the following code captures this logic: ` candidate_rank, community_candidate = community_lst[0] if len(community_lst) > 0 else (0, -1)

        if community_candidate != -1 and candidate_rank > 1:
            new_community = community_candidate
        else:
            new_community = max(community_candidate, curr_community)

`

Since you found the bug, would you like to open a PR with the fix? Otherwise I am happy to do it.

Good catch and thank you!

prasmussen15 avatar Mar 21 '25 16:03 prasmussen15

Yes, I am willing to open a PR, thanks for your reply

FuJiaJie123 avatar Mar 24 '25 08:03 FuJiaJie123