0% found this document useful (0 votes)
9 views38 pages

Unit 5 Graphs Ds

The document provides a comprehensive overview of graphs, defining them as non-linear data structures made up of nodes and edges, and categorizing various types of graphs such as undirected, directed, weighted, and bipartite graphs. It also discusses properties of graphs, advantages and disadvantages of using graphs, and methods of representing graphs, including adjacency matrices and adjacency lists. Additionally, it highlights the applications of graphs in modeling complex systems and analyzing relationships.

Uploaded by

Seema Seema
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)
9 views38 pages

Unit 5 Graphs Ds

The document provides a comprehensive overview of graphs, defining them as non-linear data structures made up of nodes and edges, and categorizing various types of graphs such as undirected, directed, weighted, and bipartite graphs. It also discusses properties of graphs, advantages and disadvantages of using graphs, and methods of representing graphs, including adjacency matrices and adjacency lists. Additionally, it highlights the applications of graphs in modeling complex systems and analyzing relationships.

Uploaded by

Seema Seema
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/ 38

Graphs & Types of Graphs with Examples


A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any
two nodes in the graph. More formally a Graph can be defined as, A Graph consisting
of a finite set of vertices(or nodes) and a set of edges that connect a pair of nodes
1. Undirected Graphs: A graph in which edges have no direction, i.e., the edges do
not have arrows indicating the direction of traversal. Example: A social network
graph where friendships are not directional.
2. Directed Graphs: A graph in which edges have a direction, i.e., the edges have
arrows indicating the direction of traversal. Example: A web page graph where
links between pages are directional.
3. Weighted Graphs: A graph in which edges have weights or costs associated with
them. Example: A road network graph where the weights can represent the
distance between two cities.
4. Unweighted Graphs: A graph in which edges have no weights or costs associated
with them. Example: A social network graph where the edges represent
friendships.
5. Complete Graphs: A graph in which each vertex is connected to every other
vertex. Example: A tournament graph where every player plays against every other
player.
6. Bipartite Graphs: A graph in which the vertices can be divided into two disjoint
sets such that every edge connects a vertex in one set to a vertex in the other set.
Example: A job applicant graph where the vertices can be divided into job
applicants and job openings.
7. Trees: A connected graph with no cycles. Example: A family tree where each
person is connected to their parents.
8. Cycles: A graph with at least one cycle. Example: A bike-sharing graph where the
cycles represent the routes that the bikes take.
9. Sparse Graphs: A graph with relatively few edges compared to the number of
vertices. Example: A chemical reaction graph where each vertex represents a
chemical compound and each edge represents a reaction between two compounds.
10. Dense Graphs: A graph with many edges compared to the number of vertices.
Example: A social network graph where each vertex represents a person and each
edge represents a friendship.
Types of Graphs:
1. Finite Graphs
A graph is said to be finite if it has a finite number of vertices and a finite number of
edges. A finite graph is a graph with a finite number of vertices and edges. In other
words, both the number of vertices and the number of edges in a finite graph are
limited and can be counted. Finite graphs are often used to model real-world
world
situations, where there is a limited number of objects and relationships between the

2. Infinite Graph:
A graph is said to be infinite if it has an infinite number of vertices as well as an
infinite number of edges.

3. Trivial Graph:
A graph is said to be trivial if a finite graph contains only one vertex and no edge. A
trivial graph is a graph with only one vertex and no edges. It is also known as a
singleton graph or a single vertex graph. A trivial graph is the simplest type of graph
and is often used as a starting point for building more complex graphs. In graph
theory, trivial graphs are considered to be a degenerate case and are not typically
studied in detail
4. Simple Graph:
A simple graph is a graph that does not contain more than one edge between the pair
of vertices. A simple railway track connecting different cities is an example of a
simple graph.

5. Multi Graph:
Any graph which contains some parallel edges but doesn’t contain any self-loop is
called a multigraph. For example a Road Map.
 Parallel Edges: If two vertices are connected with more than one edge then such
edges are called parallel edges that are many routes but one destination.
 Loop: An edge of a graph that starts from a vertex and ends at the same vertex is
called a loop or a self-loop.
6. Null Graph:
A graph of order n and size zero is a graph where there are only isolated vertices with
no edges connecting any pair of vertices.A null graph is a graph with no edges. In
other words, it is a graph with only vertices and no connections between them. A null
graph can also be referred to as an edgeless graph, an isolated graph, or a discrete
graph

7. Complete Graph:
A simple graph with n vertices is called a complete graph if the degree of each vertex
is n-1, that is, one vertex is attached with n-1 edges or the rest of the vertices in the
graph. A complete graph is also called Full Graph.
8. Pseudo Graph:
A graph G with a self-loop and some multiple edges is called a pseudo graph. A
pseudograph is a type of graph that allows for the existence of self-loops
loops (edges that
connect a vertex to itself) and multiple edges (more than one edge connecting two
vertices). In contrast, a simple
le graph is a graph that does not allow for loops or
multiple edges.

9. Regular Graph:
A simple graph is said to be regular if all vertices of graph G are of equal degree. All
complete graphs are regular but vice versa is not possible. A regular graph is a type of
undirected graph where every vertex has the same number of edges or neighbors. In
other words, if a graph is regular, then every vertex has the same degree.
10. Bipartite Graph:
A graph G = (V, E) is said to be a bipartite graph if its vertex set V(G) can be
partitioned into two non-empty disjoint subsets. V1(G) and V2(G) in such a way that
each edge e of E(G) has one end in V1(G) and another end in V2(G). The partition V1
U V2 = V is called Bipartite of G. Here in the figure: V1(G)={V5, V4, V3} and
V2(G)={V1, V2}

11. Labeled Graph:


If the vertices and edges of a graph are labeled with name, date, or weight then it is
called a labeled graph. It is also called Weighted Graph.
12. Digraph Graph:
A graph G = (V, E) with a mapping f such that every edge maps onto some ordered
pair of vertices (Vi, Vj) are called a Digraph. It is also called Directed Graph.
Graph The
ordered pair (Vi, Vj) means an edge between Vi and Vj with an arrow directed from
Vi to Vj. Here in the figure: e1 = (V1, V2) e2 = (V2, V3) e4 = (V2, V4)

13. Subgraph:
A graph G1 = (V1, E1) is called a subgraph of a graph G(V, E) if V1(G) is a subset of
V(G) and E1(G) is a subset of E(G) such that each edge of G1 has same end vertices
as in G.
14. Connected or Disconnected Graph:
Graph G is said to be connected if any pair of vertices (Vi, Vj) of a graph G is
reachable from one another. Or a graph is said to be connected if there exists at least
one path between each and every pair of vertices in graph G, otherwise, it is
disconnected. A null graph with n vertices is a disconnected graph consisting of n
components. Each component consists of one vertex and no edge.

15. Cyclic Graph:


A graph G consisting of n vertices and n> = 3 that is V1, V2, V3- – – – Vn and edges
(V1, V2), (V2, V3), (V3, V4)- – – – (Vn, V1) are called cyclic graph.
16. Types of Subgraphs:
 Vertex disjoint subgraph: Any two graph G1 = (V1, E1) and G2 = (V2, E2) are
said to be vertex disjoint of a graph G = (V, E) if V1(G1) intersection V2(G2) =
null. In the figure, there is no common vertex between G1 and G2.
 Edge disjoint subgraph: A subgraph is said to be edge-disjoint if E1(G1)
intersection E2(G2) = null. In the figure, there is no common edge between G1 and
G2.
Note: Edge disjoint subgraph may have vertices in common but a vertex disjoint
graph cannot have a common edge, so the vertex disjoint subgraph will always be an
edge-disjoint subgraph.
17. Spanning Subgraph
Consider the graph G(V,E) as shown below. A spanning subgraph is a subgraph that
contains all the vertices of the original graph G that is G'(V’,E’) is spanning if V’=V
and E’ is a subset of E.
So one of the spanning subgraph can be as shown below G'(V’,E’). It has all the
vertices of the original graph G and some of the edges of G.

This is just one of the many spanning subgraphs of graph G. We can create various
other spanning subgraphs by different combinations of edges. Note that if we consider
a graph G'(V’,E’) where V’=V and E’=E, then graph G’ is a spanning subgraph of
graph G(V,E).
Advantages of graphs:
1. Graphs can be used to model and analyze complex systems and relationships.
2. They are useful for visualizing and understanding data.
3. Graph algorithms are widely used in computer science and other fields, such as
social network analysis, logistics, and transportation.
4. Graphs can be used to represent a wide range of data types, including social
networks, road networks, and the internet.
Disadvantages of graphs:
1. Large graphs can be difficult to visualize and analyze.
2. Graph algorithms can be computationally expensive, especially for large graphs.
3. The interpretation of graph results can be subjective and may require domain-
specific knowledge.
4. Graphs can be susceptible to noise and outliers, which can impact the accuracy of
analysis results.

Basic Properties of a Graph



A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any
two nodes in the graph.
The basic properties of a graph include:
1. Vertices (nodes): The points where edges meet in a graph are known as vertices or
nodes. A vertex can represent a physical object, concept, or abstract entity.
2. Edges: The connections between vertices are known as edges. They can be
undirected (bidirectional) or directed (unidirectional).
3. Weight: A weight can be assigned to an edge, representing the cost or distance
between two vertices. A weighted graph is a graph where the edges have weights.
4. Degree: The degree of a vertex is the number of edges that connect to it. In a
directed graph, the in-degree of a vertex is the number of edges that point to it, and
the out-degree is the number of edges that start from it.
5. Path: A path is a sequence of vertices that are connected by edges. A simple path
does not contain any repeated vertices or edges.
6. Cycle: A cycle is a path that starts and ends at the same vertex. A simple cycle
does not contain any repeated vertices or edges.
7. Connectedness: A graph is said to be connected if there is a path between any two
vertices. A disconnected graph is a graph that is not connected.
8. Planarity: A graph is said to be planar if it can be drawn on a plane without any
edges crossing each other.
9. Bipartiteness: A graph is said to be bipartite if its vertices can be divided into two
disjoint sets such that no two vertices in the same set are connected by an edge.
Adjacency Matrix Representation

Adjacency Matrix is a square matrix used to represent a finite graph. The elements of
the matrix indicate whether pairs of vertices are adjacent or not in the graph. An
adjacency matrix is a simple and straightforward way to represent graphs and is
particularly useful for dense graphs.
What is Adjacency Matrix?
Adjacency Matrix is a square matrix used to represent a finite graph by
storing the relationships between the nodes in their respective cells. For a
graph with V vertices, the adjacency matrix A is an V X V matrix or 2D array.
1. Adjacency Matrix for Undirected and Unweighted
graph:
Consider an Undirected and Unweighted graph G with 4 vertices and 3
edges. For the graph G, the adjacency matrix would look like:

Here's how to interpret the matrix:


 A[i][i] = 1, there is an edge between vertex i and vertex j.
 A[i][i] = 0, there is NO edge between vertex i and vertex j.

2. Adjacency Matrix for Undirected and Weighted


graph:
Consider an Undirected and Weighted graph G with 5 vertices and 5 edges.
For the graph G, the adjacency matrix would look like:
Here's how to interpret the matrix:
 A[i][j] = INF, then there is no edge between vertex i and j
 A[i][j] = w, then there is an edge between vertex i and j having weight =
w.

3. Adjacency Matrix for Directed and Unweighted


graph:
Consider an Directed and Unweighted graph G with 4 vertices and 4 edges.
For the graph G, the adjacency matrix would look like:

Here's how to interpret the matrix:


 A[i][j] = 1, there is an edge from vertex i to vertex j
 A[i][j] = 0, No edge from vertex i to j.
4. Adjacency Matrix for Directed and Weighted graph:
Consider an Directed and Weighted graph G with 5 vertices and 6 edges.
For the graph G, the adjacency matrix would look like:

Here's how to interpret the matrix:


 A[i][j] = INF, then there is no edge from vertex i to j
 A[i][j] = w, then there is an edge from vertex i having weight w

Properties of Adjacency Matrix


 Diagonal Entries: The diagonal entries A[i][i] are usually set to 0 (in case
of unweighted) and INF in case of weighted, assuming the graph has no
self-loops.
 Undirected Graphs: For undirected graphs, the adjacency matrix is
symmetric. This means A[i][j] = A[j][i] for all i and j.
Applications of Adjacency Matrix:
 Graph Representation: The adjacency matrix is one of the most
common ways to represent a graph computationally.
 Connectivity: By examining the entries of the adjacency matrix, one can
determine whether the graph is connected or not. If the graph is
undirected, it is connected if and only if the corresponding adjacency
matrix is irreducible (i.e., there is a path between every pair of vertices). In
directed graphs, connectivity can be analyzed using concepts like strongly
connected components.
 Degree of Vertices: The degree of a vertex in a graph is the number of
edges incident to it. In an undirected graph, the degree of a vertex can be
calculated by summing the entries in the corresponding row (or column)
of the adjacency matrix. In a directed graph, the in-degree and out-degree
of a vertex can be similarly determined.
Advantages of Adjacency Matrix:
 Simple: Simple and Easy to implement.
 Space Efficient for Dense Graphs: Space efficient when the graph is
dense as it requires V * V space to represent the entire graph.
 Faster access to Edges: Adjacency Matrix allows constant look up to
check whether there exists an edge between a pair of vertices.
Disadvantages of Adjacency Matrix:
 Space inefficient for Sparse Graphs: Takes up O(V* V) space even if
the graph is sparse.
 Costly Insertions and Deletions: Adding or deleting a vertex can be
costly as it requires resizing the matrix.
 Slow Traversals: Graph traversals like DFS, BFS takes O(V * V) time to
visit all the vertices whereas Adjacency List takes only O(V + E).

Adjacency List Representation


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 (neighbour) to vertex 0.
 adjList[1] will have all the nodes which are connected (neighbour) to vertex 1 and
so on.
Representation of Undirected Graph as 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.
Undirected Graph to Adjacency list

Representation of Directed Graph as 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.

Directed Graph to Adjacency list


What is Incidence matrix
The incidence matrix can be described as a matrix that shows the graph. That means the
incidence matrix is used to draw a graph. We will use the symbol [Ac] to represent the
incidence matrix. Just like all other matrices, this matrix also contains rows and columns.

In the graph, the number of nodes is indicated with the help of rows of incidence matrix [Ac],
and the number of branches is indicated with the help of columns of that matrix. If the given
incidence matrix contains the n number of rows, then it will show that the graph of this
matrix has n number of nodes. Similarly, if the given incidence matrix contains the m
number of columns, then it will show that the graph of this matrix has the m number of
branches.

The above graph is a directed graph that has 6 branches and 4 nodes. So we can say that
this graph contains the 6 columns and 4 rows for the incidence matrix. The incidence matrix
will always take the entries as -1, 0, +1. The incidence matrix is always analogous to the
KCL, which stands for the Kirchhoff Current Law. Thus, the following things can be derived
from the KCL:

Types of Branch Value

Incoming branch to kth node -1

Outcome branch to kth node +1

Others 0
Steps to Construct Incidence matrix
The incidence matrix can be drawn with the help of some steps, which are described as
follows:

1. We will write the +1 if there is an outgoing branch in the kth node.


2. We will write the -1 if there is an incoming branch in the kth node.
3. We will write 0 in all the other branches.

Examples of Incidence matrix


In this example, we have a directed graph, and we have to draw the incidence matrix of this
graph.

The incidence matrix of the above graph is described as follows:

[AC] =

Branches a b c d e f

Nodes

1 1 -1 0 0 1 0
2 -1 0 -1 0 0 -1

3 0 0 0 1 -1 1

4 0 1 1 -1 0 0

GRAPH TRAVERSAL
Difference between BFS and DFS
Breadth-First Search (BFS
(BFS) and Depth-First Search (DFS) are two fundamental
algorithms used for traversing or searching graphs and trees. This article covers the
basic difference between Bre
Breadth-First Search and Depth-First Search.

Difference between BFS and DFS

Parameters BFS DFS

BFS stands for Breadth First


DFS stands for Depth First Search.
Stands for Search
Search.
Parameters BFS DFS

BFS(Breadth First Search) uses


Data DFS(Depth First Search) uses Stack
Queue data structure for finding
data structure.
Structure the shortest path.

DFS is also a traversal approach in


BFS is a traversal approach in
which the traverse begins at the root
which we first walk through all
node and proceeds through the nodes
nodes on the same level before
as far as possible until we reach the
moving on to the next level.
Definition node with no unvisited nearby nodes.

Conceptual DFS builds the tree sub-tree by sub-


BFS builds the tree level by level.
Difference tree.

Approach It works on the concept of FIFO It works on the concept of LIFO (Last In
used (First In First Out). First Out).

BFS is more suitable for


DFS is more suitable when there are
searching vertices closer to the
solutions away from source.
Suitable for given source.

DFS is used in various applications


BFS is used in various
such as acyclic graphs and finding
applications such as bipartite
strongly connected components etc.
graphs, shortest paths, etc. If
There are many applications where
weight of every edge is same,
both BFS and DFS can be used like
then BFS gives shortest pat from
Topological Sorting, Cycle Detection,
source to every other vertex.
Applications etc.

You might also like