-
Notifications
You must be signed in to change notification settings - Fork 21.1k
feat(graph): add DSU-based account merge algorithm #7377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickzerjeski
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
nickzerjeski:feat-graph-account-merge-dsu-6831
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dbc9534
feat(graph): add DSU-based account merge algorithm
nickzerjeski 29137c9
test(graph): add null and transitive account merge cases
nickzerjeski 28f3f7a
Handle no-email accounts in account merge
nickzerjeski fc851fb
Apply clang-format style to account merge tests
nickzerjeski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/main/java/com/thealgorithms/graph/AccountMerge.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package com.thealgorithms.graph; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Merges account records using Disjoint Set Union (Union-Find) on shared emails. | ||
| * | ||
| * <p>Input format: each account is a list where the first element is the user name and the | ||
| * remaining elements are emails. | ||
| */ | ||
| public final class AccountMerge { | ||
| private AccountMerge() { | ||
| } | ||
|
|
||
| public static List<List<String>> mergeAccounts(List<List<String>> accounts) { | ||
| if (accounts == null || accounts.isEmpty()) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| UnionFind dsu = new UnionFind(accounts.size()); | ||
| Map<String, Integer> emailToAccount = new HashMap<>(); | ||
|
|
||
| for (int i = 0; i < accounts.size(); i++) { | ||
| List<String> account = accounts.get(i); | ||
| for (int j = 1; j < account.size(); j++) { | ||
| String email = account.get(j); | ||
| Integer previous = emailToAccount.putIfAbsent(email, i); | ||
| if (previous != null) { | ||
| dsu.union(i, previous); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Map<Integer, List<String>> rootToEmails = new LinkedHashMap<>(); | ||
| for (Map.Entry<String, Integer> entry : emailToAccount.entrySet()) { | ||
| int root = dsu.find(entry.getValue()); | ||
| rootToEmails.computeIfAbsent(root, ignored -> new ArrayList<>()).add(entry.getKey()); | ||
| } | ||
| for (int i = 0; i < accounts.size(); i++) { | ||
| if (accounts.get(i).size() <= 1) { | ||
| int root = dsu.find(i); | ||
| rootToEmails.computeIfAbsent(root, ignored -> new ArrayList<>()); | ||
| } | ||
| } | ||
|
|
||
| List<List<String>> merged = new ArrayList<>(); | ||
| for (Map.Entry<Integer, List<String>> entry : rootToEmails.entrySet()) { | ||
| int root = entry.getKey(); | ||
| List<String> emails = entry.getValue(); | ||
| Collections.sort(emails); | ||
|
|
||
| List<String> mergedAccount = new ArrayList<>(); | ||
| mergedAccount.add(accounts.get(root).getFirst()); | ||
| mergedAccount.addAll(emails); | ||
| merged.add(mergedAccount); | ||
| } | ||
|
|
||
| merged.sort((a, b) -> { | ||
| int cmp = a.getFirst().compareTo(b.getFirst()); | ||
| if (cmp != 0) { | ||
| return cmp; | ||
| } | ||
| if (a.size() == 1 || b.size() == 1) { | ||
| return Integer.compare(a.size(), b.size()); | ||
| } | ||
| return a.get(1).compareTo(b.get(1)); | ||
| }); | ||
| return merged; | ||
| } | ||
|
|
||
| private static final class UnionFind { | ||
| private final int[] parent; | ||
| private final int[] rank; | ||
|
|
||
| private UnionFind(int size) { | ||
| this.parent = new int[size]; | ||
| this.rank = new int[size]; | ||
| for (int i = 0; i < size; i++) { | ||
| parent[i] = i; | ||
| } | ||
| } | ||
|
|
||
| private int find(int x) { | ||
| if (parent[x] != x) { | ||
| parent[x] = find(parent[x]); | ||
| } | ||
| return parent[x]; | ||
| } | ||
|
|
||
| private void union(int x, int y) { | ||
| int rootX = find(x); | ||
| int rootY = find(y); | ||
| if (rootX == rootY) { | ||
| return; | ||
| } | ||
|
|
||
| if (rank[rootX] < rank[rootY]) { | ||
| parent[rootX] = rootY; | ||
| } else if (rank[rootX] > rank[rootY]) { | ||
| parent[rootY] = rootX; | ||
| } else { | ||
| parent[rootY] = rootX; | ||
| rank[rootX]++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/test/java/com/thealgorithms/graph/AccountMergeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.thealgorithms.graph; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class AccountMergeTest { | ||
|
|
||
| @Test | ||
| void testMergeAccountsWithSharedEmails() { | ||
| List<List<String>> accounts = List.of(List.of("abc", "abc@mail.com", "abx@mail.com"), List.of("abc", "abc@mail.com", "aby@mail.com"), List.of("Mary", "mary@mail.com"), List.of("John", "johnnybravo@mail.com")); | ||
|
|
||
| List<List<String>> merged = AccountMerge.mergeAccounts(accounts); | ||
|
|
||
| List<List<String>> expected = List.of(List.of("John", "johnnybravo@mail.com"), List.of("Mary", "mary@mail.com"), List.of("abc", "abc@mail.com", "abx@mail.com", "aby@mail.com")); | ||
|
|
||
| assertEquals(expected, merged); | ||
| } | ||
|
|
||
| @Test | ||
| void testAccountsWithSameNameButNoSharedEmailStaySeparate() { | ||
| List<List<String>> accounts = List.of(List.of("Alex", "alex1@mail.com"), List.of("Alex", "alex2@mail.com")); | ||
|
|
||
| List<List<String>> merged = AccountMerge.mergeAccounts(accounts); | ||
| List<List<String>> expected = List.of(List.of("Alex", "alex1@mail.com"), List.of("Alex", "alex2@mail.com")); | ||
|
|
||
| assertEquals(expected, merged); | ||
| } | ||
|
|
||
| @Test | ||
| void testEmptyInput() { | ||
| assertEquals(List.of(), AccountMerge.mergeAccounts(List.of())); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullInput() { | ||
| assertEquals(List.of(), AccountMerge.mergeAccounts(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void testTransitiveMergeAndDuplicateEmails() { | ||
| List<List<String>> accounts = List.of(List.of("A", "a1@mail.com", "a2@mail.com"), List.of("A", "a2@mail.com", "a3@mail.com"), List.of("A", "a3@mail.com", "a4@mail.com", "a4@mail.com")); | ||
|
|
||
| List<List<String>> merged = AccountMerge.mergeAccounts(accounts); | ||
|
|
||
| List<List<String>> expected = List.of(List.of("A", "a1@mail.com", "a2@mail.com", "a3@mail.com", "a4@mail.com")); | ||
|
|
||
| assertEquals(expected, merged); | ||
| } | ||
|
|
||
| @Test | ||
| void testAccountsWithNoEmailsArePreserved() { | ||
| List<List<String>> accounts = List.of(List.of("Alex"), List.of("Alex", "alex1@mail.com"), List.of("Bob")); | ||
|
|
||
| List<List<String>> merged = AccountMerge.mergeAccounts(accounts); | ||
| List<List<String>> expected = List.of(List.of("Alex"), List.of("Alex", "alex1@mail.com"), List.of("Bob")); | ||
|
|
||
| assertEquals(expected, merged); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accounts that contain only a name (i.e., no emails) are currently omitted from the output.
rootToEmailsis populated only by iteratingemailToAccount, so accounts with an empty email list never become a root entry and are dropped. Consider explicitly handling accounts withaccount.size() <= 1(or no emails after filtering) so they are returned as standalone merged accounts, and add a test for this case.