2SAT是P问题,能在多项式时间内解决。这里对方法之一进行总结。
测试数据格式如下:
1000000
-383037 -585864
575603 -494722
-628477 805085
-502855 685868
923336 -606921
-653863 424389
333542 -656925
-809061 874253
即范围为1~1000000,“-”号表示取逆。比如-383037 -585864表示~383037U~585864,575603 -494722表示575603U~494722
判断是否存在一种取法,能够满足上述所有的clause。
解法是用图论中的强连通子图。具体来说,首先构造一个有向图。每个数字和它的逆都是一个单独的节点。对于每个条目,如AUB,则在图中加入两条边:~A->B和~B->A。即A如果为负,则B为正;反之,如果B为负,则A为正。这两条边和AUB是等效的,都制约了A和B的取值。构造好有向图后就构建强连通子图。一个强连通子图中的变量相互制约,必须同时成立。
所以,如果有一对变量A和~A在同一个强连通子图中,则该2SAT无法满足。因为变量A和~A是无法同时成立的。
public class TwoSAT { public static void main(String[] args) { In in = new In(args[0]); int N = in.readInt(); int V = 2 * N; Digraph digraph = new Digraph(V); while (!in.isEmpty()) { int v = in.readInt(); int w = in.readInt(); int negv = 2 * Math.abs(v) - 1, posv = 2 * Math.abs(v) - 2; int negw = 2 * Math.abs(w) - 1, posw = 2 * Math.abs(w) - 2; digraph.addEdge(v > 0 ? negv : posv, w > 0 ? posw : negw); digraph.addEdge(w > 0 ? negw : posw, v > 0 ? posv : negv); } StdOut.println("input done."); KosarajuSharirSCC scc = new KosarajuSharirSCC(digraph); boolean satisfied = true; for (int i = 0; i < N; ++i) { if (scc.stronglyConnected(2 * i, 2 * i + 1)) { satisfied = false; break; } } if (satisfied) { StdOut.println(true); } else { StdOut.println(false); } } }
/************************************************************************* * Compilation: javac KosarajuSharirSCC.java * Execution: java KosarajuSharirSCC filename.txt * Dependencies: Digraph.java TransitiveClosure.java StdOut.java In.java * Data files: http://algs4.cs.princeton.edu/42directed/tinyDG.txt * * Compute the strongly-connected components of a digraph using the * Kosaraju-Sharir algorithm. * * Runs in O(E + V) time. * * % java KosarajuSCC tinyDG.txt * 5 components * 1 * 0 2 3 4 5 * 9 10 11 12 * 6 * 7 8 * * % java KosarajuSharirSCC mediumDG.txt * 10 components * 21 * 2 5 6 8 9 11 12 13 15 16 18 19 22 23 25 26 28 29 30 31 32 33 34 35 37 38 39 40 42 43 44 46 47 48 49 * 14 * 3 4 17 20 24 27 36 * 41 * 7 * 45 * 1 * 0 * 10 * *************************************************************************/ public class KosarajuSharirSCC { private boolean[] marked; // marked[v] = has vertex v been visited? private int[] id; // id[v] = id of strong component containing v private int count; // number of strongly-connected components public KosarajuSharirSCC(Digraph G) { // compute reverse postorder of reverse graph DepthFirstOrder dfs = new DepthFirstOrder(G.reverse()); // run DFS on G, using reverse postorder to guide calculation marked = new boolean[G.V()]; id = new int[G.V()]; for (int v : dfs.reversePost()) { if (!marked[v]) { dfs(G, v); count++; } } // check that id[] gives strong components assert check(G); } // DFS on graph G private void dfs(Digraph G, int v) { marked[v] = true; id[v] = count; for (int w : G.adj(v)) { if (!marked[w]) dfs(G, w); } } // return the number of strongly connected components public int count() { return count; } // are v and w strongly connected? public boolean stronglyConnected(int v, int w) { return id[v] == id[w]; } // id of strong component containing v public int id(int v) { return id[v]; } // does the id[] array contain the strongly connected components? private boolean check(Digraph G) { TransitiveClosure tc = new TransitiveClosure(G); for (int v = 0; v < G.V(); v++) { for (int w = 0; w < G.V(); w++) { if (stronglyConnected(v, w) != (tc.reachable(v, w) && tc.reachable(w, v))) return false; } } return true; } public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); KosarajuSharirSCC scc = new KosarajuSharirSCC(G); // number of connected components int M = scc.count(); StdOut.println(M + " components"); // compute list of vertices in each strong component Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M]; for (int i = 0; i < M; i++) { components[i] = new Queue<Integer>(); } for (int v = 0; v < G.V(); v++) { components[scc.id(v)].enqueue(v); } // print results for (int i = 0; i < M; i++) { for (int v : components[i]) { StdOut.print(v + " "); } StdOut.println(); } } }