Unit 10 Session 2 Standard (Click for link to problem statements)
Unit 10 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
dislikes
array represent?
[a, b]
indicates that celebrity a
and celebrity b
dislike each other and cannot be placed in the same group.False
if it is impossible to split the celebrities into two groups.HAPPY CASE
Input:
```python
dislikes_1 = [[1, 2], [1, 3], [2, 4]]
n = 4
```
Output:
```markdown
True
Explanation: The celebrities can be split into two groups: Group 1: [1, 4], Group 2: [2, 3]. No celebrity dislikes another celebrity in their own group.
```
EDGE CASE
Input:
```python
dislikes_2 = [[1, 2], [1, 3], [2, 3]]
n = 3
```
Output:
```markdown
False
Explanation: It is impossible to split the celebrities into two groups such that no two celebrities in the same group dislike each other.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Graph Bipartition problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Build a graph where each celebrity is a node, and each pair of celebrities who dislike each other is an edge. Use DFS to attempt to color the graph with two colors (representing the two groups). If the graph can be colored in such a way that no two adjacent nodes have the same color, return True
. Otherwise, return False
.
1) Create an adjacency list representation of the graph from the `dislikes` array.
2) Initialize a `color` array to keep track of the color of each celebrity. Use `0` for uncolored, `1` for one color, and `-1` for the other color.
3) Define a DFS function to attempt to color the graph:
a) Color the current celebrity.
b) For each connected celebrity (neighbor), try to color them with the opposite color.
c) If a neighbor is already colored with the same color as the current celebrity, return `False`.
4) For each uncolored celebrity, start a DFS traversal. If the graph cannot be colored, return `False`.
5) If the entire graph is successfully colored, return `True`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def can_split(n, dislikes):
# Create the adjacency list for the graph
graph = {i: [] for i in range(1, n+1)}
for a, b in dislikes:
graph[a].append(b)
graph[b].append(a)
# Color array: 0 means uncolored, 1 and -1 represent the two colors
color = [0] * (n + 1)
# DFS function to attempt to color the graph
def dfs(node, current_color):
color[node] = current_color
for neighbor in graph[node]:
if color[neighbor] == 0: # If not colored, color with the opposite color
if not dfs(neighbor, -current_color):
return False
elif color[neighbor] == current_color: # If same color, graph is not bipartite
return False
return True
# Try to color each component
for celebrity in range(1, n+1):
if color[celebrity] == 0: # If the celebrity is uncolored
if not dfs(celebrity, 1): # Start with color 1
return False
return True
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
dislikes_1 = [[1, 2], [1, 3], [2, 4]]
dislikes_2 = [[1, 2], [1, 3], [2, 3]]
print(can_split(4, dislikes_1)) # Expected output: True
print(can_split(3, dislikes_2)) # Expected output: False
True
False
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(V + E)
, where V
is the number of celebrities (vertices) and E
is the number of dislikes (edges). Each celebrity and connection is visited once in the DFS traversal.O(V + E)
for storing the graph and the color array.