🏗️ ΘρϵηΠατπ🚧 (under construction)

Uniform Cost Search

        def uniform_cost_search(start):
            cost_to_get_to_node = {}
            frontier = PriorityQueue() // Higher Priority equals Lower Cost
            expanded_nodes = Set()

            cost_to_get_to_node[start] = 0
            frontier.push((start, 0))
            expanded_nodes.add(start)

            while front.has_elements():
                u = frontier.pop()

                if is_goal(u):
                    return path(u)

                for v in u.successors:
                    if v not in expanded_nodes:
                        if v in frontier:
                            # if we already have it in the frontier, but we found a cheaper way to get there, update the cost to get here.
                            cost_to_get_to_node[v] = min(cost_to_get_to_node[v], cost_to_get_to_node[u] + u.action_cost(v))
                        else:
                            frontier.push(v)
                            cost_to_get_to_node[v] = cost_to_get_to_node[u] + u.action_cost(v))

            return Failure

    

In general UCS is not complete, because given an infinite number of zero-weight edges deviating away from the goal state, then UCS will follow this path forever and never find the goal. On the other hand, if there exists some ϵ>0 such that every edge has at least this weight, then UCS will find the minimum cost solution, this is because it explores nodes in the search space in non-decreasing order of cost so it must find a minimum cost path to a goal before finding any higher cost paths leading to a goal.

UCS Example Graph
Show the history of the frontier and what node is selected in a table for the following graph, where UCS is called without the cycle checking.
Later Expansion means Higher Cost
Suppose that node a is expanded after node b then c(b)c(a)
Strictly Less Cost Implies Earlier Expansion Time
Let v be a node expanded by UCS, then for any node xV such that c(x)<c(v), then x will be expanded before v will be.
UCS Produces Minimal Cost Paths
Let p be the first path that UCS produces that leads to some node x, then p is a minimal cost path to x, so long as all costs are lower bounded by some ϵR>0.
UCS Runtime
Suppose that we have a directed graph with maximum branching factor b with edge weights such that there exists some ϵ>0 such that every edge weight is at least this amount. If s,g are two connected in the graph and let C* be the minimal cost a path can have that starts from s and goes to g, then the runtime and space complexity of calling UCS on s is given by O(bC*ϵ+1)