Program to count number of unhappy friends in Python


Suppose we have a list of preferences for n(even) different friends. For each person i, the preferences[i] holds a list of friends sorted in the order of preference. So, a friend earlier in the list is more preferred than a friend later in the list. The friends in each list are numbered by integers from 0 to n-1. All of the friends are divided into different pairs. where pairs[i] = [xi, yi] represents xi is paired with yi and/or yi is paired with xi. But a friend x is unhappy if x is paired with y and there exists a friend u who is also paired with v but −

  • x prefers u over y, and
  • u prefers x over v.

We have to find the number of unhappy friends.

So, if the input is like preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]] pairs = [[0, 1], [2, 3]], then the output will be 2 because first friend is unhappy because person 1 is paired with person 0 but prefers person 3 over person 0, and person 3 prefers person 1 over person 2. And friend 3 is unhappy because person 3 is paired with person 2 but prefers person 1 over person 2, and person 1 prefers person 3 over person 0.

To solve this, we will follow these steps −

  • graph := an adjacency list to make graph, initially empty
  • for each pair (s, e) in pairs, do
    • for each pref in preferences[s], do
      • if pref is same as end, then
        • come out from loop
      • graph[s, pref] := 1
    • for each pref in preferences[e], do
      • if pref is same as start, then
        • come out from loop
      • graph[e, pref] := 1
  • unhappy := 0
  • for each pair (s, e) in pairs, do
    • for each pref in graph[s], do
      • if graph[pref][s] is not empty, then
        • unhappy := unhappy + 1
        • come out from loop
    • for each pref in graph[end], do
      • if graph[pref][e] is not empty, then
        • unhappy := unhappy + 1
        • come out from loop
  • return unhappy

Example

Let us see the following implementation to get better understanding −

from collections import defaultdict
def solve(preferences, pairs):
   graph = defaultdict(dict)
   for start, end in pairs:
      for pref in preferences[start]:
         if pref == end:
            break
         graph[start][pref] = 1
      for pref in preferences[end]:
         if pref == start:
            break
         graph[end][pref] = 1

   unhappy = 0

   for start, end in pairs:
      for pref in graph[start]:
         if graph[pref].get(start, None):
            unhappy += 1
            break
      for pref in graph[end]:
         if graph[pref].get(end, None):
            unhappy += 1
            break
   return unhappy

preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]]
pairs = [[0, 1], [2, 3]]
print(solve(preferences, pairs))

Input

[[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]]

Output

2

Updated on: 04-Oct-2021

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements