Advertisement
hoangreal

Divide graph into two unrelated groups

Oct 21st, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. """
  2. Given a list of pair of "unrelated images", we need to know whether we can divide the images into two groups
  3. such that no two unrelated images are in the same group.
  4.  
  5. Case 1
  6. I_1 <-> I_4
  7. I_4 <-> I_8
  8. I_8 <-> I_2
  9.  
  10. Result:
  11. Group 1 -> [I_1, I_8]
  12. Group 2 -> [I_4, I_2]
  13.  
  14. Case 2
  15. I_1 <-> I_4
  16. I_4 <-> I_8
  17. I_8 <-> I_1
  18. """
  19.  
  20. from collections import defaultdict
  21. from typing import List, Tuple
  22.  
  23. class Solution:
  24.     def possibleBipartition(self, N: int, dislikes: List[List[int]]) -> Tuple[bool, List[int], List[int]]:
  25.        
  26.         # Constants defined for color drawing
  27.         BLUE, GREEN = 1, -1
  28.        
  29.         def draw(person_id, color):
  30.             # Color the current person
  31.             color_of[person_id] = color
  32.            
  33.             # Append the person to the respective group
  34.             if color == BLUE:
  35.                 group_one.append(person_id)
  36.             else:
  37.                 group_two.append(person_id)
  38.            
  39.             # Iterate through all persons disliked by the current person
  40.             for the_other in dislike_table[person_id]:
  41.                 if color_of[the_other] == color:
  42.                     # Conflict found, return False
  43.                     return False
  44.                
  45.                 if color_of[the_other] == 0 and not draw(the_other, -color):
  46.                     # If the other person is uncolored, color them with the opposite color
  47.                     return False
  48.                    
  49.             return True
  50.        
  51.         # Simple case checks
  52.         if N == 1 or not dislikes:
  53.             return True, list(range(1, N + 1)), []
  54.        
  55.         # Adjacency list for dislikes
  56.         dislike_table = defaultdict(list)
  57.        
  58.         # Color mapping for persons
  59.         color_of = defaultdict(int)
  60.        
  61.         # Groups to store the result
  62.         group_one = []
  63.         group_two = []
  64.        
  65.         # Build the dislike graph
  66.         for p1, p2 in dislikes:
  67.             dislike_table[p1].append(p2)
  68.             dislike_table[p2].append(p1)
  69.        
  70.         # Try to color the graph
  71.         for person_id in range(1, N + 1):
  72.             if color_of[person_id] == 0:
  73.                 # Clear the groups for new DFS/BFS
  74.                 group_one = []
  75.                 group_two = []
  76.                 if not draw(person_id, BLUE):
  77.                     return False, [], []  # Return false if unable to color
  78.                
  79.         return True, group_one, group_two
  80.  
  81. def solve_return_all_groups():
  82.     # Example cases
  83.     case1_n = 8
  84.     case1_dislikes = [[1, 4], [4, 8], [8, 2]]
  85.     case2_n = 8
  86.     case2_dislikes = [[1, 4], [4, 8], [8, 1]]
  87.    
  88.     solution = Solution()
  89.    
  90.     result_case1 = solution.possibleBipartition(case1_n, case1_dislikes)
  91.     result_case2 = solution.possibleBipartition(case2_n, case2_dislikes)
  92.    
  93.     print(f"Case 1: Can be partitioned: {result_case1[0]}, Group 1: {result_case1[1]}, Group 2: {result_case1[2]}")  # Expected: True
  94.     print(f"Case 2: Can be partitioned: {result_case2[0]}, Group 1: {result_case2[1]}, Group 2: {result_case2[2]}")  # Expected: False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement