类 BipartiteX
- java.lang.Object
-
- edu.princeton.cs.algs4.BipartiteX
-
public class BipartiteX extends java.lang.Object
TheBipartiteX
class represents a data type for determining whether an undirected graph is bipartite or whether it has an odd-length cycle. The isBipartite operation determines whether the graph is bipartite. If so, the color operation determines a bipartition; if not, the oddCycle operation determines a cycle with an odd number of edges.This implementation uses breadth-first search and is nonrecursive. 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. See
Bipartite
for a recursive version that uses depth-first search.For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
构造器概要
构造器 构造器 说明 BipartiteX(Graph G)
Determines whether an undirected graph is bipartite and finds either a bipartition or an odd-length cycle.
-
方法概要
修饰符和类型 方法 说明 boolean
color(int v)
Returns the side of the bipartite that vertexv
is on.boolean
isBipartite()
Returns true if the graph is bipartite.static void
main(java.lang.String[] args)
Unit tests theBipartiteX
data type.java.lang.Iterable<java.lang.Integer>
oddCycle()
Returns an odd-length cycle if the graph is not bipartite, andnull
otherwise.
-
-
-
构造器详细资料
-
BipartiteX
public BipartiteX(Graph G)
Determines whether an undirected graph is bipartite and finds either a bipartition or an odd-length cycle.- 参数:
G
- the graph
-
-
方法详细资料
-
isBipartite
public boolean isBipartite()
Returns true if the graph is bipartite.- 返回:
true
if the graph is bipartite;false
otherwise
-
color
public boolean color(int v)
Returns the side of the bipartite that vertexv
is on.- 参数:
v
- the vertex- 返回:
- the side of the bipartition that vertex
v
is on; two vertices are in the same side of the bipartition if and only if they have the same color - 抛出:
java.lang.IllegalArgumentException
- unless0 <= v < V
java.lang.UnsupportedOperationException
- if this method is called when the graph is not bipartite
-
oddCycle
public java.lang.Iterable<java.lang.Integer> oddCycle()
Returns an odd-length cycle if the graph is not bipartite, andnull
otherwise.- 返回:
- an odd-length cycle if the graph is not bipartite
(and hence has an odd-length cycle), and
null
otherwise
-
main
public static void main(java.lang.String[] args)
Unit tests theBipartiteX
data type.- 参数:
args
- the command-line arguments
-
-