public class Bipartite extends java.lang.Object
This implementation uses depth-first search. The constructor takes time proportional to V + E (in the worst case), where V is the number of vertices and E is the number of edges. Afterwards, the isBipartite and color operations take constant time; the oddCycle operation takes time proportional to the length of the cycle.
For additional documentation, see Section 4.1 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
Bipartite(Graph G)
Determines whether an undirected graph is bipartite and finds either a
bipartition or an odd-length cycle.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
color(int v)
Returns the side of the bipartite that vertex v is on.
|
boolean |
isBipartite()
Is the graph bipartite?
|
static void |
main(java.lang.String[] args)
Unit tests the Bipartite data type.
|
java.lang.Iterable<java.lang.Integer> |
oddCycle()
Returns an odd-length cycle if the graph is not bipartite, and
null otherwise.
|
public Bipartite(Graph G)
G - the graphpublic boolean isBipartite()
public boolean color(int v)
java.lang.UnsupportedOperationException - if this method is called when the graph
is not bipartitepublic java.lang.Iterable<java.lang.Integer> oddCycle()
public static void main(java.lang.String[] args)