


Given an undirected graph of ‘V’ vertices (labeled 0,1,..., V-1) and ‘E’ edges . Your task is to check whether the graph is bipartite or not.
A bipartite graph is a graph whose vertices can be divided into two sets such that each edge of the graph connects one vertex from the first set and another vertex from the second set.
We can also define a bipartite graph as a graph which can be colored using two colors such that no two adjacent vertices have the same color.
For example:
Input:
4 4
0 1
0 2
1 3
2 3
An undirected graph to the above input is shown below:

In the given input, the number of vertices is 4, and the number of edges is 4.
In the input, following the number of vertices and edges, a list of pairs of numbers is given where each pair (u, v) denotes an edge between vertex u and v.
As per the input, there is an edge between vertex 0 and vertex 1.
The vertices 0 and 2 have an edge between them.
The vertices 1 and 3 have an edge between them.
The vertices 2 and 3 have an edge between them.
As the graph can be colored using two colors, and no adjacent vertices share the same color, the graph is bipartite.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two integers ‘V’ and ‘E’ denoting the number of vertices in the undirected graph and the number of edges in the undirected graph, respectively.
The next E lines contain two space-separated integers ‘u’, ‘v’, denoting two vertices connected by an edge.
Output format:
For each test case, return “True” (without quotes) if the given graph is bipartite else return “False”.
The output of each test case is printed in a separate line.
Note:
1. There are no parallel edges i.e no two vertices are directly connected by more than 1 edge.
2. The graph is necessarily undirected.
3. You are not required to print the output explicitly, it has already been taken care of. Just implement the function that checks whether the given graph is bipartite or not.
1 <= T <= 10
2 <= V <= 200
1 <= E <= V * (V - 1)/2
where ‘T’ is the number of test cases, ‘V’ is the number of vertices, ‘E’ is the number of edges.
1
4 4
0 1
0 2
2 3
1 3
True
The graph formed by the given input is shown in the above example. We can clearly see that the graph is colored using two colors only and no adjacent vertices have the same color. It means, we can divide the vertices of the graph in two sets such that each edge of the graph connects one vertex from the first set and another vertex from the second set.
First Set: { 0, 2 }
Second Set: { 1, 3 }
Hence, the given graph is bipartite and the answer is ‘True’.
1
4 5
0 1
0 2
1 2
1 3
2 3
False
The graph formed from the given input is shown below:
We can't color the vertex 2 by green as it is adjacent to the already green colored vertex 1. Thus, we can’t color the graph using only two colors. It means the graph is not bipartite.
Use the fact that a bipartite graph can be colored using 2 colors such that no two adjacent vertices have the same color. Can we use DFS to traverse the graph?
The idea is very simple. To separate the vertices of the graph in two sets such that no two vertices of a set are connected, we will try to color the vertices of the graph using two colors only.
If any of the adjacent vertices are of the same color, the graph is not bipartite. Else, it is bipartite.
Note that, if there are more than 1 connected components in the graph, the graph will be bipartite if and only if all the connected components are bipartite.
Algorithm:
O(V + E) where V is the number of vertices in the graph, and E is the number of edges in the graph.
We are creating an adjacency list which requires O(V + E) time. We are using depth first search for traversing the graph using adjacency list. Scanning for all adjacent vertices takes O(E) time, since the sum of lengths of adjacency lists of all vertices is E. Thus, the total time complexity is O(V + E) + O(V + E) = O(V + E).
O(V ^ 2), where V is the number of vertices in the graph.
We are using an adjacency list. In the worst case, the graph can be proper and the number of edges will be (V * (V - 1) / 2). Thus, the size of the adjacency list will be O(V ^ 2). Space required by the visited array, and the color array is O(V). Also, if the entire graph is traversed in a single recursive call, O(V) recursion stack space is used. Thus, the final space complexity is O(V ^ 2) + O(V) + O(V) = O(V ^ 2).