uniformed tree searching

20
Uniformed Tree Searching Prepared by Aya Elshiwi Supervisor Dr.Eman Eldaydamony

Upload: ayaelshiwi

Post on 17-Dec-2014

443 views

Category:

Education


1 download

DESCRIPTION

Uniformed Tree Searching Algorithms Depth First Search Breadth First Search Uniform Cost Search

TRANSCRIPT

Page 1: Uniformed tree searching

Uniformed Tree Searching

Prepared by Aya Elshiwi Supervisor

Dr.Eman Eldaydamony

Page 2: Uniformed tree searching

Tree searching

• A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem)

• For some problems, any goal node is acceptable (N or J); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J)

2

L M N O P

G

Q

H JI K

FED

B C

A

Goal nodes

Page 3: Uniformed tree searching

Search AlgorithmsUninformed vs. informed

Search algorithms differ by how they pick which node to expand.

• Uninformed search Algorithms– In making this decision, these look only at the structure of the

search tree and not at the states inside the nodes.– Also known as “blind search”. – Uninformed search methods: Breadth-first, depth-first, depth-

limited, uniform-cost, depth-first iterative deepening, bidirectional.

• Informed search Algorithms– In making this decision , these look at the states inside the

nodes.– Also known as “heuristic search”.– Informed search methods: Hill climbing, best-first, greedy

search, beam search, A, A* 3

Page 4: Uniformed tree searching

Example

Uniformed (blind) search Informed (heuristic) search

4

Page 5: Uniformed tree searching

Evaluating Search Strategies Strategies are evaluated along the following

dimensions:

– Completeness: Does it always find a solution if one exists?

– Time complexity: How long does it take to find a solution ?

– Space complexity: How much memory is needed to perform the search?

– Optimality: Does the strategy find the optimal solution?

5

Page 6: Uniformed tree searching

Uniformed search Algorithms

• Depth First Search [DFS]

• Breadth First Search [BFS]

• Uniform Cost Search [UCS]

6

Page 7: Uniformed tree searching

Depth-first searching• A depth-first search (DFS)

explores a path all the way to a leaf before backtracking and exploring another path.

• The search proceeds immediately to the deepest level of the search tree, where the nodes have no successors . As those nodes are expanded , they are dropped from the Frontier [stack] ,so then the search “backs up” to the deepest next node that still has unexplored successors .

• Use Stack data structure [FILO] as a frontier .

7

Animated Graphic

Page 8: Uniformed tree searching

How to do depth-first searching

Algorithm

Put the root node on a stack; while (stack is not empty) {

remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack;}return failure;

Example

8

L M N O P

G

Q

H JI K

FED

B C

A

• For example, after searching A, then B, then D, the search backtracks and tries another path from B

• Node are explored in the order A B D E H L M N I O P C F G J K Q

• N will be found before J

Animated Graphic

Page 9: Uniformed tree searching

Example

9

Page 10: Uniformed tree searching

Properties of Depth First Algorithm b( branching factor) : Maximum number of successors of any

node.m: maximal depth of a leaf node

• Number of nodes generated (worst case): 1 + b + b2 + … + bm = O(bm)• Complete: only for finite search tree• Optimal : Not optimal• Time complexity: O(bm)• Space complexity : O(bm) [or O(m)]

10

Page 11: Uniformed tree searching

Breadth-first searching• A breadth-first search (BFS) is

a simple strategy in which the root node is expanded first , then all the successors of the root node , then their successors.

• In general , all the nodes are expanded at a given depth in the search tree before any nodes at the next level are expanded.

• Use a queue data structure [FIFO].

11

Animated Graphic

Page 12: Uniformed tree searching

How to do breadth-first searching

Algorithm • Enqueue the root node• Dequeue a node and examine it

– If the element sought is found in this node, quit the search and return a result.

– Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.

• If the queue is empty, every node on the graph has been examined – quit the search and return "not found".

• If the queue is not empty, repeat from Step 2.

• For example, after searching A, then B, then C, the search proceeds with D, E, F, G

• Node are explored in the order A B C D E F G H I J K L M N O P Q12

L M N O P

G

Q

H JI K

FED

B C

A

Animated Graphic

Page 13: Uniformed tree searching

Properties of breadth-first search

b: branching factor d: depth of shallowest goal node

Total number of nodes generated is: 1+b+b2+b3+… … + bd = O(bd )• Complete Yes (if b is finite)• Time 1+b+b2+b3+… … + bd + b(bd -1) = O(bd+1)Because the whole layer of nodes at depth d would be

expanded before detecting the goal .• Space O(bd) (keeps every node in memory)• Optimal if all operators have the same cost.

13

Page 14: Uniformed tree searching

Depth- vs. breadth-first searching• When a breadth-first search succeeds, it finds a

minimum-depth (nearest the root) goal node.

• When a depth-first search succeeds, the found goal node is not necessarily minimum depth.

• For a large tree, breadth-first search memory requirements may be excessive.

• For a large tree, a depth-first search may take an excessively long time to find even a very nearby goal node.

14

Page 15: Uniformed tree searching

Uniform cost search • Find the least-cost goal .

• The search begins at the root node. The search continues by visiting the next node which has the least total cost from the root. Nodes are visited in this manner until a goal state is reached.

• Each node has a path cost from start (= sum of edge costs along the path). Expand the least cost node first.

• Equivalent to breadth-first if step costs all equal

• Use a priority queue (ordered by path cost) instead of a normal queue .

15

Animated Graph

Page 16: Uniformed tree searching

Pesudocode procedure UniformCostSearch(Graph, root, goal)

node := root, cost = 0frontier := priority queue containing node onlyexplored := empty setDoif frontier is emptyreturn failurenode := frontier.pop()if node is goalreturn solutionexplored.add(node)for each of node's neighbors nif n is not in exploredif n is not in frontierfrontier.add(n)else if n is in frontier with higher costreplace existing node with n

16

Page 17: Uniformed tree searching

How to do Uniform Cost Search

if- Frontier : empty >Return failelse- Add node to frontier.- Check: node (goal)>solution- Add node to explored.- Neighbor s: if not explored

>add to frontier- Else :if was with higher cost

replace it .

17

Algorithm

Solution Explored : A D B E F Cpath: A to D to F to GCost = 8

Example

Page 18: Uniformed tree searching

Properties of uniform cost search- Equivalent to breadth-first if step costs all equal.

• Complete Yes .assuming that operator costs are nonnegative (the cost of

path never decreases)

• Optimal Yes. Returns the least-cost path.

18

Page 19: Uniformed tree searching

Reference

• Solving problems by searching :http://www.pearsonhighered.com/samplechapter/0136042597.pdf• Depth-First search :http://en.wikipedia.org/wiki/Depth-first_search• Breadth-First search:http://en.wikipedia.org/wiki/Breadth-first_search• Uniform cost search :http://en.wikipedia.org/wiki/Uniform-cost_search• Depth First & Breadth First:https://www.youtube.com/watch?v=zLZhSSXAwxI

19

Page 20: Uniformed tree searching

Questions?

20