SlideShare a Scribd company logo
1 of 25
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking
2
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
General Concepts
Algorithm strategy
Approach to solving a problem
May combine several approaches
Algorithm structure
Iterative ⇒ execute action in loop
Recursive ⇒ reapply action to subproblem(s)
Problem type
Satisfying ⇒ find any satisfactory solution
Optimization ⇒ find best solutions (vs. cost metric)
3
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
A short list of categories
Many Algorithm types are to be considered:
Simple recursive algorithms
Backtracking algorithms
Divide and conquer algorithms
Dynamic programming algorithms
Greedy algorithms
Branch and bound algorithms
Brute force algorithms
Randomized algorithms
4
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking
Suppose you have to make a series of
decisions, among various choices, where
You don’t have enough information to know what to
choose
Each decision leads to a new set of choices
Some sequence of choices (possibly more than one)
may be a solution to your problem
Backtracking is a methodical way of trying out
various sequences of decisions, until you find
one that “works”
5
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking Algorithm
Based on depth-first recursive search
Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recur
c) If recursion returns a solution, return it
4. If no choices remain, return failure
Some times called “search tree”
6
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking Algorithm – Example
Find path through maze
Start at beginning of maze
If at exit, return true
Else for each step from current location
– Recursively find path
– Return with first successful step
– Return false if all steps fail
7
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking Algorithm – Example
Color a map with no more than four colors
If all countries have been colored return success
Else for each color c of four colors and country n
– If country n is not adjacent to a country that has been
colored c
Color country n with color c
Recursively color country n+1
If successful, return success
Return failure
8
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking
Start
Success!
Success!
Failure
Problem space consists of states (nodes) and actions
(paths that lead to new states). When in a node can
can only see paths to connected nodes
If a node only leads to failure go back to its "parent"
node. Try other alternatives. If these all lead to failure
then more backtracking may be necessary.
9
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Recursive Backtracking
Pseudo code for recursive backtracking algorithms
If at a solution, return success
for( every possible choice from current state /
node)
Make that choice and take one step along path
Use recursion to solve the problem for the new node / state
If the recursive call succeeds, report the success to the next
high level
Back out of the current choice to restore the state at the
beginning of the loop.
Report failure
10
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Backtracking
Construct the state space tree:
Root represents an initial state
Nodes reflect specific choices made for a solution’s
components.
– Promising and nonpromising nodes
– leaves
Explore the state space tree using depth-first search
“Prune” non-promising nodes
dfs stops exploring subtree rooted at nodes leading to no
solutions and...
“backtracks” to its parent node
11
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Example: The n-Queen problem
Place n queens on an n by n chess board
so that no two of them are on the same
row, column, or diagonal
12
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
State Space Tree of the Four-queens Problem
13
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
The backtracking algorithm
Backtracking is really quite simple--we “explore”
each node, as follows:
To “explore” node N:
1. If N is a goal node, return “success”
2. If N is a leaf node, return “failure”
3. For each child C of N,
3.1. Explore C
3.1.1. If C was successful, return “success”
4. Return “failure”
14
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Exercises
Continue the backtracking search for a solution
to the four-queens problem to find the second
solution to the problem.
A trick to use: the board is symmetric, obtain
another solution by reflections.
Get a solution to the 5-queens problem found by
the back-tracking algorithm?
Can you (quickly) find at least 3 other solutions?
15
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
The m-Coloring Problem and Hamiltonian Problem
2-color
3-color
Hamiltonian Circuit
(use alphabet order to
break the ties)
c d
a
eb
16
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Coloring a map
You wish to color a map with
not more than four colors
red, yellow, green, blue
Adjacent countries must be in
different colors
You don’t have enough information to choose colors
Each choice leads to another set of choices
One or more sequences of choices may (or may not) lead
to a solution
Many coloring problems can be solved with backtracking
17
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Comments
Backtracking provides the hope to solve some problem
instances of nontrivial sizes by pruning non-promising
branches of the state-space tree.
The success of backtracking varies from problem to problem
and from instance to instance.
Backtracking possibly generates all possible candidates in an
exponentially growing state-space tree.
18
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Other Backtracking Problems *
8 Queens
Knight's Tour
Knapsack problem / Exhaustive Search
Filling a knapsack. Given a choice of items with various
weights and a limited carrying capacity find the optimal
load out. 50 lb. knapsack. items are 1 40 lb, 1 32 lb. 2
22 lbs, 1 15 lb, 1 5 lb. A greedy algorithm would
choose the 40 lb item first. Then the 5 lb. Load out =
45lb. Exhaustive search 22 + 22 + 5 = 49.
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Branch and Bound
20
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Branch and Bound ( B& B)
An enhancement of backtracking
Similarity
–A state space tree is used to solve a problem.
Difference
–The branch-and-bound algorithm does not
limit us to any particular way of traversing
the tree.
–Used only for optimization problems (since
the backtracking algorithm requires the using
of DFS traversal and is used for non-
optimization problems.
21
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Branch and Bound
The idea:
Set up a bounding function, which is used to compute a
bound (for the value of the objective function) at a node
on a state-space tree and determine if it is promising
Promising (if the bound is better than the value of the
best solution so far): expand beyond the node.
Nonpromising (if the bound is no better than the value
of the best solution so far): not expand beyond the node
(pruning the state-space tree).
22
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Traveling Salesman Problem
Construct the state-space tree:
A node = a vertex: a vertex in the graph.
A node that is not a leaf represents all the tours that start
with the path stored at that node; each leaf represents a tour
(or non-promising node).
Branch-and-bound: we need to determine a lower bound for
each node
– For example, to determine a lower bound for node [1, 2]
means to determine a lower bound on the length of any
tour that starts with edge 1—2.
Expand each promising node, and stop when all the
promising nodes have been expanded. During this procedure,
prune all the nonpromising nodes.
– Promising node: the node’s lower bound is less than
current minimum tour length.
– Non-promising node: the node’s lower bound is NO less
than current minimum tour length.
23
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Traveling Salesman Problem—Bounding Function 1
Because a tour must leave every vertex exactly once, a
lower bound on the length of a tour is b (lower
bound) minimum cost of leaving every vertex.
The lower bound on the cost of leaving vertex v1 is
given by the minimum of all the nonzero entries in
row 1 of the adjacency matrix.
…
The lower bound on the cost of leaving vertex vn is
given by the minimum of all the nonzero entries in
row n of the adjacency matrix.
Note: This is not to say that there is a tour with this
length. Rather, it says that there can be no shorter tour.
Assume that the tour starts with v1.
24
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Traveling Salesman Problem—Bounding Function 2
Because every vertex must be entered and exited exactly once,
a lower bound on the length of a tour is the sum of the
minimum cost of entering and leaving every vertex.
For a given edge (u, v), think of half of its weight as the
exiting cost of u, and half of its weight as the entering cost of
v.
The total length of a tour = the total cost of visiting( entering
and exiting) every vertex exactly once.
The lower bound of the length of a tour = the lower bound of
the total cost of visiting (entering and exiting ) every vertex
exactly once.
– Calculation:
for each vertex, pick top two shortest adjacent edges
(their sum divided by 2 is the lower bound of the total
cost of entering and exiting the vertex);
add up these summations for all the vertices.
Assume that the tour starts with vertex a and that b is visited
before c.
25
PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi
Traveling salesman example 2

More Related Content

What's hot

data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back trackingAbinaya B
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIADheeraj Kataria
 
Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 QueenHa Ninh
 
Knights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingKnights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingAbhishek Singh
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structuresKàŕtheek Jåvvàjí
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 

What's hot (20)

Backtracking
BacktrackingBacktracking
Backtracking
 
Backtracking
BacktrackingBacktracking
Backtracking
 
data structures- back tracking
data structures- back trackingdata structures- back tracking
data structures- back tracking
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
Backtracking
BacktrackingBacktracking
Backtracking
 
21 backtracking
21 backtracking21 backtracking
21 backtracking
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 Queen
 
Sudoku
SudokuSudoku
Sudoku
 
Knights tour on chessboard using backtracking
Knights tour on chessboard using backtrackingKnights tour on chessboard using backtracking
Knights tour on chessboard using backtracking
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
Unit 5 jwfiles
Unit 5 jwfilesUnit 5 jwfiles
Unit 5 jwfiles
 
Sudoku
SudokuSudoku
Sudoku
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Q
QQ
Q
 

Viewers also liked

Viewers also liked (8)

2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
BFS
BFSBFS
BFS
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 

Similar to PSUCS 311 Backtracking Algorithm Design

ETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptxETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptxRahulSingh190790
 
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptfdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptKartikGupta711
 
Branch and bound technique
Branch and bound techniqueBranch and bound technique
Branch and bound techniqueishmecse13
 
AI_03_Solving Problems by Searching.pptx
AI_03_Solving Problems by Searching.pptxAI_03_Solving Problems by Searching.pptx
AI_03_Solving Problems by Searching.pptxYousef Aburawi
 
Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxRatnakar Mikkili
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring AlgorithmsSri Prasanna
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxRaviKiranVarma4
 
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptxbcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptxB.T.L.I.T
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programmingAmisha Narsingani
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptBinayakMukherjee4
 
Introduction to machine learning
Introduction to machine learningIntroduction to machine learning
Introduction to machine learningKnoldus Inc.
 
Optimization problems
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptdakccse
 

Similar to PSUCS 311 Backtracking Algorithm Design (20)

ETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptxETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptx
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptfdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Branch and bound technique
Branch and bound techniqueBranch and bound technique
Branch and bound technique
 
AI_03_Solving Problems by Searching.pptx
AI_03_Solving Problems by Searching.pptxAI_03_Solving Problems by Searching.pptx
AI_03_Solving Problems by Searching.pptx
 
Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptx
 
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
 
Exploring Algorithms
Exploring AlgorithmsExploring Algorithms
Exploring Algorithms
 
Back tracking
Back trackingBack tracking
Back tracking
 
DAA-Module-5.pptx
DAA-Module-5.pptxDAA-Module-5.pptx
DAA-Module-5.pptx
 
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptxPPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
 
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptxbcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
DS ppt
DS pptDS ppt
DS ppt
 
Introduction to machine learning
Introduction to machine learningIntroduction to machine learning
Introduction to machine learning
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
 

More from Krish_ver2

5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithmKrish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing extKrish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashingKrish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 

More from Krish_ver2 (20)

5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 

Recently uploaded

Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 

Recently uploaded (20)

Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 

PSUCS 311 Backtracking Algorithm Design

  • 1. PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking
  • 2. 2 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi General Concepts Algorithm strategy Approach to solving a problem May combine several approaches Algorithm structure Iterative ⇒ execute action in loop Recursive ⇒ reapply action to subproblem(s) Problem type Satisfying ⇒ find any satisfactory solution Optimization ⇒ find best solutions (vs. cost metric)
  • 3. 3 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi A short list of categories Many Algorithm types are to be considered: Simple recursive algorithms Backtracking algorithms Divide and conquer algorithms Dynamic programming algorithms Greedy algorithms Branch and bound algorithms Brute force algorithms Randomized algorithms
  • 4. 4 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Suppose you have to make a series of decisions, among various choices, where You don’t have enough information to know what to choose Each decision leads to a new set of choices Some sequence of choices (possibly more than one) may be a solution to your problem Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works”
  • 5. 5 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Algorithm Based on depth-first recursive search Approach 1. Tests whether solution has been found 2. If found solution, return it 3. Else for each choice that can be made a) Make that choice b) Recur c) If recursion returns a solution, return it 4. If no choices remain, return failure Some times called “search tree”
  • 6. 6 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Algorithm – Example Find path through maze Start at beginning of maze If at exit, return true Else for each step from current location – Recursively find path – Return with first successful step – Return false if all steps fail
  • 7. 7 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Algorithm – Example Color a map with no more than four colors If all countries have been colored return success Else for each color c of four colors and country n – If country n is not adjacent to a country that has been colored c Color country n with color c Recursively color country n+1 If successful, return success Return failure
  • 8. 8 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Start Success! Success! Failure Problem space consists of states (nodes) and actions (paths that lead to new states). When in a node can can only see paths to connected nodes If a node only leads to failure go back to its "parent" node. Try other alternatives. If these all lead to failure then more backtracking may be necessary.
  • 9. 9 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Recursive Backtracking Pseudo code for recursive backtracking algorithms If at a solution, return success for( every possible choice from current state / node) Make that choice and take one step along path Use recursion to solve the problem for the new node / state If the recursive call succeeds, report the success to the next high level Back out of the current choice to restore the state at the beginning of the loop. Report failure
  • 10. 10 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Backtracking Construct the state space tree: Root represents an initial state Nodes reflect specific choices made for a solution’s components. – Promising and nonpromising nodes – leaves Explore the state space tree using depth-first search “Prune” non-promising nodes dfs stops exploring subtree rooted at nodes leading to no solutions and... “backtracks” to its parent node
  • 11. 11 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Example: The n-Queen problem Place n queens on an n by n chess board so that no two of them are on the same row, column, or diagonal
  • 12. 12 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi State Space Tree of the Four-queens Problem
  • 13. 13 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi The backtracking algorithm Backtracking is really quite simple--we “explore” each node, as follows: To “explore” node N: 1. If N is a goal node, return “success” 2. If N is a leaf node, return “failure” 3. For each child C of N, 3.1. Explore C 3.1.1. If C was successful, return “success” 4. Return “failure”
  • 14. 14 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Exercises Continue the backtracking search for a solution to the four-queens problem to find the second solution to the problem. A trick to use: the board is symmetric, obtain another solution by reflections. Get a solution to the 5-queens problem found by the back-tracking algorithm? Can you (quickly) find at least 3 other solutions?
  • 15. 15 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi The m-Coloring Problem and Hamiltonian Problem 2-color 3-color Hamiltonian Circuit (use alphabet order to break the ties) c d a eb
  • 16. 16 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Coloring a map You wish to color a map with not more than four colors red, yellow, green, blue Adjacent countries must be in different colors You don’t have enough information to choose colors Each choice leads to another set of choices One or more sequences of choices may (or may not) lead to a solution Many coloring problems can be solved with backtracking
  • 17. 17 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Comments Backtracking provides the hope to solve some problem instances of nontrivial sizes by pruning non-promising branches of the state-space tree. The success of backtracking varies from problem to problem and from instance to instance. Backtracking possibly generates all possible candidates in an exponentially growing state-space tree.
  • 18. 18 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Other Backtracking Problems * 8 Queens Knight's Tour Knapsack problem / Exhaustive Search Filling a knapsack. Given a choice of items with various weights and a limited carrying capacity find the optimal load out. 50 lb. knapsack. items are 1 40 lb, 1 32 lb. 2 22 lbs, 1 15 lb, 1 5 lb. A greedy algorithm would choose the 40 lb item first. Then the 5 lb. Load out = 45lb. Exhaustive search 22 + 22 + 5 = 49.
  • 19. PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Branch and Bound
  • 20. 20 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Branch and Bound ( B& B) An enhancement of backtracking Similarity –A state space tree is used to solve a problem. Difference –The branch-and-bound algorithm does not limit us to any particular way of traversing the tree. –Used only for optimization problems (since the backtracking algorithm requires the using of DFS traversal and is used for non- optimization problems.
  • 21. 21 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Branch and Bound The idea: Set up a bounding function, which is used to compute a bound (for the value of the objective function) at a node on a state-space tree and determine if it is promising Promising (if the bound is better than the value of the best solution so far): expand beyond the node. Nonpromising (if the bound is no better than the value of the best solution so far): not expand beyond the node (pruning the state-space tree).
  • 22. 22 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Traveling Salesman Problem Construct the state-space tree: A node = a vertex: a vertex in the graph. A node that is not a leaf represents all the tours that start with the path stored at that node; each leaf represents a tour (or non-promising node). Branch-and-bound: we need to determine a lower bound for each node – For example, to determine a lower bound for node [1, 2] means to determine a lower bound on the length of any tour that starts with edge 1—2. Expand each promising node, and stop when all the promising nodes have been expanded. During this procedure, prune all the nonpromising nodes. – Promising node: the node’s lower bound is less than current minimum tour length. – Non-promising node: the node’s lower bound is NO less than current minimum tour length.
  • 23. 23 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Traveling Salesman Problem—Bounding Function 1 Because a tour must leave every vertex exactly once, a lower bound on the length of a tour is b (lower bound) minimum cost of leaving every vertex. The lower bound on the cost of leaving vertex v1 is given by the minimum of all the nonzero entries in row 1 of the adjacency matrix. … The lower bound on the cost of leaving vertex vn is given by the minimum of all the nonzero entries in row n of the adjacency matrix. Note: This is not to say that there is a tour with this length. Rather, it says that there can be no shorter tour. Assume that the tour starts with v1.
  • 24. 24 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Traveling Salesman Problem—Bounding Function 2 Because every vertex must be entered and exited exactly once, a lower bound on the length of a tour is the sum of the minimum cost of entering and leaving every vertex. For a given edge (u, v), think of half of its weight as the exiting cost of u, and half of its weight as the entering cost of v. The total length of a tour = the total cost of visiting( entering and exiting) every vertex exactly once. The lower bound of the length of a tour = the lower bound of the total cost of visiting (entering and exiting ) every vertex exactly once. – Calculation: for each vertex, pick top two shortest adjacent edges (their sum divided by 2 is the lower bound of the total cost of entering and exiting the vertex); add up these summations for all the vertices. Assume that the tour starts with vertex a and that b is visited before c.
  • 25. 25 PSUCS 311 – Design and Algorithms Analysis Dr. Mohamed Tounsi Traveling salesman example 2

Editor's Notes

  1. Careful to draw the state-space by coloring vertices in alphabetical order. This way you get some actual “backtracking” – most small examples of graph coloring yield a solution so fast that it is hard to appreciate what is going on.
  2. Generate all n-bit binary strings.