INTRODUCTION TO GRAPH TRAVERSAL
UNDERSTANDING THE BREADTH-FIRST SEARCH ALGORITHM WITH AN EXAMPLE
APPLICATIONS OF BREADTH-FIRST SEARCH
WHAT IS THE BREADTH-FIRST SEARCH?
www.edureka.co
BREADTH-FIRST SEARCH ALGORITHM PSEUDOCODE
INTRODUCTION TO GRAPH TRAVERSAL
www.edureka.co
The process of visiting and exploring a graph for processing is called graph traversal. To be more
specific it is all about visiting and exploring each vertex and edge in a graph such that all the
vertices are explored exactly once.
Introduction To Graph Traversal
www.edureka.co
WHAT IS THE BREADTH-FIRST SEARCH?
www.edureka.co
Breadth-First Search algorithm is a graph traversing technique, where you select a random initial node
(source or root node) and start traversing the graph layer-wise in such a way that all the nodes and their
respective children nodes are visited and explored.
What is the Breadth-First Search Algorithm?
www.edureka.co
a
b
d
c
e
a
b
d
c
e
ExploringVisiting
BREADTH-FIRST SEARCH EXAMPLE
www.edureka.co
Breadth-First Search algorithm follows a simple, level-based approach to solve a problem. Consider
the above binary tree (which is a graph). Our aim is to traverse the graph by using the Breadth-First
Search Algorithm.
Understanding the Breadth-First Search Algorithm with an example
www.edureka.co
a
b c
d e f g
What is a
Queue?
Binary Tree
A queue is an abstract data structure that follows the First-In-First-Out methodology (data inserted
first will be accessed first). It is open on both ends, where one end is always used to insert data and
the other is used to remove data.
What is a Queue?
www.edureka.co
Enqueue operation Dequeue operation
Enqueue operation: To insert data
Dequeue operation: To remove data
www.edureka.co
a
b c
d e f g
Step 1: Take an Empty Queue.
Queue
Binary Tree
www.edureka.co
a
b c
d e f g
Step 2: Select a starting node (visiting a node) and insert it into the Queue.
Queue
Binary Tree
a
www.edureka.co
a
b c
d e f g
Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its child nodes
(exploring a node) into the Queue.
Queue
Binary Tree
a
www.edureka.co
a
b c
d e f g
Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its child nodes
(exploring a node) into the Queue.
Queue
Binary Tree
bc a
www.edureka.co
a
b c
d e f g
Step 4: Print the extracted node.
Queue
Binary Tree
bc
Print:
a
a
www.edureka.co
www.edureka.co
• ‘a’ is the root node: insert it into the Queue
• Extract node ‘a’ and insert the child nodes of ‘a’, i.e.,
‘b’ and ‘c
• Print node ‘a’
• The queue is not empty and has node ‘b’ and ‘c’.
Extract ‘b’ and insert the child nodes of ‘b’, i.e., node
‘d’ and ‘e’
• Repeat the steps until the queue gets empty
BREADTH-FIRST SEARCH ALGORITHM
PSEUDOCODE
www.edureka.co
www.edureka.co
(G, s) is input, here G is the graph and s is the root node
A queue ‘Q’ is created and initialized with the source node ‘s’
All child nodes of ‘s’ are marked
Extract ‘s’ from queue and visit the child nodes
Process all the child nodes of v
Stores w (child nodes) in Q to further visit its child nodes
APPLICATIONS OF BREADTH-FIRST SEARCH
ALGORITHM
www.edureka.co
Breadth-first Search is a simple graph traversal method that has a surprising range of applications. Here are a few
interesting ways in which Bread-First Search is being used:
Applications Of Breadth-First Search Algorithm
www.edureka.co
Breadth First
Search Applications
Web crawling
GPS Navigation Finding the
shortest path
Broadcasting
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co

Breadth First Search Algorithm In 10 Minutes | Artificial Intelligence Tutorial | Edureka

  • 2.
    INTRODUCTION TO GRAPHTRAVERSAL UNDERSTANDING THE BREADTH-FIRST SEARCH ALGORITHM WITH AN EXAMPLE APPLICATIONS OF BREADTH-FIRST SEARCH WHAT IS THE BREADTH-FIRST SEARCH? www.edureka.co BREADTH-FIRST SEARCH ALGORITHM PSEUDOCODE
  • 3.
    INTRODUCTION TO GRAPHTRAVERSAL www.edureka.co
  • 4.
    The process ofvisiting and exploring a graph for processing is called graph traversal. To be more specific it is all about visiting and exploring each vertex and edge in a graph such that all the vertices are explored exactly once. Introduction To Graph Traversal www.edureka.co
  • 5.
    WHAT IS THEBREADTH-FIRST SEARCH? www.edureka.co
  • 6.
    Breadth-First Search algorithmis a graph traversing technique, where you select a random initial node (source or root node) and start traversing the graph layer-wise in such a way that all the nodes and their respective children nodes are visited and explored. What is the Breadth-First Search Algorithm? www.edureka.co a b d c e a b d c e ExploringVisiting
  • 7.
  • 8.
    Breadth-First Search algorithmfollows a simple, level-based approach to solve a problem. Consider the above binary tree (which is a graph). Our aim is to traverse the graph by using the Breadth-First Search Algorithm. Understanding the Breadth-First Search Algorithm with an example www.edureka.co a b c d e f g What is a Queue? Binary Tree
  • 9.
    A queue isan abstract data structure that follows the First-In-First-Out methodology (data inserted first will be accessed first). It is open on both ends, where one end is always used to insert data and the other is used to remove data. What is a Queue? www.edureka.co Enqueue operation Dequeue operation Enqueue operation: To insert data Dequeue operation: To remove data
  • 10.
    www.edureka.co a b c d ef g Step 1: Take an Empty Queue. Queue Binary Tree
  • 11.
    www.edureka.co a b c d ef g Step 2: Select a starting node (visiting a node) and insert it into the Queue. Queue Binary Tree a
  • 12.
    www.edureka.co a b c d ef g Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its child nodes (exploring a node) into the Queue. Queue Binary Tree a
  • 13.
    www.edureka.co a b c d ef g Step 3: Provided that the Queue is not empty, extract the node from the Queue and insert its child nodes (exploring a node) into the Queue. Queue Binary Tree bc a
  • 14.
    www.edureka.co a b c d ef g Step 4: Print the extracted node. Queue Binary Tree bc Print: a a
  • 15.
  • 16.
    www.edureka.co • ‘a’ isthe root node: insert it into the Queue • Extract node ‘a’ and insert the child nodes of ‘a’, i.e., ‘b’ and ‘c • Print node ‘a’ • The queue is not empty and has node ‘b’ and ‘c’. Extract ‘b’ and insert the child nodes of ‘b’, i.e., node ‘d’ and ‘e’ • Repeat the steps until the queue gets empty
  • 17.
  • 18.
    www.edureka.co (G, s) isinput, here G is the graph and s is the root node A queue ‘Q’ is created and initialized with the source node ‘s’ All child nodes of ‘s’ are marked Extract ‘s’ from queue and visit the child nodes Process all the child nodes of v Stores w (child nodes) in Q to further visit its child nodes
  • 19.
    APPLICATIONS OF BREADTH-FIRSTSEARCH ALGORITHM www.edureka.co
  • 20.
    Breadth-first Search isa simple graph traversal method that has a surprising range of applications. Here are a few interesting ways in which Bread-First Search is being used: Applications Of Breadth-First Search Algorithm www.edureka.co Breadth First Search Applications Web crawling GPS Navigation Finding the shortest path Broadcasting
  • 21.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved. www.edureka.co
  • 22.