Uniform-cost search

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer science, uniform-cost search (UCS) is a tree search algorithm used for traversing or searching a weighted tree, tree structure, or graph. The search begins at the root node. The search continues by visiting the next node which has the least total cost from the root. Nodes are visited in this manner until a goal state is reached.

Typically, the search algorithm involves expanding nodes by adding all unexpanded neighboring nodes that are connected by directed paths to a priority queue. In the queue, each node is associated with its total path cost from the root, where the least-cost paths are given highest priority. The node at the head of the queue is subsequently expanded, adding the next set of connected nodes with the total path cost from the root to the respective node. The uniform-cost search is complete and optimal if the cost of each step exceeds some positive bound ε.[1] The worst-case time and space complexity is O(b1 + C*/ε), where C* is the cost of the optimal solution and b is the branching factor. When all step costs are equal, this becomes O(bd + 1).[2]


procedure UniformCostSearch(Graph, root, goal)
  node := root, cost = 0
  frontier := priority queue containing node only
  explored := empty set
    if frontier is empty
      return failure
    node := frontier.pop()
    if node is goal
      return solution
    for each of node's neighbors n
      if n is not in explored
        if n is not in frontier
        else if n is in frontier with higher cost
          replace existing node with n

Expansion showing the explored set and the frontier (priority queue):
Start Node: A
Goal Node: G

Step Frontier: a set of (node, its cost) objects Expand[*] Explored: a set of nodes
1 {(A,0)} A
2 {(D,3),(B,5)} D {A}
3 {(B,5),(E,5),(F,5)} B {A,D}
4 {(E,5),(F,5),(C,6)} E {A,D,B}
5 {(F,5),(C,6)}[**] F {A,D,B,E}
6 {(C,6),(G,8)} C {A,D,B,E,F}
7 {(G,8)} G {A,D,B,E,F,C}

^ * The node chosen to be expanded for the next step.
^ ** B is not added to the frontier because it is found in the explored set.
Found the path: A to D to F to G.

Relationship to other algorithms[edit]

Dijkstra's algorithm, which is perhaps better-known, can be regarded as a variant of uniform-cost search, where there is no goal state and processing continues until all nodes have been removed from the priority queue, i.e. until shortest paths to all nodes (not just a goal node) have been determined. As in Dijkstra's algorithm, UCS guarantees that (if all edge weights are non-negative) the shortest path to a particular node has been found once the node is extracted from the priority queue.

Uniform-cost search is a special case of the A* search algorithm if its heuristic is a constant function. If A* is used with a monotonic heuristic, then it can be turned into a uniform cost search by subtracting from each edge cost the decrease in heuristic value along that edge. Breadth-first search (BFS) is a special case of uniform-cost search when all edge costs are positive and identical. Where BFS first visits the node with the shortest path length (number of nodes) from the root node, UCS first visits the node with the shortest path costs (sum of edge weights) from the root node.

Uniform-cost search is a variant of best-first search.


  1. ^ Russell, Stuart J.; Norvig, Peter (2003), Artificial Intelligence: A Modern Approach (2nd ed.), Upper Saddle River, New Jersey: Prentice Hall, ISBN 0-13-790395-2 
  2. ^ Stuart Russell; Peter Norvig (2010). Artificial Intelligence: A Modern Approach (3 ed.). Prentice Hall. ISBN 978-0-13-604259-4.