Stanford Algorithms: Design and Analysis, Part 2[week 6]

Problem Set-6



Programming Assignment-6

Question 1

In this assignment you will implement one or more algorithms for the 2SAT problem. Here are 6 different 2SAT instances:#1 #2 #3 #4 #5 #6.

The file format is as follows. In each instance, the number of variables and the number of clauses is the same, and this number is specified on the first line of the file. Each subsequent line specifies a clause via its two literals, with a number denoting the variable and a "-" sign denoting logical "not". For example, the second line of the first data file is "-16808 75250", which indicates the clause  ¬x16808x75250 .

Your task is to determine which of the 6 instances are satisfiable, and which are unsatisfiable. In the box below, enter a 6-bit string, where the ith bit should be 1 if the ith instance is satisfiable, and 0 otherwise. For example, if you think that the first 3 instances are satisfiable and the last 3 are not, then you should enter the string 111000 in the box below.

DISCUSSION: This assignment is deliberately open-ended, and you can implement whichever 2SAT algorithm you want. For example, 2SAT reduces to computing the strongly connected components of a suitable graph (with two vertices per variable and two directed edges per clause, you should think through the details). This might be an especially attractive option for those of you who coded up an SCC algorithm for Part I of this course. Alternatively, you can use Papadimitriou's randomized local search algorithm. (The algorithm from lecture might be too slow, so you might want to make one or more simple modifications to it to ensure that it runs in a reasonable amount of time.) A third approach is via backtracking. In lecture we mentioned this approach only in passing; see the DPV book, for example, for more details.

from Utils import *
import networkx as nx


def process(filename):
    clauses, literalCount = read(filename)

    G=nx.DiGraph()
    G.add_nodes_from(range(1, literalCount + 1) + range(-literalCount, 0))

    for clause in clauses:
        G.add_edge(-clause[0], clause[1])
        G.add_edge(-clause[1], clause[0])

    print "Graph creation for %s completed" % filename

    components = nx.strongly_connected_components(G)

    print "Calculation of SSC for %s completed" % filename

    isSatisfiable = True
    for component in filter(lambda component : len(component) > 1, components):
        isSatisfiableComponent = True
        vertexes = set()
        for vertex in component:
            if -vertex in vertexes:
                isSatisfiableComponent = False
                break
            vertexes.add(vertex)
        if not isSatisfiableComponent:
            isSatisfiable = False
            break

    print "%s satisfiable result: %s" % (filename, isSatisfiable)
    return isSatisfiable

assert(process("2sat_test1_true.txt") == True)
assert(process("2sat_test2_true.txt") == True)
assert(process("2sat_test3_false.txt") == False)

process("2sat1.txt")
process("2sat2.txt")
process("2sat3.txt")
process("2sat4.txt")
process("2sat5.txt")
process("2sat6.txt")



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值