How to Solve Constraint Satisfaction Problems (CSPs) Using AC-3 Algorithm in Python
AI Optimization Algorithm
Published in
4 min readJan 22, 2021
The AC-3 algorithm simplifies a constraint satisfaction problem using the constraints to prune out values from the variables domain.
In this article, we will see how the AC-3 algorithm works and the implementation in Python.
How It Works
We can represent the AC-3 algorithm in 3 steps:
- Get all the constraints and turn each one into two arcs. For example:
π΄ > π΅ becomes π΄ > π΅ and π΅ < π΄. - Add all the arcs to a queue.
- Repeat until the queue is empty:
3.1. Take the first arc (π₯, π¦), off the queue (dequeue).
3.2. For every value in the π₯ domain, there must be some value of the π¦ domain.
3.3. Make π₯ arc consistent with π¦. To do so, remove values from π₯ domain for which there is no possible corresponding value for π¦ domain.
3.4. If the π₯ domain has changed, add all arcs of the form (π, π₯) to the queue (enqueue). Here π is another variable different from π¦ that has a relation to π₯.