2. Dhaka International University
2
Presentation on
Presented To
Mr. Tahzib Ul Islam
Assistant Professor
Department of CSE
Dhaka International University
Presented By
Group: Quiet Fools
Name, Roll
Shuvongkor Barman (38)
Abdullah Al-Helal (33)
Subarna (17)
CSE-E59-7th
3. 3
1. Introduction
2.What is BFS?
3. Necessary Terms
4. BFS Illustration with Example
5. Pseudocode of BFS
6. BFS Program (Java) and Output
7.Algorithm with Example
8. Application of BFS?
9.Time and Space Complexity
10. Conclusion
11. Questions
4. BFS and its application in finding connected
components of graphs were originally invented
in 1945 by Konrad Zuse.
He was a German Civil Engineer, inventor and
computer pioneer. His greatest achievement
was the world's first programmable computer.
4
Inventor of BFS
Konrad Zuse
5. 5
Breadth-first search (BFS) is an algorithm for
traversing or searching Tree or Graph data structures.
It starts at the tree root or some arbitrary node of a graph,
sometimes referred to as a 'search key’ and explores all of
the neighbor nodes at the present depth prior to moving on
to the nodes at the next depth level.
What is Breadth First Search (BFS)?
6. A Graph G=(V, E) consists a set of vertices, V, and a set of edges, E.
A tree is a collection of entities called nodes. Nodes are connected by
edges. Each node contains a value or data, and it may or may not have
a child node
Tree is also a Graph.
6
We Should Know
7. Visiting a Vertex: It means going on a particular vertex or checking a
vertex’s value.
Exploration of Vertex: It means visiting all the adjacent vertices of a
selected node.
Traversing: Traversing means passing through nodes in a specific
order
7
We Should Know
Level-Order: It is a traversing method,
where we have to visit every node on a
level before going to a lower level.
8. * Breadth means the distance or measurement from side to
side of something; width; wideness; diameter; range; extent.
8
* Breadth First Search for a graph is similar to Breadth First
Traversal of a tree. The only catch here is, unlike trees, graphs
may contain cycles, so we may come to the same node again. To
avoid processing a node more than once, we use a Boolean
visited array. For simplicity, it is assumed that all vertices are
reachable from the starting vertex.
We Should Know
9. For example, in the following graph, we start traversal
from vertex 2. When we come to vertex 0, we look for
all adjacent vertices of it. 2 is also an adjacent vertex
of 0. If we don’t mark visited vertices, then 2 will be
processed again and it will become a non-terminating
process. A Breadth First Traversal of the following
graph is 2, 0, 3, 1.
9
14. 14
Fig: 10
* BFS is just like Level Order
& it follow 3 simple rules
Rule 1 : Visit the adjacent unvisited vertex.
Mark it as visited. Insert it in a queue &
Display it.
Rule 2 : If no adjacent vertex is found,
remove the first vertex from the queue.
Rule 3 : Repeat Rule 1 and Rule 2 until the
queue is empty.
29. BFS (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.
mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue, whose neighbour will be visited now
v = Q.dequeue( )
//processing all the neighbours of v
for all neighbours w of v in Graph G
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
mark w as visited.
29
Pseudocode
32. BFS(G) // starts from here
{
for each vertex u V-{s}
{
color[u]=WHITE;
prev[u]=NIL;
d[u]=inf;
}
color[s]=GRAY;
d[s]=0; prev[s]=NIL;
Q=empty;
ENQUEUE(Q,s);
While(Q not empty)
{
u = DEQUEUE(Q);
for each v adj[u]{
if (color[v] == WHITE){
color[v] = GREY;
d[v] = d[u] + 1;
prev[v] = u;
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
Data: color[V], prev[V],d[V]
Breadth First Search Algorithm
32
33. r s t u
r s t u
v w x y
Vertex r s t u v w x y
color W W W W W W W W
d
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
33
34.
0
r s t u
v w x y
sQ:
vertex r s t u v w x y
Color W G W W W W W W
d 0
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
34
35. 1
0
1
r s t u
v x y
w rsQ:
w
vertex r s t u v w x y
Color G B W W W G W W
d 1 0 1
prev s nil nil nil nil s nil nil
Breadth-First Search: Example
35
36. 1
0
1
2
2
r s t u
v
w x
y
t xw rsQ:
vertex r s t u V w X y
Color G B G W W B G W
d 1 0 2 1 2
prev s nil w nil nil s w nil
Breadth-First Search: Example
36
37. 1
2
0
1
2
2
r s t u
v w x
y
vt xw rsQ:
vertex r s t u v w x y
Color B B G W G B G W
d 1 0 2 2 1 2
prev s nil w nil r s w nil
Breadth-First Search: Example
37
38. 1
2
0
1
2
2
3
v
w x
y
uvt xw rsQ:
vertex r s t u v w x y
Color B B B G G B G W
d 1 0 2 3 2 1 2
prev s nil w t r s w nil
Breadth-First Search: Example
38
39. 1
2
0
1
2
2
3
3
r s t u
v
w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G G B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
Breadth-First Search: Example
39
40. 1
2
0
1
2
2
3
3
r s t u
v
w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G G B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
Breadth-First Search: Example
40
41. 1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B B B B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
Breadth-First Search: Example
41
42. 1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G B B B B
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
Breadth-First Search: Example
42
43. There are many applications Breadth First Search. Let’s discuss some of them.
1) Shortest Path and Minimum Spanning Tree for unweighted graph: In an
unweighted graph, the shortest path is the path with least number of edges.
With Breadth First, we always reach a vertex from given source using the
minimum number of edges. Also, in case of unweighted graphs, any spanning
tree is Minimum Spanning Tree and we can use Breadth first traversal for
finding a spanning tree.
2) Peer to Peer Networks: In Peer to Peer Networks like BitTorrent, Breadth
First Search is used to find all neighbor nodes.
3) Ford–Fulkerson method for computing the maximum flow in a flow network.
43
Applications of Breadth First Search
44. 4) Crawlers in Search Engines: Crawlers build index using Breadth First Search.
The idea is to start from source page and follow all links from source and keep doing
same. Depth First Traversal can also be used for crawlers, but the advantage with
Breadth First Traversal is, depth or levels of the built tree can be limited.
5) Social Networking Websites: In social networks, we can find people within a
given distance ‘k’ from a person using Breadth First Search till ‘k’ levels.
6) GPS Navigation systems: Breadth First Search is used to find all neighboring
locations. Navigation systems such as the Google Maps, which can give directions to
reach from one place to another use BFS. They take your location to be the source
node and your destination as the destination node on the graph. (A city can be
represented as a graph by taking landmarks as nodes and the roads as the edges
that connect the nodes in the graph.) BFS is applied and the shortest route is
generated which is used to give directions or real time navigation.
44Applications of Breadth First Search
45. 7) Broadcasting in Network: In networks, a broadcasted packet follows
Breadth First Search to reach all nodes.
8) In Garbage Collection: Breadth First Search is used in copying
garbage collection.
9) Cycle detection in undirected graph: In undirected graphs, either
Breadth First Search or Depth First Search can be used to detect cycle.
In directed graph, only depth first search can be used.
10) Path Finding: We can either use Breadth First or Depth First
Traversal to find if there is a path between two vertices.
45
Applications of Breadth First Search
46. The time complexity of BFS is, O(V + E)
where V is the number of nodes and E is the number of edges.
The space complexity can be expressed as, O(V),
where V is the cardinality of the set of vertices
46Time and Space Complexity
47. 47
Conclusion
Breadth First Search (BFS) algorithm is a very important algorithm that traverses a
graph in a breadthward motion and uses a queue (FIFO, First-In-First-Out method)
to remember to get the next vertex to start a search, when a dead end occurs in any
iteration.
Breadth-first search produces a so-called breadth-first tree and it is same as level-
order traversing method.
Breadth-first search can be used to solve many problems in graph theory. It can
also
be applied to solve of many real life problems for example GPS Navigation systems,
Computer Networks, Facebook structure, Web Crawlers for search engine etc.
That is why As a computer engineer we should have a proper knowledge of Breadth
First Search (BFS).