

If this boy is paired with one girl, the other girl will have no boy to be paired with. Example Matching Scenarioįigure 4: No matching of girls and boys (Image by Author) When we cannot get a Matching?Īs shown in Figure 4, two girls can prefer the same boy and no other boys. Otherwise, the vertex is considered unmatched. If we consider a bipartite graph, the matching will consist of edges connecting one vertex in U and one vertex in V and each vertex (in U and V) has either zero or one edge incident to it.Ī vertex is considered as matched if it connects to one of the edges in the matching. In simple terms, a matching is a graph where each vertex has either zero or one edge incident to it. # imports import networkx as nx from networkx.algorithms import bipartite # Initialise graph B = nx.Graph() # Add nodes with the node attribute "bipartite" top_nodes = bottom_nodes = B.add_nodes_from(top_nodes, bipartite=0) B.add_nodes_from(bottom_nodes, bipartite=1) # Add edges only between nodes of opposite node sets B.add_edges_from() Matching of Bipartite GraphsĪ matching or independent edge set in an undirected graph is a set of edges without common vertices. Now let’s create the bipartite graph shown in Figure 1. If you do not have NetworkX installed on your machine, you can follow the instructions given here. Let’s see how we can represent a bipartite graph using NetworkX. Please note that if the graph has many connected components and each component is bipartite, then the graph is bipartite.Figure 1: Bipartite graph (Image by Author) So, the odd-level vertices will form one set, and the even-level vertices will form another.


Here, the vertex level is its minimum distance from the starting vertex v. If in the BFS, we find an edge, both of whose endpoints are at the same level, then the graph is not Bipartite, and an odd-cycle is found. To check if a given graph contains an odd-cycle or not, do a breadth–first search starting from an arbitrary vertex v. If a graph contains an odd cycle, we cannot divide the graph such that every adjacent vertex has a different color. A graph is a bipartite graph if and only if it does not contain an odd cycle. If there exists an edge connecting the current vertex to a previously colored vertex with the same color, then we can safely conclude that the graph is not bipartite.Ģ. While doing BFS traversal, each node in the BFS tree is given its parent’s opposite color. A graph is a bipartite graph if and only if it is 2–colorable. There are two ways to check for a bipartite graph:ġ. It is possible to test whether a graph is bipartite or not using a Breadth–first search (BFS) algorithm. The following graph is bipartite as we can divide it into two sets, U and V, with every edge having one endpoint in set U and the other in set V: A bipartite graph (or bigraph) 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. Given an undirected graph, check if it is bipartite or not.
