Performance Analysis of BFS & DFS Algorithms For Various Applications
Performance Analysis of BFS & DFS Algorithms For Various Applications
Various Applications
Amritam Sarcar
Dept of Computer Science,
500 W. University Avenue
El Paso, TX -79902
[email protected]
1. Abstract
One of the most simplest and intuitive algorithms for graph traversal are Breadth First Search and Depth First
Search algorithms. In this paper, we try to investigate the application domain of these algorithms, in particular, how
well they perform against each other for specific applications. This paper documents the several experiments that
were carried out, along with their results and findings. Through this experimental study, our hypothesis that BFS or
DFS works better in these conditions or applications are proved or disproved. It takes into account the performance
issues including time, space and quality of results.
2. Introduction
In graph theory, breadth-first search (BFS) is one of the simplest graph searching algorithms that traverses the entire
graph starting from the root node and visiting all the neighboring nodes. Then for each of these nodes it in turn again
visits their neighboring nodes, and so on, until it finds its goal. BFS is an uninformed search algorithm that aims to
expand all nodes of a given graph until it finds its solution. In other words, it blindly searches every node in the
graph without considering the goal until it finds it. It does not use any heuristic.
Depth-First search (DFS) is an uninformed search that searching by visiting the first child node of the graph and
moves deeper and deeper until it finds its goal or the node has no further child nodes. At his point, the search
backtracks and returns to the most recent node that it has not yet explored. And this process continues.
3. Method / Algorithm
Algorithm BFS(G) 1
1. Input a graph G
2. for all n G, vertices
2.1 setLabel(n, unexplored)
3. for all e G, edges
3.1 setLabel(e, unexplored)
4. for all v G, vertices
4.1 if getLabel(v) == unexplored
4.2 BFS(G, v)
Algorithm BFS(G,v) 2
Algorithm DFS(G) 1
1. Input a graph G
2. for all n G, vertices
2.1 setLabel(n, unexplored)
3. for all e G, edges
3.1 setLabel(e, unexplored)
4. for all v G, vertices
4.1 if getLabel(v) == unexplored
4.2 DFS(G, v)
Algorithm DFS(G,v) 2
5.1. BFS
BFS(G, s) visits all the vertices and The discovery edges labeled BFS(G, s) For each vertex v in Li
edges in the connected component of Gs. form a spanning tree of the connected The path of Ts from s to v has i edges
component of Gs. Every path from s to v has at least i
edges.
5.2. DFS
Property 1 Property 2
DFS(G, v) visits all the vertices and edges The discovery edges labeled DFS(G, v)
in the connected component of v form a spanning tree of the connected
component of v.
6. Index of diagrams
7. Index of elements
8. Diagrams
ClassDiagram Content of breadthfirstsearch (breadthfirstsearch)
9. Elements
Component breadthfirstsearch
owner Component View
Component depthfirstsearch
owner Component View
Component library
owner Component View
Component main
owner Component View
Component mazegen
owner Component View
Package breadthfirstsearch
owner src
Package depthfirstsearch
owner src
Package library
owner src
Package main
owner src
source of Dependency breadthfirstsearch library Unknown Externals graphgen depthfirstsearch Java Profile
relation
owner src
ownedMember MazeGenerator
Class BFS
diagram
hierarchy
owner breadthfirstsearch
general GraphQueue
target of ComponentRealization breadthfirstsearch
relation
ownedMember return
Class GraphQueue
diagram
hierarchy
owner breadthfirstsearch
specific BFS
hierarchy
owner depthfirstsearch
general GraphList
Class GraphList
diagram
hierarchy
owner depthfirstsearch
specific DFS
Class FindNode
diagram
owner graphgen
hierarchy
owner graphgen
general MazeGenerator
owner graphgen
Class Graph
diagram
owner library
ownedMember addEdge addVertex adjacentTo degree E E Graph Graph hasEdge hasVertex st toString V vertices
Class In
diagram
owner library
ownedMember charsetName close exists hasNextLine In In In In isEmpty readAll readBoolean readByte readChar readDouble
readFloat readInt readLine readLong readString scanner
Class SET
Diagram
hierarchy
owner library
implemented Iterable<T1->Key>
interfaces
Class ST
Diagram
hierarchy
owner library
implemented Iterable<T1->Key>
interfaces
bound ST<Key->String,Val->SET<Key->String>>
elements
Class Visitor
diagram
owner library
Class Main
diagram
owner main
ownedMember main
Class Stats
diagram
owner main
owner main
Class MazeGenerator
diagram
hierarchy
owner mazegen
ownedMember generateMaze
specific GraphGenerator
11. Results
The above two graphs shows what happens when the implementation can go wrong. In the first graph, using
recursion we see that DFS always take more time to travel the entire graph, which may not always hold true.
However this happens because Recursion does not use pushing and popping of elements, since it is done implicitly
by the JVM. Thus using a stack for the DFS is better suited and shows that there is no correlation between vertices
and the type of search. It shows that under certain circumstances, BFS wins over DFS and other times the reverse
happens.
The above graph shows how DFS and BFS varies in their time of traversal when both the number of vertices and the
number of edges are varied. This graph also shows that although there is no clear winner, however BFS is almost
always better than DFS for finding whether the graph is connected or not.
This shows how DFS and BFS reacts when edges are varied keeping the number of vertices fixed. BFS does
perform better than DFS, however, we can also observe that when edges are almost equal to the number of vertices,
DFS tend to perform a bit better.
The graph above shows how vertices are varied keeping the number of edges to be constant. We can note that both
DFS and BFS are very close to each other, in terms of performance time. We may also note that both strategies tend
to decrease with increase number of vertices as edges are fixed. This observation is clear with our theoretical
knowledge that since for a fixed number of edges, increasing the number of edges has no appreciable change to BFS
or DFS.
How both components of the Graph that is edge and vertex are changed, and so is the performance of the two
strategies. It does show that by increasing both the components, BFS and DFS react in the same manner.
The above two graphs depicts the results when a particular node (or vertex) is to be found. This was simulated by
varying the number of vertices and a random vertex was chosen to be found. The first graph shows the number of
vertices that were traversed before the particular node was found. It shows that both DFS and BFS are very closely
matched to each other. The second graph shows the execution time taken to find the particular node. This also shows
how time and number of vertices graph show almost the same nature, showing that the execution is nearly perfect
free from noise.
The classic problem of traversal through a maze has been simulated here. The first graph shows how the no. of
vertices is traversed before they can get out of the maze. Although, the author and the audience is aware of the fact
that DFS works better for traversal through a maze. However we can observe that in both the graphs, the BFS
algorithm performs very poorly. And it actually grows linearly with increase number of vertices. Since this
simulation was for a fixe point Exit which was basically situated at the last cell of the maze, it is evident that DFS
would traverse much faster than BFS which in fact was traversing the entire graph/maze.
The classic problem of traversal through a maze is being re-visited. The first graph shows how the no. of vertices is
traversed before they can get out of the maze. However this time we can observe that in both the graphs, since now
the exit point has been randomly fixed and not the last cell, the BFS algorithm does not perform as poorly as in the
previous graph. In fact it actually performs better in some cases than DFS, showing that choosing the exit point is
crucial to chose which strategy is better.
12. Conclusion & Future Work
From the results and analysis, we clearly see that there is no clear winner in terms of performance across all
applications. Our hypothesis started by stating that BFS and DFS are individually better than each other for
certain specific applications.
We thus conclude by stating that, It is indeed true that DFS and BFS are better than each other for
certain applications.
For the future, it remains to be seen whether novel algorithms which may use hybrid techniques and may
outperform BFS and DFS individually.