/// <summary> /// Initialize Multiset from HashSet, each ID has multiplicity 1 /// </summary> /// <param name="friends"></param> /// <returns></returns> public static IDMultiset Create(HashSet<SubscriberID> friends) { var multiset = new IDMultiset(); foreach (SubscriberID friend in friends) { multiset[friend] = 1; } return multiset; }
/// <summary> /// Initialize Multiset from HashSet, each ID has multiplicity 1 /// </summary> /// <param name="friends"></param> /// <returns></returns> public static IDMultiset Create(HashSet <SubscriberID> friends) { var multiset = new IDMultiset(); foreach (SubscriberID friend in friends) { multiset[friend] = 1; } return(multiset); }
public IDMultisetItemList PotentialFriendsParallel(SubscriberID id, int maxCandidates) { object locker = new object(); IDMultiset candidates = new IDMultiset(); Parallel.ForEach(subscribers[id].Friends, // Map over friends () => Multiset.Create(new HashSet <SubscriberID>()), // init thread-local state localFoafs with empty Multiset (friend, loopState, localFoafs) => { var foafs = subscribers[friend].FriendsCopy(); foafs.RemoveWhere(foaf => foaf == id || subscribers[id].Friends.Contains(foaf)); // remove self, own friends return(Multiset.Union(localFoafs, Multiset.Create(foafs))); // Reduce, thread-local }, localFoafs => { lock (locker) candidates = Multiset.Union(localFoafs, candidates); }); // Reduce, among threads return(Multiset.MostNumerous(candidates, maxCandidates)); // postprocess results of Reduce }
/// <summary> /// Multiset union. /// </summary> /// <param name="multiset1">First Multiset</param> /// <param name="multiset2">Second Multiset</param> /// <returns>Union of Multiset1 and Multiset2</returns> public static IDMultiset Union(IDMultiset multiset1, IDMultiset multiset2) { if (multiset1.Count < multiset2.Count) { return Union(multiset2, multiset1); } else { var union = new IDMultiset(); foreach (var item in multiset1) { union.Add(item.Key, item.Value); } foreach (var item in multiset2) { var friend = item.Key; var count = item.Value; union[friend] = union.ContainsKey(friend) ? union[friend] + count : count; } return union; } }
/// <summary> /// Multiset union. /// </summary> /// <param name="multiset1">First Multiset</param> /// <param name="multiset2">Second Multiset</param> /// <returns>Union of Multiset1 and Multiset2</returns> public static IDMultiset Union(IDMultiset multiset1, IDMultiset multiset2) { if (multiset1.Count < multiset2.Count) { return(Union(multiset2, multiset1)); } else { var union = new IDMultiset(); foreach (var item in multiset1) { union.Add(item.Key, item.Value); } foreach (var item in multiset2) { var friend = item.Key; var count = item.Value; union[friend] = union.ContainsKey(friend) ? union[friend] + count : count; } return(union); } }
public IDMultisetItemList PotentialFriendsSequential(SubscriberID id, int maxCandidates) { // Map var foafsList = new List <IDMultiset>(); foreach (SubscriberID friend in subscribers[id].Friends) { var foafs = subscribers[friend].FriendsCopy(); foafs.RemoveWhere(foaf => foaf == id || subscribers[id].Friends.Contains(foaf)); // remove self, own friends foafsList.Add(Multiset.Create(foafs)); } // Reduce IDMultiset candidates = new IDMultiset(); foreach (IDMultiset foafs in foafsList) { candidates = Multiset.Union(foafs, candidates); } // Postprocess return(Multiset.MostNumerous(candidates, maxCandidates)); }
public IDMultisetItemList PotentialFriendsSequential(SubscriberID id, int maxCandidates) { // Map var foafsList = new List<IDMultiset>(); foreach (SubscriberID friend in subscribers[id].Friends) { var foafs = subscribers[friend].FriendsCopy(); foafs.RemoveWhere(foaf => foaf == id|| subscribers[id].Friends.Contains(foaf)); // remove self, own friends foafsList.Add(Multiset.Create(foafs)); } // Reduce IDMultiset candidates = new IDMultiset(); foreach (IDMultiset foafs in foafsList) { candidates = Multiset.Union(foafs, candidates); } // Postprocess return Multiset.MostNumerous(candidates, maxCandidates); }
public IDMultisetItemList PotentialFriendsParallel(SubscriberID id, int maxCandidates) { object locker = new object(); IDMultiset candidates = new IDMultiset(); Parallel.ForEach(subscribers[id].Friends, // Map over friends () => Multiset.Create(new HashSet<SubscriberID>()), // init thread-local state localFoafs with empty Multiset (friend, loopState, localFoafs) => { var foafs = subscribers[friend].FriendsCopy(); foafs.RemoveWhere(foaf => foaf == id || subscribers[id].Friends.Contains(foaf)); // remove self, own friends return Multiset.Union(localFoafs, Multiset.Create(foafs)); // Reduce, thread-local }, localFoafs => { lock (locker) candidates = Multiset.Union(localFoafs, candidates); }); // Reduce, among threads return Multiset.MostNumerous(candidates, maxCandidates); // postprocess results of Reduce }