Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS 353: Design and Analysis of Algorithms Backtracking King Fahd University of Petroleum & Minerals Information & Computer Science Department.

Similar presentations


Presentation on theme: "ICS 353: Design and Analysis of Algorithms Backtracking King Fahd University of Petroleum & Minerals Information & Computer Science Department."— Presentation transcript:

1 ICS 353: Design and Analysis of Algorithms Backtracking King Fahd University of Petroleum & Minerals Information & Computer Science Department

2 BacktrackingICS 353: Design and Analysis of Algorithms Reading Assignment M. Alsuwaiyel, Introduction to Algorithms: Design Techniques and Analysis, World Scientific Publishing Co., Inc. 1999. Chapter 13, Sections 1, 2 and 4. 2

3 BacktrackingICS 353: Design and Analysis of Algorithms Coping with Hard Problems There are three useful methodologies that could be used to cope with problems having no efficient algorithm to solve: A methodic examination of the implicit state space induced by the problem instance under study. suitable for problems that exhibit good average time complexity. A probabilistic notion of accuracy where a solution is a simple decision maker or test that can accurately perform one task (either passing or failing the alternative) and not say much about the complementary option. An iteration through this test will enable the construction of the solution or the increase in the confidence level in the solution to the desired degree. Obtain an approximate solution Only some classes of hard problems admit such polynomial time approximations. Backtracking belongs to the first category. 3

4 BacktrackingICS 353: Design and Analysis of Algorithms Backtracking A systematic technique of searching To reduce the search space Can be considered as an “organized” exhaustive search 4

5 BacktrackingICS 353: Design and Analysis of Algorithms 3-Coloring Problem Optimization Problem Input: G = (V, E), an undirected graph with n vertices and m edges. Output: A 3-coloring of G, if possible. A coloring can be represented as an n-tuple (c 1, c 2,..., c n ) The number of different possible colorings for a graph is........... These possibilities can be represented as a complete ternary tree 5

6 BacktrackingICS 353: Design and Analysis of Algorithms 3-Coloring: Search Tree Search tree of all possible colorings of a graph with 4 vertices (When could this be for a graph with 5 vertices?) 6

7 BacktrackingICS 353: Design and Analysis of Algorithms Example a bc de 7

8 BacktrackingICS 353: Design and Analysis of Algorithms Example In the example Nodes are generated in a depth-first search manner No need to store the whole search tree, just the current active path What is the time complexity of the algorithm in the worst case 8

9 BacktrackingICS 353: Design and Analysis of Algorithms Algorithm 3-ColorRec 9

10 BacktrackingICS 353: Design and Analysis of Algorithms Algorithm 3-ColorIter 10

11 BacktrackingICS 353: Design and Analysis of Algorithms The General Backtracking Method Assume that the solution is of the form (x 1, x 2,..., x i ) where 0  i  n and n is a constant that depends on the problem formulation. Here, the solution is assumed to satisfy certain constraints. i in the case of the 3-coloring problem is fixed. i may vary from one solution to another. 11

12 BacktrackingICS 353: Design and Analysis of Algorithms The General Backtracking Method: Example Consider the following version of the Partition Problem Input: X = {x 1, x 2,..., x n } a set of n integers, and an integer y. Output: Find a subset Y  X such that the sum of its elements is equal to y. For example, consider X = {10, 20, 30, 40, 50, 60} and y = 50. There is more than one solution to this problem: What are they and what is their length? 12

13 BacktrackingICS 353: Design and Analysis of Algorithms The General Backtracking Algorithm (1) Each x i in the solution vector belongs to a finite linearly ordered set X i. The backtracking algorithm considers the elements of the Cartesian product X 1  X 2 ...  X n in lexicographic order. Initially starting with the empty vector. Suppose that the algorithm has detected the partial solution (x 1, x 2,..., x j ). It then considers the vector v = (x 1, x 2,..., x j, x j+1 ). We have the following cases: 1.If v represents a final solution to the problem, the algorithm records it as a solution and either terminates in case only one solution is desired or continues to find other solutions. 2.(The advance Step): If v represents a partial solution, the algorithm advances by choosing the least element in the set X j+2. 13

14 BacktrackingICS 353: Design and Analysis of Algorithms The General Backtracking Algorithm (2) 3.If v is neither a final nor a partial solution, we have two sub-cases: a. If there are still more elements to choose from in the set X j+1, the algorithm sets x j+1 to the next member of X j+1. b.(The Backtrack Step): If there are no more elements to choose from in the set X j+1, the algorithm backtracks by setting x j to the next member of X j. If again there are no more elements to choose from in the set X j, the algorithm backtracks by setting x j  1 to the next member of X j  1, and so on. 14

15 BacktrackingICS 353: Design and Analysis of Algorithms BacktrackRec 15

16 BacktrackingICS 353: Design and Analysis of Algorithms BacktrackIter 16


Download ppt "ICS 353: Design and Analysis of Algorithms Backtracking King Fahd University of Petroleum & Minerals Information & Computer Science Department."

Similar presentations


Ads by Google