Cours1 Converti
Cours1 Converti
◦ If n = 10 then 10 ! = 3, 628 , 800 paths are possible. This is very large nb.
◦ This phenomenon of growing nb. of possible paths as n increases is called “
Combinatorial Explosion”
Some problems
The knapsack problem
Given a set of items, each with a weight and a value, determine the number of each item
to include in a collection so that the total weight is less than or equal to a given limit and
the total value is as large as possible.
◦ Maximize Subject to
n n
W and xi 0,1
v x
i =1
i i w x
i =1
i i
0-1 knapsack problem : restricts the number of each kind of item to zero or one.
Example :
Values = [20, 5, 10, 40, 15, 25] Sol = items 1 and 4.
Weight = [1, 2, 3, 8, 7, 4] Weight = 1+8=9
Limit = 10 Value = 20+40= 60
Example of Complex Problems
Informed search
◦ – Try to take the goal into account
◦ – Need heuristic function to approximate distance to the goal
◦ – guided by heuristic, but heuristic might be wrong …
◦ – (greedy) best-first search, A* algorithm
Example : Romania
On holiday in Romania; currently in Arad.
Flight leaves tomorrow from Bucharest
Formulate goal:
– be in Bucharest
Formulate problem:
– states: various cities
– actions: drive between cities
Find solution:
– sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
State Space Problem Formulation
A problem is defined by four items:
1. set of states, e.g., "at some city“
option: path cost (additive) e.g., sum of distances, number of actions executed, etc
A cycle is a nonempty path such that the end node is the same as the start node
a G
In a search graph, each state occurs only once
b c
e
d f
S h
p r
q
Search Tree
a G Each NODE d e p
b c
in the search tree b c e h r q
d
e is an entire PATH
f a a h r p q f
S in the state space graph.
h
p q f q c G
p q r
q c G a
a
Search Tree
•A search tree: 6 4 7
U 7 5 8 7 5 8 5 8
D R D R
U
L R
1 2 3 1 2 3 1 2 3 1 3 2 3 1 2 3 1 2 3 1 2 3
4 5 6 4 6 4 6 4 2 6 1 4 6 4 6 4 6 7 4 6
7 8 7 5 8 7 5 8 7 5 8 7 5 8 7 5 8 7 5 8 5 8
R
L
1 2 3 1 2 3
4 5 6 4 5 6
7 8 7 8
Tree search algorithms
Basic idea:
◦ simulated exploration of state space by generating successors
of already-explored states (a.k.a. expanding states)
Search strategies
A search strategy is defined by the order of node expansion
Implementation:
– put new successor nodes in front of frontier
– i.e. select last element added to the frontier
– i.e. treat frontier as a stack, i.e. LIFO
Uniformed search stratigies:
DFS : Depth First Search
A
Strategy: Expand deepest unexpanded node
Uses Stack (LiFo)
Time complexity: O(bm); terrible if m is much larger than d
Space: O(bm) B C
◦ Not complete:
d could be infinite, so only if we prevent cycles
D E F G
Not optimal:
◦ it finds the “leftmost” solution, regardless of depth or cost
D
A B D E C F G B E E F
C C C C G G
Properties of depth-first search
Complete? No: fails in infinite-depth state-spaces, and state-
spaces with loops
◦ But can be modified to avoid repeated states along path
complete in finite spaces
Implementation:
◦ – put new successor nodes at end of frontier
◦ – i.e. select first element added to the frontier
◦ – i.e. treat frontier as a queue, i.e. FIFO
Uniformed search stratigies:
BFS : Breadth First Search
Strategy: Expand shallowest A KLM
unexpanded node BCD LMN
Uses FiFo Queue 0 A
CDEF MN
Complete if b is finite
Time complexity = O(bd)
DEFGH N 1 B C D
C2
P
Ci
Cn
Boolean Explore (noeud N)
{
Si N est une solution
return True
Sinon Si N est un échec
return False
Sinon Pour tout fils F étendant N
return Explore(F)
return False /* tous les autres cas */
}
Exemple: Recherche de circuit hamiltonien
On dispose d’un graphe G(X,E)
Problème : On veut rechercher un circuit éventuel qui passe par tous les nœuds de G
Un backtracking serait de
1. Commencer à un noeud n0 1
2. Explorer systématiquement les nœuds adjacents et marquer les nœuds visités
3. Progresser récursivement vers le nœud n0
2 3 6
Exemple : le graphe suivant contient un circuit hamiltonien
1 3
1 2 4
1 2 7 5
5 3 7
6 3 4 6 4 7 4 5
4 5 4 6 7
5 1
1 5