Professional Documents
Culture Documents
www.math.sc.chula.ac.th/~jaruloj/C681/6_BB.ppt
Outline
• Problem space/ State space
• Exhaustive search
• Depth-first search
• Breadth-first search
• Backtracking
• Branch-and-bound
• Problem space
• Set of all possible path between u and v.
• {(u, n1, n2, …, nk, v)| ni is in V, for 1≤i≤k}.
• Problem space
• Any subset of S.
• Problem space
• A set of any n positions on the board.
(?, …, ?)
(0, ?, …, ?) (1, ?, …, ?)
8 (1)
3 5
2
5 2 (1,2) (1,3)
1 -6 1
-1
2 (1,2,3) (1,2,4) (1,3,4) (1,3,5)
2 4
(1,2,3,4) (1,2,3,5) (1,2,4,5) (1,3,4,5)
(1,2,3,4,5)
path(p)
last = last node in the path p
for next = 1 to n
do np = p
if next is not in np and E[last][next] != 0
then np = np || next
path(np)
else return
{1,2,3,4,5}
Jaruloj Chongstitvatana Search 13
State-Space Tree : n Queen
Q Q
Q Q
Q Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q Q
Q
Q
Q
Q Q
Jaruloj Chongstitvatana Search 14
Depth-first Search
• Traverse the tree from root until a leaf is
reached.
• Then, traverse back up to visited the next
unvisited node.
depthFirst(v)
visited[v] = 1
for each node k adjacent to v
do if not visited[k]
then depthFirst(k)
DFknapsack(sack, unchosen)
for each object p in unchosen
do unchosen=unchosen-{p}
sack=sack U {p}
val=evaluate(sack)
if unchosen is empty
► A leaf is reached.
then maxV=max(maxV, val)
if maxV=val then maxSack=sack
return
else DFknapsack(sack, unchosen)
return
breadthFirst(v)
Q = empty queue
enqueue(Q, v) visited[v] = 1
while not empty (Q)
do u = dequeue(Q)
for each node k adjacent to u
do if not visited[k]
then visited[k] = true
enqueue(Q, k)
Jaruloj Chongstitvatana Search 17
0/1 Knapsack: Breadth-first Search
BFknapsack
Q = empty queue maxV=0
sack = { }
unchosen = set of all objects
enqueue(Q, <sack, unchosen>)
while not empty (Q)
do <sack, unchosen> = dequeue(Q)
if unchosen is not empty
then for each object p in unchosen
do enqueue(Q,<sackU{p}, unchosen-{p}>)
else maxV = max(maxV, evaluate(sack))
if maxV=evaluate(sack) then maxSack = sack
State
bound
State State State
bound bound bound