``` 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 ``` ```package algs41; import stdlib.*; import algs13.Stack; /* *********************************************************************** * Compilation: javac Bipartite.java * Dependencies: Graph.java * * Given a graph, find either (i) a bipartition or (ii) an odd-length cycle. * Runs in O(E + V) time. * * *************************************************************************/ public class Bipartite { private boolean isBipartite; // is the graph bipartite? private final boolean[] color; // color[v] gives vertices on one side of bipartition private final boolean[] marked; // marked[v] = true if v has been visited in DFS private final int[] edgeTo; // edgeTo[v] = last edge on path to v private Stack cycle; // odd-length cycle public Bipartite(Graph G) { isBipartite = true; color = new boolean[G.V()]; marked = new boolean[G.V()]; edgeTo = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { // color[v] = false; dfs(G, v); } } assert check(G); } private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) { // short circuit if odd-length cycle found if (cycle != null) return; // found uncolored vertex, so recur if (!marked[w]) { edgeTo[w] = v; color[w] = !color[v]; dfs(G, w); } // if v-w create an odd-length cycle, find it else if (color[w] == color[v]) { isBipartite = false; cycle = new Stack<>(); cycle.push(w); // don't need this unless you want to include start vertex twice for (int x = v; x != w; x = edgeTo[x]) { cycle.push(x); } cycle.push(w); } } } boolean isBipartite() { return isBipartite; } boolean color(int v) { return color[v]; } public Iterable cycle() { return cycle; } private boolean check(Graph G) { // graph is bipartite if (isBipartite) { for (int v = 0; v < G.V(); v++) { for (int w : G.adj(v)) { if (color[v] == color[w]) { System.err.format("edge %d-%d with %d and %d in same side of bipartition\n", v, w, v, w); return false; } } } } // graph has an odd-length cycle else { // verify cycle int first = -1, last = -1; for (int v : cycle()) { if (first == -1) first = v; last = v; } if (first != last) { System.err.format("cycle begins with %d and ends with %d\n", first, last); return false; } } return true; } public static void main(String[] args) { // create random bipartite graph with V vertices and E edges; then add F random edges args = new String [] { "200", "100", "20" }; int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); int F = Integer.parseInt(args[2]); Graph G = new Graph(V); int[] vertices = new int[V]; for (int i = 0; i < V; i++) vertices[i] = i; StdRandom.shuffle(vertices); for (int i = 0; i < E; i++) { int v = StdRandom.uniform(V/2); int w = StdRandom.uniform(V/2); G.addEdge(vertices[v], vertices[V/2 + w]); } // add F extra edges for (int i = 0; i < F; i++) { int v = (int) (Math.random() * V); int w = (int) (Math.random() * V); G.addEdge(v, w); } StdOut.println(G); Bipartite b = new Bipartite(G); if (b.isBipartite()) { StdOut.println("Graph is bipartite"); for (int v = 0; v < G.V(); v++) { StdOut.println(v + ": " + b.color(v)); } } else { StdOut.print("Graph has an odd-length cycle: "); for (int x : b.cycle()) { StdOut.print(x + " "); } StdOut.println(); } } } ```