You are on page 1of 31

Introduction To Minimum Cost Problems & Uniform Cost Search

Shortest Path Problems


Such problems are common in real-life problems
e.g. select routes for planes so as to minimise fuel use edge = 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 shortest path problems Such shortest path problems are a key part of this course

Weighted Graphs
Shortest path problems use weighted graphs These are graphs in which each edge is given a number called its weight The weight of an edge is intended to be its cost, or value, or length, etc Paths then have a weight which is just the sum of the weights of the edges in the path

Shortest Path Problems


Suppose we want to find the shortest journey from Nottingham to 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 no longer doing trees might have multiple paths might have no path

Shortest path in a graph

Shortest journey from Nottingham (N) to Edinburgh (E) converts to


given the weighted graph find the shortest path from node N to node E

Desired Search Properties


Completeness: will find a goal if one is legally reachable Optimality: will find

Systematic: dont do the same work too many times

goal with shortest path to it and (usually) also find the shortest path itself

but there is no generally agreed exact definition sometimes taken to mean to never repeat the same work

Weighted Trees
As usual, trees are easier to understand than graphs so will start with rooted trees first

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:

BFS and min-cost paths


Suppose that we have a tree in which all the weights are one Weight of a path is its length Weight of a path from the root to a node N is just the depth of node N But:
BFS searches all nodes of depth d before any of depth d+1 hence, in this case, BFS finds minimum cost (mincost) goals

BFS in Tree with unit weights


Suppose that E and C are goal nodes Costs of nodes are just their depth The mincost goal is C DFS would find E first
c=0 F

c=1

BFS would search all the c=1 nodes before it searches the c=2 node,
would correctly find goal C

not the mincost goal simple DFS is not optimal and complete

c=2

c=3

c=4

Trees with non-unit weights


Suppose that some edges have cost > 1 Think of the cost as a depth Redraw the tree so that nodes are at the depth given by their cost The mincost goal is now E Want to do a BFS but in which the search pattern is by cost not depth
c=0 F

c=1

J 3

c=2

c=3 C

c=4

Uniform Cost Search


Search Pattern: smaller cost nodes root before larger Fringe in red outline of tree Visited in blue
increasing cost

Uniform Cost Search (UCS)


Cost of a node n, is the total cost of the path from the root of the tree to n
usually write this as g or g(n)

Search all nodes of cost c before those of cost c+1 How to implement? In BFS the effect of the depth was implicitly handled adding new nodes to the end of the queue

Breadth First Search


1. 2. 3. 4. fringe MAKE-EMPTY-QUEUE() fringe INSERT( root_node ) found false // boolean flag loop {
1. 2. 3.

if fringe is empty then return found // finished the search node REMOVE-FIRST(fringe) if node is a goal
print node // if want to list all matches to the goal found true // so we remember we succeeded (if only want first goal then return true)

1. 2. 3.

4. 5.

L EXPAND(node) fringe INSERT-ALL-AT-END( L, fringe ) // insert at end for BFS

Uniform Cost Search (UCS)


Search all nodes of cost c before those of cost c+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 node 2. explicitly use such costs in deciding the ordering in the queue

Uniform Cost Search (UCS)


Explicitly store the cost of a node with that node convention g is the path-cost Always remove the smallest cost node first
if using REMOVE-FIRST then sort the queue in increasing order alternatively, instead of REMOVE-FIRST, just scan all queue and use REMOVE-SMALLEST-COST

Called a priority queue as highest priority removed first, not just by order of arrival

Uniform Cost Search in Tree


1. 2. 3. fringe MAKE-EMPTY-QUEUE() fringe INSERT( root_node ) // with g=0 loop {
1. 2. 3. if fringe is empty then return false // finished without goal node REMOVE-SMALLEST-COST(fringe) if node is a goal
1. 2. print node and g return true // that found a goal

4.

Lg EXPAND(node) // Lg is set of children with their g costs

// NOTE: do not check Lg for goals here!!


5. fringe INSERT-ALL(Lg, fringe )

From Trees To Graphs


Most real-life examples
journey planning, etc

are graphs not trees more accurately, they are weighted graphs

Weighted Graph Example


This is still not a map! The positions of the nodes are not directly meaningful (Though often try to draw them at least approximately correctly)
3
A B

5
F D

6 7
E

2
C

11
G

Graph vs. Trees


In rooted trees: we have (implicitly) downwards edges and children In contrast in a graph: there is no automatic meaning to away from the start node Implications: change notation from children to neigbours or successors have to be more careful to avoid loops

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 the neighbourhood of n is just the set of neighbours to n note that it is just an (unordered) set if the nodes have labels then we might chose to create a list of neighbours with a specific ordering
e.g. by using a lexicographical (dictionary) ordering or any other method of ordering we like no concept of first or second element it does not have any order unless we chose to impose one

Neighbourhood

Uniform Cost Search in Graphs


Search Pattern: smaller cost nodes before larger Fringe in red Visited in blue Blind: still ignores goals
outline of graph goals start

increasing path cost from start

Preventing Loopy Paths


Graphs have neighbours instead of children Can also have multiple paths to a node, though just want the mincost path
hence paths can go backwards, ABCB can get paths with loops ABCDB

Both problems arise because we rediscover nodes that the search has already discovere Need to consider methods to suppress nodes that have already been visited

Preventing Loopy Paths


If already found a path to a node but the new path has higher cost
do not need to consider the new path consider it anyway
saves the effort of implementing a check for whether 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

Preventing Loopy Paths


Practical Options (in order of power and implementation cost) 1. Prevent paths returning to the immediately previous node 2. Prevent return to node higher on the path (just prevent loops) 3. Full: suppress anything on the union of visited and fringe lists (unless the new path is shorter)

Uniform Cost Search in Graph


1. 2. 3. fringe MAKE-EMPTY-QUEUE() fringe INSERT( root_node ) // with g=0 loop {
1. 2. 3. if fringe is empty then return false // finished without goal node REMOVE-SMALLEST-COST(fringe) if node is a goal
1. 2. print node and g return true // that found a goal

4.

Lg EXPAND(node) // Lg is set of neighbours with their g costs

// NOTE: do not check Lg for goals here!!


5. fringe INSERT-IF-NEW(Lg, fringe ) // ignore revisited nodes // unless is with new better g

Keeping the Path


Suppose that the output not only wants the mincost goal, and the cost, but also wants the path Implementation needs extra book-keeping: Have to keep back pointers
for each node keep a pointer to the previous node on the min cost path to that node by following the back pointers from any node n can 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

Uniform cost search


A breadth-first search finds the shallowest goal state and will therefore be the cheapest solution provided the path cost is a function of the depth of the solution. But, if this is not the case, then breadth-first search is not guaranteed to find 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 to paths are the cost of using that path.

Consider the following problem

A
1 5 5

10

S
15

G
5

C
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

Once nodeis with beenfrom the queue removedrevealed nodes are G)the added to the queue.queue Nodenow expand our node at queueis expand queue, node A. Press spaceto the queue. (node We AstartBremovedinitial state front andthe it the queue and is revealed node The Node is removed from the the and of the from node (node added to continue. We S has the expanded it and the revealed G)is then sorted queue is again sorted Note, we have costfoundpriority.In this case the The queue isTheon sorted on path cost.on cheaper pathnownode Ganow appears indo not queue is added. again path cost. Nodes with path cost. Note, have goal state but the queue twice, once as G10 and at the as G11. As G10 is node front the cheaper node. Press space. recognise Node A (1), node B (5), followed byatNode B isof Press space. now proceed to will be it it is not once front of the queue. the C (15). the queue, we goal state. Press space.

A
5 5

10

S
15

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.

Press space to begin the search Size of Queue: 0 3 1 Nodes expanded: 0 3 2 1 Queue: Empty11, C15 G B, B, C A, G11 S 10, G, C CurrentFINISHED SEARCH action: Waiting. Backtracking Expanding Current level: n/a 2 1 0

UNIFORM COST SEARCH PATTERN

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 expands nodes that you can implement UCS

Questions?

You might also like