Skip to content

Commit b3e31b5

Browse files
authored
feat(graph): add DSU-based account merge algorithm (#7377)
* feat(graph): add DSU-based account merge algorithm * test(graph): add null and transitive account merge cases * Handle no-email accounts in account merge * Apply clang-format style to account merge tests
1 parent 14b6f99 commit b3e31b5

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.LinkedHashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* Merges account records using Disjoint Set Union (Union-Find) on shared emails.
12+
*
13+
* <p>Input format: each account is a list where the first element is the user name and the
14+
* remaining elements are emails.
15+
*/
16+
public final class AccountMerge {
17+
private AccountMerge() {
18+
}
19+
20+
public static List<List<String>> mergeAccounts(List<List<String>> accounts) {
21+
if (accounts == null || accounts.isEmpty()) {
22+
return List.of();
23+
}
24+
25+
UnionFind dsu = new UnionFind(accounts.size());
26+
Map<String, Integer> emailToAccount = new HashMap<>();
27+
28+
for (int i = 0; i < accounts.size(); i++) {
29+
List<String> account = accounts.get(i);
30+
for (int j = 1; j < account.size(); j++) {
31+
String email = account.get(j);
32+
Integer previous = emailToAccount.putIfAbsent(email, i);
33+
if (previous != null) {
34+
dsu.union(i, previous);
35+
}
36+
}
37+
}
38+
39+
Map<Integer, List<String>> rootToEmails = new LinkedHashMap<>();
40+
for (Map.Entry<String, Integer> entry : emailToAccount.entrySet()) {
41+
int root = dsu.find(entry.getValue());
42+
rootToEmails.computeIfAbsent(root, ignored -> new ArrayList<>()).add(entry.getKey());
43+
}
44+
for (int i = 0; i < accounts.size(); i++) {
45+
if (accounts.get(i).size() <= 1) {
46+
int root = dsu.find(i);
47+
rootToEmails.computeIfAbsent(root, ignored -> new ArrayList<>());
48+
}
49+
}
50+
51+
List<List<String>> merged = new ArrayList<>();
52+
for (Map.Entry<Integer, List<String>> entry : rootToEmails.entrySet()) {
53+
int root = entry.getKey();
54+
List<String> emails = entry.getValue();
55+
Collections.sort(emails);
56+
57+
List<String> mergedAccount = new ArrayList<>();
58+
mergedAccount.add(accounts.get(root).getFirst());
59+
mergedAccount.addAll(emails);
60+
merged.add(mergedAccount);
61+
}
62+
63+
merged.sort((a, b) -> {
64+
int cmp = a.getFirst().compareTo(b.getFirst());
65+
if (cmp != 0) {
66+
return cmp;
67+
}
68+
if (a.size() == 1 || b.size() == 1) {
69+
return Integer.compare(a.size(), b.size());
70+
}
71+
return a.get(1).compareTo(b.get(1));
72+
});
73+
return merged;
74+
}
75+
76+
private static final class UnionFind {
77+
private final int[] parent;
78+
private final int[] rank;
79+
80+
private UnionFind(int size) {
81+
this.parent = new int[size];
82+
this.rank = new int[size];
83+
for (int i = 0; i < size; i++) {
84+
parent[i] = i;
85+
}
86+
}
87+
88+
private int find(int x) {
89+
if (parent[x] != x) {
90+
parent[x] = find(parent[x]);
91+
}
92+
return parent[x];
93+
}
94+
95+
private void union(int x, int y) {
96+
int rootX = find(x);
97+
int rootY = find(y);
98+
if (rootX == rootY) {
99+
return;
100+
}
101+
102+
if (rank[rootX] < rank[rootY]) {
103+
parent[rootX] = rootY;
104+
} else if (rank[rootX] > rank[rootY]) {
105+
parent[rootY] = rootX;
106+
} else {
107+
parent[rootY] = rootX;
108+
rank[rootX]++;
109+
}
110+
}
111+
}
112+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.graph;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
class AccountMergeTest {
9+
10+
@Test
11+
void testMergeAccountsWithSharedEmails() {
12+
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"));
13+
14+
List<List<String>> merged = AccountMerge.mergeAccounts(accounts);
15+
16+
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"));
17+
18+
assertEquals(expected, merged);
19+
}
20+
21+
@Test
22+
void testAccountsWithSameNameButNoSharedEmailStaySeparate() {
23+
List<List<String>> accounts = List.of(List.of("Alex", "alex1@mail.com"), List.of("Alex", "alex2@mail.com"));
24+
25+
List<List<String>> merged = AccountMerge.mergeAccounts(accounts);
26+
List<List<String>> expected = List.of(List.of("Alex", "alex1@mail.com"), List.of("Alex", "alex2@mail.com"));
27+
28+
assertEquals(expected, merged);
29+
}
30+
31+
@Test
32+
void testEmptyInput() {
33+
assertEquals(List.of(), AccountMerge.mergeAccounts(List.of()));
34+
}
35+
36+
@Test
37+
void testNullInput() {
38+
assertEquals(List.of(), AccountMerge.mergeAccounts(null));
39+
}
40+
41+
@Test
42+
void testTransitiveMergeAndDuplicateEmails() {
43+
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"));
44+
45+
List<List<String>> merged = AccountMerge.mergeAccounts(accounts);
46+
47+
List<List<String>> expected = List.of(List.of("A", "a1@mail.com", "a2@mail.com", "a3@mail.com", "a4@mail.com"));
48+
49+
assertEquals(expected, merged);
50+
}
51+
52+
@Test
53+
void testAccountsWithNoEmailsArePreserved() {
54+
List<List<String>> accounts = List.of(List.of("Alex"), List.of("Alex", "alex1@mail.com"), List.of("Bob"));
55+
56+
List<List<String>> merged = AccountMerge.mergeAccounts(accounts);
57+
List<List<String>> expected = List.of(List.of("Alex"), List.of("Alex", "alex1@mail.com"), List.of("Bob"));
58+
59+
assertEquals(expected, merged);
60+
}
61+
}

0 commit comments

Comments
 (0)