Graph 2
Graph 2
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.
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.
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
For queries
Email: [email protected]
22