Search the most popular friends in my network

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); //populate the map
  }
}

priority_queue< pair<like, int> > pq;
for each like in like2friendsMap {
  int count = like2friendsMap[like].size(); //no. of friends who like this or "popularity"
  pq.push(like, count); //count is the priority
}

map< like, set<friend> > result
for i in 1 to N { //N is how many popular items I want
   result = pq.top();  //gives me the element with highest priority (most popular like)
   pq.pop();
}

STL Red Black Tree min/max heap , . 100 100, . , , , id , .

( , )? - , . ++, , STL boost, .

+5
3
create an integer list allPages which can be referenced by page
initialize it with 0
for every friend f
{
    for every page p liked by f
    {
        allPages[p]++;
    }
}
get the maximum of the allPages[p]

P - , O(P).

F - , L - , . O(F*L). , , , , .

O(F*L) + O(F) would remain O(F*L)

, , .

. , , . .

+1

, priority_queue. , . . :

priority_queue< pair<like, int> > pq;
std::priority_queue< pair<like, int> >::const_iterator max_friends = pq.begin()
for(i = like2friendsMap.begin() to .end())  {
  if (max_friends->size() < i->size()) max_friends = i;
}

, N = 1, " X, Y Z, ".

0

" ", , "", , -5, -10 ..? , , , N, , , , , top X '. N ( . , , ,...), , N , , , , . , "-10 ", 10 , , N, , N "-10 ", , , , , . , , , . , , , , "-10" .

0
source

All Articles