Slithering the Link David Anderson, Dan Hamalainen, Edward - - PowerPoint PPT Presentation

slithering the link
SMART_READER_LITE
LIVE PREVIEW

Slithering the Link David Anderson, Dan Hamalainen, Edward - - PowerPoint PPT Presentation

Slithering the Link David Anderson, Dan Hamalainen, Edward Kwiatkowski, Valerie Lambert, Sam Spaeth Carleton College Department of Computer Science March 12, 2016 Roadmap What is a Slitherlink Puzzle? How to Define a Slitherlink Puzzle How


slide-1
SLIDE 1

Slithering the Link

David Anderson, Dan Hamalainen, Edward Kwiatkowski, Valerie Lambert, Sam Spaeth

Carleton College Department of Computer Science

March 12, 2016

slide-2
SLIDE 2

Roadmap

What is a Slitherlink Puzzle? How to Define a Slitherlink Puzzle How to Solve a Slitherlink Puzzle How to Make a Slitherlink Puzzle

slide-3
SLIDE 3

What is Slitherlink?

Logic puzzle developed by Nikoli Played on:

◮ a rectangular lattice of dots, creating ”cells” ◮ with some cells containing numbers

slide-4
SLIDE 4

What is Slitherlink?

Objective of the game is to create a single loop throughout the puzzle where:

◮ the final solution is a

continuous line that does not cross itself

◮ each numbered cell

corresponds to the number of solution lines around it

◮ the puzzle should have

ONLY ONE unique solution

slide-5
SLIDE 5

Solving a Slitherlink Puzzle

slide-6
SLIDE 6

Solving a Slitherlink Puzzle

slide-7
SLIDE 7

Solving a Slitherlink Puzzle

slide-8
SLIDE 8

Solving a Slitherlink Puzzle

slide-9
SLIDE 9

Solving a Slitherlink Puzzle

slide-10
SLIDE 10

Solving a Slitherlink Puzzle

Conceptis Puzzles Slitherlink Techniques

slide-11
SLIDE 11

Solving a Slitherlink Puzzle

slide-12
SLIDE 12

Solving a Slitherlink Puzzle

Solving a Slitherlink puzzle is an NP-complete problem, as well as determining if there are multiple solutions. On the NP-completeness of the Slither Link Puzzle Takayuki YATO

slide-13
SLIDE 13

Puzzle Representation

Components of a puzzle:

◮ the grid

◮ lines ◮ numbers

◮ rules and contradictions ◮ contours ◮ what it means to be solved

slide-14
SLIDE 14

The Grid

M by N grid has 3 2D arrays:

◮ M by N 2D array for numbers; values 0 to 3 or empty ◮ M + 1 by N 2D array for horizontal lines; values line, x, or

empty

◮ M by N + 1 2D array for vertical lines; values line, x, or empty

A rule-based approach to the puzzle of Slither Link. Stefan Herting.

slide-15
SLIDE 15
slide-16
SLIDE 16

Rules and Contradictions

Each rule has:

◮ dimensions ◮ prerequisites ◮ consequences

Each contradiction has:

◮ dimensions ◮ prerequisities

slide-17
SLIDE 17

Examples of Rules

slide-18
SLIDE 18

Static Rules

Static rules are rules that do not contain lines or x’s in their

  • prerequisites. We identified 3 static rules.
slide-19
SLIDE 19

Rule and Contradiction in action

We chose to cover rules that are at most 3 by 3 in dimension, and contradictions that are at most 2 by 2 in dimension.

slide-20
SLIDE 20

Contours

◮ Use 2D array to keep track of contour endpoints. ◮ Update endpoints as we add lines. ◮ Keep track of the number of open and closed contours as we

add lines.

Table 1: Contour Endpoint Array

3,1 0,1 1,2 0,2 1,3 2,2 numClosed = 0 numOpen = 3

slide-21
SLIDE 21

How Can We Tell Our Grid is Solved?

◮ Every number in the

grid is satisfied

slide-22
SLIDE 22

How Can We Tell Our Grid is Solved?

◮ Every number in the

grid is satisfied

◮ There is exactly one

closed loop, and no

  • pen loops.
slide-23
SLIDE 23

Applying Rules

◮ for every position in the grid...

◮ for every defined rule... ◮ for every orientation...

Do the prerequisites in the rule match where we’re looking at on the grid?

◮ If so, add consequences to the grid.

slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29
slide-30
SLIDE 30

Guessing

Method:

slide-31
SLIDE 31

Guessing

Method:

  • 1. Find a particular open position
slide-32
SLIDE 32

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line
slide-33
SLIDE 33

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules

slide-34
SLIDE 34

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules 2.2 If there’s a contradiction, we know the position is an X

slide-35
SLIDE 35

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules 2.2 If there’s a contradiction, we know the position is an X

  • 3. Guess that position is an X
slide-36
SLIDE 36

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules 2.2 If there’s a contradiction, we know the position is an X

  • 3. Guess that position is an X

3.1 Run deterministic rules

slide-37
SLIDE 37

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules 2.2 If there’s a contradiction, we know the position is an X

  • 3. Guess that position is an X

3.1 Run deterministic rules 3.2 If there’s a contradiction, we know the position is a Line

slide-38
SLIDE 38

Guessing

Method:

  • 1. Find a particular open position
  • 2. Guess that position is a Line

2.1 Run deterministic rules 2.2 If there’s a contradiction, we know the position is an X

  • 3. Guess that position is an X

3.1 Run deterministic rules 3.2 If there’s a contradiction, we know the position is a Line

  • 4. If neither results in a contradiction, take the intersection of

the two resulting grids

slide-39
SLIDE 39
slide-40
SLIDE 40
slide-41
SLIDE 41
slide-42
SLIDE 42
slide-43
SLIDE 43

Recursive Guessing

If single guesses don’t result in anything, we can nest our guesses. New information from nested guesses propagates out to the canonical grid.

slide-44
SLIDE 44

Recursive Guessing

If single guesses don’t result in anything, we can nest our guesses. New information from nested guesses propagates out to the canonical grid. We call n nested guesses a ”depth n guess”.

slide-45
SLIDE 45

Example of a depth 2 guess

Grid

slide-46
SLIDE 46

Example of a depth 2 guess

Grid A

slide-47
SLIDE 47

Example of a depth 2 guess

Grid A Line X

slide-48
SLIDE 48

Example of a depth 2 guess

Grid A Line B Line X X

slide-49
SLIDE 49

Example of a depth 2 guess

Grid A Line B Line X X C Line X

slide-50
SLIDE 50

Example of a depth 2 guess

Grid A Line B Line contradiction X X C Line X

slide-51
SLIDE 51

Example of a depth 2 guess

Grid A Line B Line contradiction X valid X C Line X

slide-52
SLIDE 52

Example of a depth 2 guess

Grid A Line B X valid X C Line X

slide-53
SLIDE 53

Example of a depth 2 guess

Grid A Line B X valid X C Line contradiction X

slide-54
SLIDE 54

Example of a depth 2 guess

Grid A Line B X valid X C Line contradiction X contradiction

slide-55
SLIDE 55

Example of a depth 2 guess

Grid A Line B X valid X contradiction

slide-56
SLIDE 56

Example of a depth 2 guess

Grid A Line B X

slide-57
SLIDE 57

Repeated guessing algorithm

◮ If at any point new information is found, restart algorithm

slide-58
SLIDE 58

Repeated guessing algorithm

◮ If at any point new information is found, restart algorithm ◮ Run deterministic rules

slide-59
SLIDE 59

Repeated guessing algorithm

◮ If at any point new information is found, restart algorithm ◮ Run deterministic rules ◮ Run every possible guess (depth 1 guessing)

slide-60
SLIDE 60

Repeated guessing algorithm

◮ If at any point new information is found, restart algorithm ◮ Run deterministic rules ◮ Run every possible guess (depth 1 guessing) ◮ Run every possible guess, and within each guess, make all

possible guesses (depth 2 guessing)

slide-61
SLIDE 61

Repeated guessing algorithm

◮ If at any point new information is found, restart algorithm ◮ Run deterministic rules ◮ Run every possible guess (depth 1 guessing) ◮ Run every possible guess, and within each guess, make all

possible guesses (depth 2 guessing)

◮ · · ·

slide-62
SLIDE 62

Solving a Slitherlink Puzzle

slide-63
SLIDE 63

Solving a Slitherlink Puzzle

slide-64
SLIDE 64

Solving a Slitherlink Puzzle

slide-65
SLIDE 65

Solving a Slitherlink Puzzle

slide-66
SLIDE 66

Solving a Slitherlink Puzzle

slide-67
SLIDE 67

Solving a Slitherlink Puzzle

slide-68
SLIDE 68

Solving a Slitherlink Puzzle

slide-69
SLIDE 69

Solving a Slitherlink Puzzle

slide-70
SLIDE 70

Solving a Slitherlink Puzzle

slide-71
SLIDE 71

Solving a Slitherlink Puzzle

slide-72
SLIDE 72

Solving a Slitherlink Puzzle

slide-73
SLIDE 73

Solving a Slitherlink Puzzle

slide-74
SLIDE 74

Solving a Slitherlink Puzzle

slide-75
SLIDE 75

Solving a Slitherlink Puzzle

slide-76
SLIDE 76

Solving a Slitherlink Puzzle

slide-77
SLIDE 77

Solving a Slitherlink Puzzle

slide-78
SLIDE 78

Solving a Slitherlink Puzzle

slide-79
SLIDE 79

Solving a Slitherlink Puzzle

slide-80
SLIDE 80

Solving a Slitherlink Puzzle

slide-81
SLIDE 81

Solving a Slitherlink Puzzle

slide-82
SLIDE 82

Solving a Slitherlink Puzzle

slide-83
SLIDE 83

Solving a Slitherlink Puzzle

slide-84
SLIDE 84

Solving a Slitherlink Puzzle

slide-85
SLIDE 85

Checking for Multiple Solutions

For every guess we made to get to the solution:

slide-86
SLIDE 86

Checking for Multiple Solutions

For every guess we made to get to the solution:

◮ Go back to the state of the grid before the guess was made,

and solve the corresponding grid with the opposite guess at that same spot:

slide-87
SLIDE 87

Checking for Multiple Solutions

For every guess we made to get to the solution:

◮ Go back to the state of the grid before the guess was made,

and solve the corresponding grid with the opposite guess at that same spot:

◮ If the opposite guess eventually leads to a contradiction, we

know that the original guess has to be true (given all previous guesses). Continue to check other guesses.

slide-88
SLIDE 88

Checking for Multiple Solutions

For every guess we made to get to the solution:

◮ Go back to the state of the grid before the guess was made,

and solve the corresponding grid with the opposite guess at that same spot:

◮ If the opposite guess eventually leads to a contradiction, we

know that the original guess has to be true (given all previous guesses). Continue to check other guesses.

◮ If the opposite guess eventually leads to one or more solutions,

then we know that this grid has more than one solution.

slide-89
SLIDE 89

Checking for Multiple Solutions

For every guess we made to get to the solution:

◮ Go back to the state of the grid before the guess was made,

and solve the corresponding grid with the opposite guess at that same spot:

◮ If the opposite guess eventually leads to a contradiction, we

know that the original guess has to be true (given all previous guesses). Continue to check other guesses.

◮ If the opposite guess eventually leads to one or more solutions,

then we know that this grid has more than one solution.

If the opposite of every guess we had to make leads to a contradiction, then we know that the original solution we found is the only one.

slide-90
SLIDE 90

Checking for Multiple Solutions

For every guess we made to get to the solution:

◮ Go back to the state of the grid before the guess was made,

and solve the corresponding grid with the opposite guess at that same spot:

◮ If the opposite guess eventually leads to a contradiction, we

know that the original guess has to be true (given all previous guesses). Continue to check other guesses.

◮ If the opposite guess eventually leads to one or more solutions,

then we know that this grid has more than one solution.

If the opposite of every guess we had to make leads to a contradiction, then we know that the original solution we found is the only one.

slide-91
SLIDE 91

Checking for Multiple Solutions

slide-92
SLIDE 92

Checking for Multiple Solutions

slide-93
SLIDE 93

Checking for Multiple Solutions

slide-94
SLIDE 94

Checking for Multiple Solutions

slide-95
SLIDE 95

Time Complexity

Important details:

slide-96
SLIDE 96

Time Complexity

Important details:

◮ For each depth, O(mn) new guesses, each taking O(mn) time

to instantiate

slide-97
SLIDE 97

Time Complexity

Important details:

◮ For each depth, O(mn) new guesses, each taking O(mn) time

to instantiate

◮ Guessing at the max depth is by far the most important factor

in runtime

slide-98
SLIDE 98

Time Complexity

Important details:

◮ For each depth, O(mn) new guesses, each taking O(mn) time

to instantiate

◮ Guessing at the max depth is by far the most important factor

in runtime

◮ We can maximize this by never filling anything in at lower

depths

slide-99
SLIDE 99

Time Complexity

Overall runtime O((mn)d) with d bounded above by O(mn)

Table 2: Empty grid completion time

Size Max Depth Time (sec) 3x3 0.001111 3x3 1 0.033743 3x3 2 1.19998 3x3 3 57.2004 3x3 4 2587.52

slide-100
SLIDE 100

Empirical Results: Typical Puzzles

Table 3: Solve Times

Size Max Depth Solve Time 10x10 1 0.048542 seconds 10x10 1 0.385348 seconds 10x10 2 1.77571 seconds 10x10 2 3.23716 seconds 10x10 3 150.824 seconds* 30x25 1 1.95466 seconds 30x25 1 2.38471 seconds 30x25 1 4.4892 seconds 40x30 1 1.97524 seconds 40x30 3 66.268 seconds* *These puzzles were determined to have multiple solutions Puzzles taken from nikoli.com, kakuro − online.com, and puzzle − loop.com.

slide-101
SLIDE 101

Make a Slitherlink Puzzle

Overview

  • 1. Make a loop
  • 2. Fill grid with numbers
  • 3. Remove some numbers
slide-102
SLIDE 102

Making a Loop

Start with an empty m × n grid, a simple rule, and three lists:

  • 1. available
  • 2. expandable
  • 3. unexpandable
slide-103
SLIDE 103

Making a Loop

Start with an empty m × n grid, a simple rule, and three lists:

  • 1. available
  • 2. expandable
  • 3. unexpandable

Start with every location in the grid in available but none in unexpandable and expandable. Then, add one random location to expandable and remove it from available.

slide-104
SLIDE 104

Bad Stuff

Rule: When expanding from a location, cur, in expandable to an adjacent location in available , make sure that adding pos to expandable doesn’t cause any bad stuff

Opposite: Opposite kitty-corners:

If opposite or either of the opposite kitty-corners are in expandable, then do not add pos to the loop.

slide-105
SLIDE 105

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
slide-106
SLIDE 106

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
  • 2. Look at neighbors to see if and where the loop can expand

from cur.

slide-107
SLIDE 107

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
  • 2. Look at neighbors to see if and where the loop can expand

from cur.

2.1 If a neighbor is in available and it wasn’t a valid neighbor, remove it from available

slide-108
SLIDE 108

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
  • 2. Look at neighbors to see if and where the loop can expand

from cur.

2.1 If a neighbor is in available and it wasn’t a valid neighbor, remove it from available 2.2 If there are no valid neighbors, add cur to unexpandable and remove it from expandable

slide-109
SLIDE 109

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
  • 2. Look at neighbors to see if and where the loop can expand

from cur.

2.1 If a neighbor is in available and it wasn’t a valid neighbor, remove it from available 2.2 If there are no valid neighbors, add cur to unexpandable and remove it from expandable 2.3 Otherwise, randomly choose an valid neighbor to add to add to expandable and take out of available

slide-110
SLIDE 110

Making a Loop cont.

  • 1. Choose a location, cur in expandable at random
  • 2. Look at neighbors to see if and where the loop can expand

from cur.

2.1 If a neighbor is in available and it wasn’t a valid neighbor, remove it from available 2.2 If there are no valid neighbors, add cur to unexpandable and remove it from expandable 2.3 Otherwise, randomly choose an valid neighbor to add to add to expandable and take out of available

  • 3. repeat until there are no locations in expandable
slide-111
SLIDE 111

Making a Loop Example

slide-112
SLIDE 112

Making a Loop Example

slide-113
SLIDE 113

Making a Loop Example

slide-114
SLIDE 114

Making a Loop Example

slide-115
SLIDE 115

Making a Loop Example

slide-116
SLIDE 116

Making a Loop Example

slide-117
SLIDE 117

Making a Loop Example

slide-118
SLIDE 118

Making a Loop Example

slide-119
SLIDE 119

Making a Loop Example

slide-120
SLIDE 120

Making a Loop Example

slide-121
SLIDE 121

Making a Loop Example

slide-122
SLIDE 122

Making a Loop Example

slide-123
SLIDE 123

Making a Loop Example

slide-124
SLIDE 124

Filling with Numbers

Surprise surprise, this is actually really easy (sum the differences in the neighboring locations)

slide-125
SLIDE 125

Removing Numbers

To make puzzles interesting, we want to remove numbers We want to do so until we have reached a certain count Must retain one unique solution

slide-126
SLIDE 126

Removing Numbers

The Process:

slide-127
SLIDE 127

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
slide-128
SLIDE 128

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
  • 2. Add this number to a stack of ineligible numbers
slide-129
SLIDE 129

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
  • 2. Add this number to a stack of ineligible numbers
  • 3. Check if eliminating would make the puzzle unsolvable
slide-130
SLIDE 130

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
  • 2. Add this number to a stack of ineligible numbers
  • 3. Check if eliminating would make the puzzle unsolvable

3.1 If solvable, remove the number from both the grid and set of eligible numbers

slide-131
SLIDE 131

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
  • 2. Add this number to a stack of ineligible numbers
  • 3. Check if eliminating would make the puzzle unsolvable

3.1 If solvable, remove the number from both the grid and set of eligible numbers 3.2 If unsolvable, only remove the number from the set of eligible numbers

slide-132
SLIDE 132

Removing Numbers

The Process:

  • 1. Pick a number from a set of eligible numbers
  • 2. Add this number to a stack of ineligible numbers
  • 3. Check if eliminating would make the puzzle unsolvable

3.1 If solvable, remove the number from both the grid and set of eligible numbers 3.2 If unsolvable, only remove the number from the set of eligible numbers

  • 4. Repeat until set of eligible numbers is empty
slide-133
SLIDE 133

Removing Numbers cont.

Once set of eligible numbers is empty:

slide-134
SLIDE 134

Removing Numbers cont.

Once set of eligible numbers is empty:

  • 1. Pop numbers off ineligible stack
slide-135
SLIDE 135

Removing Numbers cont.

Once set of eligible numbers is empty:

  • 1. Pop numbers off ineligible stack
  • 2. Place each back into the set of eligible numbers
slide-136
SLIDE 136

Removing Numbers cont.

Once set of eligible numbers is empty:

  • 1. Pop numbers off ineligible stack
  • 2. Place each back into the set of eligible numbers
  • 3. Do so until most recently eliminated number is found
slide-137
SLIDE 137

Removing Numbers cont.

Once set of eligible numbers is empty:

  • 1. Pop numbers off ineligible stack
  • 2. Place each back into the set of eligible numbers
  • 3. Do so until most recently eliminated number is found
  • 4. Keep eliminated in the ineligible stack, but place back into

grid

slide-138
SLIDE 138

Removing Numbers cont.

Once set of eligible numbers is empty:

  • 1. Pop numbers off ineligible stack
  • 2. Place each back into the set of eligible numbers
  • 3. Do so until most recently eliminated number is found
  • 4. Keep eliminated in the ineligible stack, but place back into

grid Repeat removing numbers until desired count is reached

slide-139
SLIDE 139

Removing Numbers cont.

It’s too hard!

Improvements

  • 1. Data
  • 2. Rule set
  • 3. Balancing
slide-140
SLIDE 140
slide-141
SLIDE 141

Ruleset Limitation

As a result, we created two subsets of rules:

◮ easy: the rules with a greater than 5% occurrence rate ◮ hard: the rules with a greater than 1% occurrence rate

slide-142
SLIDE 142

An ‘Easy’ Puzzle

slide-143
SLIDE 143

A Quick Attempt at the ‘Easy’ Puzzle

slide-144
SLIDE 144

Balancing the Numbers

slide-145
SLIDE 145

A Balanced Easy Puzzle

slide-146
SLIDE 146

A Quick Attempt at the ‘Easy’ Puzzle

slide-147
SLIDE 147

Time Complexity

Important details

◮ The Solver is run on the order of mn times. ◮ Each time the solver is run, it happends with a maximum

depth of one guess which has on the order of O((mn)2) time.

◮ Therefore, the generator run in the order of O((mn)3) time.

slide-148
SLIDE 148

References

Conceptis Puzzles Slitherlink Techniques On the NP-completeness of the Slither Link Puzzle Takayuki YATO Finding All Solutions and Instances of Numberlink and Slitherlink by ZDDs. Ryo Yoshinaka, Toshiki Saitoh, Jun Kawahara, Koji Tsuruma, Hiroaki Iwashita and Shin-ichi Minato. A rule-based approach to the puzzle of Slither Link. Stefan Herting. Puzzles and Games: A Mathematical Modeling Approach. Tony H¨ urlimann, 2015 Solving logical puzzles using mathematical models. KVIS Susanti, S Lukas.

slide-149
SLIDE 149

Thank you

slide-150
SLIDE 150

Questions?