14
Hashi-O-Kakero Puzzles Authors: Brian Blair, Karl Foreman, Ryan Wooley 11/30/2018 Dr. Tagliarini CSC 380: Design and Analysis of Algorithms

people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

Embed Size (px)

Citation preview

Page 1: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

Hashi-O-Kakero Puzzles

Authors: Brian Blair, Karl Foreman, Ryan Wooley

11/30/2018

Dr. Tagliarini

CSC 380: Design and Analysis of Algorithms

Page 2: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

Abstract

The goal of our project is to create a solver for a Hashi-o-kakero puzzle. This project implements three different algorithms to solve puzzles and will compare the speed as well as the computational complexity of each algorithm. To accomplish this, (change later) we created five separate folders based on difficulty: super easy, easy, medium, hard and super hard puzzles. Each of these folders contain around 100-130 puzzles of varying sizes contained in individual text files separated by periods. To test for speed, we ran each algorithm on every file difficulty to see if there would be a difference in runtime based on how hard a puzzle was. We also tested the runtime speed of each algorithm on all 7x7 size puzzles, all 11x11 size puzzles and all 20x20 puzzles of a certain difficulty. This project will prove that (say something about the complexity and runtime of fastest algorithm and slowest algorithm.)

IntroductionA Hashi-o-kakero puzzle is a logical

reasoning puzzle similar to a word search puzzle or a sudoku puzzle. The idea of the puzzle is that there are circles located on different places on an n-by-n grid. Each circle represents an “island” (Figure 1). Each island has a number from 1 to 8 on it, representing the number of “bridges” that the island has. A bridge represents a connection from one island to another island (Figure 1). The goal of the game is to place horizontal and vertical lines (bridges) between islands so that the number of bridges to each island is the same as the number on the island. For example, an island with a 1 on it has only one single connection, while an island with an 8 on it has eight connections. Since there are only four possible directions that lines can be

placed: North, South, East, West, each island can have at most 2 bridges in a single direction. For example, an island with an 8 on it will have 2 bridges going in all directions. Bridges are also only able to go in one direction either horizontally or vertically and cannot cross one another. The rules of this puzzle do not allow vertical lines at all. When the puzzle is complete, you should be able to place your finger on a random island in the puzzle and be able to traverse to any other island on the puzzle by using a bridge. Therefore, the entire grid of islands must be interconnected by bridges to be a completed puzzle.

FIGURE 1

Formal Problem StatementGiven a set of n nodes or "islands" located at various (x,y) coordinates on a grid and with each node i labeled with a number, vi, find an arrangement of c connections or "bridges" between the nodes such that the following constraints are met:

c = (∑(vi)) / 2

For every node i, ci = vi, where ci is the number connections to that node.

For every node i, ci-NORTH ≤ 2, ci-EAST ≤ 2, ci-

SOUTH ≤ 2, ci-WEST ≤ 2, where ci-[direction] indicates the number of connections in the given cardinal direction.

Page 3: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

When the nodes are rotated 45° about the origin of the grid, every connection, k, between nodes two nodes, i and j,  can be represented by a piecewise linear function Bk(x) defined for xi < x < xj, where xi and xj are the x coordinates of the nodes i and j.

Every function Bk(x) has a slope of 1 or -1.

For all Bk(x) and Bl(x), if Bk(x1) = Bl(x1), then Bk(x2) = Bl(x2), where x1 ≠ x2.

For all Bk(x), Bk(xi) ≠ yi for any given node, i.

The nodes comprise exactly one connected component.

FrameworkThe HashiSolver class is by far the

largest of the classes we used for our project. This is because all the formatting of the puzzle is done by this class. All three of our algorithms use this framework to interact with island and bridge objects and manipulate them. It first creates an array-based representation of the game board by reading the data in from a text file and stripping it of all period (.) symbols from the file. We created 3,525 puzzles of different sizes and difficulty levels. There are roughly 110-130 puzzles of each dimension per difficulty, averaging around 700 puzzles for each difficulty. Since the data is not the exact same for each set of puzzles, we had to consider the different numbers for each puzzle set to keep the results fair. The complete data set of puzzles can be seen in Figure 2 below.

FIGURE 2

It also creates all island and bridge objects and their properties. The island objects are created first and then stored in a dictionary with the (x,y) coordinates of the islands as their key (Figure 3).

FIGURE 3

While creating the islands, the expected number of connections is also calculated by dividing the summation of all island numbers by two. For example, you get the number 32 if you add up all the island numbers in Figure 1. Divide 32 by 2 and get 16, which is the number of connections that the puzzle needs to be solved. The neighbors are determined by iterating across a row to make east and west neighbors (Figure 4). If there is an island there is no previous island, it is the first in the row and has no west neighbors. If there is an island that is not the first in the row, update the neighbors for it and the previous island. At this point, the island will be the furthest east and have no east neighbor. The north and south neighbors are found the exact same way. (Figure 4). The board is then cleared and repopulated with only the islands.

Page 4: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

FIGURE 4

In this class there is a method called AddBridge() that adds a single bridge to an island in a specified direction and tells all other islands that a bridge is added, allowing the appropriate islands to update themselves if needed. There are also two methods of removing bridges. One is called RemoveBridge() and it removes a single bridge from an island in a specified direction. Just like AddBridge(), the other islands are told when a bridge is removed so they can update themselves if needed. The other methods is called DestroyBridges() and does the same thing as RemoveBridge(), but with all bridges that it has in a given direction. All three of these methods have a big O complexity of O(n) where n is the number of islands on the grid. All other methods in this class also have either a complexity of O(1) or O(n), making the overall Big O complexity of this class O(n).

Algorithm One: Exhaustive SearchOne method of solving a problem as

this is by using an exhaustive search to try all possible combinations of bridges until arriving at a solution. It is important to note that our exhaustive search only places bridges to the south or east because of the way it iterates through the board. Placing

bridges to the north or west would only add duplicate bridges. The class uses a blacklist to keep track of bridges that are not currently allowed for an island at that given time. The algorithm accepts a puzzle from the HashiPuzzle class as input. It iterates over the list of islands until an incomplete island is found and attempts to add east bridges for the island as long as east bridges for the island are not currently blacklisted. Then it does the same for south bridges if south bridges for the island are not currently blacklisted. If a bridge fails to be added, the method backtracks because either one of the previously added bridges must be incomplete or the number of bridges is correct but not in a continuous chain. Each of these blocks of code are O(n) in complexity, where n is the number of islands in the puzzle.

The ExhaustiveSearch class has a Big O complexity of O(n^n) where n is the number of islands. The outer loop itself would be O(n) and four of the inner blocks are also O(n), but the outer loop can and will be reset while executing. The worst-case scenario possible with this class is that for every island, the bridges are all placed incorrectly but nearly finishes the puzzle. It would then need to iterate from island 0 back to the last island, which is n hops, to

Page 5: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

fix the mistake but finds another mistake for the island prior. It then must iterate back another n hops only to find a mistake on the final island again. This process would repeat until every island had fixed its mistakes n times for every other island, thus n^n total iterations.

Algorithm Two: Heuristic TechniqueOur team agreed that a heuristic

approach to this problem would serve as a mirror to how a person would go about solving a Hashi puzzle by hand. We implemented a set of rules that the algorithm follows. Originally, we had eight different sets of rules for each island number, but we were able to condense them into four main recurring rules.

Eight queues, one for each possible island number, are initialized and processed separately. Queues containing 7’s and 8’s are processed outside of the main loop because the queues will always be depleted on the first pass, although they can follow the general rules as the other queues. For islands with the number 8, a method is called to immediately fill them up with two bridges going in every direction and then locking the island, not allowing it to be changed once it is locked. If the island number is a 7, a single bridge is placed in each direction and the island is dequeued from the queue holding 7’s and placed into the queue holding 3’s. Adding bridges has a complexity of O(n) and the queues have a complexity of O(n) because the greater the number of islands there are, the more likely it is to have more islands in the queue. Therefore, processing 7 and 8 queues is of Big O complexity O(n2) where n is the number of islands.

All queues containing islands with numbers 1 to 6 are processed in a while loop. Each element in the queue is only processed once. First, the loop checks to see if the island was modified while processing

another island. If it was, it skips the rest of the current iteration and places it in the appropriate queue. If not, it continues to the set of rules. The first rule is that if there exactly as many open spots as bridges to be placed, place the bridges. If there are not, if the number of bridges to be placed is only one less than the island number, it might be possible to place bridges there. If there are twice as many open spots as there are open directions, a bridge can be placed in each direction. Otherwise, if the number of open directions is less than or equal to the number of remaining bridges, a bridge can be placed at each location that can still hold two bridges. Nothing can be done if the number of bridges is two or more less than the island number. Then, move to the next queue (Figure 5).

FIGURE 5

Again, we determined the Big O of this method by looking at the worst-case

Page 6: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

scenario: if every island needed to be looked at to find one island that can have a single bridge added to it. Every step after would also require looking at every single island. The operation would be n*(n+n), making it O(n2). The +n part of the operation is from the AddBridge method but does not impact complexity. Therefore, the complexity of the HeuristicTechnique class would be O(n2).

Algorithm Three: Simulated AnnealingWe originally were going to

implement a “smarter algorithm” for our third algorithm, which would have been a variation of our heuristic algorithm with different rules to eliminate iterating through unnecessary steps. Instead, we decided to try simulated annealing for our third algorithm and use the rules in our already implemented heuristic algorithm.

First, the algorithm iterates over the list of islands and if the number of bridges corresponds to the number on the island and there is no question of where the bridges need to go, then it will place the bridges, lock them and continue. If not, it will place the island into a list of islands that need to be processed. Then, a copy of the puzzle is created in case the algorithm gets stuck and needs to reset. While the puzzle is not solved, the algorithm attempts to solve the puzzle by randomly placing the appropriate amount of bridges for each island. The complexity of this is O(n2) The most likely outcome is that it did not randomly assign the correct order of bridges, so the places that are left open on islands are also randomly filled in. If the solution is found, it will break out of the loop and make a copy of the puzzle (Figure 6). The complexity of making the copy is O(n). A random island with bridges is chosen and all bridges are removed and randomly placed again. If the number of bridges is the same amount of total bridges and the puzzle is completely connected, it is solved. If not, the copy is

used in place of the original and is now the one being modified and the loop will start again. The overall complexity of this method is O(n2).

FIGURE 6

AnalysisWe tested each of the three

algorithms on puzzle banks having around 110-130 puzzles each that varied in size and difficulty level. The sizes we tested were 7x7, 9x9, 11x11, 13x13, 17x17 and 20x20. The difficulty levels we tested were super easy, easy, medium, hard and super hard puzzles. These puzzle sizes were taken from a data bank and converted into period separated text files for processing. We decided it would be best to separate each of the files into separate folders categorized by both difficulty and size respectively. We felt it was important to run tests on the different difficulty levels of puzzles as well as different sizes of puzzles because they may influence the runtime of the algorithm.

A trend we noticed immediately when testing the algorithms was that the number of bridges tended to decrease as the

Page 7: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

difficulty level of the puzzle increased (Figure 7).

FIGURE 7

The connection we made is that the harder puzzles tend to have less bridges because the harder bridges to interpret are the ones with the most ambiguity. For example, an 8 island has only one solution: to place two bridges in each direction. On the other hand, a 2 island has many more possible combinations of bridges depending on its placement on the board. The lower difficulty puzzles had many more 6, 7 and 8’s because that was a way of giving the solver a better clue.

In our original predictions, we thought the simulated annealing algorithm would be the fastest and the exhaustive search would be the slowest in terms of runtime. Our results showed that the heuristic approach solved puzzles much faster than either exhaustive search or simulated annealing methods with about 95% accuracy. This held true for all puzzles that were super easy, easy or medium in difficulty. When the difficulty was increased to hard or extra hard, the heuristic technique became very inaccurate and began failing to complete even one puzzle. We realized that the hard and super hard puzzles contained many more edge cases that would only be

solvable if the original rules implemented in our heuristic algorithm were modified or new rules were added. The graph of the heuristic technique run on a single puzzle without hard and super hard difficulties is shown below.

The exhaustive search performed slower than the heuristic, but faster than the simulated annealing in all puzzle sizes and difficulties. It outperformed the heuristic algorithm with a 100% accuracy on every puzzle as well as a faster time for puzzles with a hard or super hard difficulty. The graph of the exhaustive search run on a single puzzle is shown below.

The simulated annealing algorithm was a big letdown for the group, being the slowest of all three algorithms regardless of difficulty or board size. It took over 7 hours to run the algorithm on the folder containing all super easy puzzles and around 10 seconds per puzzle to solve when the puzzle was in the super hard category. It was very hard to run tests on the entire set of puzzles

Page 8: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

and we did not have enough time to do so. The graph of simulated annealing algorithm running on a single puzzle is shown below.

ConclusionsWe conclude that if we were given

the task of solving all of the puzzles that we provided, we would use the heuristic technique for all puzzles ranging in difficulty from super easy to medium. When the puzzles change from medium to hard we agree that the best algorithm to use would be the exhaustive search because even though it may take longer to complete, it has the correct results every time. The simulated annealing algorithm did not compare very well to its competition and we would have to revisit the algorithm from a different direction if we planned on making it useful in the slightest.

Future WorkIf we were tasked with approaching

this same problem over again in the future, there are a few things that we would like to consider and attempt to implement to vary our project further and approach it from a new angle.

To begin with we would try to have more variation in our puzzles. We tested a range of puzzles from size 7x7 to 20x20. If we had the task again we would try to increase the variance in puzzle sizes as well as increase the number of puzzles we used. Instead of using a different number of

puzzles in each category we would also should have used a fixed number of puzzles for each size and difficulty. Doing so would have made it easier to interpret the data without having to normalize all of it.

We also all agree that if we had more time we would have liked to try and make our “Smarter Algorithm” approach work and test to see if there would have been able to compete with the heuristic algorithm. We ended up not using the Smarter Algorithm because we felt the complexity of the algorithm versus the benefit we would get from using it was not worth pursuing.

Graph 1

Graph 2

Page 9: people.uncw.edupeople.uncw.edu/tagliarinig/Courses/380/F2018 papers and...  · Web viewA Hashi-o-kakero puzzle is a logical reasoning puzzle similar to a word search puzzle ... we

Questions1. Is the Hashi o Kakero problem a

constraint satisfaction problem or an optimization problem?

Answer: Constraint Satisfaction Problem

2. What are two data structures that we used in any of our algorithms?

Answer: Exhaustive Search used a stack and the heuristic technique used 8 queues.

3. What was the computational complexity of one of our algorithms?

Answer: Exhaustive Search: O(n^n)Heuristic Approach: O(n^2)Simulated Annealing: O(n^2)

4. Out of the three algorithms, which one of them was the fastest? Why?

Answer: Heuristic Approach UNTIL difficulty is hard or super hard, where exhaustive is faster.

5. Is solving a Hashi o Kakero puzzle and returning a Boolean value of whether or not it can be solved a P or NP problem?

Answer: NP. Solving an arbitrarily sized Hashi puzzle is an NP-Complete problem. Problems in NP have their solutions verified in polynomial time and all NP-complete problems are in NP and can be reached by reducing any problem in NP.

Works Cited “Breadth-First Search.” Wikipedia, Wikimedia Foundation, 24 Nov. 2018, en.wikipedia.org/wiki/Breadth-first_search.

“Hashi Solving Strategy.” Hashi Solving Strategy - The Bridge Building Logic Puzzle, www.dailyhashi.com/how/strategy.html. “Hashi Techniques.”

Conceptis Puzzles, www.conceptispuzzles.com/index.aspx?uri=puzzle/hashi/techniques.

“Hashiwokakero.” Wikipedia, Wikimedia Foundation, 8 Sept. 2018, en.wikipedia.org/wiki/Hashiwokakero. “ Hashi/Hashiwokakero Puzzles.”

Sudoku Puzzles, www.menneske.no/hashi/7x7/eng/index.html.