vision_2024_algorithm_chapter_6_graph_algorithms_32
vision_2024_algorithm_chapter_6_graph_algorithms_32
com
1
byjusexamprep.com
ALGORITHM
6 GRAPH ALGORITHMS
1. GRAPHS
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 −
2
byjusexamprep.com
● Path − Path represents a sequence of edges between the two vertices. In the following
example, ABCD represents a path from A to D.
Basic Operations
Following are basic primary operations of a Graph −
● Add Vertex − Adds a vertex to the graph.
● Add Edge − Adds an edge between the two vertices of the graph.
● Display Vertex − Displays a vertex of the graph.
2. Types of Graphs
In the above graph, there are three vertices named ‘a’, ‘b’, and ‘c’, but there are no
edges among them. Hence it is a Null Graph.
2.2 Trivial Graph
A graph with only one vertex is called a Trivial Graph.
Example
In the above shown graph, there is only one vertex ‘a’ with no other edges. Hence it is
a Trivial graph.
3
byjusexamprep.com
2.3 Non-Directed Graph
A non-directed graph contains edges but the edges are not directed ones.
Example
In this graph, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ are the vertices, and ‘ab’, ‘bc’, ‘cd’, ‘da’, ‘ag’, ‘gf’,
‘ef’ are the edges of the graph. Since it is a non-directed graph, the edges ‘ab’ and ‘ba’
are the same. Similarly other edges are also considered in the same way.
2.4 Directed Graph
In a directed graph, each edge has a direction.
Example
In the above graph, we have seven vertices ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, and ‘g’, and eight
edges ‘ab’, ‘cb’, ‘dc’, ‘ad’, ‘ec’, ‘fe’, ‘gf’, and ‘ga’. As it is a directed graph, each edge
bears an arrow mark that shows its direction. Note that in a directed graph, ‘ab’ is
different from ‘ba’.
4
byjusexamprep.com
2.5 Simple Graph
A graph with no loops and no parallel edges is called a simple graph.
● The maximum number of edges possible in a single graph with ‘n’ vertices
is nC2 where nC2 = n(n – 1)/2.
● The number of simple graphs possible with ‘n’ vertices = 2 nc2 = 2n(n-1)/2.
Example
In the following graph, there are 3 vertices with 3 edges which is maximum excluding
the parallel edges and loops. This can be proved by using the above formulae.
= 3(3–1)/2
= 6/2
= 3 edges
The maximum number of simple graphs with n=3 vertices −
2nC2 = 2n(n-1)/2
= 23(3-1)/2
= 23
=8
These 8 graphs are as shown below −
5
byjusexamprep.com
2.6 Connected Graph
A graph G is said to be connected if there exists a path between every pair of
vertices. There should be at least one edge for every vertex in the graph. So that we
can say that it is connected to some other vertex at the other side of the edge.
Example
In the following graph, each vertex has its own edge connected to another edge. Hence
it is a connected graph.
The two components are independent and not connected to each other. Hence it is
called disconnected graph.
Example 2
6
byjusexamprep.com
In this example, there are two independent components, a-b-f-e and c-d, which are not
connected to each other. Hence this is a disconnected graph.
2.8 Regular Graph
A graph G is said to be regular, if all its vertices have the same degree. In a graph,
if the degree of each vertex is ‘k’, then the graph is called a ‘k-regular graph’.
Example
In the following graphs, all the vertices have the same degree. So these graphs are called
regular graphs.
In both the graphs, all the vertices have degree 2. They are called 2-Regular Graphs.
Complete Graph
A simple graph with ‘n’ mutual vertices is called a complete graph and it is denoted by
‘Kn’. In the graph, a vertex should have edges with all other vertices, then it called
a complete graph.
In other words, if a vertex is connected to all other vertices in a graph, then it is called a
complete graph.
Example
In the following graphs, each vertex in the graph is connected with all the remaining
vertices in the graph except by itself.
7
byjusexamprep.com
2.9 Cycle Graph
A simple graph with ‘n’ vertices (n >= 3) and ‘n’ edges is called a cycle graph if all its
edges form a cycle of length ‘n’.
If the degree of each vertex in the graph is two, then it is called a Cycle Graph.
Notation − Cn
Example
Take a look at the following graphs −
● Graph I has 3 vertices with 3 edges which form a cycle ‘ab-bc-ca’.
● Graph II has 4 vertices with 4 edges which form a cycle ‘pq-qs-sr-rp’.
● Graph III has 5 vertices with 5 edges which form a cycle ‘ik-km-ml-lj-ji’.
3. Graph Traversal
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is
also used to decide the order of vertices visited in the search process. A graph traversal finds
the edges to be used in the search process without creating loops. That means using graph
traversal we visit all the vertices of the graph without getting into a looping path.
There are two graph traversal techniques and they are as follows.
1. DFS (Depth First Search)
2. BFS (Breadth First Search)
DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Stack data structure with maximum size of total number of vertices in
the graph to implement DFS traversal.
8
byjusexamprep.com
● Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it onto the stack.
● Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.
● Step 5 - When there is no new vertex to visit then use backtracking 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.
Depth First Search (DFS) algorithm traverses a graph in a depth ward motion and uses a
stack to remember to get the next vertex to start a search, when a dead end occurs in any
iteration.
9
byjusexamprep.com
10
byjusexamprep.com
As C does not have any unvisited adjacent node so we keep popping the stack until we find a
node that has an unvisited adjacent node. In this case, there's none and we keep popping
until the stack is empty.
Breadth first Search
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a
queue to remember to get the next vertex to start a search, when a dead end occurs in any
iteration.
11
byjusexamprep.com
As in the example given above, BFS algorithm traverses from A to B to E to F first then to C
and G lastly to D. It employs the following rules.
● Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in
a queue.
● 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.
12
byjusexamprep.com
13
byjusexamprep.com
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we
keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the
program is over.
BFT DFT
BFS stands for Breadth First Search. DFS stands for Depth First Search.
BFS(Breadth First Search) uses Queue data DFS(Depth First Search) uses Stack data
structure for finding the shortest path. structure.
BFS is more suitable for searching vertices DFS is more suitable when there are solutions
which are closer to the given source. away from source.
The Time complexity of BFS is O(V + E) when The Time complexity of DFS is also O(V + E)
Adjacency List is used and O(V^2) when when Adjacency List is used and O(V^2) when
Adjacency Matrix is used, where V stands for Adjacency Matrix is used, where V stands for
vertices and E stands for edges. vertices and E stands for edges.
****
14