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
这里写图片描述
Harsh Bhasin, "Algorithms: Design and Analysis" English | ISBN: 0199456666 | 2015 | 692 pages Algorithms: Design and Analysis of is a textbook designed for the undergraduate and postgraduate students of computer science engineering, information technology, and computer applications. It helps the students to understand the fundamentals and applications of algorithms. The book has been divided into four sections: Algorithm Basics, Data Structures, Design Techniques and Advanced Topics. The first section explains the importance of algorithms, growth of functions, recursion and analysis of algorithms. The second section covers the data structures basics, trees, graphs, sorting in linear and quadratic time. Section three discusses the various design techniques namely, divide and conquer, greedy approach, dynamic approach, backtracking, branch and bound and randomized algorithms used for solving problems in separate chapters. The fourth section includes the advanced topics such as transform and conquer, decrease and conquer, number thoeretics, string matching, computational geometry, complexity classes, approximation algorithms, and parallel algorithms. Finally, the applications of algorithms in Machine Learning and Computational Biology areas are dealt with in the subsequent chapters. This section will be useful for those interested in advanced courses in algorithms. The book also has 10 appendixes which include topics like probability, matrix operations, Red-black tress, linear programming, DFT, scheduling, a reprise of sorting, searching and amortized analysis and problems based on writing algorithms. The concepts and algorithms in the book are explained with the help of examples which are solved using one or more methods for better understanding. The book includes variety of chapter-end pedagogical features such as point-wise summary, glossary, multiple choice questions with answers, review questions, application-based exercises to help readers test their understanding of the learnt concepts.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值