Professional Documents
Culture Documents
in Artificial Intelligence
Marcus Gerhold (marcus.gerhold@gmail.com)
In this lecture
Graphs and Trees.
Data structures.
Search strategies.
Goal of this lecture:
provide sufficient material to implement greedy
search and A* search
Graphs and Trees
Graphs and Trees
Important properties:
There is a unique path to the root node from every node in the tree.
Graphs and Trees
...
Graphs and Mazes
• start at the root.
• as long as there are unvisited
nodes:
●
if there is an unvisited child,
move to that child.
●
If there is no unvisited child,
go back to your parent.
How to go
back?
Backtracking and Stacks
level = 0;
while (level < height_of_the_tree){
visit all nodes of level level;
level++;
}
Queues
Pseudo Code
Queue_of_nodes queue;
queue.add(root);
while (!queue.isempty()) {
actual = queue.remove();
for all children child of actual{
queue.add(child);
}
}
Graphs and Mazes
0
(Deterministic) depths first
1 12 search
11
2 10
9
4
7
3 8
5 6
Graphs and Mazes
0
Breadth first search
1 3
2
4 5
6
8
9
7 11
10 12
Graphs and Mazes
1 12 1 3
11 2
2 4 5
10 6
9
4 8
7 9
8 7 11
3
5 6 10 12
Idea:
From all the nodes waiting
to be explored, take the
most promising to
continue.
Greedy Search
Idea:
From all the nodes waiting
to be explored, take the
most promising to
continue.
Greedy Search
Idea:
From all the nodes waiting
to be explored, take the
most promising to
continue.
The most promising is the
one that has least
estimated costs to the goal
Greedy Search
Idea:
• The most promising is the one
that has least estimated costs to
the goal.
Greedy Search
Idea:
The most promising is the one
that has least estimated costs to
the goal..
• This requires find the smallest
nodes that still needs to be
explored.
Greedy Search
Idea:
The most promising is the one
that has least estimated costs to
the goal.
This requires find the smallest
nodes that still needs to be
explored.
Greedy Search
Idea:
The most promising is the one
that has least estimated costs to
the goal.
This requires find the smallest
nodes that still needs to be
explored.
One way to achieve this to have
a sorted list.
Finding the Smallest Element in a List
●
Similar to queues
●
Elements are added according to priority
●
Elements are taken from the front of the queue.
2 5 14 16 28
27 29 3
135
Priority Queue
●
Similar to queues
●
Elements are added according to priority
●
Elements are taken from the front of the queue.
– For the assignment you get a data structure that provides
efficient representation of an ordered list, and has efficient
insertion/deletion.
– The data structure is called a “priority queue”
Greedy
Priority Queue
Keep the states that you still
have to visit in a priority queue.
Observation
Greedy does not find the
shortest path.
Greedy
Priority Queue
Keep the states that you still
have to visit in a priority queue.
Observation
Greedy does not find the
shortest path.
It would be better to also
consider the path so far
A*
Idea:
The most promising node is
the one where costs made
so far and expected costs.
Add the length so far to the
Manhattan distance.
A*
At least 34
steps
Complication:
You might find a shorter
path to an intermediate
node later.
This means you have to
update the cost
This will change the
ranking. Remove the node,
and reinsert it, to keep
preserve sorting.
At least 36
steps
A*
A* is guaranteed to
Pseudo code find the shortest
path.
Put start node in a list
While the goal is not yet reached:
Usually
Take most promising node n from list faster than
Mark the node as finally checked BFS.