Uniform Cost Search
Uniform Cost Search is a general version of Dijkstra’s algorithm. Dijkstra’s algorithm is a uniform cost search with with no goal state. UCS finds the shortest path to a goal state given a non-negative weighted graph & a starting state.
Algorithm
UCS is very similar to Breadth-First Search except for that it uses a step-cost function & a priority queue to determine the order in which it visits nodes. It also does not check for goal states until they have been selected for expansion from the priority queue because it might be the case that there are many path with different costs to a node which is something BFS doesn’t need to worry about.
Single Source Shortest Paths
Given an undirected graph and a starting node, determine the lengths of the shortest paths from the starting node to all other nodes in the graph. If a node is unreachable, its distance is -1. Nodes will be numbered consecutively from to , and edges will have varying distances or lengths.
Explore
5
2◄─────────┐
▲ 6 ▼ Input:
│5 3 ◄────────► 4 n=4
▼ 15 ▲ 2 graph=[(1,2,5),(1,3,15),(2,3,6),(3,4,2)]
S/1◄───────┘ start=1
- Classic Dijkstra’s problem, use Uniform Cost Search version due to its generalizability.
- Convert the into an adjacency list.
- Consider edge cases at the end by walking them through the code.