|
| 1 | +# Approach 1: Graph Coloring + Longest Shortest Path |
| 2 | + |
| 3 | +# n = no. of nodes in graph, m = len(edges) |
| 4 | +# Time: O(n * (n + m)) |
| 5 | +# Space: O(n) |
| 6 | + |
| 7 | +from collections import deque |
| 8 | + |
| 9 | +class Solution: |
| 10 | + def magnificentSets(self, n: int, edges: List[List[int]]) -> int: |
| 11 | + adj_list = [[] for _ in range(n)] |
| 12 | + |
| 13 | + for edge in edges: |
| 14 | + adj_list[edge[0] - 1].append(edge[1] - 1) |
| 15 | + adj_list[edge[1] - 1].append(edge[0] - 1) |
| 16 | + |
| 17 | + colors = [-1] * n |
| 18 | + |
| 19 | + # Check if the graph is bipartite |
| 20 | + for node in range(n): |
| 21 | + if colors[node] != -1: |
| 22 | + continue |
| 23 | + # Start coloring from uncolored nodes |
| 24 | + colors[node] = 0 |
| 25 | + if not self._is_bipartite(adj_list, node, colors): |
| 26 | + return -1 |
| 27 | + |
| 28 | + # Calculate the longest shortest path for each node |
| 29 | + distances = [ |
| 30 | + self._get_longest_shortest_path(adj_list, node, n) |
| 31 | + for node in range(n) |
| 32 | + ] |
| 33 | + |
| 34 | + # Calculate the total maximum number of groups across all components |
| 35 | + max_number_of_groups = 0 |
| 36 | + visited = [False] * n |
| 37 | + |
| 38 | + for node in range(n): |
| 39 | + if visited[node]: |
| 40 | + continue |
| 41 | + # Add the number of groups for this component to the total |
| 42 | + max_number_of_groups += self._get_number_of_groups_for_component(adj_list, node, distances, visited) |
| 43 | + |
| 44 | + return max_number_of_groups |
| 45 | + |
| 46 | + # Checks if the graph is bipartite starting from the given node |
| 47 | + def _is_bipartite(self, adj_list, node, colors): |
| 48 | + for neighbor in adj_list[node]: |
| 49 | + |
| 50 | + # If the neighbor has the same color, the graph is not bipartite |
| 51 | + if colors[neighbor] == colors[node]: |
| 52 | + return False |
| 53 | + |
| 54 | + # If the neighbor is already colored, skip it |
| 55 | + if colors[neighbor] != -1: |
| 56 | + continue |
| 57 | + |
| 58 | + # Assign the opposite color to the neighbor |
| 59 | + colors[neighbor] = (colors[node] + 1) % 2 |
| 60 | + |
| 61 | + # Recursively check bipartiteness for the neighbor; return False if its fails |
| 62 | + if not self._is_bipartite(adj_list, neighbor, colors): |
| 63 | + return False |
| 64 | + |
| 65 | + # If all neighbors are properly colored, return True |
| 66 | + return True |
| 67 | + |
| 68 | + # Computes the longest shortest path (height) in the graph starting from the source node |
| 69 | + def _get_longest_shortest_path(self, adj_list, src_node, n): |
| 70 | + nodes_queue = deque([src_node]) |
| 71 | + visited = [False] * n |
| 72 | + visited[src_node] = True |
| 73 | + distance = 0 |
| 74 | + |
| 75 | + # Perform BFS layer by layer |
| 76 | + while nodes_queue: |
| 77 | + # Process all nodes in the current layer |
| 78 | + for _ in range(len(nodes_queue)): |
| 79 | + current_node = nodes_queue.popleft() |
| 80 | + |
| 81 | + # Visit all unvisited neighbors of the current node |
| 82 | + for neighbor in adj_list[current_node]: |
| 83 | + if visited[neighbor]: |
| 84 | + continue |
| 85 | + visited[neighbor] = True |
| 86 | + nodes_queue.append(neighbor) |
| 87 | + |
| 88 | + # Increment the distance for each layer |
| 89 | + distance += 1 |
| 90 | + |
| 91 | + return distance |
| 92 | + |
| 93 | + # Calculates the max number of groups for a connected component |
| 94 | + def _get_number_of_groups_for_component(self, adj_list, node, distances, visited): |
| 95 | + # Start with the distance of the current node as maximum |
| 96 | + max_number_of_groups = distances[node] |
| 97 | + visited[node] = True |
| 98 | + |
| 99 | + # Recursively calculate the maximum for all unvisited neighbors |
| 100 | + for neighbor in adj_list[node]: |
| 101 | + if visited[neighbor]: |
| 102 | + continue |
| 103 | + max_number_of_groups = max( |
| 104 | + max_number_of_groups, |
| 105 | + self._get_number_of_groups_for_component( |
| 106 | + adj_list, neighbor, distances, visited |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + return max_number_of_groups |
| 111 | + |
| 112 | + |
0 commit comments