Backtracking algorithm

User Generated

Yhpvsre

Computer Science

Description

Unformatted Attachment Preview

Circuit-Satisfiability Assignment The text discusses circuit satisfiability, an important topic in NP-completeness. The basic idea is to simulate a circuit, outputting the assignments to input variables where the circuit output is true. The text Figure 34.8 is equivalent to: ((¬x3∧ x1∧ x2)∨¬x3)∧ ((x1∨x2)∨¬x3)∧ (¬x3∧ x1∧ x2) Satisfied by the assignment x1=true, x2=true, x3=false The brute force approach would make all possible Boolean assignments to input variables, outputting the assignments where the output were true. The following would do the trick: for x1 ∈ {false, true} do for x2 ∈ {false, true} do for x3 ∈ {false, true} do if ((¬x3∧ x1∧ x2)∨¬x3)∧ ((x1∨x2)∨¬x3)∧ (¬x3∧ x1∧ x2) then print x1, x2, x3 However, backtracking can often reduce the complexity which in above algorithm is Θ(2n), where n = number of Boolean variables. Assignment 1) Implement a backtracking algorithm to print all assignments satisfying the expression: (w∨¬x∨¬z) ∧ (¬x∨y) ∧ (¬y∨¬z) ∧ (¬w∨¬x∨y∨z) ∧ (¬v∨¬w∨¬x∨y∨z) ∧ (v∨w) ∧ (¬v∨w∨z) Not is ¬ Or is ∨ And is ∧ Your algorithm must be patterned after backtracking examples from the notes discussed in class, similar to the Generic Backtracking Algorithm below. Generic Backtracking Algorithm Backtrack( X[1..i] ) -- pre: X[1..i] specifies first i components of a solution -- post: Solution X[1..i] -Si+1 is solution i+1 1. if X[1..i] is a solution then print X[1..i] All variables assigned // 2. else 3. for each x ∈ Si+1 do // x ∈ {0, 1} 4. if x is consistent with X[1..i] and valid option for problem constraints then // Circuit with X[i+1]=x evaluates to true 5. X[i+1] ← x 6. print(X[1..i] -> X[1..i+1]); // Print partial solution for debugging 7. Backtrack( X[1..i+1] ) // Generate next sub-solution 2) Draw the recursion tree similar to below. To assist in drawing the recursion tree, Line 6 prints the partial solution at each step. For example, assuming X[1..i] assignments of {true, false} and X[1..i+1] assignments of {true,false,true}, Line 6 would print: {true, false} -> {true,false,true} Using 0=false and 1=true, the tree explored for ((¬x3∧ x1∧ x2)∨¬x3) ∧ ((x1∨x2)∨¬x3) ∧ (¬x3∧¬x2) is below. When the circuit is determined to be not satisfiable (e.g. cannot be true), the algorithm backtracks. In the figure below, the 000 represents exploring (¬x3∧¬x2) where x1 is not a factor but x2=0 and x3=0 is required. There are two complete solutions listed below. No partial solutions are displayed and backtracked over that do not lead to a complete solution. For example, x1=0 is part of a complete solution and is displayed while x1=0 and x2=1 is not part of a solution so is not displayed. Note: To draw many of the recursion trees presented in class, I use a graphics program named dot or Graphviz, which is freely available. The program, backtracking in this case, while generating the recursion tree, prints the corresponding space as graphics commands to a file from which dot generates the graphics. The dot drawing file for the above recursion tree is: digraph g { rank=source; compound=true; size="12,4"; n [shape=none label="" fontsize=48 ]; n -> n0 [weight=12 label="x1=0" color=red fontsize=48 ]; n0 [shape=none label="0" fontsize=48 ]; n0 -> n01 [weight=12 label="x2=1" color=red fontsize=48 ]; n01 [shape=none label="01" fontsize=48 ]; n -> n1 [weight=12 label="x1=1" color=red fontsize=48 ]; n1 [shape=none label="1" fontsize=48 ]; n1 -> n11 [weight=12 label="x2=1" color=red fontsize=48 ]; n11 [shape=none label="11" fontsize=48 ]; n11 -> n110 [weight=12 label="x3=0" color=red fontsize=48 ]; n110 [shape=none label="110" fontsize=48 ]; } This beats drawing the tree by hand and is a good debugging tool, allowing you to quickly visualize the recursion generated. Hints 1. Decompose the expression into clauses, which contain only OR and NOTs such as: (w∨¬x∨¬z) 2. Implement a function for each expression that returns true when any variable is unassigned (neither true or false) or when assignments evaluate to true (w=true, x=false, z=false). 3. Implement a function that ANDs the result of each expression from 2. for the current variable assignments.
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Related Tags