0% found this document useful (0 votes)
7 views18 pages

Graph Traversing

The document provides an overview of graph theory, defining a graph as a set of vertices and edges. It explains two standard graph traversal methods: Breadth-First Search (BFS) and Depth-First Search (DFS), detailing their algorithms and processes. Additionally, it describes the status of nodes during traversal, categorizing them into ready, waiting, and processed states.

Uploaded by

A1Virus
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 views18 pages

Graph Traversing

The document provides an overview of graph theory, defining a graph as a set of vertices and edges. It explains two standard graph traversal methods: Breadth-First Search (BFS) and Depth-First Search (DFS), detailing their algorithms and processes. Additionally, it describes the status of nodes during traversal, categorizing them into ready, waiting, and processed states.

Uploaded by

A1Virus
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/ 18

Graph Traversing

2
Graph
 A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.
 Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set
of edges, connecting the pairs of vertices. Take a look at the following graph − Graph
Basics.

 V = {a, b, c, d, e}
 E = {ab, ac, bd, cd, de}

03/24/2025
3
Types of Traversing

 There are two standard way for travelling a graph.


1. BFS (Breadth-First-Search)
a. BFS use a Queue for future processing
b. This follows FIFO(First In First Out)

2. DFS(Depth-First-Search)
a. DFS use a Stack for future processing
b. This follows LIFO (Last In First Out)

03/24/2025
4
Define Status
 During the execution of any algorithm, each node N of G will be in one of the three
states. called the status of N as follows:

 STATUS 1: (Ready State) the initial state of the node N.


 STATUS 2: (Waiting State) the node is on the queue or stack, waiting to be processed .
 STATUS 3: (Processed State) the node N has been processed.

03/24/2025
5
BFS
 Algorithm
Step 1: Initialize all nodes to the ready state (STATUS = 1)
Step 2: Put the starting node A in Queue and change its status to the waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty:
Step 4: Remove the front node N of Queue. Process N and change the status of N
to the processed state (STATUS = 3)
Step 5: Add to the rear of QUEUE all the neighbors of N that are in the steady state
(STATUS= 1), and change their status to the waiting state (STATUS = 2).
[End of Step 3 loop]
Step 6: Exit.

03/24/2025
6
BFS Process
 Step 1: Define a Queue of size total number of vertices in the graph.
 Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert
it into the Queue.
 Step 3: Visit all the adjacent vertices of the vertex which is at front of the Queue
which is not visited and insert them into the Queue.
 Step 4: When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
 Step 5: Repeat step 3 and 4 until queue becomes empty.
 Step 6: When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph

03/24/2025
BFS Example
BFS Example
BFS Example
BFS Example
11
DFS
 Algorithm
Step 1: Initialize all nodes to the ready state (STATUS = 1)
Step 2: Put the starting node A onto STACK and change its status to the waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until STACK is empty:
Step 4: POP the top node N of STACK. Process N and change its status to the processed
state(STATUS=3)
Step 5: Push onto STACK all neighbors of N that are still in the ready state(STATUS=1),
and change their Status to the waiting state(STATUS=2).
[End of Step 3 loop]
Step 6: Exit.

03/24/2025
12
DFS Process
 Step 1: Define a Stack of size total number of vertices in the graph.
 Step 2: Select any vertex as starting point for traversal. Visit that vertex and push
it on to the Stack.
 Step 3: Visit any one of the adjacent vertex of the vertex which is at top of the stack
which is not visited and push it on to the stack.
 Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top
of the stack.
 Step 5: When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
 Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7: When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

03/24/2025
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example

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