Skip to content

Solutions: Uniform-cost search

Solutions to the practice problems at the end of Uniform-cost search.

ExploredCostParent
park0none
cafe3park
school4cafe
home6school
bank8cafe
store10bank
gym13store

Three entries are skipped: (7, home, cafe), since home was already explored with cost 6, by way of the school; (13, store, school), since the store was explored with cost 10; and (14, gym, bank), since the gym was explored with cost 13. The gym and the store are both in the frontier with cost 13 at one point; the tuples are compared by their second entries, and "gym" < "store", so the gym is removed first. Either order gives the same costs. Following the parents back from the gym gives the cheapest path park, cafe, bank, store, gym, with cost 3+5+2+3=133 + 5 + 2 + 3 = 13.

The first entry for the gym is added when the bank is explored, with cost 8+6=148 + 6 = 14. The search would stop there, returning home, school, cafe, bank, gym, with cost 14. The cheapest path, through the store, costs 13, but its entry for the gym isn’t added until the store is explored, at cost 10.

When home is explored, the cafe is marked with cost 4 and parent home, and the school with cost 2. When the school is explored, the path to the cafe through the school, with cost 3, is found, but the cafe is already marked, so it’s ignored. The cafe keeps cost 4, which is wrong.

The errors spread. The bank is marked with cost 4+5=94 + 5 = 9 instead of 8; the store is marked with cost 11, from the school, before the bank is explored; and the gym is marked from the bank with cost 9+6=159 + 6 = 15, before the store is explored. The recorded cost for the gym is 15, not 13. Breadth-first search can mark a vertex as soon as it’s reached because the first path found to a vertex has the fewest edges; with weights, the first path found need not be the cheapest.

If every edge has weight 5, a path with kk edges costs 5k5k, so ordering paths by cost is the same as ordering them by number of edges. The search explores the start, with cost 0, then all the vertices one edge away, with cost 5, then those two edges away, with cost 10, and so on, the same layers as breadth-first search. The cost it records for each vertex is 5 times the distance found by breadth-first search.

Take three vertices, ss, aa, and tt, with edges from ss to tt of weight 5, from ss to aa of weight 1, and from aa to tt of weight 1. The cheapest path from ss to tt is s,a,ts, a, t, with cost 2. Add 10 to every weight: the edge from ss to tt now has weight 15, and the path s,a,ts, a, t costs 11+11=2211 + 11 = 22. The cheapest path is now the single edge. Adding a constant to every weight adds that constant once for each edge in a path, so it penalizes paths with many edges, and changes which path is cheapest.

Entries are added only when a vertex is explored, one for each neighbor not yet explored. When the first vertex is explored, it adds at most n−1n - 1 entries; the second adds at most n−2n - 2, since at least two vertices are explored by then; and so on. Altogether, at most 1+(n−1)+(n−2)+⋯+1+0=1+n(n−1)/21 + (n - 1) + (n - 2) + \cdots + 1 + 0 = 1 + n(n - 1)/2 entries are ever added, counting the entry for the start, so the frontier can never hold more than that. This is Θ(n2)\Theta(n^2), which is Θ(m)\Theta(m) for a graph in which every vertex is adjacent to every other.