Possible Bipartition

Solution For Possible Bipartition

Possible Bipartition is a graph problem on LeetCode. The problem statement is as follows:

Given a set of N people (numbered 1, 2, …, N), we would like to split everyone into two groups of any size.

Each person may dislike some other people, and they should not go into the same group.

Formally, if dislikes[i] = [a, b], it means it is not allowed to put the people numbered a and b into the same group.

Return true if and only if it is possible to split everyone into two groups in this way.

Solution:

This is a graph problem where we need to check if the graph can be divided into two disjoint sets such that there is no edge within the sets. We need to use the concept of bipartite graphs to solve this problem.

A bipartite graph is a graph whose vertices can be divided into two disjoint sets, U and V, such that every edge connects a vertex in U to one in V.

We can use the idea of color-coding to check if the graph is bipartite or not. We can assign colors to the vertices such that no two adjacent vertices have the same color.

We can start with any vertex and assign it a color, then we can assign a different color to all its neighbors. We can continue this process recursively for all the vertices. If at any point we find that two adjacent vertices have the same color, then the graph is not bipartite.

Algorithm:

  1. Create a graph with adjacency list from dislikes[].
  2. Initialize visited[] with false for all vertices.
  3. For every vertex, if it is not visited yet call dfs() function.
  4. At each step, assign the vertex the opposite color of its parent node or its initial color if it does not have a parent.
  5. If you encounter an adjacent vertex with the same color as the parent node, return false.
  6. If the dfs() function terminates successfully, return true.

Code:

Here is the Python code for the solution:

class Solution:
def possibleBipartition(self, N: int, dislikes: List[List[int]]) -> bool:
graph = [[] for _ in range(N+1)] for u, v in dislikes:
graph[u].append(v)
graph[v].append(u)
color = [0] * (N+1)
def dfs(node, c):
color[node] = c
for nei in graph[node]:
if color[nei] == c:
return False
if color[nei] == 0 and not dfs(nei, -c):
return False
return True
for i in range(1, N+1):
if color[i] == 0 and not dfs(i, 1):
return False
return True

Time Complexity: O(N+E), where N is the number of vertices and E is the number of edges.

Space Complexity: O(N+E), where N is the number of vertices and E is the number of edges.

Step by Step Implementation For Possible Bipartition

There are a few possible ways to solve this problem:

1) Use a graph data structure to represent the relationships between the people. Then, use a depth-first search or breadth-first search algorithm to try to find a way to split the graph into two groups such that there are no edges between members of different groups.

2) Use a Union-Find data structure to keep track of which people are in the same group. Start by putting each person in their own group. Then, iterate over the edges in the graph, and for each edge, union the two groups of the people connected by that edge. If at any point there is an edge between two people in the same group, then it is not possible to bipartite the graph, and the algorithm can return false. Otherwise, it will return true.
def possibleBipartition(N, dislikes):
        if not dislikes: return True
        # construct graph
        graph = collections.defaultdict(list)
        for u, v in dislikes:
            graph[u].append(v)
            graph[v].append(u)
        # color nodes
        color = {}
        def dfs(node, c = 0):
            if node in color:
                return color[node] == c
            color[node] = c
            return all(dfs(nei, c ^ 1) for nei in graph[node])
        return all(dfs(node) for node in range(1, N+1) if node not in color)
var possibleBipartition = function(N, dislikes) {
    
    // create a map of all the dislikes
    let map = {};
    for (let i = 0; i < dislikes.length; i++) {
        if (!(dislikes[i][0] in map)) {
            map[dislikes[i][0]] = [];
        }
        if (!(dislikes[i][1] in map)) {
            map[dislikes[i][1]] = [];
        }
        map[dislikes[i][0]].push(dislikes[i][1]);
        map[dislikes[i][1]].push(dislikes[i][0]);
    }
    
    // create a visited map to keep track of which nodes have been visited
    let visited = {};
    for (let i = 1; i <= N; i++) {
        visited[i] = false;
    }
    
    // create a queue and push the first node into it
    let queue = [];
    queue.push(1);
    
    // create a colors map to keep track of the colors of the nodes
    let colors = {};
    colors[1] = 0;
    
    // while the queue is not empty
    while (queue.length > 0) {
        // get the first node from the queue
        let node = queue.shift();
        
        // if the node has not been visited yet
        if (!visited[node]) {
            // mark the node as visited
            visited[node] = true;
            
            // get all the nodes that the current node dislikes
            let dislikeNodes = map[node];
            
            // if there are any dislike nodes
            if (dislikeNodes) {
                // for each dislike node
                for (let i = 0; i < dislikeNodes.length; i++) {
                    // get the node
                    let dislikeNode = dislikeNodes[i];
                    
                    // if the node has not been visited yet
                    if (!visited[dislikeNode]) {
                        // push the node into the queue
                        queue.push(dislikeNode);
                        
                        // set the color of the node to be the opposite color of the current node
                        colors[dislikeNode] = colors[node] === 0 ? 1 : 0;
                    }
                    // if the node has been visited and it has the same color as the current node
                    else if (colors[dislikeNode] === colors[node]) {
                        // return false since we cannot have two nodes with the same color that dislike each other
                        return false;
                    }
                }
            }
        }
    }
    
    // return true since we were able to successfully color all the nodes
    return true;
};
There are many possible solutions to this problem. One approach would be to use a depth-first search algorithm to check if the graph can be partitioned into two sets. If so, then return true. Otherwise, return false.

Another approach would be to use a breadth-first search algorithm to find the shortest path between two nodes. If the path exists, then return true. Otherwise, return false.
There are a number of ways to solve this problem. One approach would be to use a Depth First Search algorithm. Another approach would be to use a Breadth First Search algorithm.


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]