Bipartite Graph in PythonIn this tutorial, we will learn about bipartite graphs and the ways to check if a given graph is bipartite or not. A bipartite graph is one whose vertices can be separated into two independent sets. Let the two sets be S1 and S2. The two sets will contain elements s1 and s2, where s1 and s2 are connected through an edge in the graph. Hence, the elements (s1, s2) are the edges connecting a vertex from the set S1 to the vertex of the second set s2. Also, the elements of a set cannot have an edge connecting each other. A graph is called a bipartite graph if there is a way to give two colors to the vertices of the graph such that all the pairs of adjacent nodes have different colors. A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color. Note that it is possible to color a cycle graph with an even cycle using two colors. For example, see the following graph. It is not possible to color a cycle graph with an odd cycle using two colors. Algorithm to check if a graph is Bipartite: The simple way to check if the graph is bipartite or not is to give each node a color. We will have a palette of two colors, and we color each node with an alternative color. If we can color each node with no adjacent nodes having the same color, then the graph is bipartite; otherwise, it is not. We will give the current vertex color and give the alternate color to each of its neighbors. This way, we will traverse each vertex and color the nodes. If, at any point, we find that the parent node and the adjacent node have the same color, we will stop the algorithm and print that the given graph is not bipartite. Code Output The given graph is not a Bipartite graph Time Complexity: We are traversing through the elements of the adjacency matrix of size V * V. In the worst-case scenario, we will traverse each element of the matrix; hence, the time complexity will be O(V * V). Space Complexity: We are creating a queue and an array to store the color of the vertices. Hence, the space complexity is O(V). The algorithm we have created will only work for a Singly Connected graph. If the graph has multiple components, then only one component will be traversed. We need to modify the code so that it works for multiple components. We will start from source 0, and we know that the connected nodes will be visited. A graph with no edge is a Bipartite graph. This is because, in the bipartite graph, a node of a single set should not be connected to itself. We will modify the code to handle these cases. The BFS traversal will be called for each component of the graph until all the nodes of the graph are visited. Code Output The given graph is not a Bipartite graph Time complexity: We are traversing the total number of vertices and edges. Hence, the time complexity is O(V + E). Space Complexity: We have created an array to store the visited nodes. Hence, the space complexity is O(V). In the above examples, the graph was represented by an adjacency matrix. What if an adjacency list represents the graph? Hence, now we will see the BFS traversal on the adjacency list. The time complexity of this algorithm will be O(V + E). This is a general algorithm that will work for both Singly connected and Multi-Connected graphs. Code Output The graph is a bipartite graph Time Complexity: The time complexity of this approach is same as the previous algorithm, i.e., O(V * V). Space Complexity: We have created an array to store the visited nodes. Hence, the space complexity is O(V). Let us see how to check if the graph is bipartite or not if the adjacency list of the graph is given Code Output The graph is a bipartite graph Time Complexity: If we traverse the graph given in the form of the adjacency list the time complexity reduces to O(V + E), here V is the number of vertices and E is the number of edges present in the graph. Space Complexity: We have created an array to store the visited nodes. Hence, the space complexity is O(V). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India