Solutions: Uniform-cost search
Solutions to the practice problems at the end of Uniform-cost search.
Problem 1: Trace uniform-cost search
Section titled “Problem 1: Trace uniform-cost search”| Explored | Cost | Parent |
|---|---|---|
| park | 0 | none |
| cafe | 3 | park |
| school | 4 | cafe |
| home | 6 | school |
| bank | 8 | cafe |
| store | 10 | bank |
| gym | 13 | store |
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 .
Problem 2: Stopping too soon
Section titled “Problem 2: Stopping too soon”The first entry for the gym is added when the bank is explored, with cost . 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.
Problem 3: Marking too soon
Section titled “Problem 3: Marking too soon”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 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 , 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.
Problem 4: Equal weights
Section titled “Problem 4: Equal weights”If every edge has weight 5, a path with edges costs , 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.
Problem 5: Adding a constant
Section titled “Problem 5: Adding a constant”Take three vertices, , , and , with edges from to of weight 5, from to of weight 1, and from to of weight 1. The cheapest path from to is , with cost 2. Add 10 to every weight: the edge from to now has weight 15, and the path costs . 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.
Problem 6: How big can the frontier get?
Section titled “Problem 6: How big can the frontier get?”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 entries; the second adds at most , since at least two vertices are explored by then; and so on. Altogether, at most entries are ever added, counting the entry for the start, so the frontier can never hold more than that. This is , which is for a graph in which every vertex is adjacent to every other.