Celebrity Feuds - codepath/compsci_guides GitHub Wiki

Unit 10 Session 2 Standard (Click for link to problem statements)

Unit 10 Session 2 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 25-35 mins
  • 🛠️ Topics: Graph Traversal, DFS, Bipartite Graph

1: U-nderstand

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?
  • Q: What does the dislikes array represent?
    • A: Each pair [a, b] indicates that celebrity a and celebrity b dislike each other and cannot be placed in the same group.
  • Q: How should the celebrities be split?
    • A: The goal is to split the celebrities into two groups such that no pair of celebrities who dislike each other are in the same group.
  • Q: What should be returned if it is not possible to split the celebrities?
    • A: The function should return False if it is impossible to split the celebrities into two groups.
HAPPY CASE
Input: 
```python
dislikes_1 = [1, 2], [1, 3], [2, 4](/codepath/compsci_guides/wiki/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](/codepath/compsci_guides/wiki/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.

2: M-atch

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:

  • Graph Coloring: The problem asks if it is possible to color the graph (representing celebrities and their dislikes) with two colors such that no two adjacent nodes (celebrities) have the same color.
  • Depth First Search (DFS): DFS can be used to attempt to color each connected component of the graph, ensuring that no two adjacent celebrities have the same color.

3: P-lan

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

  • Forgetting to check for disconnected components in the graph, which could cause incomplete traversals.
  • Not handling the case where a celebrity dislikes themselves, though the problem assumes no such input.

4: I-mplement

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

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Input:
dislikes_1 = [1, 2], [1, 3], [2, 4](/codepath/compsci_guides/wiki/1,-2],-[1,-3],-[2,-4)
dislikes_2 = [1, 2], [1, 3], [2, 3](/codepath/compsci_guides/wiki/1,-2],-[1,-3],-[2,-3)

print(can_split(4, dislikes_1))  # Expected output: True
print(can_split(3, dislikes_2))  # Expected output: False
  • Output:
True
False

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: 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.
  • Space Complexity: O(V + E) for storing the graph and the color array.