0% found this document useful (0 votes)
31 views22 pages

Graph 2

1) The document discusses operations on graphs such as adding/removing vertices and edges. It also discusses traversing graphs using breadth-first search (BFS) and depth-first search (DFS) algorithms. 2) BFS uses a queue to visit all neighbor nodes at the present level before moving to the next level. DFS uses a stack to first fully explore all branches of the graph from the root node before moving to the next node. 3) Graph traversal algorithms have applications in search engines, GPS navigation, minimum spanning trees, and detecting cycles in graphs.

Uploaded by

manudev8924
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)
31 views22 pages

Graph 2

1) The document discusses operations on graphs such as adding/removing vertices and edges. It also discusses traversing graphs using breadth-first search (BFS) and depth-first search (DFS) algorithms. 2) BFS uses a queue to visit all neighbor nodes at the present level before moving to the next level. DFS uses a stack to first fully explore all branches of the graph from the root node before moving to the next node. 3) Graph traversal algorithms have applications in search engines, GPS navigation, minimum spanning trees, and detecting cycles in graphs.

Uploaded by

manudev8924
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/ 22

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: 21CSH-241
Chapter: Graph
Subject Coordinator:
Mr. Nirmalya Basu
(E13248)

Graph
Lecture No.-3.1
DISCOVER . LEARN . EMPOWER
Index
• Operations on graph
• Traversing a graph

2 2
Operations on graph
Here are the Basic Operations of Graph mention below
1. Add/Remove Vertex
This is the simplest operation in the graph. You simply add
a vertex to a graph. It may or may not be connected to any
other vertex through an edge. We remove a vertex from
the graph. But, before removing the vertex, we also
remove all the edges associated with that vertex. It means,
while removing a vertex, we remove any edge that is
outgoing or ingoing to that vertex.
2. Add/Remove Edge
This operation adds or removes an edge between two
vertices. When all the edges originating from and ending
at a vertex are deleted, the vertex becomes isolated.

3 3
Graph traversal
The graph has two types of traversal algorithms.
1. Breadth First Search (BFS) and
2. Depth First Search (DFS).
BFS algorithm:-
A standard BFS implementation puts each vertex of the graph into one of two categories:

1. Visited

2. Not Visited

The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The algorithm works as follows:

1. Start by putting any one of the graph's vertices at the back of a queue.

2. Take the front item of the queue and add it to the visited list.

3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.

4. Keep repeating steps 2 and 3 until the queue is empty.


BFS example
Let's see how the Breadth First Search algorithm
works with an example. We use an undirected graph
with 5 vertices.
Continue..
We start from vertex 0, the BFS algorithm starts by
putting it in the Visited list and putting all its
adjacent vertices in the queue.
Continue..
Next, we visit the element at the front of queue i.e. 1
and go to its adjacent nodes. Since 0 has already
been visited, we visit 2 instead.
Continue..
Vertex 2 has an unvisited adjacent vertex in 4, so
we add that to the back of the queue and visit 3,
which is at the front of the queue.
Continue..
Only 4 remains in the queue since the only adjacent
node of 3 i.e. 0 is already visited. We visit it.
Continue..

Since the queue is empty, we have completed the Breadth First


Traversal of the graph.
BFS Algorithm Applications
• To build index by search index
• For GPS navigation
• Path finding algorithms
• In Ford-Fulkerson algorithm to find maximum flow
in a network
• Cycle detection in an undirected graph
• In minimum spanning tree
Depth First Search (DFS).
A standard DFS implementation puts each vertex of the graph into
one of two categories:
• Visited
• Not Visited
The purpose of the algorithm is to mark each vertex as visited while
avoiding cycles.
The DFS algorithm works as follows:
1. Start by putting any one of the graph's vertices on top of a
stack.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones
which aren't in the visited list to the top of the stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
DFS example

We start from vertex 0, the DFS algorithm starts by putting it in


the Visited list and putting all its adjacent vertices in the stack.
DFS example

Next, we visit the element at the top of stack i.e. 1 and go to


its adjacent nodes. Since 0 has already been visited, we visit
2 instead.
DFS example

Vertex 2 has an unvisited adjacent vertex in 4, so


we add that to the top of the stack and visit it.
DFS example
DFS example
DFS example

After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so
we have completed the Depth First Traversal of the graph.
Application of DFS Algorithm

1.For finding the path


2.To test if the graph is bipartite
3.For finding the strongly connected components of
a graph
4.For detecting cycles in a graph
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-
tutorial
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=xlVX7dXLS64
THANK YOU

For queries
Email: [email protected]

22

You might also like