0% found this document useful (0 votes)
7 views

Graph traversal presentation of DSA

The document discusses graph traversal techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as far as possible along each branch using a stack. It also outlines the applications and differences between BFS and DFS, highlighting their respective strengths and use cases.

Uploaded by

areebahfiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Graph traversal presentation of DSA

The document discusses graph traversal techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as far as possible along each branch using a stack. It also outlines the applications and differences between BFS and DFS, highlighting their respective strengths and use cases.

Uploaded by

areebahfiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Graph Traversal

BFS AND DFS


CONTENT
 Breath-First Search (BFS)
 Application OF Breath-First
Search
 Depth-First Search (DFS)
 Application OF Depth-First
Search
 Difference between BFS & DFS
Introduction of Graph traversal

 Graph traversal:-is the process of visiting all the nodes (vertices) in a graph in a
systematic way. It is essential for exploring graphs and solving various problems
like searching, pathfinding, and connectivity.
There are two main types of graph traversal:
 Depth-First Search (DFS): Explores as far as possible along each branch before
backtracking. It uses a stack or recursion.
 Breadth-First Search (BFS): Explores all neighbors at the current level before
moving to the next level. It uses a queue.
Breath First Search

Definition of BFS
Breadth-First Search (BFS)
is a graph traversal algorithm used to explore all the nodes (vertices) in a graph level by level.
It starts at a given node (source node), explores all neighboring nodes at the present depth
level before moving on to nodes at the next depth level.
Working Principle
 BFS uses a queue data structure for storing nodes to be explored.
 It follows the FIFO (First-In-First-Out) principle, ensuring nodes are processed level by
level.
Pseudo Code (BFS)

from collections import deque


def. bfs(graph, start):
visited = set() # Keep track of visited nodes
queue = deque([start]) # Start from the given node while queue:
node = queue.popleft() # Take the first node from the queue if node not in visited:
print(node, end=" ") # Process the node (print it)
visited.add(node) # Mark the node as visited
# Add all the unvisited neighbors of the node to the queue for neighbor in graph[node]:
if neighbor not in visited:
queue. Append(neighbor)
Applications of Breath First Search

 Breadth-First Search (BFS) is a fundamental graph traversal algorithm with a variety of


applications across different domains. Here are some of its key applications:
 1. Shortest Path in an Unweighted Graph
 2. Peer-to-Peer Networks
 3. Social Network Analysis
 4. Web Crawlers
 5. Network Broadcasting
 6. Cycle Detection in Undirected Graphs
Shortest Path in an Unweighted Graph

•Application: BFS is used to find the shortest path between two


nodes in an unweighted graph or maze.
•Example: Finding the minimum number of moves to solve a
puzzle like a maze or a board game.
Peer-to-Peer Networks

•Application: BFS helps find all neighboring nodes or peers in


a distributed network.
•Example: File sharing networks like BitTorrent use BFS to
search for data among peers.
Social Network Analysis

•Application: BFS is used to measure degrees of separation (e.g., the


shortest path between two users) and recommend connections.
•Example: Suggesting friends on platforms like Facebook or
LinkedIn.
Web Crawlers

•Application: BFS is used to traverse web pages layer by layer


starting from a seed URL.
•Example: Indexing all reachable web pages for a search
engine.
Network Broadcasting

•Application: BFS can simulate broadcasting messages across a


network to ensure all nodes receive the information.
•Example: Distributing updates in a network or resolving
Distributed Denial of Service (DDoS) scenarios.
Cycle Detection in Undirected Graphs

Application: BFS can be adapted to detect


cycles by checking for visited neighbors
other than the parent node.
Depth First Search (DFS)

Definition Depth-First Search (DFS):


 Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along
each branch before backtracking. It starts at a selected node (source node) and explores each
branch of the graph deeply before moving to the next branch. DFS can be implemented using
either recursion or an explicit stack.
 Key Characteristics of DFS:
Explores deeper: DFS goes deep into a graph before backtracking.
Uses Stack: DFS uses a stack (can be recursive or explicit) to remember which nodes to visit next.
Not level-order: It doesn't visit nodes level by level, unlike BFS.
Can handle disconnected graphs: DFS can be used to explore all nodes in disconnected graphs by
initiating a DFS from multiple unvisited nodes.
Working of DFS
Pseudo Code (DFS)

Recursive DFS Pseudocode:

DFS(graph, node, visited):


f node is not in visited:
add node to visited for each neighbor of node:
DFS(graph, neighbor, visited)
Iterative DFS Pseudocode (using stack):

DFS(graph, start):
create stack S create set visited push start onto S while S is not empty:
node = pop from S if node is not in visited:
add node to visited for each neighbor of node:
push neighbor onto S
Application OF Depth-First
Search
1. Detecting cycle in a graph: A graph has a cycle if and
only if we see a back edge during DFS. So we can run
DFS for the graph and check for back edges.
2. Path Finding: We can specialize the DFS algorithm to
find a path between two given vertices u and z.
3. Web crawlers: Depth-first search can be used in the
implementation of web crawlers to explore the links
on a website.
4. Maze generation: Depth-first search can be used to
generate random mazes.
Application OF Depth-First
Search

5. Model checking: Depth-first search can be used in


model checking, which is the process of checking that
a model of a system meets a certain set of
properties.
6. Backtracking: Depth-first search can be used in
backtracking algorithms.
Difference Between BFS & DFS

Breath-First Search Depth-First Search


1. BFS stands for Breath-First search. 1. DFS stands for Depth-First
2. BFS(Breath-First Search) uses Queue Search.
data structure for finding the
2. DFS(Depth-First Search) uses
shortest path.
stacks data structure.
3. BFS can be used to find single source
shortest path in an unweighted 3. In DFS, we might traverse
graph, because in BFS, we reach a through more edges to reach a
vertex with minimum number of destination vertex from a
edges form a source vertex. source.
Difference Between BFS & DFS

Breath-First Search Depth-first Search


4. BFS is more suitable for 4. DFS is more suitable when there
searching vertices which are are solutions away from source.
closer to the given source.
5. DFS is more suitable for game or
5. BFS considers all neighbors puzzle problems. We make decision,
first and therefore not suitable for then explore all paths through this
decision making trees used in decision. And if this decision leads
to win situation, we stop.
games and puzzle.
Difference Between BFS & DFS

Breath-First Search Depth-First Search


6. Generally requires more 6. Generally requires less
memory than DFS. memory than BFS.
7. Time complexity: O(V+E). 7. Time complexity : O(V+E).
8. Optimal for finding the 8. Not optimal for finding the
shortest distance. shortest distance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy