Stanford Algorithms Design and Analysis Part 2 week 6

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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 .

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 nxdef 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 isSatisfiableassert(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")



           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jffyuhgv/article/details/83888092