Solving N+k Queens Using Dancing Links Matthew Wolff CS 499c May 3, 2006.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Parallel List Ranking Advanced Algorithms & Data Structures Lecture Theme 17 Prof. Dr. Th. Ottmann Summer Semester 2006.
Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006.
A backtrack data structure and algorithm
BackTracking Algorithms
Fundamentals of Python: From First Programs Through Data Structures
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Shirokuro : A Backtracking Approach Benjamin Bush Faculty Advisors: Dr. Russ Abbott, Dr. Gary Brookfield Department of Computer Science, Department of.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Advanced Topics in Algorithms and Data Structures 1 Two parallel list ranking algorithms An O (log n ) time and O ( n log n ) work list ranking algorithm.
Backtracking.
analysis, plug ‘n’ chug, & induction
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Please snarf the code for today’s class. Then think about the famous 8-Queen’s Puzzle. The question is: is there a way to arrange 8 queens on a (8x8) chessboard.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structure Introduction.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
DGrid: A Library of Large-Scale Distributed Spatial Data Structures Pieter Hooimeijer,
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
LINKED LISTS.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
CSG3F3/ Desain dan Analisis Algoritma
Problem Solving: Brute Force Approaches
CSC 321: Data Structures Fall 2017
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Decrease-and-Conquer Approach
CSCI 104 Backtracking Search
CSC 421: Algorithm Design & Analysis
Analysis and design of algorithm
Problem Solving: Brute Force Approaches
Applied Algorithms (Lecture 17) Recursion Fall-23
Optimizing Malloc and Free
Building Java Programs
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
CSC 421: Algorithm Design & Analysis
CSC 321: Data Structures Fall 2018
Presentation transcript:

Solving N+k Queens Using Dancing Links Matthew Wolff CS 499c May 3, 2006

Agenda  Motivation  Definitions  Problem Definition  Solved Problems with Results  Future Work

Motivation  NASA EPSCoR grant  Began working with Chatham and Skaggs in November  Doyle added DLX (Dancing Links) at beginning of semester  New to me (and the rest of the team, I think)  A lot more work!

Category of Problems  8 Queens  8 attacking queens on an 8x8 chess board  N Queens  N attacking queens on an NxN chess board  N+1 Queens  N+1 attacking queens on an NxN chess board  1 Pawn used to block two or more attacking queens  N+k Queens  N+k attacking queens on an NxN chess board  k Pawns used to block numerous attacking queens

8 Queens Example

Solutions  Solutions – A class of Queen placements such that no two Queens can attack each other.  Fundamental Solutions – A class of solutions such that all members of the class are simply rotations or reflections of one another.  Given the set of solutions, a set of fundamental solutions can be generated. And vice versa  The fundamental solutions are a subset of all solutions.

Fundamental Solutions for 8 Queens

Recursion  "To understand recursion, one must first understand recursion" -- Tina Mancuso understandrecursion understandrecursionunderstandrecursion understandrecursion  “A function is recursive if it can be called while active (on the stack).”  i.e. It calls itself

Recursion in Art

Recursion in Computer Science  // precondition: n >= 0 // postcondition: n! is returned factorial (int n) { if (n == 1) or (n == 0) return 1; else return (n*factorial(n-1)); }

Backtracking  An example of backtracking is used in a depth-first search in a binary tree:  Let t be a binary tree  depthfirst(t) { if (t is not empty) { access root item of t; depthfirst(left(t)); depthfirst(right(t)); } }

Backtracking Example Output: A – B – D – E – H – I – C – F - G

4 Queens Backtracking Example  Solved by iterating over all solutions, using backtracking

N Queens  Extend to N board  Similar to 8 Queens  Use a more general board of size NxN  Same algorithm as 8 Queens

N+1 Queens  What happens when you add a pawn?  For a large enough board, we can add an extra Queen  Slightly more complex  Another loop over Pawn placements  More checking for fundamental solutions

8x8 Board, 1 Pawn

Main Focus: N+k Queens  Why?  Instead of focusing on specific solutions (N+1, N+2,...), we will be able to solve any general statement (N+k) of the “Queens Problem.”  Implementing a solution is rigorous and utilizes many important techniques in computer science such as parallel algorithm development, recursion, and backtracking

Chatham, Fricke, Skaggs  Proved N+k queens can be placed on an NxN board with k pawns.

What did I do?  Translate Chatham’s Python Code (for N+1) into a sequential C++ program  Modify sequential C++ code to run in Parallel with MPI  Design and implement the N+k Queens solution  (Iterative) k * (Recursive) N = No.  Dancing Links

N+1 Sequential Solution  Optimized to exploit the geometry of the problem  Pawns may not be placed in first or last column or row  Pawns are only placed on roughly 1/8 of the board (in a wedge shape)  The Need for Speed  Even with optimizations, program can run for days for large N  Roughly 6x faster than Python

N+1 Results NSolutions Fundamental Solutions

Python versus C++

N+1 Parallel Solution  Almost exactly the same as Sequential except:  For-loop over Pawn Placements is distributed over p processors  Evidence suggests that more solutions are found when the Pawns are near the center of the chess board  More solutions implies more computations, thus more time  Pawns are specially numbered for more optimization

Pawn Placements for Parallel N+1 Queens Solution

N+1 Queens, Parallel vs. Sequential C++

N+K – what to do?  N+k presents a very large problem  1 Pawn meant an extra for loop around everything  k Pawns would imply k for loops around everything  Dynamic for loops? “That’s Unpossible” – Ralph Wiggum  Search for a better way…  Dancing Links

Why “Dancing Links?”  Structure & Algorithm  Comprehendible (Open for Debate…)  Increased performance  Parallel computing is utilized mainly for performance advantages… so, why run a sub- par algorithm (backtracking) when the goal is to achieve the quickest run-time?  Made popular by Knuth via his circa 2000 article

“The Universe”  Multi-Dimensional structure composed of circular, doubly linked-lists  Each row and column is a circular, doubly linked-list

Visualization of “The Universe”

The Header node  The “root” node of the entire structure  Members:  Left pointer  Right pointer  Name (H)  Size: Number of “Column Headers” in its row.

Column Headers  Column Headers are nodes linked horizontally with the Header node  Members:  Left pointer  Right pointer  Up pointer  Down pointer  Name (R w, F x, A y, or B z )  Size: the number of “Column Objects” linked vertically in their column

Column Objects  Grouped in two ways:  All nodes in the same column are members of the same Rank, File, or Diagonal on the chess board  Linked horizontally in sets of 4  {R w, F x, A y, or B z }  Each set represents a space on the chess board  Same members as Column Headers, but with an additional “top pointer” which points directly to the Column Header

Mapping the Chess Board

The Amazing TechniColor Chess Board

Dance, Dance: Revolution  The entire algorithm is based off of two simple ideas:  Cover: remove an item  Node.right.left = Node.left  Node.left.right = Node.right  Uncover: insert the item back  Node.right.left = Node  Node.left.right = Node

The Latest Dance Craze  void search(k): if (header.right == header) {finished} else c = choose_column() cover(c) r = c.down while (r != c) j = r.right while (j != r) cover(j.top) j = j.right # place next queen search(k+1) c = r.top j = r.left while (j != r) uncover(j.top) j = j.left # completed search(k) uncover(c) {finished}

1x1 Universe: Before

1x1 Universe: After

The “Aha!” Moment  N Queens worked, now what?  N+k Queens… hmm  What needs to be modified?  Do I have to start from scratch?!?!  ….  Nope  Nope  As it turns out, the way the universe is built is the only needed modification to go from N Queens to N+k Queens

Modifying for N+k Queens  1 Pawn will cut its row, column, and diagonal into 2 separate pieces  Just add these 4 new Column Headers to the universe, along with their respective Column Objects  k Pawns will cut their rows, columns, and diagonals into…. ? separate pieces.  Still need to add these extra Column Headers, but how many are there and how many Column Objects are in each?

It Slices, It Dices…  Find ALL valid Pawn Placements  Wolff’s Theorem:  (N-2) 2 choose k = lots of combinations  Then build 4 NxN arrays  One for each Rank, File, and Diagonal  “Scan” through arrays:  For Ranks: scan horizontally (Files: vertically, Diagonals: diagonally)  Reach the end or a Pawn, increment 1

Example of Rank “Scan”

And now for the moment you’ve all been waiting for!  DRUM ROLL! ….  GRAPHS AND STUFF!

N+1 Queens Varying Language, Algorithm

N+1 Queens Parallel Backtracking vs. DLX

N+1 Queens Sequential DLX vs. Parallel DLX

Interesting Tidbit: Sequential DLX vs. Parallel C++

N+k Results  Still running in Lappin 241L…  Maybe next week  Maybe next week

Further Work  Finish module that will properly count the fundamental solutions  Since run-times will decrease over time (newer processors, etc), compare amount of updates to the structure to see if Dancing Links is actually doing less work, which would explain the decrease in run- time.

Future Work (Project)  Find a more efficient way to account for k Pawns in the universe  Using Dancing Links itself?  Find patterns so parallelization can be done efficiently, similar to N+1 specific parallel program  Find more results for larger values of N and k  May involve use of Genetic Algorithms  Domination Problem?  Fewest number of Queens to cover entire chess board.

Questions?  Thank you!  Dr. Chatham  Dr. Doyle  Mr. Skaggs

References