0% found this document useful (0 votes)
15 views49 pages

Ar23 Rec Ds Unit-V

The document provides an overview of graph theory, including definitions, types of graphs, and their representations. It covers concepts such as directed and undirected graphs, cycles, trees, and graph traversals (BFS and DFS), along with practical applications in various fields like social media analysis and network monitoring. Additionally, it explains how to represent graphs using adjacency matrices and lists.

Uploaded by

yashthegamerboy
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)
15 views49 pages

Ar23 Rec Ds Unit-V

The document provides an overview of graph theory, including definitions, types of graphs, and their representations. It covers concepts such as directed and undirected graphs, cycles, trees, and graph traversals (BFS and DFS), along with practical applications in various fields like social media analysis and network monitoring. Additionally, it explains how to represent graphs using adjacency matrices and lists.

Uploaded by

yashthegamerboy
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/ 49

RAGHU

ENGINEERING COLLEGE
(AUTONOMOUS | VISAKHAPATNAM)

AR23 I B.Tech II Semester


DATA STRUCTURES
UNIT-V
Data Structures Raghu Engineering College (A) 1
UNIT V

Syllabus:

Graph: Introduction, terminology, representation, types of graphs,


applications of graph
What is a Graph?
DEFINITION of a Graph:
A graph is defined as set G = (V ,E) Where V is a nonempty set of
vertices (or nodes) and E is a set of edges. Each edge has either one or
two vertices associated with it, called its endpoints. An edge is said to
connect its endpoints.
Remark:
• The set of vertices V of a graph G may be infinite. A graph with an
infinite vertex set or an infinite number of edges is called an infinite
graph.
• A graph with an finite vertex set and a finite edge set is called a finite
graph.
Types of Graphs:
The edges may be directed or undirected
1) Directed graph(Digraph):
In formal terms, a directed graph is an ordered pair G = (V, E)
where
V is a set whose elements are called vertices, nodes, or points.
E is a set of ordered pairs of vertices, called directed edges(that
have arrows associated with it) or directed arcs, or directed lines.

Directed edges

Directed Graph (Digraph)


2) Undirected graph:
In formal terms, an undirected graph is an unordered pair G = (V, E).
where
V is a set of vertices, nodes, or points.
E is a set of unordered pairs of vertices, called undirected edges

Undirected edges
3) Regular graph:
A regular graph is a graph in which every vertex has the same degree.
Example:

2-regular graph 3-regular graph


4) Complete graph:
• A complete graph on n vertices, denoted by Kn, is a simple graph
that contains exactly one edge between each pair of distinct vertices.
• A complete graph Kn is a regular graph of degree n-1.
Example:
Q) How many edges are present in a complete
graph Kn
Ans:
n
• C2(n choose 2)
•i.e. n*(n-1)/2
5) Connected graph:
An undirected graph is connected when it has at least one vertex and
there is a path between every pair of vertices.
A graph that is not connected is disconnected graph.
6) Weighted Graph:
• A weighted graph is defined as a special type of graph in which the
edges are assigned some weights which represent cost, distance, and
many other relative measuring units.
• For ex,
Graph Terminology:
Vertices: A Vertex, often referred to as a Node, is a fundamental unit of
a graph. It represents an entity within the graph. Every node/vertex can
be labeled or unlabelled.
Edges: An Edge is a connection between two vertices in a graph. It can
be either directed or undirected. In a directed graph, edges have a
specific direction, indicating a one-way connection between vertices. In
contrast, undirected graphs have edges that do not have a direction
and represent bidirectional connections.
Adjacent vertices: Two vertices are called adjacent if there is an edge between
them.
In the following graph G, (A,C), (A, B) and (B, C) are adjacent vertices
Order of a graph O(G): Number of vertices in a graph.
In the following graph G, order(G) = 3
Size of a graph S(G): Number of edges in a graph.
In the following size of graph G, (G) = 4
Loop: In graph theory, a loop is an edge that connects a vertex to itself.
Loop

A and C are
adjacent vertices
Graph G
Degree of a Vertex: The Degree of a Vertex in a graph is the number of
edges incident to that vertex. In a directed graph, the degree is further
categorized into the in-degree (number of incoming edges) and
out-degree (number of outgoing edges) of the vertex.

Directed Graph
Undirected Graph
Indegree(V1) = 1 Outdegree(V1) = 1
Degree(V1) = 2 Indegree(V2) = 2 Outdegree(V2) = 2
Degree(V2) = 2 Indegree(V3) = 1 Outdegree(V3) = 1
Degree(V3) = 3 Indegree(V4) = 2 Outdegree(V4) = 1
Degree(V4) = 1 Indegree(V5) = 1 Outdegree(V5) = 2

Degree(V1) = Indegree(V1) + Outdegree(V1)


Walk:
A walk is a sequence of vertices and edges of a graph i.e. if we
traverse a graph then we get a walk.
Note: Edge and Vertices both can be repeated.
Walk can be open or closed. Walk can be repeated anything (edges or
vertices).
From the following example, 1->2->3->4->2->1->3 is a walk.
Trail:
A Walk in which no edge is repeated then we
get a trail.
▪ Vertex can be repeated
▪ Edges not repeated
Here 1->3->8->6->3->2 is trail
Also 1->3->8->6->3->2->1 will be a closed
trail/circuit.
Path:
It is a trail in which neither vertices nor edges are repeated i.e. if we
traverse a graph such that we do not repeat a vertex and nor we
repeat an edge.
▪ Vertex not repeated
▪ Edge not repeated
Here 6->8->3->1->2->4 is a Path
Cycle:
Traversing a graph such that we do not repeat a vertex nor we repeat
a edge but the starting and ending vertex must be same i.e. we can
repeat starting and ending vertex only then we get a cycle.
▪ Vertex not repeated
▪ Edge not repeated
Cycle is a closed path.
Here 1->2->4->3->1 is a cycle.
Tree:
• A tree is a connected undirected graph with no simple circuits or
cycle. So a tree is an undirected acyclic graph
• A tree cannot contain multiple edges or loops. Therefore any tree
must be a simple graph.
Which of the following graphs are trees ?
Spanning Tree:
A spanning tree is a subset of Graph
G, such that all the vertices are
connected using minimum possible
number of edges. Hence, a spanning
tree does not have cycles and a graph
may have more than one spanning
tree.
Minimum Spanning Tree (MST):
A minimum spanning tree (MST) is defined as a spanning tree that has
the minimum weight among all the possible spanning trees.
Representation of Graphs:
Here are the two most common ways to represent a graph :
• Adjacency Matrix
• Adjacency List
a) Adjacency Matrix:
• An adjacency matrix is a way of representing a graph as a matrix of
Boolean (0’s and 1’s).
• Let’s assume there are n vertices in the graph So, create a 2D matrix
adjMat[n][n] having dimension n x n.
If there is an edge from vertex i to j, mark adjMat[i][j] as 1.
If there is no edge from vertex i to j, mark adjMat[i][j] as 0.
Representation of Undirected Graph to Adjacency
Matrix:
• The below figure shows an undirected graph. Initially, the entire
Matrix is initialized to 0. If there is an edge from source to destination,
we insert 1 to both cases (adjMat[destination] and
adjMat[destination]) because we can go either way.
Ex:
Representation of Directed Graph to Adjacency
Matrix:
• The below figure shows a directed graph. Initially, the entire Matrix
is initialized to 0. If there is an edge from source to destination, we
insert 1 for that particular adjMat[destination].
Ex:
Create the adjacency matrix for the given graph.
b) Adjacency List:
• An array of Lists is used to store edges between two vertices. The size of
array is equal to the number of vertices (i.e, n). Each index in this array
represents a specific vertex in the graph. The entry at the index i of the
array contains a linked list containing the vertices that are adjacent to
vertex i.
• Let’s assume there are n vertices in the graph So, create an array of list of
size n as adjList[n].
adjList[0] will have all the nodes which are connected (neighbor) to
vertex 0.
adjList[1] will have all the nodes which are connected (neighbor) to
vertex 1 and so on.
Representation of Undirected Graph to Adjacency
list:
• The below undirected graph has 3 vertices. So, an array of list will be
created of size 3, where each indices represent the vertices. Now,
vertex 0 has two neighbours (i.e, 1 and 2). So, insert vertex 1 and 2 at
indices 0 of array. Similarly, For vertex 1, it has two neighbour (i.e, 2
and 0) So, insert vertices 2 and 0 at indices 1 of array. Similarly, for
vertex 2, insert its neighbours in array of list.
Ex:
Representation of Directed Graph to Adjacency
list:
• The below directed graph has 3 vertices. So, an array of list will be
created of size 3, where each indices represent the vertices. Now,
vertex 0 has no neighbours. For vertex 1, it has two neighbour (i.e, 0
and 2) So, insert vertices 0 and 2 at indices 1 of array. Similarly, for
vertex 2, insert its neighbours in array of list.
Ex:
Create the adjacency list for the given graph.
Graph Traversals:
• The graph has two types of traversal algorithms.
Breadth First Search
Depth First Search.
a) Breadth First Search (BFS):
The Breadth First Search (BFS) traversal is an algorithm, which is used
to visit all of the nodes of a given graph. In this traversal algorithm one
node is selected and then all of the adjacent nodes are visited one by
one. After completing all of the adjacent vertices, it moves further to
check another vertices and checks its adjacent vertices again.
BFS Algorithm:
Step 1: Initialization: Enqueue the starting node into a queue and mark
it as visited.
Step 2: Exploration: While the queue is not empty:
• Dequeue a node from the queue and visit it (e.g., print its
value).
• For each unvisited neighbor of the dequeued node:
Enqueue the neighbor into the queue.
Mark the neighbor as visited.
Step 3: Termination: Repeat step 2 until the queue is empty.
• Let us understand the working of the algorithm with the help of the
following graph.

Step1: Initially queue and visited arrays are empty.


Step2: Enqueue/Insert node 0 into queue and mark it visited.

Step 3: Dequeue/delete node 0 from the front of queue and visit the
unvisited neighbours and enqueue/insert them into queue.
Step 4: Dequeue/delete node 1 from the front of queue and visit the
unvisited neighbours and enqueue/insert them into queue.

Step 5: Dequeue/delete node 2 from the front of queue and visit the
unvisited neighbors and enqueue/insert them into queue.
Step 6: Dequeue/delete node 3 from the front of queue and visit the
unvisited neighbours and enqueue/insert them into queue.
As we can see that every neighbours of node 3 is visited, so move to
the next node that are in the front of the queue.
Step 7: Dequeue/delete node 4 from the front of queue and visit the
unvisited neighbours and enqueue/insert them into queue.
As we can see that every neighbours of node 4 are visited, so move to
the next node that is in the front of the queue.

Now, Queue becomes empty, So, terminate these process of iteration.


Ex:
Explain the step-by-step procedure to traverse the graph shown
in Figure 1 using the BFS algorithm.
b) Depth First Search (DFS):
• The Depth First Search (DFS) is a graph traversal algorithm. In this
algorithm one starting vertex is given, and when an adjacent vertex is
found, it moves to that adjacent vertex first and try to traverse in the
same manner.
DFS Algorithm:
Step 1: Initialization: Push the starting node into a stack.
Step 2: Exploration: While the stack is not empty:
• Pop a node from the stack and visit it (e.g., print its value).
• For each unvisited neighbor of the popped node:
Push the neighbor into the stack.
Step 3: Termination: Repeat step 2 until the stack is empty.
• Let us understand the working of Depth First Search with the help of
the following graph:

Step1: Initially stack and visited arrays are empty.


Step 2: Visit 0 and put its adjacent nodes which are not visited yet into
the stack.

Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it
from the stack and put all of its adjacent nodes which are not visited in
the stack.
Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it
from the stack and put all of its adjacent nodes which are not visited
(i.e, 3, 4) in the stack.

Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it
from the stack and put all of its adjacent nodes which are not visited in
the stack.
Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it
from the stack and put all of its adjacent nodes which are not visited in
the stack.

Now, Stack becomes empty, which means we have visited all the nodes
and our DFS traversal ends.
Ex:
Explain the step-by-step procedure to traverse the graph shown
in Figure 1 using the DFS algorithm.
Applications of Graphs:
• Social media analysis: Social media platforms generate vast amounts of
data in real-time, which can be analyzed using graphs to identify trends,
sentiment, and key influencers. This can be useful for marketing, customer
service, and reputation management.
• Network monitoring: Graphs can be used to monitor network traffic in
real-time, allowing network administrators to identify potential
bottlenecks, security threats, and other issues. This is critical for ensuring
the smooth operation of complex networks.
• Financial trading: Graphs can be used to analyze real-time financial data,
such as stock prices and market trends, to identify patterns and make
trading decisions. This is particularly important for high-frequency trading,
where even small delays can have a significant impact on profits.
• Internet of Things (IoT) management: IoT devices generate vast amounts
of data in real-time, which can be analyzed using graphs to identify
patterns, optimize performance, and detect anomalies. This is important
for managing large-scale IoT deployments.
• Autonomous vehicles: Graphs can be used to model the real-time
environment around autonomous vehicles, allowing them to navigate
safely and efficiently. This requires real-time data from sensors and other
sources, which can be processed using graph algorithms.
• Disease surveillance: Graphs can be used to model the spread of infectious
diseases in real-time, allowing health officials to identify outbreaks and
implement effective containment strategies. This is particularly important
during pandemics or other public health emergencies.
The best example of graphs in the real world is Facebook. Each person on
Facebook is a node and is connected through edges. Thus, A is a friend of B. B
is a friend of C, and so on.

You might also like