SlideShare a Scribd company logo
Using Constraint Propagation
To solve Sudoku
Peet Denny
June 2017
2
Numbers 1-9 must appear exactly once in each
unit: Row, Column, Square, (Diagonal)
7 x 1021
def search(values):
if values is False:
return False
if all(len(values[s]) == 1 for s in boxes):
return values
# Choose one of the unfilled squares with the fewest possibilities
n, s = min((len(values[s]), s) for s in boxes if len(values[s]) > 1)
# Now use recurrence to solve each one of the resulting sudokus, and
for value in values[s]:
new_sudoku = values.copy()
new_sudoku[s] = value
attempt = search(new_sudoku)
if attempt:
return attempt
The branching problem
5
Propagate the constraints through the search space to reduce the number of
options and make the problem tractable
Constraint Propagation
7
def eliminate(values):
solved_values = get_solved_values()
for box in solved_values:
digit = values[box]
for peer in peers[box]:
values[peer] = values[peer].replace(digit, '')
return values
def only_choice(values):
for unit in unitlist:
for digit in '123456789':
dplaces = [box for box in unit if digit in values[box]]
if len(dplaces) == 1:
values[dplaces[0]] = digit
return values
Naked twins
9
def naked_twins(values):
[prune_unit(values, r) for r in column_units]
[prune_unit(values, r) for r in row_units]
[prune_unit(values, r) for r in square_units]
[prune_unit(values, r) for r in diagonal_units]
return values
def prune_unit(values, unit):
two_dig_cells = [d for d in unit if len(values[d]) == 2]
if len(two_dig_cells) > 1:
for p1 in range(0, len(two_dig_cells)):
for p2 in range(p1 + 1, len(two_dig_cells)):
if values[two_dig_cells[p1]] == values[two_dig_cells[p2]]:
for dig in values[two_dig_cells[p1]]:
def remover(c):
values[c] = values[c].replace(dig, '')
for x in unit:
if x not in [two_dig_cells[p1], two_dig_cells[p2]]:
remover(x)
10
11
def reduce_puzzle(values):
stalled = False
while not stalled:
solved_values_before = len(get_solved_values(values))
eliminate(values)
only_choice(values)
naked_twins(values)
solved_values_after = len(get_solved_values(values))
stalled = solved_values_before == solved_values_after
if len([box for box in values.keys() if len(values[box]) == 0]):
return False
return values
def search(values):
values = reduce_puzzle(values)
if values is False:
return False
if all(len(values[s]) == 1 for s in boxes):
return values
# Choose one of the unfilled squares with the fewest possibilities
n, s = min((len(values[s]), s) for s in boxes if len(values[s]) > 1)
# Now use recurrence to solve each one of the resulting sudokus, and
for value in values[s]:
new_sudoku = values.copy()
new_sudoku[s] = value
attempt = search(new_sudoku)
if attempt:
return attempt
Constraint Propagation
Propagate the constraints through the search space to reduce the number
of options and make the problem tractable

More Related Content

What's hot

Problems problem spaces and search
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and search
Amey Kerkar
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Planning Agent
Planning AgentPlanning Agent
Planning Agent
Shiwani Gupta
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
K mean-clustering algorithm
K mean-clustering algorithmK mean-clustering algorithm
K mean-clustering algorithm
parry prabhu
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
GADAPURAMSAINIKHIL
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
Danish Aakash
 
AI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax AlgorithmAI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax Algorithm
Kiran Shahi
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna universitysangeethajames07
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
C++ Data Structure PPT.ppt
C++ Data Structure PPT.pptC++ Data Structure PPT.ppt
C++ Data Structure PPT.ppt
Mukesh Thakur
 
Outlier detection method introduction
Outlier detection method introductionOutlier detection method introduction
Outlier detection method introduction
DaeJin Kim
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
Fp growth algorithm
Fp growth algorithmFp growth algorithm
Fp growth algorithm
Pradip Kumar
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
Misssaxena
 

What's hot (20)

Problems problem spaces and search
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and search
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Planning Agent
Planning AgentPlanning Agent
Planning Agent
 
Data structures project
Data structures projectData structures project
Data structures project
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
K mean-clustering algorithm
K mean-clustering algorithmK mean-clustering algorithm
K mean-clustering algorithm
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
AI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax AlgorithmAI based Tic Tac Toe game using Minimax Algorithm
AI based Tic Tac Toe game using Minimax Algorithm
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
C++ Data Structure PPT.ppt
C++ Data Structure PPT.pptC++ Data Structure PPT.ppt
C++ Data Structure PPT.ppt
 
Outlier detection method introduction
Outlier detection method introductionOutlier detection method introduction
Outlier detection method introduction
 
More on Lex
More on LexMore on Lex
More on Lex
 
Fp growth algorithm
Fp growth algorithmFp growth algorithm
Fp growth algorithm
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 

Similar to Constraint propagation

# Imports# Include your imports here, if any are used. import.pdf
 # Imports# Include your imports here, if any are used. import.pdf # Imports# Include your imports here, if any are used. import.pdf
# Imports# Include your imports here, if any are used. import.pdf
gulshan16175gs
 
Python Programming
Python Programming Python Programming
Python Programming
Sreedhar Chowdam
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
Timothy McBush Hiele
 
Python Statistics.pptx
Python Statistics.pptxPython Statistics.pptx
Python Statistics.pptx
MalathiNagarajan5
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
Mitchell Vitez
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdf
GaneshPawar819187
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15
Hira Gul
 
Please help me fix this code! will upvote. The code needs to produce .pdf
Please help me fix this code! will upvote.  The code needs to produce .pdfPlease help me fix this code! will upvote.  The code needs to produce .pdf
Please help me fix this code! will upvote. The code needs to produce .pdf
climatecontrolsv
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
Please help this code is supposed to evaluate current node state and i.pdf
Please help this code is supposed to evaluate current node state and i.pdfPlease help this code is supposed to evaluate current node state and i.pdf
Please help this code is supposed to evaluate current node state and i.pdf
climatecontrolsv
 
5 CHM 5710 Character Tables.pptx
5 CHM 5710 Character Tables.pptx5 CHM 5710 Character Tables.pptx
5 CHM 5710 Character Tables.pptx
Cesario Ajpi Condori
 
Pp smi add. maths paper 1
Pp smi add. maths paper 1Pp smi add. maths paper 1
Pp smi add. maths paper 1
zabidah awang
 
Functional and reactive u is gwt.create 2015
Functional and reactive u is gwt.create 2015Functional and reactive u is gwt.create 2015
Functional and reactive u is gwt.create 2015
hezamu
 
Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1morabisma
 
The Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
The Weak Solution of Black-Scholes Option Pricing Model with Transaction CostThe Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
The Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
mathsjournal
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
mathsjournal
 
p.pdf
p.pdfp.pdf
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
mathsjournal
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
mathsjournal
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
mathsjournal
 

Similar to Constraint propagation (20)

# Imports# Include your imports here, if any are used. import.pdf
 # Imports# Include your imports here, if any are used. import.pdf # Imports# Include your imports here, if any are used. import.pdf
# Imports# Include your imports here, if any are used. import.pdf
 
Python Programming
Python Programming Python Programming
Python Programming
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
 
Python Statistics.pptx
Python Statistics.pptxPython Statistics.pptx
Python Statistics.pptx
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdf
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15
 
Please help me fix this code! will upvote. The code needs to produce .pdf
Please help me fix this code! will upvote.  The code needs to produce .pdfPlease help me fix this code! will upvote.  The code needs to produce .pdf
Please help me fix this code! will upvote. The code needs to produce .pdf
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Please help this code is supposed to evaluate current node state and i.pdf
Please help this code is supposed to evaluate current node state and i.pdfPlease help this code is supposed to evaluate current node state and i.pdf
Please help this code is supposed to evaluate current node state and i.pdf
 
5 CHM 5710 Character Tables.pptx
5 CHM 5710 Character Tables.pptx5 CHM 5710 Character Tables.pptx
5 CHM 5710 Character Tables.pptx
 
Pp smi add. maths paper 1
Pp smi add. maths paper 1Pp smi add. maths paper 1
Pp smi add. maths paper 1
 
Functional and reactive u is gwt.create 2015
Functional and reactive u is gwt.create 2015Functional and reactive u is gwt.create 2015
Functional and reactive u is gwt.create 2015
 
Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1
 
The Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
The Weak Solution of Black-Scholes Option Pricing Model with Transaction CostThe Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
The Weak Solution of Black-Scholes Option Pricing Model with Transaction Cost
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
 
p.pdf
p.pdfp.pdf
p.pdf
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
 
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COSTTHE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
THE WEAK SOLUTION OF BLACK-SCHOLE’S OPTION PRICING MODEL WITH TRANSACTION COST
 

Recently uploaded

PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 

Recently uploaded (20)

PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 

Constraint propagation

  • 1. Using Constraint Propagation To solve Sudoku Peet Denny June 2017
  • 2. 2 Numbers 1-9 must appear exactly once in each unit: Row, Column, Square, (Diagonal)
  • 4. def search(values): if values is False: return False if all(len(values[s]) == 1 for s in boxes): return values # Choose one of the unfilled squares with the fewest possibilities n, s = min((len(values[s]), s) for s in boxes if len(values[s]) > 1) # Now use recurrence to solve each one of the resulting sudokus, and for value in values[s]: new_sudoku = values.copy() new_sudoku[s] = value attempt = search(new_sudoku) if attempt: return attempt
  • 6. Propagate the constraints through the search space to reduce the number of options and make the problem tractable Constraint Propagation
  • 7. 7 def eliminate(values): solved_values = get_solved_values() for box in solved_values: digit = values[box] for peer in peers[box]: values[peer] = values[peer].replace(digit, '') return values
  • 8. def only_choice(values): for unit in unitlist: for digit in '123456789': dplaces = [box for box in unit if digit in values[box]] if len(dplaces) == 1: values[dplaces[0]] = digit return values
  • 9. Naked twins 9 def naked_twins(values): [prune_unit(values, r) for r in column_units] [prune_unit(values, r) for r in row_units] [prune_unit(values, r) for r in square_units] [prune_unit(values, r) for r in diagonal_units] return values def prune_unit(values, unit): two_dig_cells = [d for d in unit if len(values[d]) == 2] if len(two_dig_cells) > 1: for p1 in range(0, len(two_dig_cells)): for p2 in range(p1 + 1, len(two_dig_cells)): if values[two_dig_cells[p1]] == values[two_dig_cells[p2]]: for dig in values[two_dig_cells[p1]]: def remover(c): values[c] = values[c].replace(dig, '') for x in unit: if x not in [two_dig_cells[p1], two_dig_cells[p2]]: remover(x)
  • 10. 10
  • 11. 11 def reduce_puzzle(values): stalled = False while not stalled: solved_values_before = len(get_solved_values(values)) eliminate(values) only_choice(values) naked_twins(values) solved_values_after = len(get_solved_values(values)) stalled = solved_values_before == solved_values_after if len([box for box in values.keys() if len(values[box]) == 0]): return False return values
  • 12. def search(values): values = reduce_puzzle(values) if values is False: return False if all(len(values[s]) == 1 for s in boxes): return values # Choose one of the unfilled squares with the fewest possibilities n, s = min((len(values[s]), s) for s in boxes if len(values[s]) > 1) # Now use recurrence to solve each one of the resulting sudokus, and for value in values[s]: new_sudoku = values.copy() new_sudoku[s] = value attempt = search(new_sudoku) if attempt: return attempt
  • 13. Constraint Propagation Propagate the constraints through the search space to reduce the number of options and make the problem tractable