Professional Documents
Culture Documents
Search
Search
A
Root
Link
Node
Leaf B is Parent of C
C is child of B
A is ancestor of C
C is descendant of A
Graphs
Types
Directed graphs (one way street)
Undirected graphs (two way street)
Graph
• Network of Roads
• Airline Routes
• We are interested in a path to satisfy some
properties
• It can be any path or the shortest one or have
least cost
Lhr
Fsb
Gujr
Sarg
Isb
Road Route
Graphs (Examples)
Lhr
Fsb
Gujr
Sarg
Isb
Road Route
B
Put C on A C Put B on C C
A B A
Put C on A
A B C A
C Put A on C C
A B B
Put C on B
A
G
D
S
B
From Graph to Tree
C S
A
B
A G
D D C
S D G
C G
C G
B
The tree has a leaf node for every non-looping path in the graph starting at S
Duplications
So a complete search of this tree might do some extra work
Terminology
1. Initialize Q with search node (S) as only entry; set Visited = (S)
2. If Q is empty, fail. Else, pick some search node N from Q
3. If state (N) is a goal, return N (we’ve reached the goal)
4. (otherwise) Remove N from Q
5. Find all the descendants of state(N) not in Visited and create all the one-step
extensions of N to each descendant.
6. Add the extended paths to Q; add children of state(N) to Visited
7. Go to step 2.
Critical Decisions:
Step 2: Pick N from Q
Step 6: adding extension of N to Q
Implementing the Search Strategies
Depth-First
Pick first element of Q
Add path extensions to front of Q
Breadth-first
Pick first element of Q
Add path extensions to end of Q
Testing for the goal
If all we want to do is find a path from the start to the goal, there is
no advantage to adding a search node whose state is already the
state of another search node
Any state reachable from the node the second time would have
been reachable from that node the first time
Note that, when using Visited, each state will only ever have at
most one path to it (search node) in Q
Questions