0% found this document useful (0 votes)
77 views34 pages

Discrete Structure Presentation

Breadth First Search (BFS) and Depth First Search (DFS) are algorithms for traversing or searching tree and graph data structures. BFS uses a queue to explore all neighbor nodes at the present depth before moving to the next depth, while DFS uses a stack to explore nodes as deep as possible before backtracking. The document provides examples and explanations of how BFS and DFS work, including pseudocode rules. It discusses applications of both algorithms such as shortest paths, minimum spanning trees, peer-to-peer networks, and more. Time complexity of BFS is O(V+E) and space is O(V), while DFS can be O(V+E) or O(V^2)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views34 pages

Discrete Structure Presentation

Breadth First Search (BFS) and Depth First Search (DFS) are algorithms for traversing or searching tree and graph data structures. BFS uses a queue to explore all neighbor nodes at the present depth before moving to the next depth, while DFS uses a stack to explore nodes as deep as possible before backtracking. The document provides examples and explanations of how BFS and DFS work, including pseudocode rules. It discusses applications of both algorithms such as shortest paths, minimum spanning trees, peer-to-peer networks, and more. Time complexity of BFS is O(V+E) and space is O(V), while DFS can be O(V+E) or O(V^2)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Breadth First Search &

Depth First Search


Discrete Structure
Outline:
▪ What is BFS and DFS?
▪ Graphs.
▪ Necessary Terms.
▪ BFS and DFS Illustration with Example.
▪ Applications.
▪ Time and Space Complexity.
Breadth First Search.
Part 1
Definition.
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 neighbour nodes at the present depth prior to
moving on to the nodes at the next depth level.
A queue (FIFO-First in First Out) data structure is used
by BFS.
Breadth means the distance or measurement from side to
side of something; width; wideness; diameter; range;
extent.
A Graph G=(V, E) consists a A tree is a collection of Tree is also a Graph.
set of vertices, V, and a set entities called nodes.
of edges, E. Nodes are connected by
edges. Each node contains
a value or data, and it may
or may not have a child
node
We should know
▪ 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
▪ Level-Order: It is a traversing method, where we
have to visit every node on a level before going
to a lower level.
BFS & Level Order
Example
▪ Level-Order: 1 234 5678 9101112

▪ BFS: 1 234 5678 9101112


Rules.
▪ 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.
▪ Fig 1 ▪ Fig 2
▪ Fig 3 ▪ Fig 4
▪ Fig 5 ▪ Fig 6
▪ Fig 7 ▪ Fig 8
▪ Fig 9 ▪ Fig 10
▪ Fig 1 ▪ Fig 2
▪ Fig 3 ▪ Fig 4
Breadth First
Search –
Example.
Fig 5
Applications of BFS.
▪ There are many applications Breadth First Search. Let’s discuss
some of them.
▪ 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.
Applications.
▪ Peer to Peer Networks: In Peer to Peer Networks like BitTorrent,
Breadth First Search is used to find all neighbour nodes.
▪ Ford–Fulkerson method for computing the maximum flow in a flow
network.
▪ Path Finding: We can either use Breadth First or Depth First
Traversal to find if there is a path between two vertices
▪ Broadcasting in Network: In networks, a broadcasted packet
follows Breadth First Search to reach all nodes.
Applications.
▪ GPS Navigation systems: Breadth First Search is used to find all
neighbouring 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.
Time and Space Complexity

▪ 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
Depth First Search
Part 2
What is depth first search?
▪ Depth-first search is another strategy for exploring a graph
▪ Explore “deeper” in the graph whenever possible
▪ Edges are explored out of the most recently discovered vertex v that
still has unexplored edges
▪ When all of v’s edges have been explored, backtrack to the vertex
from which v was discovered.
▪ Depth-first: visit all neighbours of a neighbour before visiting other
neighbours
▪ A stack (LIFO-Last in First Out) data structure is used by DFS.
Rule.
▪ Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Push it in a
stack.
▪ Rule 2 − If no adjacent vertex is found, pop
up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not
have adjacent vertices.)
▪ Rule 3 − Repeat Rule 1 and Rule 2 until the
stack is empty.
Depth First
Search –
Example:
Solution:
▪ Initialize the stack.
Solution:
▪ Mark S as visited and put it onto the stack.
Explore any unvisited adjacent node from S. We
have three nodes and we can pick any of them.
For this example, we shall take the node in an
alphabetical order.
Solution:
▪ Mark A as visited and put it onto the stack.
Explore any unvisited adjacent node from A.
Both S and Dare adjacent to A but we are
concerned for unvisited nodes only.
Solution:
▪ Visit D and mark it as visited and put onto the
stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However,
we shall again choose in an alphabetical order.
Solution:
▪ We choose B, mark it as visited and put onto the
stack. Here B does not have any unvisited
adjacent node. So, we pop B from the stack.
Solution:
▪ We check the stack top for return to the previous
node and check if it has any unvisited nodes.
Here, we find D to be on the top of the stack.
Final Answer:
▪ Only unvisited adjacent node is from D is C now.
So we visit C, mark it as visited and put it onto the
stack.
Applications.
▪ Finding connected components.
▪ Topological sorting.
▪ Finding the bridges of a graph.
▪ Cycle Detecting
Finding strongly connected components.
▪ Finding bi-connectivity in graphs.
Complexity
▪ For a Graph G=(V, E) and n = |V| and m=|E|
▪ When Adjacency List is used Complexity is O(m + n)
▪ When Adjacency Matrix is used Scanning each row for checking the
connectivity of a Vertex is in order O(n). So, Complexity is O(n2 )
DFS uses space O(|V|) in the worst case to store the stack of vertices
on the current search path as well as the set of already-visited
vertices.
Thank you for your time :)

Created & presented by: Faseeh.

You might also like