{"id":16660110,"url":"https://github.com/littleredcomputer/dancinglinks","last_synced_at":"2025-04-09T18:43:00.021Z","repository":{"id":81764768,"uuid":"116205427","full_name":"littleredcomputer/dancinglinks","owner":"littleredcomputer","description":"Knuth's Dancing Links algorithm in Java","archived":false,"fork":false,"pushed_at":"2018-09-04T23:50:31.000Z","size":388,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T20:43:45.627Z","etag":null,"topics":["combinatorial","combinatorial-search","dancing-links","exact-cover","knuth","search","taocp"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/littleredcomputer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-04T02:36:37.000Z","updated_at":"2021-11-20T21:28:06.000Z","dependencies_parsed_at":"2023-03-12T14:00:42.708Z","dependency_job_id":null,"html_url":"https://github.com/littleredcomputer/dancinglinks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littleredcomputer%2Fdancinglinks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littleredcomputer%2Fdancinglinks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littleredcomputer%2Fdancinglinks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littleredcomputer%2Fdancinglinks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/littleredcomputer","download_url":"https://codeload.github.com/littleredcomputer/dancinglinks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248090373,"owners_count":21046074,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["combinatorial","combinatorial-search","dancing-links","exact-cover","knuth","search","taocp"],"created_at":"2024-10-12T10:28:00.805Z","updated_at":"2025-04-09T18:43:00.013Z","avatar_url":"https://github.com/littleredcomputer.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knuth7: Dancing Links and SAT solvers in Java\n\nImplementation of algorithms found in Knuth's fascicles \n[5C](https://www.cs.stanford.edu/~knuth/fasc5c.ps.gz) and\n[6](https://www.amazon.com/Art-Computer-Programming-Fascicle-Satisfiability/dp/0134397606),\nwhich will both become part of chapter 7 of \n[The Art of Computer Programming](https://en.wikipedia.org/wiki/The_Art_of_Computer_Programming).\n\n## Exact Cover\n\nThe text describes variants of the Dancing Links algorithm, which can be used to solve the\nExact Cover (XC) problem. One has a set of *items*, together with a set of *options* (which are \neach nonempty subsets of the items), and asks whether there is a subset of the options which\nwill *exactly cover* the items (in the sense that each item occurs exactly once in the \nchosen options).\n\nKnuth proposes a simple text representations for XC problems which this code can read:\nThe first line lists the items, separated by whitespace; each following line is an option,\nconsisting of whitespace-separated items.\n\nFor example, the problem specification\n\n```\na b c d e f g\nc e\na d g\nb c f \na d f\nb g \nd e g\n```\n\nis solved if we select the first, fourth and fifth options (`c e`, `a d f`, `b g`) and in \nfact this is the only solution.\n\nIn code, we can say:\n\n``` java\nExactCoverProblem p = ExactCoverProblem.parseFrom(\"a b c d e f g\\nc e\\na d g\\nb c f\\na d f\\nb g\\nd e g\");\n```\n\nThen `p.solutions()` is a `Stream\u003cList\u003cInteger\u003e\u003e` which will have one element: the list `[3, 4, 0]`, \nindicating which options (counting from zero) form the solution.\n\nThe stream is lazy: calling `solutions()` only pays the cost of setting up the problem tableau. \nEach demand for a new element of the stream will provoke a search for the next distinct solution,\nwhich will stop as soon as one is found. The search for solutions will be exhaustive only if you consume\nthe whole stream.\n\nAdapters are provided for a few applications of XC that Knuth proposes.\n\n### Sudoku\n\nYou can supply a Sudoku problem in the form of a string of 81 digits or dots (indicating an empty\ncell), and then generate a stream of solutions:\n\n``` java\nString ex28c = \".3. .1. ... \" +\n                \"... 4.. 1..\" +\n                \".5. ... .9.\" +\n                \"2.. ... 6.4\" +\n                \"... .35 ...\" +\n                \"1.. ... ...\" +\n                \"4.. 6.. ...\" +\n                \"... ... .5.\" +\n                \".9. ... ...\";\nSudoku.fromBoardString(ex28c).solutions().collect(Collectors.toList())                \n// [\"934 518 267 762 493 185 851 762 493 285 971 634 649 235 718 173 846 529 418 659 372 327 184 956 596 327 841 \",\n//  \"934 517 268 862 493 175 751 862 493 275 981 634 649 235 817 183 746 529 417 659 382 328 174 956 596 328 741 \"]\n```\n\nThe solver is fast; it can generate an answer within about 15ms.\n\n### Word Find\n\nYou can request the generation of word find puzzles. Just supply the grid width and height and \na list of words to fit in the grid. Words can appear in any of the 8 orientations. The wrapper\ngenerates a stream of solutions as strings (using newlines to separate rows of the grid).\n\n``` java\nnew WordFind(8, 8, Arrays.asList(\"gandalf\", \n                                 \"frodo\", \n                                 \"boromir\", \n                                 \"merry\", \n                                 \"pippin\", \n                                 \"aragorn\", \n                                 \"samwise\", \n                                 \"legolas\", \n                                 \"gimli\", \n                                 \"gollum\", \n                                 \"ring\", \n                                 \"sauron\")).solutions().limit(1)\n```\ncreates a stream which will produce the solution\n```\nG A N D A L F B\nS N Y R R E M O\nA P I F A G U R\nM I L R G O L O\nW P M O O L L M\nI P I D R A O I\nS I G O N S G R\nE N S A U R O N\n```\nA dot character is used for cells that are unused after the packing is achieved; thus in this case we\ncan see that every cell in the grid participates in the puzzle.\n\n### Secondary Items and Colored Options\n\nSecondary items (which must occur *at most* once in any solution, instead of *exactly once* are \nsupported. In the item line (i.e., the first line), you can have a lone semicolon which will \ndivide the primary from the secondary items (there should be whitespace on either side of the \nsemicolon), like: `a b c ; x y`.\n\nA secondary item within an option my be given a color using a colon suffix, like `b c x:Red`. The XCC \nalgorithm implemented here will ensure that all options chosen for a solution will agree on \nthe \"color\" of any secondary items included.\n\n## Satisfiability\n\nCurrently we implement algorithms A, B and D, which are enough for small problems. We implement\n[DIMACS format](http://people.sc.fsu.edu/~jburkardt/data/cnf/cnf.html)\nfor SAT problem input. More to follow.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flittleredcomputer%2Fdancinglinks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flittleredcomputer%2Fdancinglinks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flittleredcomputer%2Fdancinglinks/lists"}