Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

A backtrack data structure and algorithm
BackTracking Algorithms
Solving N+k Queens Using Dancing Links Matthew Wolff CS 499c May 3, 2006.
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.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Shirokuro : A Backtracking Approach Benjamin Bush Faculty Advisors: Dr. Russ Abbott, Dr. Gary Brookfield Department of Computer Science, Department of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Backtracking.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Lecture 5CSE Intro to Cognitive Science1 Algorithmic Thinking III.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
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.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
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.
1 N -Queens via Relaxation Labeling Ilana Koreh ( ) Luba Rashkovsky ( )
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
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.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
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
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Analysis & Design of Algorithms (CSCE 321)
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
Data Structure and Algorithms
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.
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.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CSG3F3/ Desain dan Analisis Algoritma
Problem Solving: Brute Force Approaches
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
CSSE 230 Day 25 Skip Lists.
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.
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Problem Solving: Brute Force Approaches
Back Tracking.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Stacks.
adapted from Recursive Backtracking by Mike Scott, UT Austin
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.
Exercise: Dice roll sum
ITEC324 Principle of CS III
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Agenda  Motivation  Terms  Problem Definition  Timing Results for various N+1 Programs  Future Work

Motivation  NASA EPSCoR grant  (Subcontract # WKURF )  Began working with Chatham and Skaggs in November  Doyle added DLX (Dancing Links) at beginning of Spring '06 semester  Senior Project

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

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

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.

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?  Search for a better way…  Dancing Links

Why “Dancing Links?”  Structure & Algorithm  Comprehendible (Open for Debate…)  Increased performance  DLX is supposedly quicker than a standard backtracking algorithm  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

The Dance Steps  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

Dance Steps, cont.  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

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  (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”

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++

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

References