Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.

Slides:



Advertisements
Similar presentations
Lecture 19: Parallel Algorithms
Advertisements

1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Backtrack Algorithm for Listing Spanning Trees R. C. Read and R. E. Tarjan (1975) Presented by Levit Vadim.
Reducibility Class of problems A can be reduced to the class of problems B Take any instance of problem A Show how you can construct an instance of problem.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
CHAPTER 11 Coping with NP-completeness. Algorithm Largest Independent Set This algorithm returns α(G), the size of a largest independent set in.
Page 1 Recursion and Exhaustion Yip Lik Yau. Page 2 Why Exhaustion?  Many problems can be solved by brute force in a reasonable amount of time if the.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Structured Graphs and Applications
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 NP-Completeness Objectives: At the end of the lesson, students should be able to: 1. Differentiate between class P, NP, and NPC 2. Reduce a known NPC.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
CHAPTER 4 Searching. Algorithm Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found,
CHAPTER 10 P and NP. Algorithm Crossword Puzzle This algorithm solves a crossword puzzle, represented by a Boolean matrix D[i,j], 1 = i,j = n,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graph Coloring and Hamiltonian cycles Lecture 22 CS 312.
Ch 13 – Backtracking + Branch-and-Bound
Algorithms All pairs shortest path
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.
Lecture 17: Spanning Trees Minimum Spanning Trees.
Dr. Jouhaina Chaouachi Siala
Fixed Parameter Complexity Algorithms and Networks.
1 The Theory of NP-Completeness 2012/11/6 P: the class of problems which can be solved by a deterministic polynomial algorithm. NP : the class of decision.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
TECH Computer Science NP-Complete Problems Problems  Abstract Problems  Decision Problem, Optimal value, Optimal solution  Encodings  //Data Structure.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
CS 312: Algorithm Analysis Lecture #32: Intro. to State-Space Search This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Algorithmics - Lecture 131 LECTURE 13: Backtracking.
COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Hard Problems Sanghyun Park Fall 2002 CSE, POSTECH.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Backtracking & Brute Force Optimization Intro2CS – weeks
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
NPC.
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Theory of Computational Complexity Yusuke FURUKAWA Iwama Ito lab M1.
Hard Problems Some problems are hard to solve.  No polynomial time algorithm is known.  E.g., NP-hard problems such as machine scheduling, bin packing,
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Hard Problems Introduction to NP
BACKTRACKING- GENERAL METHOD
Design and Analysis of Algorithm
Exact Solutions to NP-Complete Problems
ICS 353: Design and Analysis of Algorithms
Back Tracking.
Unit 3 (Part-I): Greedy Algorithms
Unit 4: Dynamic Programming
Chapter 3: Finite Constraint Domains
Backtracking and Branch-and-Bound
ICS 353: Design and Analysis of Algorithms
Instructor: Aaron Roth
Lecture 24 Classical NP-hard Problems
Presentation transcript:

Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion

Basic of Backtracking Applications: Game Theory For every given problem: Input is represented in the form of Tuple [x1..xn]. The solution is generated by selecting proper input from the tuple [x1..xn]. Each selected input must satisfy the criterion function. Since Backtracking is “Selection” based solution, at each stage the selection may be optimized by testing validity of criterion function [constraints]

Basics Basic Principle: Solution is constructed component-wise and at each point the component is tested against the criterion function. The construction of solution continues, if criterion function is satisfied. For example, if there are “n” elements then first component can be (x1..xi) is checked against (p1..pi) and if partial solution and partial criterion function are not matching then remaining part of solution is simply ignored. (x1..xi) is partial solution and (p1..pi) is partial criterion function.

Basics Problem solving using backtracking requires that all the solutions must satisfy the complex set of constraints. These constraints can be classified into two classes: Explicit Constraints Implicit Constraints Explicit Constraints: [Direct] They are set of rules which allows “xi” to take value only from the given set. For example:xi >= 0or xi = 0 or 1

Basics Implicit Constraints: [Indirect] These constraints are the rules that decides which of the tuple in the solution space of “i” satisfies criterion function

Graph Coloring: Algorithm Algorithm solution for problem solved using BACKTRACKING are RECURSIVE The input to algorithm is vertex number present in the graph The algorithm generates the color number assigned to vertex and stores it an array. If the constraint are not matched at any point, then remaining part of algorithm is not executed and new cycle is initiated. The algorithm terminates with a solution satisfying the constraints.

Chromatic Number

Part – I: mcolor This component passes the different values of vertex to “nextvalue” algorithm, and accept the value returned by “nextvalue” algorithm. The value is checked and if found suitable then stored in output array. The output array x[1..n] is solution for the given problem.

Algorithm: nextvalue This algorithm will accept the vertex number from “mcolor” and generates color value for the vertex, with constraint satisfaction. To check constraints following conditions are tested: Let vertex “k” is in process, then all vertices in the graph are tested for adjacency test. If the vertices are adjacent, then, should be different colors The color information is stored in array “x” Hence G[k,j] != 0 and x[k] = x[j] then color is not suitable otherwise suitable.

Hamiltonian Cycle It is a cycle generated on graph, with same vertex as source and destination. The constraints for generating cycle: Each vertex has to be visited once Any edge can be used only once Some of the edges can be skipped, but vertices cannot be skipped. Applications: Travelling salesman problem One of the approach to solve travelling salesman problem algorithmically

Hamiltonian Cycle Algorithm Hcycle(k) { The graph is represented in the form of matrix nxn “k” is an index of vertex to be colored repeat { Nextvalue(k) If (x[k] = 0) then return If (k=n) then Write(x[1:n]) Else Hcycle(k+1) } until(false) }

Nextvalue algorithm for Hcycle Algorithm nextvalue(k) { Repeat { x[k]=(x[k]+1) mod n+1 if (x[k]=0) then return if(G[x[k-1],x[k]!=0) then { For j=1 to k-1 do If(x[j]=x[k] then break If(j=k) then If{(k<n) or [(k=n) and G[x[n],x[1]]!=0)} then Return } } until(false) }

N-Queen Problem Basic: Given a chess board of size nxn, it is required to place “n” queen on the chess board with following constraints: 1. No two queens should be in same row 2. No two queens should be in same column 3. No two queens should be diagonally opposite Why backtracking: if any of above constraint does not satisfies at “k” queen, then it is required to adjust 1..k-1 queens.

Sub-Problem: 4 Queen problem Q1 Q2 Q1 Q2

Sub-Problem: 4 Queen problem Q1 Q2 Q1 Q2 Q3 Q1 Q2 Q3 Q4

Solution to 8 Queen Problem Q1 Q2 Q3 Q4 Q5 Q6 Q7

Second Attempt Q1 Q2 Q3 Q6 Q7

Solution to 8 Queen Problem Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8

Solution to 8 Queen Problem: Constraint logic * Cell: (4,5) = (i,j) Diagonal 1 Diagonal 2 (1,2)(1,8) (2,3)(2,7) (3,4)(3,6) (5,6)(5,4) (6,7)(6,3) (7,8)(7,2) (8,1) Elements of diagonal are assumed as (k,l) Abs(j-l) = abs(i-k)

Algorithm: Part - I The algorithm will generate array x[1..n] to store the column address of the queens and index of array “x” represents row address. For example if at location x[3]=2, then one of the location in matrix is [2,3] for storing third queen. The algorithm is divided into two parts: Part 1: to pass the values of Q’s Part 2: to generate the locations.

Algorithm: Part - I Algorithm Nqueen(k,n) { //Initial value of k=n and n=8 for i = 1 to n do { if place(k,i) then x[k] = i if (k=n) then write x[1..n] else Nqueen(k+1, n) }

Algorithm: Part 2 The place(k,i) accepts different values of “i“ for “k” queen and check the constraints, if satisfied then returns true value. (“i“ represent column) Condition checked: For the “k” queen, in “x” array from [1..k-1] the location should not be used. Similarly diagonal condition is checked.

Algorithm: Part 2 Algorithm place(k,i) { //Assume that k-1 locations are already computed in array “x”. Absolute function is used in the algorithm for j = 1 to k-1 { if(x[j]=i) or (abs(x[j]-i = abs(j-k)) then return false } return true; } Applications: Game designing Security Generating obstacles in space Construction planning