0% found this document useful (0 votes)
10 views

Session 5

Uploaded by

Venkata Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Session 5

Uploaded by

Venkata Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

DATA DRIVEN

ARTIFICIAL
INTELLIGENT SYSTEMS
SESSION NO: 5

TOPIC: CONSTRAINT SATISFACTION PROBLEM AND BACKTRACKING AND FORWARD


CHECKING
TOPICS TO BE COVERED

• CSP
• Backtracking
• Forward checking

2
TOPICS TO BE COVERED

• 1.CSP
• 2.Backtracking
• 3. Forward checking

3
DEFINITION OF CSP (CONSTRAINT
SATISFACTION PROBLEM)

CSP:
A state is an assignment of values to some variables.
• state is defined by variables Xi with values from domain Di
• goal test is a set of constraints specifying allowable combinations of
values for subsets of variables

4
SOME TERMS IN CSP

• Complete assignment : every variable has a value i.e. no empty


variable .
• Partial assignment - some variables have no values.
• Consistent assignment - assignment does not violate any
constraints
• A solution is a complete and consistent assignment.
• A solution may have to maximize an objective function
5
• Special case of search problem
• Domain can be discrete or continuous
• Order in which constraints are satisfied is immaterial i.e.
commutative
• The order in which variables and values are selected is
immaterial

6
CONSTRAINT SATISFACTION PROBLEM

• A CSP is defined by
• a set of variables ex: Xi
• a domain of values for each variable ex: Vi
• a set of constraints between variables i.e. set of conditions

• A solution is
• an assignment of a value to each variable that satisfies the constraints
i.e. an answer to a problem.

7
EXAMPLE: MAP-COLORING

MAP OF AUSTRALIA SPECIFICATIONS


• Variables WA, NT, Q, NSW, V, SA,
T
• Domains Di = {red,green,blue}
• Constraints: adjacent regions must
have different colors
• e.g., WA ≠ NT

8
MAP-COLORING

SOLUTIONS ARE COMPLETE AND


MAP CONSISTENT ASSIGNMENTS
• e.g., WA = red, NT = green, Q = red,
NSW = green, V = red, SA = blue, T =
green
• A state may be complete or incomplete

9
CONSTRAINT GRAPH

BINARY CSP: EACH CONSTRAINT RELATES TWO


VARIABLES NT Q
CONSTRAINT GRAPH: NODES ARE VARIABLES, ARCS
ARE CONSTRAINTS
W
A NS
W
SA

10
TYPES OF CONSTRAINTS

• Unary constraints involve a single variable,


• e.g., SA ≠ green

• Binary constraints involve pairs of variables,


• e.g., SA ≠ WA

• Higher-order constraints involve 3 or more variables,


• e.g., crypt arithmetic column constraints

11
• Variables-set of characters
• Code – Domain (set of
values)
• Constraint- any value must
be in the range-0-9 and no
value should be repeated.

12
• Initial state: none of the
variables has a value (color)
• Successor state (intermediate):
one of the variables without a
value will get some value.
• Goal(solution): all variables
have a value and none of the
constraints is violated.

13
BACKTRACKING SEARCH

• Every solution appears at depth n with n variables


- use depth-first search
• Depth-first search for CSPs with single-variable
assignments is called backtracking search
• Backtracking search is the basic uninformed algorithm
( general-purpose search algorithms which operates in
brute force-way. )for CSPs
• Can solve n-queens problem for n ≈ 25

14
• Backtracking search uses a brute force approach for finding the desired output.
• Brute force finds all solutions to a problem.
• Solutions that do not meet constraints will be removed.
• A search tree known as the state-space tree is used to find these solutions.
• building a solution step by step, increasing levels over time, using recursion.
• A backtracking algorithm also uses the depth-first search method.

15
ALGORITHM

16
HOW DOES A BACKTRACKING ALGORITHM WORK?

• The algorithm seeks a path leading to a feasible solution with some intermediate
checkpoints.
• If the checkpoints do not lead to a viable solution, the problem can return to the
checkpoints and take another path to find a feasible solution.

17
HOW DOES A BACKTRACKING ALGORITHM WORK?

• The final algorithm is as follows:


• Step 1: Return success if the current point gives a desired solution.
• Step 2: Otherwise, if all paths have been exhausted (i.e., the current point is an endpoint),
return failure because there is no feasible solution.
• Step 3: If the current point is not an endpoint, backtrack and explore other points, then
repeat the preceding steps-1 and 2.

18
WHEN TO USE A BACKTRACKING ALGORITHM?

• The following are the scenarios to use the backtracking:


• To find a feasible solution to a decision problem.
• To find an effective solution to optimization problems.
• To find all feasible solutions to the enumeration problem.
• To find the solution to a problem does not have a time limit.
• Note: Backtracking, on the other hand, is not regarded as an optimal problem-solving
technique.

19
TYPES OF BACKTRACKING ALGORITHM

• Backtracking algorithms are classified into two types:


1. Algorithm for recursive backtracking
2. Non-recursive backtracking algorithm

20
APPLICATIONS OF BACKTRACKING ALGORITHM

• 1. To Find All Hamiltonian Paths Present in a Graph.


• 2. To Solve the N Queen Problem.
• 3. Maze Solving Problems
• 4. The Knight's Tour Problem

21
TO FIND ALL HAMILTONIAN PATHS PRESENT IN A GRAPH. (ABFECD
IS A HAMILTON PATH)

22
TO SOLVE THE N QUEEN PROBLEM.

• placing n queens on the nxn chessboard


so that no two queens attack each other
is known as the n-queens problem.
• Return all distinct solutions to the n-
queens puzzle given an integer n in any
order.
• Each solution has a unique board
configuration for the placement of the n-
queens

23
DEPTH-FIRST SEARCH

• Expand deepest unexpanded/unvisited node


• Generate only one child at a time.
• Goal-Test done when a node is inserted.
• For CSP, Goal-test is performed at bottom

24
DEPTH FIRST SEARCH

B C D E

F G

K L

25
IMPROVING BACKTRACKING EFFICIENCY

• General-purpose methods can give huge gains in speed


in three cases:
• Which variable should be assigned next?
• In what order should its values be used?
• Can we detect inevitable failure early?

26
WHAT ARE THE PROS OF BACKTRACKING?

• 1. it is easy to implement and understand, as it follows a


natural trial-and-error approach.
• 2. it is complete, meaning that it can find a solution if one
exists, or prove that none exists, by exploring the entire solution
space.
• 3. it is flexible, meaning that it can handle any type of
constraint, as long as it can check its consistency.
WHAT ARE THE CONS OF BACKTRACKING?
• 1. it is inefficient, meaning that it can take a long time and a lot of
memory to find a solution, especially for large or hard problems. This is
because it can revisit the same sub-problems many times, and it can
generate a lot of useless branches that do not lead to a solution.
• 2. it is not optimal, meaning that it does not guarantee to find the best
solution, or even a good one, as it does not use any criteria to evaluate the
quality of the solutions.
• 3. it is not incremental, meaning that it cannot easily adapt to changes in
the problem, such as adding or removing variables or constraints,
without restarting the search from scratch.
FORWARD CHECKING

• We keep tracking of remaining legal values for unassigned variables


• Delete the value from the domain when there is an assignment.
• When the domain is empty there is no stable solution and we should stop.
We should terminate the search when any variable has no legal values.
• In general, Forward checking is always better than backtracking.
• But it does not detect all inconsistencies.

29
FORWARD CHECKING

Forward checking idea: keep track of remaining legal values for


unassigned variables.
Terminate search when any variable has no legal values.

30
FORWARD CHECKING

• Forward checking prevents future conflicts.


• It only checks constraints between current and future
variables.
• It detects inconsistency earlier than backtracking

31
• When a value is assigned to the current variable, any value in
the domain of a "future" variable which conflicts with this
assignment is (temporarily) removed from the domain.
• If the domain of a future variable becomes empty, the current
partial solution is inconsistent. It reduces the domains and
removes possible conflicts

32
• The branches of the search tree that will lead to failure to be
pruned earlier than with simple backtracking.
• When a new variable is considered, all its remaining values are
guaranteed to be consistent with the past variables, so the
checking an assignment against the past assignments is no
longer necessary.

33
FORWARD CHECKING IN GRAPH COLORING

34
35
36
37
38
MERITS AND DEMERITS OF FORWARDS
CHECKING

• Forward checking detects the inconsistency earlier than


simple backtracking and thus it allows branches of the search
tree that will lead to failure to be pruned earlier than with simple
backtracking. This reduces the search tree and (hopefully) the
overall amount of work done. Forward checking is almost
always a much better choice than simple backtracking.

39
• But forward checking does more work when each assignment is added to the current
partial solution.
• It does not perform full arc consistency.

40
SELF-ASSESSMENT QUESTIONS

1. Which of the problems cannot be solved by backtracking method?

a) n-queen problem
b) subset sum problem
c) Hamiltonian circuit problem
d) Travelling salesman problem

2. Backtracking algorithm is implemented by constructing a tree of choices called as?

a) State-space tree
b) State-chart tree
c) Node tree
d) Backtracking tree
Self-Assessment Questions

3. What happens when the backtracking algorithm reaches a complete solution?

a) It backtracks to the root


b) It continues searching for other possible solutions
c) It traverses from a different route
d) Recursively traverses through the same route

4. A node is said to be ____________ if it has a possibility of reaching a complete solution.

a) Non-promising
b) Promising
c) Succeeding
d) Preceding
REFERENCES FOR FURTHER LEARNING OF THE
SESSION

Sites and Web links:

1. https://www.interviewbit.com/
2. https://www.sanfoundry.com/backtracking-multiple-choice-questions-answers-mcqs/
3. https://www.geeksforgeeks.org/introduction-to-backtracking-data-structure-and-algorithm-
tutorials/
4. https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/t
utorial/
5. https://ktiml.mff.cuni.cz/~bartak/constraints/propagation.html

Textbook: Artificial Intelligence A Modern Approach Third Edition Stuart J. Russell and Peter Norvig
(Page No-202 chapter No-06)
THANK YOU

Team – DDAIS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy