SlideShare a Scribd company logo
1 of 13
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

Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...
Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...
Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...GMO-Z.com Vietnam Lab Center
 
Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Nhóc Nhóc
 
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPT
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPTBài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPT
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPTMasterCode.vn
 
Chuong 4 - CSDL phân tán
Chuong 4 - CSDL phân tánChuong 4 - CSDL phân tán
Chuong 4 - CSDL phân tánduysu
 
Đề thi Kỹ thuật lập trình có lời giải
Đề thi Kỹ thuật lập trình có lời giảiĐề thi Kỹ thuật lập trình có lời giải
Đề thi Kỹ thuật lập trình có lời giảinataliej4
 
Hệ điều hành (chương 3)
Hệ điều hành (chương 3)Hệ điều hành (chương 3)
Hệ điều hành (chương 3)realpotter
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2Nguyễn Công Hoàng
 
Trí tuệ nhân tạo "Game cờ Caro"
Trí tuệ nhân tạo "Game cờ Caro"Trí tuệ nhân tạo "Game cờ Caro"
Trí tuệ nhân tạo "Game cờ Caro"Ham Hau
 
Slide bài giảng về lập trình Scratch dành cho GV
Slide bài giảng về lập trình Scratch dành cho GVSlide bài giảng về lập trình Scratch dành cho GV
Slide bài giảng về lập trình Scratch dành cho GVBùi Việt Hà
 
bài giảng lập trình hướng đối tượng
bài giảng lập trình hướng đối tượngbài giảng lập trình hướng đối tượng
bài giảng lập trình hướng đối tượngMountain Nguyen
 

What's hot (20)

Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...
Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...
Ứng dụng NLP vào việc xác định ý muốn người dùng (Intent Detection) và sửa lỗ...
 
Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02
 
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPT
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPTBài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPT
Bài 7: Danh sách liên kết (LINKED LIST) và tập hợp (SET) - Giáo trình FPT
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Chuong 4 - CSDL phân tán
Chuong 4 - CSDL phân tánChuong 4 - CSDL phân tán
Chuong 4 - CSDL phân tán
 
Đề thi Kỹ thuật lập trình có lời giải
Đề thi Kỹ thuật lập trình có lời giảiĐề thi Kỹ thuật lập trình có lời giải
Đề thi Kỹ thuật lập trình có lời giải
 
Hệ điều hành (chương 3)
Hệ điều hành (chương 3)Hệ điều hành (chương 3)
Hệ điều hành (chương 3)
 
Chapter1234
Chapter1234Chapter1234
Chapter1234
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Hadoop
HadoopHadoop
Hadoop
 
Unit 1
Unit 1Unit 1
Unit 1
 
Computer Graphics - clipping
Computer Graphics - clippingComputer Graphics - clipping
Computer Graphics - clipping
 
Giáo trình MSWLOGO Lớp 5
Giáo trình MSWLOGO Lớp 5Giáo trình MSWLOGO Lớp 5
Giáo trình MSWLOGO Lớp 5
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
 
Clique
Clique Clique
Clique
 
Trí tuệ nhân tạo "Game cờ Caro"
Trí tuệ nhân tạo "Game cờ Caro"Trí tuệ nhân tạo "Game cờ Caro"
Trí tuệ nhân tạo "Game cờ Caro"
 
Slide bài giảng về lập trình Scratch dành cho GV
Slide bài giảng về lập trình Scratch dành cho GVSlide bài giảng về lập trình Scratch dành cho GV
Slide bài giảng về lập trình Scratch dành cho GV
 
bài giảng lập trình hướng đối tượng
bài giảng lập trình hướng đối tượngbài giảng lập trình hướng đối tượng
bài giảng lập trình hướng đối tượng
 
Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
 

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.pdfgulshan16175gs
 
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.pdfTimothy McBush Hiele
 
fds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdffds Practicle 1to 6 program.pdf
fds Practicle 1to 6 program.pdfGaneshPawar819187
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15Hira 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 .pdfclimatecontrolsv
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani 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.pdfclimatecontrolsv
 
Pp smi add. maths paper 1
Pp smi add. maths paper 1Pp smi add. maths paper 1
Pp smi add. maths paper 1zabidah 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 2015hezamu
 
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 Costmathsjournal
 
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 COSTmathsjournal
 
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 COSTmathsjournal
 
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 COSTmathsjournal
 
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 COSTmathsjournal
 

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

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

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