Skip to main content

Backtracking Python package

Project description

BackTracker Class

The BackTracker class is a Python implementation of the backtracking algorithm, designed for solving problems where a sequence of decisions leads to a solution. This generic framework can be adapted to various problems, such as puzzles, crosswords, combinatorial problems, etc.

This is based on Steven Skiena.

GitHub Page: https://github.com/ryan-najafi/backtracker

Features

Generic backtracking implementation. Customizable for a wide range of problems. Allows defining problem-specific solution processing, candidate generation, and move making/unmaking.

Installation

pip install backtracker==0.0.7

Usage

To use the BackTracker class, you need to define functions for:

Checking if a current state is a solution (is_solution). Processing a complete solution (process_solution). Generating candidate moves (get_candidates). Making a move (make_move). Unmaking a move (unmake_move).

Here's an example demonstrating how to use the BackTracker class to generate combinations of elements:

from backtracker import backtracker as bt

from backtracker import BackTracker

a = []
level = 0

def is_solution(a, k, input):
    return len(a) == len(input)

def process_solution(a, k, input):
    result = []
    for i, item in enumerate(a):
        if item:
            result.append(input[i])
    return ''.join(result)

def get_candidates(a, k, input):
    return [False, True]

def make_move(a, k, input, c):
    a.append(c)

def unmake_move(a, k, input, c):
    a.pop()

input = list('abc')
back_tracker = BackTracker(a=a, level=0, input=input, 
                           is_solution=is_solution, 
                           process_solution=process_solution, 
                           get_candidates=get_candidates, 
                           make_move=make_move, 
                           unmake_move=unmake_move)

for sol in back_tracker.get_next_solution():
    print(sol)
  

In this example, the BackTracker class is used to generate all combinations of the elements in the list 'abc'. The is_solution, process_solution, get_candidates, make_move, and unmake_move functions are defined to suit this specific problem.

Contributing

Contributions to enhance BackTracker are welcome. Please adhere to the standard Python coding guidelines.

License

This project is open-source and available under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backtracker-0.0.7.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

backtracker-0.0.7-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file backtracker-0.0.7.tar.gz.

File metadata

  • Download URL: backtracker-0.0.7.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for backtracker-0.0.7.tar.gz
Algorithm Hash digest
SHA256 0b2b88f4e8829612fd7ca6ab7ff413c61915b54e05d4210380d06dbbd2b2bdc0
MD5 b1760629bf675cb00bc001b0246d40f2
BLAKE2b-256 76797386b4be76c2ce25ee045d145433ad7a19b4bf410e726c576602fd0c0ac7

See more details on using hashes here.

File details

Details for the file backtracker-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: backtracker-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for backtracker-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 57eda9b97ad639cd29b0af288508fda10d4baad481513fe558ea2ef2c3b9e758
MD5 dc1fa7092a4d0586376dbb5b0a2951dc
BLAKE2b-256 cb4714f852f61b80e16a3844c631c22441b3b21337d492ad2f7ec25edd7bdd3a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page