CSC 172 DATA STRUCTURES.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

BackTracking Algorithms
Types of Algorithms.
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.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Algorithms in Exponential Time. Outline Backtracking Local Search Randomization: Reducing to a Polynomial-Time Case Randomization: Permuting the Evaluation.
Backtracking CSC 172 SPRING 2004 LECTURE 11. Reminders  Project 3 (mastermind) is due before Spring break  Friday, March 5 th 5PM  Computer Science.
Backtracking.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Search.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
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.
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Source: David Lee Matuszek
Recursion – some examples. Sum of Squares Write Vertical.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Recursion Topic 5.
Breadth First and Depth First
Comments on the “Three Piles” Problem
Computer Science 4 Mr. Gerb
Intro to Computer Science II
CSCI 104 Backtracking Search
8-Queens Puzzle.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 19 More Recursive Backtracking
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Search
Artificial Intelligence Problem solving by searching CSC 361
Types of Algorithms.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Back Tracking.
Analysis and design of algorithm
Types of Algorithms.
searching Concept: Linear search Binary search
Depth-First Searches Introduction to AI.
Tree Searching.
adapted from Recursive Backtracking by Mike Scott, UT Austin
A General Backtracking Algorithm
Backtracking.
Artificial Intelligence
CSE 143 Lecture 18 More Recursive Backtracking
Backtracking and Branch-and-Bound
Types of Algorithms.
CSC 143 Binary Search Trees.
Tree Searching.
Revision I This presentation gives you some hints for solution of selected problems.
ALGORITHM TYPES Divide and Conquer, Dynamic Programming, Backtracking, and Greedy. Note the general strategy from the examples. The classification is neither.
Tree Searching.
Tree Searching.
CSE 143 Lecture 18 More Recursive Backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
David Kauchak CS51A – Spring 2019
Backtracking.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Principles of Object Oriented Programming
Depth-First Searches.
Data Structures & Programming
Presentation transcript:

CSC 172 DATA STRUCTURES

BACKTRACKING Read Weiss 10.5

BACKTRACKING Backtracking is a recursive strategy to explore possible solutions. Every possible solution – exhaustive search “Depth first search” “Branch & Bound”

BACKTRACKING METHODOLOGY View picking a solution as a sequence of choices For each choice, consider every option recursively Return the best solution found

BACKTRACKING Generic enough to be applied to most problems. Probably still take exponential time Exact time analysis of backtracking algorithms can be extremely difficult simpler upperbounds that may not be tight are given.

Design Example

Design Example

Design Example The “N-queens” problem Given an n-by-n checkerboard place n queens on the board in such a way that no two queens are mutually attacking each other. Issues: What data structure? What algorithm?

Data Board Location Level

placeQueen(int [][] board, int x, int y) { board[x][y]++; for (int k=0; k<board[x].length;k++) board[x][k]++ ; removeQueen(int [][] board, int x, int y) { board[x][y]--; board[x][k]--;

boolean nQueen(int [][] board, int[]result, int level) { if (level >= board.length) return true; for (int k=0; k<board[level].length;k++){ if(board[level][k] <= 0) { placeQueen(board,level,k); result[level] = k; if (nQueen(board,result,level+1)) return true ; else //BACKTRACK removeQueen(board,level,k); } return false ;