I am working on finding the most popular friends on my network. "Most Popular in My Friends Network" is defined as "having the most number of my friends that I liked."
Suppose each friend has a unique identifier and has several pages that he likes. Therefore, given the many friends I like, I want to find the ones I like most of my friends, as well as those who like it. Essentially, I want to show something like "Your friend X, Y, and Z like this."
My first solution is to use Map (to preserve the inverse mapping: like-> set) and Priority Queue (to search for the top N). Here is my algorithm (using C ++ STL):
map< like, set<friend> > like2friendsMap;
for each friend {
for each like {
like2friendsMap[like].insert(friend);
}
}
priority_queue< pair<like, int> > pq;
for each like in like2friendsMap {
int count = like2friendsMap[like].size();
pq.push(like, count);
}
map< like, set<friend> > result
for i in 1 to N {
result = pq.top();
pq.pop();
}
STL Red Black Tree min/max heap , . 100 100, . , , , id , .
( , )? - , . ++, , STL boost, .