What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
BFS finds a path with the fewest edges in an unweighted graph, DFS explores deeply before backtracking, and uniform-cost search (UCS) finds a path with the lowest total cost when step costs vary and are nonnegative. The right choice depends on what “best path” means for your problem.
What do BFS, DFS, and UCS do?
These algorithms explore a graph or search space from a starting point. They differ in which option they explore next—the frontier—and therefore in what they can guarantee about a path.
As an Amazon Associate I earn from qualifying purchases.
Breadth-first search (BFS)
BFS expands the shallowest unvisited nodes first: it visits the start, then its immediate neighbors, then nodes two edges away, and so on. A first-in, first-out (FIFO) queue preserves that order. If every edge represents an equal-cost step, BFS returns a path with the fewest edges to a reachable goal. It does not generally minimize total cost when edges have different weights. Boost.Graph’s traversal documentation and UIUC CS 225’s BFS and DFS resource describe this minimum-hop guarantee.
Depth-first search (DFS)
DFS follows one branch as far as it can, then backtracks to try another. An iterative implementation typically uses a last-in, first-out (LIFO) stack; a recursive implementation uses the program’s call stack. DFS is useful for graph traversal and structural tasks such as cycle detection and topological sorting. The order in which it explores nodes does not generally give a shortest path.
#1 Best Overall
Uniform-cost search (UCS)
UCS expands the frontier path with the lowest cumulative cost so far, often using a min-priority queue. It considers the cost already paid to reach a node, not how close that node looks to the goal. With nonnegative step costs and the standard rule of testing for the goal when a node is selected for expansion, the first goal selected has minimum total path cost. UCS is closely related to Dijkstra’s algorithm: UCS can stop once it selects a goal, while Dijkstra’s algorithm is commonly used to compute distances to more than one destination. UC Berkeley CS 188’s chapter on uninformed search explains UCS and its assumptions.
How are the three algorithms different?
| Algorithm | What it prioritizes | Typical frontier | Path guarantee | Typical use |
|---|---|---|---|---|
| BFS | Number of edges from the start | FIFO queue | Fewest edges when all steps have equal cost | Minimum-hop routes in an unweighted graph |
| DFS | Continuing along the current branch | LIFO stack or recursion | No general shortest-path guarantee | Traversal, cycle detection, and topological sorting |
| UCS | Cumulative path cost | Min-priority queue | Lowest total cost with nonnegative step costs and the stated goal test | Finding a least-cost route when edge costs differ |
For ordinary graph traversal using adjacency lists and visited tracking, BFS and DFS each run in O(V + E), where V is the number of vertices and E is the number of edges. This bound is for that graph representation and traversal framing; it should not be assumed to describe every search-tree formulation. Boost.Graph documents the traversal bounds.
Rank #2
Which one should you use?
- Choose BFS when you need the fewest edges in an unweighted graph, such as the route with the fewest links between two nodes.
- Choose DFS when you need to explore branches or analyze graph structure, and do not need the first path found to be shortest.
- Choose UCS when actions have different nonnegative costs and you need the route with the lowest accumulated cost.
“Shortest” needs a precise meaning. A path with fewer edges can cost more overall than a path with more edges. BFS optimizes edge count under equal step costs; UCS optimizes the sum of step costs under its assumptions.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →What can go wrong when choosing or implementing one?
Assuming BFS minimizes weighted cost
BFS orders paths by depth, not by the sum of their edge weights. If edges represent different costs, its minimum-edge path may not be the least-cost path; use UCS for nonnegative costs when total cost is the objective.
Rank #3
Assuming DFS returns the best path
DFS commits to a branch before exploring alternatives at the same depth. Finding a goal first only means that goal was encountered first in that traversal order—not that it is closest or cheapest.
Changing the frontier but expecting the same result
Replacing BFS’s queue with a stack changes the exploration order to DFS. The stack does not preserve BFS’s shallowest-first guarantee.
Rank #4
Ignoring cycles or the input’s structure
In a graph, track discovered or visited states so cycles do not cause the traversal to revisit the same vertices indefinitely. Also identify whether the input is a tree or a general graph, and whether its edges are weighted: those details affect both implementation and guarantees.
Treating UCS like heuristic search
UCS prioritizes the actual cumulative cost, often written as g(n). It does not use an estimate of the remaining distance to the goal. A heuristic-driven search is a different approach; do not choose UCS expecting it to prioritize nodes that merely appear closer.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




