Giter Site home page Giter Site logo

algs's People

Contributors

ryanbhayward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

algs's Issues

fix_literal() skips over some clauses

When stepping through the program for the sake of my own understanding, I came across a nasty bug in the fix_literal() method in backsat.py.

The problem is on these lines:

algs/hard/sat/backsat.py

Lines 44 to 51 in 38b5341

for c in f:
if t in c:
f.remove(c)
elif -t in c:
c.remove(-t)
if len(c)==0: # empty clause so f unsatisfiable
print(' *- empty clause -*')
return ''

Essentially, there is a for loop iterating on a list whose order is modified during the for loop. In some cases this doesn't cause any problems but is generally considered bad practice. Python's for x in y loop syntax uses indexing in the background, so removing an element might shift a further unseen element to an index for which Python has explored already.

Specifically, when there are two elements side by side in the list, that both match the removal condition, the first one will be removed and the second one will be skipped. Running the following input to the function:

f = [[1, 4, -5], [1, 3, -4], [2, -3, -4], [3, 4, 5]]
fix_literal(1, f, '?????')

produces the following result:

[[1, 3, -4], [2, -3, -4], [3, 4, 5]]

instead of the expected result:

[[2, -3, -4], [3, 4, 5]]

I'm not exactly sure what issues this might cause when the broader program is considered, as I have not analyzed it to that extent at the moment. I would imagine some valid satisfying assignments are thrown out because of this bug, though I haven't confirmed this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.