**Objective: **Given a graph represented by the adjacency matrix, write a Depth-First Search(DFS) algorithm to check whether the graph is bipartite or not.

**Bipartite Graphs** OR **Bigraphs** is a graph whose vertices can be divided into two independent groups or sets so that for every edge in the graph, each end of the edge belongs to a separate group. There should not be any edge where both ends belong to the same set. Please read “Introduction to **Bipartite Graphs** OR **Bigraphs”.**

**Example:**

Please read the following recommended articles before continue

**Approach:** Coloring of vertices – Check if Graph Two-Colorable

Choose three colors- ** RED, GREEN, WHITE**. Let’s say two sets are

**and**

*RED***and for the graph to be bipartite, for each edge one end has to be**

*GREEN***and another end has to be**

*RED***.**

*GREEN*- Initially color all the vertices in
.*WHITE* - Pick a vertex with color
, now color it with*WHITE*. Now check all the neighbors of the vertex. If these neighbors are in*RED*color, color these in*WHITE*, after that check neighbors of neighbors, If these neighbors are in*GREEN*color, color them in*WHITE*and so on, color the neighbors in alternate of*RED*and*RED*colors. Use Depth-First Search for this traversal.*GREEN* - Do the step above until all the vertices are in either
or*RED*color if that happens means graph is bipartite.*GREEN* - During this process, if you find a neighbor vertex which is already colored in the same color as the current vertex or in other words you find an edge where both vertices are in the same color, stop the further process since the graph is not bipartite.
- In this problem graph is represented as an adjacency matrix, graph[][]. So for example, if traversing from vertex
**u**the neighbor vertex**v**, checkfor the edge.*graph[u][v]=1*

**Time Complexity**– O(V^{2})

We will improve the complexity using the Adjacency List. Click here – coming soon

**Code:**

**Output:**

Graph is Bipartite: false -------------------------- Graph is Bipartite: true