5 intro to ucs
TRANSCRIPT
-
8/3/2019 5 Intro to UCS
1/31
Introduction ToMinimum Cost Problems& Uniform Cost Search
/
-
8/3/2019 5 Intro to UCS
2/31
Shortest Path Problems
Such problems are common in real-lifeproblems e.g. select routes for planes so as to minimise fuel
useedge = fuel used on a segment after taking
advantage of local winds
In the course, we will see other, less obvious,examples which are also hidden shortestpath problems
Such shortest path problems are a key partof this course
-
8/3/2019 5 Intro to UCS
3/31
Weighted Graphs
Shortest path problems use weightedgraphs
These are graphs in which each edge is givena number called its weight
The weight of an edge is intended to be itscost, or value, or length, etc
Paths then have a weight which is just thesum of the weights of the edges in the path
-
8/3/2019 5 Intro to UCS
4/31
Shortest Path Problems
Suppose we want to find the shortest journey from Nottinghamto Edinburgh
Road system converts to a weighted graph edge = segments of roads between intersection
nodes = intersections weights or costs of edges = distance between intersections/nodes
Shortest path in a graph no longer doing trees might have multiple paths might have no path
Shortest journey from Nottingham (N) to Edinburgh (E)converts to given the weighted graph find the shortest path from node N to node E
-
8/3/2019 5 Intro to UCS
5/31
Desired Search Properties
Completeness: will find a goal if one islegally reachable
Optimality: will find goal with shortest path to it and (usually) also find the shortest path itself
Systematic: dont do the same work toomany times
but there is no generally agreed exact definition sometimes taken to mean to never repeat the
same work
-
8/3/2019 5 Intro to UCS
6/31
Weighted Trees
As usual, trees are easier to understandthan graphs so will start with rooted
trees first
-
8/3/2019 5 Intro to UCS
7/31
Finding Min-Cost Paths
Blind Search:
Try to search shortest paths first, until find
one that reaches a goal
How to achieve this?
Do we already have anything that that
almost works in some cases?Yes. BFS does the job in some cases:
-
8/3/2019 5 Intro to UCS
8/31
BFS and min-cost paths
Suppose that we have a tree in which all theweights are one
Weight of a path is its length Weight of a path from the root to a node N isjust the depth of node N
But:
BFS searches all nodes of depth d before any ofdepth d+1
hence, in this case, BFS finds minimum cost(mincost) goals
-
8/3/2019 5 Intro to UCS
9/31
BFS in Tree with unit weights
Suppose that E and C are goalnodes
Costs of nodes are just theirdepth
The mincost goal is C DFS would find E first
not the mincost goal simple DFS is not optimal and
complete
BFS would search all the c=1
nodes before it searches thec=2 node, would correctly find goal C
JB C
D
E
F
G
A
H
I
c=0
c=1
c=2
c=3
c=4
-
8/3/2019 5 Intro to UCS
10/31
Trees with non-unit weights
Suppose that some edgeshave cost > 1
Think of the cost as a
depth Redraw the tree so thatnodes are at the depthgiven by their cost
The mincost goal is now E
Want to do a BFSbut in which thesearch pattern is bycost not depth
JB
C
D
E
F
G
A
H
I
c=0
c=1
c=2
c=3
c=4
3
-
8/3/2019 5 Intro to UCS
11/31
Uniform Cost Search
Search Pattern: smaller cost nodesbefore larger
Fringe in red
Visited in blue
root
outline of tree
increasing cost
-
8/3/2019 5 Intro to UCS
12/31
-
8/3/2019 5 Intro to UCS
13/31
Breadth First Search
1. fringe MAKE-EMPTY-QUEUE()2. fringe INSERT( root_node )3. found false // boolean flag4. loop {
1. if fringe is empty then return found // finished the search2. node REMOVE-FIRST(fringe)3. if node is a goal
1. print node // if want to list all matches to the goal2. found true // so we remember we succeeded3. (if only want first goal then return true)
4. L EXPAND(node)5. fringe INSERT-ALL-AT-END( L, fringe ) // insert at end for BFS
}
-
8/3/2019 5 Intro to UCS
14/31
Uniform Cost Search (UCS)
Search all nodes of cost c before those of costc+1
In BFS deeper nodes always arrive after
shallower nodes In UCS the costs of new nodes do not have
such a nice pattern of always increasing In UCS we need to
1. explicitly store the cost g of a node2. explicitly use such costs in deciding the ordering
in the queue
-
8/3/2019 5 Intro to UCS
15/31
Uniform Cost Search (UCS)
Explicitly store the cost of a node with thatnode convention g is the path-cost
Always remove the smallest cost nodefirst if using REMOVE-FIRST then sort the queue in
increasing order
alternatively, instead of REMOVE-FIRST, just scan allqueue and use REMOVE-SMALLEST-COST
Called a priority queue as highest priorityremoved first, not just by order of arrival
-
8/3/2019 5 Intro to UCS
16/31
Uniform Cost Search in Tree
1. fringe MAKE-EMPTY-QUEUE()2. fringe INSERT( root_node ) // with g=03. loop {
1. if fringe is empty then return false // finished without goal
2. node REMOVE-SMALLEST-COST(fringe)
3. if node is a goal
1. print node and g
2. return true// that found a goal
4. Lg EXPAND(node)// Lgis set of children with their g costs
// NOTE: do not check Lgfor goals here!!
5. fringe INSERT-ALL(Lg, fringe )
}
-
8/3/2019 5 Intro to UCS
17/31
From Trees To Graphs
Most real-life examples
journey planning, etc
are graphs not trees
more accurately, they are weightedgraphs
-
8/3/2019 5 Intro to UCS
18/31
Weighted Graph Example
This is still not amap!
The positions of
the nodes arenot directlymeaningful
(Though often
try to draw themat leastapproximatelycorrectly)
B
C
D
E
F
G
A
35
2
5
6
711
-
8/3/2019 5 Intro to UCS
19/31
Graph vs. Trees
In rooted trees: we have (implicitly) downwards edges and childrenIn contrast in a graph:
there is no automatic meaning to away from the startnode
Implications:
change notation from children to neigbours orsuccessors have to be more careful to avoid loops
-
8/3/2019 5 Intro to UCS
20/31
Graph Search: Neighbours
Neighbour Given a node n then a neighbour is any other node that is
connected to n by an edge m is a neighbor of n if and only if (n,m) 2 E
Neighbourhood the neighbourhood of n is just the set of neighbours to n note that it is just an (unordered) set
no concept of first or second element it does not have any order unless we chose to impose one
if the nodes have labels then we might chose to create a listof neighbours with a specific ordering
e.g. by using a lexicographical (dictionary) ordering or any other method of ordering we like
-
8/3/2019 5 Intro to UCS
21/31
Uniform Cost Search in
Graphs Search Pattern:
smaller cost nodesbefore larger
Fringe in red
Visited in blue
Blind: still ignoresgoals
goals
outline of graph
increasing path
cost from start
start
-
8/3/2019 5 Intro to UCS
22/31
Preventing Loopy Paths
Graphs have neighbours instead of children hence paths can go backwards, ABCB can get paths with loops ABCDB
Can also have multiple paths to a node,though just want the mincost path
Both problems arise because we rediscovernodes that the search has already discovere
Need to consider methods to suppressnodes that have already been visited
-
8/3/2019 5 Intro to UCS
23/31
Preventing Loopy Paths
If already found a path to a node butthe new path has higher cost
do not need to consider the new path consider it anyway
saves the effort of implementing a check forwhether a node already had a path to it
algorithm is still complete and optimal but at runtime, the implementation will probably
use a lot more time and memory
-
8/3/2019 5 Intro to UCS
24/31
Preventing Loopy Paths
Practical Options (in order of power andimplementation cost)
1.Prevent paths returning to the immediatelyprevious node
2.Prevent return to node higher on the path(just prevent loops)
3.Full: suppress anything on the union ofvisited and fringe lists (unless the new path isshorter)
-
8/3/2019 5 Intro to UCS
25/31
Uniform Cost Search in Graph
1. fringe MAKE-EMPTY-QUEUE()2. fringe INSERT( root_node ) // with g=03. loop {
1. if fringe is empty then return false // finished without goal
2. node REMOVE-SMALLEST-COST(fringe)
3. if node is a goal1. print node and g
2. return true// that found a goal
4. Lg EXPAND(node)// Lgis set ofne i ghbou r s with their g costs// NOTE: do not check Lgfor goals here!!
5. fringe INSERT-IF-NEW(Lg, fringe ) // ignore revisited nodes
// unless is with new better g
}
-
8/3/2019 5 Intro to UCS
26/31
Keeping the Path
Suppose that the output not only wants themincost goal, and the cost, but also wants thepath
Implementation needs extra book-keeping: Have to keep back pointers
for each node keep a pointer to the previous nodeon the min cost path to that node
by following the back pointers from any node ncan recreate the min cost path from the start to n storing all these previous nodes and the pointers
can require a lot of extra memory and runtime
-
8/3/2019 5 Intro to UCS
27/31
Uniform cost search
A breadth-first search finds the shallowest goal state and
will therefore be the cheapest solution provided thepath
cost is a function of the depth of the solution. But, if this is
not the case, then breadth-first search is not guaranteed tofind the best (i.e. cheapest solution).
Uniform cost search remedies this by expanding the lowest
cost node on the fringe, where cost is the path cost,g(n).
In the following slides those values that are attached topaths are the cost of using that path.
-
8/3/2019 5 Intro to UCS
28/31
Consider the following problem
B
C
S G
A
15
1 10
5
55
We wish to find the shortest route from node S to node G; that is, node S is the initial
state and node G is the goal state. In terms of path cost, we can clearly see that the route
SBG is the cheapest route. However, if we let breadth-first search loose on the problem
it will find the non-optimal path SAG, assuming that A is the first node to be expanded
at level 1. Press space to see a UCS of the same node set
-
8/3/2019 5 Intro to UCS
29/31
S
Size of Queue: 0
Nodes expanded: 0
Queue: Empty
Current action: Waiting. Current level: n/a
UNIFORM COST SEARCH PATTERN
Press space to begin the search
Current action: Expanding Current level: 0
Queue: SSize of Queue: 1
A
B
C
Size of Queue: 3 Queue: A, B, C
1
15
5
We start with our initial state and expand itNode S is removed from the queue and the revealed nodes are added to the queue. The queue
is then sorted on path cost. Nodes with cheaper path cost have priority.In this case the queue
will be Node A (1), node B (5), followed by node C (15). Press space.
We now expand the node at the front of the queue, node A. Press space to continue.
Nodes expanded: 1
S
Current level: 1
G
Node A is removed from the queue and the revealed node (node G) is added to the queue.
The queue is again sorted on path cost. Note, we have now found a goal state but do not
recognise it as it is not at the front of the queue. Node B is the cheaper node. Press space.
10A
Nodes expanded: 2
Queue: B, G11, C
Current action: Backtracking Current level: 0Current level: 1Current action: Expanding
5
Nodes expanded: 3
Queue: G10, G11, C15
B
Once node B has been expanded it is removed from the queue and the revealed node (node
G) is added. The queue is again sorted on path cost. Note, node G now appears in the queue
twice, once as G10 and once as G11. As G10 is at the front of the queue, we now proceed to
goal state. Press space.
Current level: 2
Queue: EmptySize of Queue: 0
FINISHED SEARCH
GGGGG
The goal state is achieved and the path
S-B-G is returned. In relation to path
cost, UCS has found the optimal
route. Press space to end.
-
8/3/2019 5 Intro to UCS
30/31
Summary
Added costs (weights) to the edges
Modified BFS to create Uniform Cost Search
(UCS) that finds optimal (minimum cost) paths to goal(s)
Expectations:
that you can run UCS by hand
you can predict the order in which UCS expandsnodes
that you can implement UCS
-
8/3/2019 5 Intro to UCS
31/31
Questions?