Advanced maths

Bipartite graph test calculator with odd cycle

Find a two-color partition of an undirected graph without requiring parts in advance, or return an explicit odd cycle proving failure.

Your numbers

Labels are 1 through this count. Include isolated vertices.
One edge per row. Use distinct valid endpoints; no repeated unordered pairs. Leave empty for isolates.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A bipartite graph admits two vertex parts with every edge crossing between them. This calculator discovers the parts rather than assuming they were supplied. Breadth-first search colors each component by alternating colors across edges. If an edge joins two vertices with the same color, their parent-tree paths expose an odd cycle. The result returns either a complete valid partition or that closed cycle as a negative certificate. An incomplete coloring from a failed search is never presented as though it were a solution.

THE FORMULAA simple undirected graph is bipartite if and only if it contains no odd cycle.

A worked example

For square edges 1–2,2–3,3–4,4–1, one valid partition is A={1,3} and B={2,4}. Every edge crosses the parts, so the graph is bipartite. Adding diagonal 1–3 creates triangle 1–2–3–1, a closed walk of three edges. Alternating colors around those three edges returns to the start with the wrong color, proving that no two-part assignment can satisfy all edges. Swapping all A/B names in the original square gives another equally valid partition.

Good to know

Simple undirected graphs on 1–40 vertices with at most 200 edges. Repeated unordered pairs and self-loops are rejected by this page’s input grammar. Disconnected components are colored separately; isolates are assigned deterministically to part A. Part labels have no numeric ordering or substantive group meaning. This is a two-color feasibility test, not a maximum matching, proper coloring count, directed reachability analysis or test of an already supplied group classification.

Alternating along a breadth-first tree supplies the candidate parts

Choose a starting vertex in a component and give it color A. Its unseen neighbors receive color B, their unseen neighbors receive color A, and so on. A search tree always permits this alternation because a tree has no cycle. The important checks are the remaining edges: every one must connect opposite colors. A result is valid only after all components and all their edges have been processed.

When two same-colored vertices are joined, their search-tree paths meet at a common ancestor. Combining those paths with the conflicting edge produces an odd closed cycle. The displayed witness ends at its starting label, so its edge count is one less than the number of listed labels. Verify consecutive pairs against the original edge list. A vague statement that a conflict occurred would be weaker evidence than the actual cycle.

A four-cycle alternates between parts A and B, while a diagonal creates an odd cycle1234ABBAWithout diagonal: YesWith diagonal: NoOdd cycle: 1,2,3,1
Square edges 1–2,2–3,3–4,4–1 cross between A={1,3} and B={2,4}. The dashed diagonal 1–3 stays within A and creates triangle 1–2–3–1, which has odd length.

Part names are arbitrary and do not establish a matching

Each connected component can independently exchange its A and B names. Thus a disconnected graph can have many valid global partitions, all describing the same bipartite property. Isolated vertices can belong to either part because they have no edge to violate the crossing rule. The deterministic assignment here makes repeated runs reproducible; it is not a claim that one part is larger, more important or inherently the first group.

Bipartiteness also does not guarantee that every vertex can be paired. A star with one center and several leaves is bipartite, but at most one leaf can be matched to the center in a disjoint matching. The Maximum bipartite matching calculator from an edge list starts from two supplied parts and finds the largest feasible matching; this page first decides whether any valid parts exist. Keeping those tasks distinct prevents a positive two-color test from being mistaken for a perfect-assignment certificate.

A few questions, answered

Does a cycle always make a graph nonbipartite?

No. Even cycles admit alternating colors. An odd cycle is the obstruction; an acyclic graph is always bipartite.

Can I compare two proposed groups with this tool?

The tool finds its own valid partition when one exists. It does not preserve external group labels or validate a proposed assignment against a separate classification.

Find your calculator

Start typing to explore the library.