You are on page 1of 6

Shortest path finding: A comparison between A*,

Breadth First Search, and Uniform Cost Search


Nguyen Nguyen

Bao Truong

Khoa Vo

Advanced Program in Computer Science Advanced Program in Computer Science Advanced Program in Computer Science
Faculty of Information Technology
Faculty of Information Technology
Faculty of Information Technology
University of Science, VNU - HCM
University of Science, VNU - HCM
University of Science, VNU - HCM
vtdkhoa13@apcs.vn
tmbao@apcs.vn
npnguyen@apcs.vn

AbstractThe shortest path problem is the problem of finding


the path of minimum weight between two vertices in a graph.
It has many real world applications such as Very-large-scale
integration design, routing in computer networks, robotics design.
One of the most prominent applications is finding the shortest
path between two locations on a map. For this application, the
authors propose the use of A*, Breadth First Search, and Uniform
Cost Search to solve the problem and make comparisons between
those search algorithms. The algorithms are implemented in
Python with a program allowing users to input a map, choose the
start and goal locations then output the shortest path from start
to goal. The program is run on 10 real world and randomized
map data. The results show that each algorithm has its own uses
in particular situations. The authors believe this result can be
used to help in developing a road directions application for public
release in the future.
Index TermsShortest path; Breadth First Search; Uniformcost Search; A* Search

I. I NTRODUCTION
A. Problem Statement
Pathfinding, an application of Artificial Intelligence has
proved to be one of the stepping stones for technological
revolutions. Indeed, applications of pathfinding can be found
from real world problems such as finding optimal route in
transportation to the more common usages in video games
industry. In gaming industry, pathfinding is used in determining a moving pattern of an entity through obstacles. For
each of these entities, a suitable route is calculated from the
stating position of the entity to the goal. In addition, the route
must be sensible, and as short as possible [1]. An example of
pathfinding is shown in Figure 1.

Fig. 1. An example of pathfinding in the video game Pacman. The red ghost
has to find the shortest path to Pacman

B. Background
Two main functionalities of pathfinding are finding a route
between two nodes in a unweight graph and computing the
shortest path in the graph with heuristic factor. The shortest
path problem is designed essentially to find a path of minimum length between two specified vertices of a connected
weighted graph.[2] It has many real world applications such
as Very-large-scale integration (VLSI) design [3], etc. One of
the most prominent applications is finding the shortest path
between two locations on a map.
The problem was first introduced in April 1954 at the
Symposium on Information Networks by Shimbel in New
York, in the extend of matrix methods for unit-length shortest
paths, in which the proposed method has the time complexity
of O(n4 ) to find the distances between all pairs of points.
Breadth-first Search was discovered by E.F. Moore(19592003), who implemented the algorithm for finding the shortest
path out of a maze and also independently discovered by C. Y.
Lee as an algorithm for wire routing. A* Search, an extension
of Dijkstra algorithm, was first described by Peter Hart, Nils
Nilsson and Bertram Raphael of Stanford Research Institute
in 1968.

C. Related Works
Many studies has been conducted to make comparisons
between shortest path algorithms. In 2014, Girish P Potdar
and R. C. Thool used 4 algorithms: Hill Climbing, Steepest
Ascent Hill Climbing, Best-fist Search, and A* to find the
shortest path in the dataset of 1000 to 3000 nodes. From the
test results, A* is shown to be the algorithm that produces
the least number of nodes, and also has the least number of
edges[4].
In 2011, Ariel Felner conducted a research to compare two
similar shortest path algoriths: Dijkstra algorithm and Uniform
Cost Search. The results show that Uniform Cost Search is
more superior in term of running time and number of created
vertices. To be specific, the maximum number of vertices in
the queue at a given time and the running time of Uniform
Cost Search is less than that of Dijsktra algorithm[5].

D. Purpose and Methods


The purpose of this paper focuses on the efficiency of the
proposed algorithms to solve the shortest path problem. For
this application, the authors propose the use of A*, Breadth
First Search, and Uniform Cost Search to solve the problem
and make comparisons between those search algorithms in
terms of time complexity, space complexity, completeness, and
optimality. The main reasons why the authors choose these
three methods are as followed:
Breadth-first Search: An uniformed search strategy but
complete. Although it is bad memory-wise, Breadth-first
Search is still acceptable if the goal is not too deep
and there are many paths and loops, which is similar
to finding the shortest path in a real world map.
Uniform Cost Search: An algorithm that similar to
Breadth-first Search. The authors choose this algorithm
to compare the results with Breadth-first Search in order
to find the best algorithm for real world map.
A* Search: has a wide range of practical uses and is
flexible because of the freedom of heuristic choice.
A brief comparison of the algorithms can be found in table 1.
Uniform Cost
A*
Search
Complete?
Yes*
Yes
Optimal?
Yes
Yes
Time
O(bd )
O(b1+[C/] )
1+[C/]
Space
O(bd )
O(b
)
TABLE I
C OMPARISONS BETWEEN ALGORITHMS B : BRANCHING FACTOR
Criterion

Breadth-first
Search
Yes
Yes*
O(bd )
O(bd )

In the table, b is the branching factor, d is the depth of the


solution and C is the cost of the optimal solution. Notice
that, Breadth-first Search is only optimal under a restricted
constraint. In addition, there is a condition for Uniform Cost
Search to achieve the total completeness, all of which will be
addressed in Section II.
The remainder of this paper is organized as follows: Section
II discusses in details the three methods used in the project.
The experiment, results and the final conclusion are written in
Section III of the paper.
II. M ETHODOLOGY
A. Uninformed Search strategies and Heuristic Search strategies
Uninformed Search(or Blind Search) strategies are strategies that, besides the information provided by the problem
definition, have no additional information regarding the states
of the problem. These strategies distinguish goal state from
non-goal state, and generate the successors; in other words,
expending the non-goal states node. The order in which nodes
are expanded is the core difference of algorithms.
Heuristic Search(or Informed Search) strategies are strategies that uses problem-specific knowledge to efficiently find
solutions for the problem in addition to information already

provided. The additional knowledge is in the form of a


Heuristic function.
Of all the algorithms used in the project, Breadth-first
Search and Uniform Cost Search belong to Uninformed Search
strategies, while A* Search belongs to Heuristic Search strategies. The specific details of the three algorithms are given
below.
B. Breadth-first Search
Breadth-first Search is implemented by expanding the root
node first then all of its successors, and so on. In other words,
the main idea of Breadth-first Search is to expand shallowest
unexpanded node [3]. Breadth-first Search uses a FIFO queue
in order to expand the shallowest unexpanded node. As a
result, new nodes go to the back of the queue because they
are deeper than their parents, old nodes, which are shallower
than the new nodes, get expanded first. In Breadth-first Search,
the goal test is applied to each node when it is generated
rather than when it is selected for expansion. Also, because
the nature of algorithm is to discard any new path to a state
already in CLOSED or OPEN set). Thus, making the path
to a found node is always the shallowest in CLOSED. A
simple illustration of the algorithm can be found in Figure 2,
in which all explored nodes with no descendants are removed
from memory.
The algorithm Breath-first Search is observed to be complete. Since the algorithm can always find the shallowest goal
node at some finite depth d, after generating all shallower
nodes (provided the branching factor b is finite). However, the
shallowest goal node is not necessarily the optimal one. Since
the condition for Breadth-first Search to be optimal is only if
path cost is a nondecreasing function of the depth of the node.
In term of time complexity, in the worst case scenario, if
each node has b child nodes, and their child nodes also have
b child nodes. Then the time it takes to find the goal node, the
last node generated at the solutions depth d is: b + b2 + b3 +
+ bd = O(bd ). In the case of space complexity of breadthfirst Search, every node generated remains in memory. As a
result, there will be O(bd1 ) nodes in the explored set and
O(bd ) nodes in the frontier. Therefore, the space complexity
of Breadth-first Search is O(bd ).
The authors observes that Breadth-first Search is useful in
the following cases:

Space is not a problem.


The solution containing the fewest arcs.
Few solutions may exist, and at least one has a short path
length.
Infinite paths may exist, because it explores all of the
search space, even with infinite paths. . . .

The above cases are similar to common case of finding


the shortest path in a real world map. Therefore, the authors
choose Breadth-first Search as one of the three methods used
in the project.

C. Uniform Cost Search


The condition for Breadth-first Search to be optimal is all
step-cost must be equal, a restricted circumstance. Thus, by
adding a simple extension, a newly implemented algorithm
can be optimal with any step-cost function, this algorithm is
called Uniform Cost Search[3]. Uniform Cost Search expands
the node n with the lowest path cost g(n) instead of expanding
the shallowest node. This is done by storing the OPEN as a
priority queue ordered by g.
Note that aside from the ordering of the queue by path cost,
Uniform Cost Search also has two significant differences from
Breadth-first Search. First, the goal test is applied to a node
when it is selected for expansion since the first generated goal
node may be on a suboptimal path. Second, a test is added in
case a better path is found to a node currently in OPEN or
CLOSED.
Given that Uniform Cost Search only cares about the total
cost of a path. Thus, the algorithm will get stuck in an
infinite loop should a path with an infinite sequence of zerocost actions exists. Therefore, completeness is only guaranteed
when the costs of every steps are at least some constant  > 0.
In cases that every step cost more than 0, and assumed that
the solution has a finite branching factor, then there exist a
finite number of expansions required until the total path cost
is equal to the path cost of the goal state. Thus, the algorithm
will reach its goal state in a finite number of steps.

costs are the same, uniform-cost search is similar to BreadthFirst Search. However, Breadth-first Search ends when the
goal node is generated but Uniform Cost Search continues
to examine all the nodes at the goals depth to see if one has
a lower cost. Thus, Uniform Cost Search cost more time and
space than Breadth-first Search in the worst case scenario.
D. A* Search
The main idea of Best-first search is to follow the best path
available from the current position, rather than always following the depth-first approach. At each node, the algorithm uses
an evaluation function f (n) to estimate the node quality[3].
A, perhaps the most widely known form of best-first search.
It is an algorithm that uses heuristic to guide search while
ensuring that it will compute a path with minimum cost. It
evaluates nodes by combining g(n), the cost to reach the node,
and h(n), the cost to get from the node to the goal. Thus an
evaluation function is created: f (n) = g(n) + h(n), which
estimates total cost of path through n to goal. The authors
observe that A* Search is both optimal and complete.
The first condition we require for optimality is that h(n)
be an admissible heuristic that never overestimates the cost to
reach the goal. Since g(n) is the actual cost to reach n along
the current path to n , and f (n) = g(n) + h(n), it is clear that
f (n) do not overestimates the true cost of a solution along the
current path through n.
The second condition require for optimality is the consistency of h(n). A heuristic is consistent if for every successor
n of a node n generated by any action a, h(n) c(n, a, n0 ) +
h(n0 ). Admissible heuristics are generally consistent: if there
were a route from n to Gn that was cheaper than h(n), that
would violate the property that h(n) is a lower bound on the
cost to reach Gn . This is also called a form of the triangle
inequality as shown in Figure 2.

Fig. 2. Breadth-first search on a simple binary tree[3].

In general, Uniform Cost Search is optimal. Proving Uniform Cost Searchs optimality using indirect proof by assuming that it is not optimal. Then there must be exists a goal
state with path cost smaller than the goal state which was
found. However, this contradicts the definition of Uniform
Cost Search, which is the smaller cost path must be expanded
first. For that reason, the first goal node selected for expansion
must be the optimal solution.
Uniform Cost Search is directed by path costs instead of
depths; therefore, the complexity of the algorithm is characterized in terms of b and C , the cost of the optimal
solution. Assumed that every action costs at least , then in
the worst case scenario, the time and space complexity are
both O(b1+[C/] ). This is due to the fact that when all step

Fig. 3. The triangle inequality

Overall, A* Search is not only complete, but is also optimal


with the time complexity in the worst case is O(bd ) and the
space complexity in the worst case is also O(bd ). In which b
is the branching factor and d is the depth of the solution.
III. E XPERIMENT AND R ESULT
A. Set up
The algorithms are implemented in python, the full implementation is available at: https://github.com/tmbao/cs420project. Networkx package is used to render a graph with the

shortest path as the output. The road networks are built from
55 real world and randomized map data. All algorithms use a
list named trace to store the path traversed. The authors also
provide a simple User interface as in Figure 4:

the shortest path from Arad to Bucharest. For A* Search, the


heuristic distances from nodes to the goal are also provided.
For better visualization, see Figure 5 and Table .

Fig. 5. Romania map

Arad: 366
Bucharest: 0
Crainova: 160
Dobreta: 242
Eforie: 161
Fagaras: 176
Giurgiu: 77
Hirsova: 151
Iasi: 226
Lugoj: 244

Mehadia: 241
Neamt: 234
Oradea: 280
Pitesti: 100
Rimnicu Vilcea: 193
Sibiu: 253
Timisoara: 329
Urziceni: 80
Vaslui: 199
Zerind: 374
TABLE II
S TRAIGHT- LINE DISTANCE TO B UCHAREST

Fig. 4. User interface of the implemented program

B. Result
The shortest path from Arad to Bucharest is as followed:
Arad Sibiu Rimnicu Vilcea Pitesti Bucharest
Below here are the results returned by the three algorithm,
the shortest path is visualized as the blue nodes
Neamt
Eforie

87.0

86.0

Iasi
92.0

Hirsova

Vaslui
98

.0

142.
0

Urziceni
85.0

Giu_rgiu

90.0

Bucharest
101.0

.0

1
21

Pitesti
138

97.0

Fagaras

Rimnicu_Vilcea

99.0

.0

146.0

Craiova

80.0

12

0.0

Sibiu

Dobreta
75.0

.0
51

140.0

Mehadia

Oradea
.0

Zerind

75.0

Arad

.0

71

70

As shown in Figure 4, the input (map information) is read


from two text files whose directory should be filled in the
textboxes next to Heuristic File and Edge File. To edit
the current map data, buttons labeled as Edit Heuristic File
and Edit Edge File are provided, respectively. The format
of these input files are as followed:
Each line of the heuristic file contains a list of cities and
the heuristic value associated with each city in the form:
City Name Heuristic Value.
The edge file contains information of edges, each line is
in the form: First Vertex Second Vertex Edge Weight.
In addition, when using A* Search algorithm, another text
file is required. The text file contains the information of the
heuristic value of each city. The directory of the heuristic file
in input in the textbox that is next to Heuristic File and the
button Edit Heuristic File is provided to set up that file. The
format of the heuristic file is as followed:
The result of a running instance will be store as an image.
To change the name of the output image, fill in the textbox
next to Output image. To choose which algorithm will be
used in the program, fill in the textbox next to Algorithm
with the following formats:
Astar if the desired algorithm is A* Search.
BFS if the desired algorithm is Breadth-first Search.
UCS if the desired algorithm is Uniform Cost Search.
1) Test case: The experiment use the map of Romania as
the test input. The requirement for this test case is to find

118.
0

Timisoara

111.0

Lugoj

Fig. 6. The result returned by Breadth-first Search algorithm

1) Breadth-first Search: Figure 6 is the returned graph of


Breadth-first Search. The path, running time and the total cost
are as followed:

Found path: Arad Sibiu Fagaras Bucharest


Running time: 1302 ms
Total cost: 450

Found path: Arad - Sibiu - Rimnicu Vilcea - Pitesti Bucharest


Running time: 1512 ms
Total cost: 418
Furthermore, the authors also conducts experiments on ten
randomized tests. The results are shown in figure 9, 10, and
11.

Neamt
87.0

Eforie

Vertices
20764
19539
46736
11157
16052
22528
21887
39338
45665
27543

86.0

Iasi
92.0

Hirsova

Vaslui
.0

142.

98

Urziceni
85.0

Giu_rgiu

90.0

Bucharest
101.0

1.0

21

Pitesti
138

97.0

Fagaras

.0

Rimnicu_Vilcea

99.0

146.0

Craiova

80.0

UCS
11270
12613
12622
22664
4297
2446
25354
56760
14526
9931

0.0

75.0

140.0

Fig. 9. The number of loops corresponding to the three algorithms

Mehadia

Oradea
71

.0

Arad

75.0

Zerind

70

.0

118.

111.0

Timisoara

Lugoj

Fig. 7. The result returned by Uniform Cost Search algorithm

2) Uniform Cost Search: Figure 7 is the returned graph of


Uniform Cost Search. The path, running time and the total
cost are as followed:

BFS
6214
6224
10135
4781
12425
3692
18644
35448
23589
26884

Dobreta

1.0

15

A*
7036
9235
12131
21439
2913
1858
22411
53837
13013
6875

12

Sibiu

Edges
332224
234468
46736
145041
256832
180224
109435
196690
91330
495774

Found path: Arad Sibiu


Bucharest
Running time: 1411 ms
Total cost: 418

Rimnicu Vilcea

Pitesti

Vertices
20764
19539
46736
11157
16052
22528
21887
39338
45665
27543

Edges
332224
234468
46736
145041
256832
180224
109435
196690
91330
495774

A*
0.466814041
0.503247976
0.35687995
0.659502983
0.377344847
0.139426947
0.815513134
1.489337921
0.447788
1.011003017

BFS
0.256626129
0.219310999
0.157799959
0.224136829
0.493091106
0.147074223
0.489060879
0.896718025
0.397804976
1.163685083

UCS
0.527646065
0.549454927
0.319380999
0.698724031
0.256123066
0.119521856
0.629976988
1.572528124
0.44672823
0.797452927

Fig. 10. The running time corresponding to the three algorithms


Neamt
87.0

Eforie
86.0

Iasi
92.0

Vertices
20764
19539
46736
11157
16052
22528
21887
39338
45665
27543

Hirsova

Vaslui
.0

142.

98

Urziceni
85.0

Giu_rgiu

90.0

Bucharest
101.0

1.0

21

Pitesti
138

97.0

Fagaras

Rimnicu_Vilcea

99.0

.0

146.0

Craiova

80.0

12

0.0

Sibiu

UCS
7699
10804
77514
13361
7343
11349
17726
19992
27844
9338

75.0

.0

Arad

Fig. 11. The shortest distance corresponding to the three algorithms

70

71

Zerind

BFS
13406
15303
77514
18222
13727
11349
32538
28560
39050
24598

75.0

Mehadia

Oradea
.0

A*
6799
10804
77514
13361
7343
11349
17726
19992
27844
9338

Dobreta

.0
51

140.0

Edges
332224
234468
46736
145041
256832
180224
109435
196690
91330
495774

118.

Timisoara

111.0

Lugoj

Fig. 8. The result returned by A* Search algorithm

3) A* Search: Figure 8 is the returned graph of A* Search.


The path, running time and the total cost are as followed:

Our randomized tests involving up to about 50000 vertices


and 500000 edges show the differences in performance and
optimality among the three algorithms. As the figure 9, 10,
and 11 provide, Breadth-first Search runs much faster than A*
and Uniform Cost Search in most of the cases since it does
not have to spend time in order to maintain a priority queue.

Although Breadth-first Search gives us the smallest number of


edges connecting the source city and the target one, it does
not provide results as good as the other two algorithms in
terms of distance between the source city and the target one.
On the other hand, A* and Uniform Cost Search both return
the shortest path within approximately the same running time.
However, because of applying heuristic function, A* seems
to produce smoother path. More specifically, the number of
vertices visited by A* is less than those of Uniform Cost
Search.
C. Conclusion
According to the experiment results, the authors conclude
the followings:
The path found by A* Search and Uniform Cost Search is
the most optimal path. While the path found by Breadthfirst Search is not the shortest one.
Breadth-first Search is the fastest algorithm, which is followed by Uniform Cost Search, and A* Search produces
the slowest running time.
The path provided by Breadth-first Search is not the optimal
path because Breadth-first Search only takes the shallowest
path, which is not the shortest path in the test case. However,
Breadth-first Search use queue whose access time complexity
is O(1), while Uniform Cost Search and A* Search use
priority queue, which requires the access time complexity
O(logn). Uniform Cost Search runs faster than A* Search
due to the fact that in A* Search, there is an additional
time required to calculate the heuristic function; thus, making
its running time approximately slower than that of that of
Uniform Cost Search. However, the running time of A* Search
can be improved with better heuristics which focuses contours
around the optimal path; thus, improves the running time of
A* Search.
In conclusion, each of the three algorithms has its own uses
in different situations:
Breadth-first Search: The solution containing the fewest
arcs; for example, the least number of flights to go from
one city to another.
Uniform Cost Search and A* Search can be used interchangeably. However, in the case that a reasonable
heuristic function is indicated, the author recommend to
use A* Algorithm.
R EFERENCES
[1] Ian Millington and John Funge, Artificial intelligence for games, 2nd ed.
Morgan Kaufmann, 2009.
[2] Andrew Butterfield, Gerard Ekembe Ngondi, and Anne Kerr , A Dictionary of Computer Science, 7th ed. Oxford University Press, 2016.
[3] Stuart Russell and Peter Norvig, Artificial Intelligence A Modern Approach, 3rd ed., Stuart Russell and Peter Norvig, Eds. Pearson Education,
Inc., 2010.
[4] Girish P Potdar and R. C. Thool, Comparison of various heuristic search
techniques for finding shortest path, 2014.
[5] A. Felner, Position paper: Dijkstras algorithm versus uniform cost search
or a case against dijkstras algorithm, 2011.

You might also like