|
| 1 | +package Medium; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class WebsiteSimilarity { |
| 5 | + public static void main(String[] args) { |
| 6 | + List<Pair<String, Integer>> userVisits = Arrays.asList( |
| 7 | + new Pair<>("a", 1), new Pair<>("a", 3), new Pair<>("a", 5), |
| 8 | + new Pair<>("b", 2), new Pair<>("b", 6), |
| 9 | + new Pair<>("c", 1), new Pair<>("c", 2), new Pair<>("c", 3), |
| 10 | + new Pair<>("c", 4), new Pair<>("c", 5), |
| 11 | + new Pair<>("d", 4), new Pair<>("d", 5), new Pair<>("d", 6), new Pair<>("d", 7), |
| 12 | + new Pair<>("e", 1), new Pair<>("e", 3), new Pair<>("e", 5), new Pair<>("e", 6) |
| 13 | + ); |
| 14 | + |
| 15 | + int k = 1; // You can change this to the desired value |
| 16 | + |
| 17 | + Map<Integer, Set<String>> websiteUserMap = new HashMap<>(); |
| 18 | + |
| 19 | + // Build a map of websites to the set of users who visited them |
| 20 | + for (Pair<String, Integer> visit : userVisits) { |
| 21 | + websiteUserMap.computeIfAbsent(visit.getSecond(), key -> new HashSet<>()).add(visit.getFirst()); |
| 22 | + } |
| 23 | + |
| 24 | + List<Pair<String, String>> websitePairs = new ArrayList<>(); |
| 25 | + |
| 26 | + // Calculate the similarity between website pairs |
| 27 | + for (String user1 : websiteUserMap.get(1)) { |
| 28 | + for (String user2 : websiteUserMap.get(1)) { |
| 29 | + if (!user1.equals(user2)) { |
| 30 | + Set<String> commonWebsites = new HashSet<>(websiteUserMap.get(1)); |
| 31 | + commonWebsites.retainAll(websiteUserMap.get(1)); |
| 32 | + |
| 33 | + double similarity = (double) commonWebsites.size() / (websiteUserMap.get(1).size() + websiteUserMap.get(1).size() - commonWebsites.size()); |
| 34 | + |
| 35 | + websitePairs.add(new Pair<>(user1, user2)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Sort the website pairs by similarity and get the top k |
| 41 | + websitePairs.sort((pair1, pair2) -> Double.compare(calculateSimilarity(pair2, websiteUserMap), calculateSimilarity(pair1, websiteUserMap))); |
| 42 | + |
| 43 | + for (int i = 0; i < Math.min(k, websitePairs.size()); i++) { |
| 44 | + System.out.println("Website Pair: " + websitePairs.get(i).getFirst() + ", " + websitePairs.get(i).getSecond()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static double calculateSimilarity(Pair<String, String> websitePair, Map<Integer, Set<String>> websiteUserMap) { |
| 49 | + Set<String> commonWebsites = new HashSet<>(websiteUserMap.get(1)); |
| 50 | + commonWebsites.retainAll(websiteUserMap.get(1)); |
| 51 | + |
| 52 | + return (double) commonWebsites.size() / (websiteUserMap.get(1).size() + websiteUserMap.get(1).size() - commonWebsites.size()); |
| 53 | + } |
| 54 | + |
| 55 | + static class Pair<F, S> { |
| 56 | + private final F first; |
| 57 | + private final S second; |
| 58 | + |
| 59 | + public Pair(F first, S second) { |
| 60 | + this.first = first; |
| 61 | + this.second = second; |
| 62 | + } |
| 63 | + |
| 64 | + public F getFirst() { |
| 65 | + return first; |
| 66 | + } |
| 67 | + |
| 68 | + public S getSecond() { |
| 69 | + return second; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments