CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) {...
Transcript of CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) {...
CS 561, Lectures 3-5 1
Estrutura de dados para árvore de busca
Estrutura do tipo nó (node) {• Estado (no espaço de estados ao qual o nó corresponde);• Pai (nó que gerou o nó corrente);• Operador (ação realizada para gerar o nó corrente);• Profundidade (número de nós desde a raiz);• Custo (do caminho desde a raiz);
}• Nó = (State, Parent-node, Operator, Depth, Path-cost)
CS 561, Lectures 3-5 2
Encapsulando informação sobre estado em nós
CS 561, Lectures 3-5 3
Relembrando: algoritmo de busca geral
Function General-Search(problem, Queuing-Fn) returns a solution, or failurenodes make-queue(make-node(initial-state[problem]))loop doif nodes is empty then return failurenode Remove-Front(nodes)if Goal-Test[problem] applied to State(node) succeeds then return nodenodes Queuing-Fn(nodes, Expand(node, Operators[problem]))end
Queuing-Fn(queue, elements) é uma função que insere um conjunto de elementos numa fila e determina a ordem de expansão dos nós. Variando esta função produz diferentes algoritmos de busca.
CS 561, Lectures 3-5 4
Avaliação de estratégias de busca
• Uma estratégia é definida pela sequência de expansão dos nós.
• São geralmente avaliados segundo 4 critérios:• Completude: encontra sempre uma solução (se existir uma)?• Complexidade de tempo: quanto demora (função do número
de nós)?• Complexidade em memória: quanta memória requer?• Optimalidade: garante solução de custo mínimo?
• Tempo e complexidade são medidos em termos de:• b – número máximo de galhos da árvore de busca• d – profundidade da solução de custo mínimo• m – profundidade máxima da árvore de busca (pode ser infinito)
CS 561, Lectures 3-5 5
Estratégias de busca “Uninformed”
Usa apenas informação disponível na formulação do problema, sem número de passos, custo de caminhos
Apenas distingue estado objetivo de estado não-objetivo (blind search)
• Breadth-first (primeiro em largura)• Uniform-cost (custo uniforme)• Depth-first (primeiro em profundidade)• Depth-limited (limitado em profundidade)• Iterative deepening (aprofundando iterativamente)
CS 561, Lectures 3-5 6
Breadth-first search (primeiro em largura)
Expande nó mais raso ainda não expandidoImplementação:
Function Breadth-first-search (problem) returns asolution or failure
return General-search(problem, Enqueue-at-end).
Enqueue-at-end = coloca sucessores no final da fila
CS 561, Lectures 3-5 7
Exemplo: viajando de Arad para Bucharest
CS 561, Lectures 3-5 8
Breadth-first search
CS 561, Lectures 3-5 9
Breadth-first search
CS 561, Lectures 3-5 10
Breadth-first search
CS 561, Lectures 3-5 11
Propiedades do algoritmo breadth-first
• Complettude:• Tempo:• Memória:• Otimalidade:
• Search algorithms are commonly evaluated according to the following four criteria:
• Completeness: does it always find a solution if one exists?• Time complexity: how long does it take as function of num. of nodes?• Space complexity: how much memory does it require?• Optimality: does it guarantee the least-cost solution?
• Time and space complexity are measured in terms of:• b – max branching factor of the search tree• d – depth of the least-cost solution• m – max depth of the search tree (may be infinity)
CS 561, Lectures 3-5 12
Propriedades do algoritmo breadth-first
• Completude: Sim, se b for finito• Tempo: 1+b+b2+…+bd = O(b d), exponencial
em d• Memória: O(b d), keeps every node in memory• Otimalidade: Sim (assumindo custo = 1 por passo)
Por que manter todos os nós em memória? Para evitar ter que revisitar nós já visitados, os quais podem levar a loops infinitos.
CS 561, Lectures 3-5 13
Complexidade de tempo para o breadth-first search
• Se um nó objetivo for encontrado em profundidade d da árvore, todos os nós até esta profundidade são criados.
mmGGbb
dd
• Então: O(bd)
CS 561, Lectures 3-5 14
• FILA contém todos os nós e . (Então: 4) .
• Em geral: bd
Complexidade de memória do breadth-first search
• Maior número de nós na FILA são atingidos no nível d do nó objetivo.
GGmmbb
dd
GG
CS 561, Lectures 3-5 15
Uniform-cost search (custo uniforme)
Então, a função que coloca na fila mantém uma lista de nós ordenada por custo de caminho crescente, e expandimos o primeiro nó não expandido (com menor custo)
Custo uniforme é refinamento de breadth-first: Breadth-first = uniform-cost, basta fazer custo = profundidade do nó
Expandir nó não expandido de custo mínimo. Implementação:
QueuingFN = inserir em ordem de custo de caminho crescente.
CS 561, Lectures 3-5 16
Romania com custo de cada passo em KM
CS 561, Lectures 3-5 17
Uniform-cost search
CS 561, Lectures 3-5 18
Uniform-cost search
CS 561, Lectures 3-5 19
Uniform-cost search
CS 561, Lectures 3-5 20
Propriedades do algoritmo uniform-cost
• Completude: Sim, se custo de cada passo >0• Tempo: # nós com g custo da solução ótima, O(b d)
• Espaço: # nodes with g cost of optimal solution, O(b d)
• Otimalidade: Sim, se custo dos caminhos nunca diminuirem
g(n) é o custo do caminho para o nó nLembre-se:
b = número de galhos (branching factor)d = profundidade da solução de custo mínimo
CS 561, Lectures 3-5 21
Implementação da busca uniform-cost
• Initialize Queue with root node (built from start state)
• Repeat until (Queue is empty) or (first node has Goal state):
• Remove first node from front of Queue• Expand node (find its children)• Reject those children that have already been considered, to
avoid loops• Add remaining children to Queue, in a way that keeps
entire queue sorted by increasing path cost
• If Goal was reached, return success, otherwise failure
CS 561, Lectures 3-5 22
Precaução!
• Uniform-cost search não é ótimo se for terminado quando qualquer nó na fila te o estado objetivo.
GG
100100
55
DD
55
1010
EE55
1515
FF55
2020
SSAA CC
11 5555
11
BB11
22 • Uniform cost retorna o caminho com custo 102 (se qq nó contendo objetivo for considerado solução), enquanto há um caminho com custo 25.
CS 561, Lectures 3-5 23
Nota: detecção de Loop
• Vimos que a busca pode falhar ou ser sub-ótima se :
- não detectar loop: então o algoritmo roda infinitos ciclos
(A -> B -> A -> B -> …)
- não tirar da fila um nó contendo um estado já visitado: pode levar a uma solução sub-ótima
- simplesmente evitando voltar aos nosso pai: parece promissor, mas não provamos que isso funciona
Solução? Não colocar na fila um nó se seu estado casa com o estado de algum de seus pais (assumindo custo de caminho > 0).
Assim, se custo de caminho > 0,sempre custará mais considerar um nó com aquele estado novamente do que ele já custou na primeira vez.
Isto é suficiente??
CS 561, Lectures 3-5 24
Exemplo
G
CS 561, Lectures 3-5 25
Solução pelo Breadth-First Search
CS 561, Lectures 3-5 26
Solução pelo Uniform-Cost Search
CS 561, Lectures 3-5 27
Note: Queueing in Uniform-Cost Search
In the previous example, it is wasteful (but not incorrect) to queue-up three nodes with G state, if our goal if to find the least-cost solution:
Although they represent different paths, we know for sure that the one with smallest path cost (9 in the example) will yield a solution with smaller total path cost than the others.
So we can refine the queueing function by:- queue-up node if
1) its state does not match the state of any parentand 2) path cost smaller than path cost of any
unexpanded node with same state in the queue (and in this case, replace old node with same
state by our new node)Is that it??
CS 561, Lectures 3-5 28
A Clean Robust Algorithm
Function UniformCost-Search(problem, Queuing-Fn) returns a solution, or failureopen make-queue(make-node(initial-state[problem]))closed [empty]loop do
if open is empty then return failurecurrnode Remove-Front(open)if Goal-Test[problem] applied to State(currnode) then return
currnodechildren Expand(currnode, Operators[problem])while children not empty
[… see next slide …]endclosed Insert(closed, currnode)open Sort-By-PathCost(open)
end
CS 561, Lectures 3-5 29
A Clean Robust Algorithm
[… see previous slide …]children Expand(currnode, Operators[problem])while children not empty
child Remove-Front(children)if no node in open or closed has child’s state
open Queuing-Fn(open, child)else if there exists node in open that has child’s state
if PathCost(child) < PathCost(node)open Delete-Node(open, node)open Queuing-Fn(open, child)
else if there exists node in closed that has child’s state
if PathCost(child) < PathCost(node)closed Delete-Node(closed, node)open Queuing-Fn(open, child)
end[… see previous slide …]
CS 561, Lectures 3-5 30
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -
CS 561, Lectures 3-5 31
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 13 C 1 5 1
Insert expanded nodesSuch as to keep open queuesorted
Black = open queueGrey = closed queue
CS 561, Lectures 3-5 32
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 23 C 1 5 1
Node 2 has 2 successors: one with state Band one with state S.
We have node #1 in closed with state S;but its path cost 0 is smaller than the pathcost obtained by expanding from A to S.So we do not queue-up the successor ofnode 2 that has state S.
CS 561, Lectures 3-5 33
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 46 G 3 102 4
Node 4 has a successor with state C andCost smaller than node #3 in open thatAlso had state C; so we update openTo reflect the shortest path.
CS 561, Lectures 3-5 34
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 47 D 4 8 56 G 3 102 4
CS 561, Lectures 3-5 35
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 47 D 4 8 58 E 5 13 76 G 3 102 4
CS 561, Lectures 3-5 36
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 47 D 4 8 58 E 5 13 79 F 6 18 86 G 3 102 4
CS 561, Lectures 3-5 37
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 47 D 4 8 58 E 5 13 79 F 6 18 810 G 7 23 96 G 3 102 4
CS 561, Lectures 3-5 38
Example
GG
100100
55
DD55
EE55
FF 55
SSAA CC
1155
BB11
11
# State Depth Cost Parent
1 S 0 0 -2 A 1 1 14 B 2 2 25 C 3 3 47 D 4 8 58 E 5 13 79 F 6 18 810 G 7 23 96 G 3 102 4
Goal reached
CS 561, Lectures 3-5 39
Depth-first search
• Expande o nó não expandido de maior profundidade
• Implementação:• QueuingFN = insere sucessores na frente da fila
CS 561, Lectures 3-5 40
Romania with step costs in km
CS 561, Lectures 3-5 41
Depth-first search
CS 561, Lectures 3-5 42
Depth-first search
CS 561, Lectures 3-5 43
Depth-first search
CS 561, Lectures 3-5 44
Properties of depth-first search
• Completeness: No, fails in infinite state-space (yes if finite state space)
• Time complexity: O(b m)• Space complexity: O(bm)• Optimality: No
Remember:
b = branching factor
m = max depth of search tree
CS 561, Lectures 3-5 45
Time complexity of depth-first: details
• In the worst case: • the (only) goal node may be on the right-most branch,
GG
mmbb
• Time complexity == bm + bm-1 + … + 1 = bm+1 -1
• Thus: O(bm) b - 1b - 1
CS 561, Lectures 3-5 46
Space complexity of depth-first
• Largest number of nodes in QUEUE is reached in bottom left-most node.
• Example: m = 3, b = 3 :
......
• QUEUE contains all nodes. Thus: 7.• In General: ((b-1) * m) + 1• Order: O(m*b)
CS 561, Lectures 3-5 47
Avoiding repeated states
In increasing order of effectiveness and computational overhead:
• do not return to state we come from, i.e., expand function will skip possible successors that are in same state as node’s parent.
• do not create paths with cycles, i.e., expand function will skip possible successors that are in same state as any of node’s ancestors.
• do not generate any state that was ever generated before, by keeping track (in memory) of every state generated, unless the cost of reaching that state is lower than last time we reached it.
CS 561, Lectures 3-5 48
Depth-limited search
Is a depth-first search with depth limit l
Implementation: Nodes at depth l have no successors.
Complete: if cutoff chosen appropriately then it is guaranteed to find a solution.
Optimal: it does not guarantee to find the least-cost solution
CS 561, Lectures 3-5 49
Iterative deepening search
Function Iterative-deepening-Search(problem) returns a solution, or failurefor depth = 0 to do
result Depth-Limited-Search(problem, depth)if result succeeds then return result
endreturn failure
Combines the best of breadth-first and depth-first search strategies.
• Completeness: Yes,• Time complexity: O(b d)• Space complexity: O(bd)• Optimality: Yes, if step cost = 1
CS 561, Lectures 3-5 50
Romania with step costs in km
CS 561, Lectures 3-5 51
CS 561, Lectures 3-5 52
CS 561, Lectures 3-5 53
CS 561, Lectures 3-5 54
CS 561, Lectures 3-5 55
CS 561, Lectures 3-5 56
CS 561, Lectures 3-5 57
CS 561, Lectures 3-5 58
CS 561, Lectures 3-5 59
Iterative deepening complexity
• Iterative deepening search may seem wasteful because so many states are expanded multiple times.
• In practice, however, the overhead of these multiple expansions is small, because most of the nodes are towards leaves (bottom) of the search tree:
thus, the nodes that are evaluated several times (towards top of tree) are in relatively small number.
• In iterative deepening, nodes at bottom level are expanded once, level above twice, etc. up to root (expanded d+1 times) so total number of expansions is:(d+1)1 + (d)b + (d-1)b^2 + … + 3b^(d-2) + 2b^(d-1) + 1b^d = O(b^d)
• In general, iterative deepening is preferred to depth-first or breadth-first when search space large and depth of solution not known.
CS 561, Lectures 3-5 60
Bidirectional search
• Both search forward from initial state, and backwards from goal.
• Stop when the two searches meet in the middle.
• Problem: how do we search backwards from goal??• predecessor of node n = all nodes that have n as successor• this may not always be easy to compute!• if several goal states, apply predecessor function to them just
as we applied successor (only works well if goals are explicitly known; may be difficult if goals only characterized implicitly).
• for bidirectional search to work well, there must be an efficient way to check whether a given node belongs to the other search tree.
• select a given search algorithm for each half. GoalGoalStartStart
CS 561, Lectures 3-5 61
Comparing uninformed search strategies
Criterion Breadth- Uniform Depth- Depth- Iterative Bidirectionalfirst cost first limited deepening (if applicable)
Time b^d b^d b^m b^l b^d b^(d/2)
Space b^d b^d bm bl bd b^(d/2)
Optimal? Yes Yes No No Yes Yes
Complete? Yes Yes No Yes if ld Yes Yes
• b – max branching factor of the search tree• d – depth of the least-cost solution• m – max depth of the state-space (may be infinity)• l – depth cutoff
CS 561, Lectures 3-5 62
Summary
• Problem formulation usually requires abstracting away real-world details to define a state space that can be explored using computer algorithms.
• Once problem is formulated in abstract form, complexity analysis helps us picking out best algorithm to solve problem.
• Variety of uninformed search strategies; difference lies in method used to pick node that will be further expanded.
• Iterative deepening search only uses linear space and not much more time than other uniformed search strategies.