Backtracking. Chapter 5

Similar documents
Chapter 5 Backtracking 1

BackTracking Introduction

Chapter-6 Backtracking

The problem: given N, find all solutions of queen sets and return either the number of solutions and/or the patterned boards.

Backtracking and Branch-and-Bound

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Algorithm Design And Analysis Asst. Prof. Ali Kadhum Idrees The Sum-of-Subsets Problem Department of Computer Science College of Science for Women

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

Trees and Tree Traversal

UNIT 4 Branch and Bound

DARSHAN INST. OF ENGG. & TECH.

Data Structures (CS 1520) Lecture 28 Name:

CS 231: Algorithmic Problem Solving

Data Structures (CS 1520) Lecture 28 Name:

Backtracking is a refinement of the brute force approach, which systematically searches for a

Recitation 9. Prelim Review

CS301 - Data Structures Glossary By

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

LECTURE 11 TREE TRAVERSALS

CS24 Week 8 Lecture 1

Assignment No 2 (Group B)

Chapter 20: Binary Trees

CMSC Introduction to Algorithms Spring 2012 Lecture 16

Search and Optimization

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 2014

Chapter 6 Backtracking Algorithms. Backtracking algorithms Branch-and-bound algorithms

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Methods and Models for Combinatorial Optimization Exact methods for the Traveling Salesman Problem

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Figure 1. A breadth-first traversal.

Programming II (CS300)

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Searching in Graphs (cut points)

[ DATA STRUCTURES ] Fig. (1) : A Tree

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

Recursive Data Structures and Grammars

) $ f ( n) " %( g( n)

Constraint Satisfaction Problems

Backtracking. Examples: Maze problem. The bicycle lock problem: Consider a lock with N switches, each of which can be either 0 or 1.

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CSE 530A. B+ Trees. Washington University Fall 2013

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Graphs & Digraphs Tuesday, November 06, 2007

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9. Heap : Priority Queue

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Binary Trees, Binary Search Trees

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

6. Algorithm Design Techniques

Towards a Memory-Efficient Knapsack DP Algorithm

Search Algorithms. IE 496 Lecture 17

COMP3121/3821/9101/ s1 Assignment 1

SELF-BALANCING SEARCH TREES. Chapter 11

Trees. (Trees) Data Structures and Programming Spring / 28

Chapter 9. Priority Queue

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Algorithm Design Techniques (III)

Name CPTR246 Spring '17 (100 total points) Exam 3

Analysis of Algorithms

Algorithm Design Methods. Some Methods Not Covered

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Geometric data structures:

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Search Algorithms for Discrete Optimization Problems

Data Structures, Algorithms & Data Science Platforms

Parallel Programming. Parallel algorithms Combinatorial Search

State Space Search. Many problems can be represented as a set of states and a set of rules of how one state is transformed to another.

Decision tree learning

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

Garbage Collection: recycling unused memory

EE 368. Weeks 5 (Notes)

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

Chronological Backtracking Conflict Directed Backjumping Dynamic Backtracking Branching Strategies Branching Heuristics Heavy Tail Behavior

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

( ) n 3. n 2 ( ) D. Ο

A surefire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the examination of all or some

3 SOLVING PROBLEMS BY SEARCHING

Basic Data Structures (Version 7) Name:

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

A surefire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the examination of all or some

CS350: Data Structures B-Trees

Brute Force: Selection Sort

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

13.4 Deletion in red-black trees

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Chapter 2: Basic Data Structures

Algorithms for Data Structures: Uninformed Search. Phillip Smith 19/11/2013

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Graph. Vertex. edge. Directed Graph. Undirected Graph

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

( ) 1 B. 1. Suppose f x

Uninformed Search Methods

B-Trees. Introduction. Definitions

13.4 Deletion in red-black trees

Friday Four Square! 4:15PM, Outside Gates

Transcription:

1 Backtracking Chapter 5

2 Objectives Describe the backtrack programming technique Determine when the backtracking technique is an appropriate approach to solving a problem Define a state space tree for a given problem Define when a node in a state space tree for a given problem is promising/non-promising Create an algorithm to prune a state space tree Create an algorithm to apply the backtracking technique to solve a given problem

3 Finding Your Way Thru a Maze Follow a path until a dead end is reached Go back until reaching a fork Pursue another path Suppose there were signs indicating path leads to dead end? Sign positioned near beginning of path time savings enormous Sign positioned near end of path very little time saved https://scratch.mit.edu/projects/11710850/

4 Back Tracking Can be used to solve NP-Complete problems such as 0-1 Knapsack more efficiently

Backtracking vs Dynamic Programming 5 Dynamic Programming subsets of a solution are generated Backtracking Technique for deciding that some subsets need not be generated Efficient for many large instances of a problem (but not all)

6 Backtracking Technique Solve problems in which a sequence of objects is chosen from a set Sequence satisfies some criterion Modified DFS of a rooted tree Pre-order traversal General-purpose algorithm does not specify order children visited we will use left to right

Procedure called by passing root at the top level 7 void depth_first_tree_search(node v) { node u; visit v for( each child u of v) depth_first_tree_search(u); }

Figure 5.1 8

9 Backtracking Procedure After determining a node cannot lead to a solution, backtrack to the node s parent and proceed with the search on the next child Non-promising node: when the node is visited, it is determined the node cannot lead to a solution Promising node: may lead to a solution Backtracking DFS of state space tree Pruning state space tree: if a node is determined to be non-promising, back track to its parent

10 Backtracking Procedure Pruned State Space Tree: sub-tree consisting of visited nodes

11 N-Queen Problem Goal: position n queens on a n x n board such that no two queens threaten each other No two queens may be in the same row, column, or diagonal Sequence: n positions where queens are placed Set: n 2 positions on the board Criterion: no two queens threaten each other

e.g. 4-Queen Problem Solution 12

Figure 5.2 13

14 4-Queen Problem Assign each queen a different row Check which column combinations yield solutions Each queen can be in any one of four columns: 4 x 4 x 4 x 4 = 256

Construct State-Space Tree Candidate Solutions 15 Root start node Column choices for first queen stored at level-1 nodes Column choices for second queen stored at level-2 nodes Etc. Path from root to a leaf is candidate solution Check each candidate solution in sequence starting with left-most path

Construct State-Space Tree Candidate Solutions 16 Example Figure 5.2 no point in checking any candidate solutions from 2,1

17 Pruning DFS of state space tree Check to see if each node is promising If a node is non-promising, backtrack to node s parent Pruned state space tree subtree consisting of visited nodes Promising function application dependent Promising function n-queen problem: returns false if a node and any of the node s ancestors place queens in the same column or diagonal

18 checknode Backtracking algorithm for the n-queens Problem State space tree implicit tree not actually created Values in current branch under investigation kept track of Algorithm inefficient Checks node promising after passing it to procedure Activation records checked and pushed onto stack for non-promising nodes

19 expand Improves efficiency Check to see if node is promising before passing the node

Algorithm 5.1 Backtracking Algorithm for N-Queens Problem 20 All solutions to N-Queens problem Promising function: 2 queens same row? col(i) == col(k) 2 queens same diagonal? col(i) col(k) == i k col(i) col(k) == k - i

Analysis of queens theoretically difficult 21 Upper bound on number of nodes checked in pruned state space tree by counting number of nodes in entire state space tree: 1 node level 0 n nodes level 1 n 2 nodes level 2... N n nodes level n

Total Number of nodes 22

23 Let n = 8 Number of nodes in state space tree = 19173961 Purpose of backtracking is to avoid checking many of these nodes Difficult to theoretically analyze savings by backtracking

Illustrate backtracking savings by executing code and counting nodes checked 24 Algorithm1 DFS of state space tree without backtracking Algorithm2 checks no two queens same row or same column

Table 5.1 25

26 Sum-of-Subsets Problem Let S = {s 1, s 2,..., s n } Let W be a positive integer Find every S S such that

27 Example S = {w 1 = 5, w 2 = 6, w 3 = 10, w 4 = 11, w 5 = 16} and W=21 Solutions: {w 1, w 2, w 3 } : 5 + 6 + 10 = 21 {w 1, w 5 } : 5 + 16 = 21 {w 3, w 4 }: 10 + 11 = 21

28 Example 5.3 n = 3 W = 6 w 1 = 2 w 2 = 4 w 3 = 5

Figure 5.8 state space tree 29

30 Prune the Tree Sort weights in non-decreasing order before search Let weight be the sum of weights that have been included up to a node at level i: node at level I is non-promising if weight + w i+1 > W Let total be the total weight of the remaining weights at a given node. A node is non-promising if weight + total < W

Figure 5.9 31

Algorithm 5.4 Backtracking Algorithm for Sum-of-Subsets 32 Total number of nodes in the state space searched by Algorithm 5.4 1 + 2 + 2 2 +... + 2 n = 2 n+1 1 Worst case could be better i.e. for every instance only a small portion of state space tree is searched Not the case For each n, it is possible to construct an instance for which algorithm visits exponentially large number of nodes even if only seeking one solution

33 0-1 Knapsack Problem Optimization problem State space tree similar to Sum-of-Subsets problem Best solution so far