You are on page 1of 63

Programming

in Artificial Intelligence
Marcus Gerhold (marcus.gerhold@gmail.com)

based on presentations by Ansgar Fehnker and Angelika Mader


Motivation
Motivation
Content

 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

 Graphs have nodes and edges


 Trees are graphs with certain properties
 Trees have a root and leaves
Trees

Basic definitions: root, nodes, leaves


Trees

Basic definitions: parent, ancestor, child, sibling


Trees

Basic definitions: height, branching degree, path, subtree


Trees

Important properties:

No cycles No cycles! Connected

There is a unique path to the root node from every node in the tree.
Graphs and Trees

 Graphs have nodes and edges


 Trees are graphs that are:
– Connected
– Acyclic
 Trees have a root and leaves
Idea
Idea
Idea
Idea

...
Graphs and Mazes

 Initial configuration is the root


 Children are available next positions
 Target is somewhere in the graph

 Note: This construction is not


necessarily a tree, because of cycles

Wanted: Graph Searching Strategies


Search Strategies Data Structures

 Depth First Search. Stacks


 Breadth First Search. Queues
 Greedy Search.
 A* Search.   Priority Queues
Depths First Search on a Tree

a binary balanced tree of height h


number of nodes n=2^h - 1
the number of nodes is
exponential in the height of a
tree.

n nodes can be stored in a


binary balanced tree of
height h= log2(n+1).
the height of a tree is
logarithmic
in the number of its nodes.
Depths First Search on a Tree
The pseudo code for depth-first search:

• 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

 Stacks: how to organize the “going back” (backtracking)?


 A stack is a data structure that models a pile of things.
 There are two important methods to work with a stack:

method push puts


an element on top
of the stack
Backtracking and Stacks

 Stacks: how to organize the “going back” (backtracking)?


 A stack is a data structure that models a pile of things.
 There are two important methods to work with a stack:

method pop gives us


the top element of the
stack
Backtracking and Stacks

 Stacks: how to organize the “going back” (backtracking)?


 A stack is a data structure that models a pile of things.
 There are two important methods to work with a stack.
 Other methods Besides push and pop:
 A method to ask whether a stack is empty.
 And, if our stack has an upper bound, a method to check whether it is
full.
 These methods with a Boolean as return type.
 A stack is sometimes also called LIFO: last in, first out.
Depths First Search on a Tree
Depth First Search

Pseudo Code In both, Java and C++,


there is a class for
Node actual = root; stacks that you can use.
Stack_of_nodes stack = empty;
while (number_of_visited_nodes < total_number_of_nodes) {
if (actual has an unvisited child nextChild){
stack.push(actual);
actual = nextChild; How to use these is
number_of_visited_nodes++; contained in the code
} else { on canvas.
actual = stack.pop(); // go back to your parent
}
}
Breadth First Search on a Tree

 Search order on a tree for breadth first search


The pseudo code for breadth-first search:

level = 0;
while (level < height_of_the_tree){
visit all nodes of level level;
level++;
}
Queues

 How to organize the search per level


 A queue is very similar to a stack.
 Only, new elements are added in the end,
 And elements are taken from the front of the queue.
 So, we have also the four operations on a queue:
 queue.add(node);
 Node node = queue.remove();
 boolean b = queue.isempty();
 boolean b = queue.isfull(); FIFO: first
in first out
Breadth First Search

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

 a grid is a graph, where each


node has at most 4 neighbors.
 in a maze the number of
neighbors is further restricted
by walls.
Graphs and Mazes

 Graph with initial node and edges

 A tree is a special type of graph


– No cycles
– One path to the root
Graphs and Mazes

 Search starting from root defines a tree on a graph

0
 (Deterministic) depths first
1 12 search
11

2 10
9
4
7
3 8

5 6
Graphs and Mazes

 Search starting from root defines a tree on a graph

0
 Breadth first search
1 3
2

4 5
6
8
9
7 11

10 12
Graphs and Mazes

 Search starting from root defines a tree on a graph


0 0

1 12 1 3
11 2

2 4 5
10 6
9
4 8

7 9
8 7 11
3

5 6 10 12

 Depths first search  Breadth first search


Graphs and Mazes

 Search may have to deal with “double parents”

To cope with this problem,


• mark each node with a label “visited” the
first time it gets visited
• using a Boolean variable in the node
object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Graphs and Mazes

 Search may have to deal with “double parents”

1 3 To cope with this problem,


• mark each node with a label “visited” the
2
first time it gets visited
• using a Boolean variable in the node
object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Graphs and Mazes

 Search may have to deal with “double parents”

2 To cope with this problem,


• mark each node with a label “visited” the
1
first time it gets visited
• using a Boolean variable in the node
3 object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Graphs and Mazes

 Search may have to deal with “double parents”

2 To cope with this problem,


• mark each node with a label “visited” the
1
first time it gets visited
• using a Boolean variable in the node
3 object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Graphs and Mazes

 Search may have to deal with “double parents”

1 To cope with this problem,


• mark each node with a label “visited” the
first time it gets visited
• using a Boolean variable in the node
2 3 object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Graphs and Mazes

 Search may have to deal with “double parents”

1 To cope with this problem,


• mark each node with a label “visited” the
first time it gets visited
• using a Boolean variable in the node
2 3 object.

The algorithms then have to be adapted


that it only considers unvisited
successors.
Search metrics

 Different search algorithms construct


different trees
→ Nodes are accessed in a different order
→ Efficiency of algorithms depends
 Output of program on canvas
Search metrics

 Different search algorithms construct


different trees
→ Nodes are accessed in a different order
→ Efficiency of algorithms depends
 Output of program on canvas
Search metrics

 Efficiency of algorithms depends on environment

Maze Obstacles Rooms Jail


Idea

 Different search algorithms yield different metrics


→ Use metrics in search algorithm itself

 Greedy search (always continue with most promising node)


 A* search (continue with most promising node + take
distance from start into account)
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.
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

 Any good estimate of the quality of the will do.


 It is often a measure of the “distance” to the goal.
 For a walk in the maze good measure would be the
Manhattan Distance
 The Manhattan distance is
inspired by the fact that in a
grid (like Manhattan) the
number of blocks is a better
measure for distance than
the Euclidian distance.
Greedy Search

 Any good estimate of the quality of the will do.


 It is often a measure of the “distance” to the goal.
 For a walk in the maze good measure would be the
Manhattan Distance
 The Manhattan distance is
the sum of
 abs(goal.x-current_x)
 abs(goal.y-current_y)

 In a plain grid there will be


multiple optimal paths
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

 In a sorted list it takes precisely one step, regardless of


the length of the list

 In an unsorted list it takes in the worst case the length of


the list n

What about the effort to


keep a list sorted?
Priority Queue


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.

For each successor s of the node n:


If s is finally checked: do nothing
If s is already in list, but has lower cost, do nothing
If s is already in list but with higher costs:
Update cost and make n its parent
If s is not yet in list
Add it with newly calculated cost and n as parent
Summary – Graphs and Trees

 Graphs have nodes and edges


 Graphs are used as data structures, where information is stored
as nodes
 Maze is stored as Graph
 Search algorithms construct tree inside graph
Summary – Search Algorithms

 Depth First Search – uses Stacks


 Breadth First Search – uses Queues
 Greedy Search – Choose best promising successor with respect to
distance to goal
 A* Search – Choose best promising successor with respect to
distance to goal + distance from start
Summary – Data Structures

 Stacks (LIFO, last in first out) – Used in DFS


 Queue (FIFO, first in first out) – Used in BFS
 Priority Queue (ordered list) – Used in Greedy & A*
Some thoughts for the Assignment

 What properties do nodes have?


 Variable for cost from start node
 Variable for estimated remaining costs
 Reference to its parent node
 Boolean whether it was visited
 Boolean whether it was checked finally

You might also like