Is Bipartite Graph Problem

You are provided with an undirected graph having 'N' nodes numbered from 0 to 'N-1'. There is a list 'EDGES' of size 'M', which contains all the edges of this graph. The task is to determine if the graph is Bipartite.

Input:
The input starts with an integer 'T' which represents the number of test cases. Then follow 'T' test cases.
Each test case begins with two integers 'N' and 'M', indicating the number of nodes and edges in the graph.
Subsequently, there are 'M' lines, each containing two integers 'EDGES[i][0]' and 'EDGES[i][1]', denoting an undirected edge between the nodes 'EDGES[i][0]' and 'EDGES[i][1]'.
Output:
For each test case, output 'Yes' if the graph is Bipartite, otherwise output 'No'.
Each result is printed on a new line for each test case.

Example:

Input:
N = 4, M = 5
EDGES = [[0, 1], [0, 3], [1, 2]]

Output:
Yes
Explanation:

The graph can be partitioned into two sets:
setA = [0, 2]
setB = [1, 3]
Each edge connects a node from setA to a node from setB, making it a bipartite graph.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 500
  • 1 <= M <= (N * (N - 1)) / 2

Note: The graph has no self-edges or parallel edges and may not be connected. A bipartite graph can be divided into two sets where each edge connects a node from one set to a node from the other set.

Be the first one to answer
Add answer anonymously...
MindTickle Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter