You are on page 1of 15

UNIT IV

What is a Graph in Data Structures?


A graph is a collection of nodes that consist of data and are connected to other nodes of the
graph. formally a Graph is composed of a set of vertices( V ) and a set of edges ( E ). The
graph is denoted by G(V, E).
The most common real-life examples of graphs are social media where a User, Photo,
Album, Event, Group, Page, Comment, Story, Video, etc represents a node. Every
relationship is an edge from one node to another. Whenever you post a photo, join a group,
like a page, etc., a new edge is created for that relationship. Thus, it can be said that a social
media platform is a collection of nodes and edges.

Graph Terminologies in Data Structures

Graph terminology in data structure refers to the specific vocabulary and concepts used to
describe and analyze graphs, which are mathematical structures composed of nodes (also
called vertices) connected by edges.
1. Node/Vertex: A fundamental unit of a graph. It represents an entity or an element
and is usually depicted as a point.
2. Edge/Arc: A connection between two nodes. It represents a relationship or a link
between the corresponding entities. An edge can be directed (arc), indicating a one-
way connection, or undirected, representing a two-way connection.
3. Adjacent Nodes: Two nodes that are directly connected by an edge. In an undirected
graph, both nodes are considered adjacent to each other. In a directed graph,
adjacency depends on the direction of the edge.
4. Degree: The degree of a node is the number of edges incident to it, i.e., the number of
edges connected to that node. In a directed graph, the degree is divided into two
categories: the in-degree (number of incoming edges) and the out-degree (number of
outgoing edges).
5. Path: A path in a graph is a sequence of edges that connects a sequence of nodes. It
can be a simple path (no repeated nodes) or a closed path/cycle (starts and ends at the
same node).
6. Bipartite Graph: A graph whose nodes can be divided into two disjoint sets such
that every edge connects a node from one set to a node from the other set. In other
words, there are no edges between nodes within the same set.
7. Spanning Tree: A subgraph of a connected graph that includes all the nodes of the
original graph and forms a tree (a connected acyclic graph) by eliminating some of
the edges.
8. Cycle: A closed path in a graph, where the first and last nodes are the same. It
consists of at least three edges.

12.2.1 Types of Graphs


1. Undirected Graph: A graph with only undirected edges is said to be
undirected graph.

2. Directed Graph:A graph with only directed edges is said to be directed


graph.

3. Complete Graph: A graph in which any V node is adjacent to all other


nodes present in the graph is known as a complete graph. An undirected
graph contains the edges that are equal to edges = n(n-1)/2 where n is the
number of vertices present in the graph. The following figure shows a
complete graph.

4. Regular Graph: Regular graph is the graph in which nodes are adjacent to
each other, i.e., each node is accessible from any other node.

5. Cycle Graph: A graph having cycle is called cycle graph. In this case the
first and last nodes are the same. A closed simple path is a cycle.
6. Acyclic Graph: A graph without cycle is called acyclic graphs.

7. Weighted Graph: A graph is said to be weighted if there are some non-


negative value assigned to each edges of the graph. The value is equal to the
length between two vertices. Weighted graph is also called a network.

Graph Representation in Data Structures

A graph can be represented using three data structures-

1. Adjacency matrix

2. Adjacency list

3. Incidence matrix

Adjacency Matrix Representation

An adjacency matrix is a type of sequential representation.

 It is used to indicate which nodes are adjacent to one another. I.e., do any edges connect
nodes in a graph?

 In this representation, an NxN matrix A must be created. If an edge connects vertex i and
vertex j, the corresponding element of A is ai,j = 1, otherwise, it is ai,j = 0.
 If there is a weighted graph, we can store the edge weight instead of 1s and 0s.

Directed Graph Representation

Undirected Graph Representation

Pros and Cons of the Adjacency Matrix

 Pros: Representation is simpler to implement and adhere to.

 Cons: It takes a lot of space and time to visit all of a vertex’s neighbors; we have to traverse
all of the vertices in the graph, which takes a long time.

Adjacency List Representation

A linked representation is an adjacency list.

 In this representation, we keep a list of neighbors for each vertex in the graph.

 It means that each vertex in the graph has a list of its neighbors.

 Each array element points to a singly linked list of v’s neighbors, and the array is indexed by
the vertex number.
Directed Graph Representation

Undirected Graph Representation

Pros and Cons of the Adjacency List

 Pros:

 A list of adjacencies saves a lot of space.

 We can easily insert and delete items because we are using a linked list.

 Such a representation is simple to understand and clearly shows the adjacent nodes.

 Cons: While the adjacency list allows you to test whether two vertices are adjacent, it is
slower to perform this operation.
BREADTH FIRST TRAVERSAL
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.

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.
Step Traversal Description

Initialize the queue.

We start from visiting S (starting node), and


mark it as visited.
We then see an unvisited adjacent node
from S. In this example, we have three nodes but alphabetically we choose A, mark it as
visited and enqueue it.

Next, the unvisited adjacent node from S is B.


We mark it as visited and enqueue it.

Next, the unvisited adjacent node from S is C.


We mark it as visited and enqueue it.

Now, S is left with no unvisited adjacent


nodes. So, we dequeue and find A.
From A we have D as unvisited adjacent node.
We mark it as visited and enqueue it.

DEPTH FIRST TRAVERSAL


Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack
to remember to get the next vertex to start a search, when a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to D to G to E to B


first, then to F and lastly to C. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a
stack.
 Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up
all the vertices from the stack, which do not have adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Step Traversal Description

Initialize the stack.


Mark S as visited and put it onto the stack.
Explore any unvisited adjacent node from S. We have three nodes and we can pick any of
them. For this example, we shall take the node in an alphabetical order.

Mark A as visited and put it onto the stack.


Explore any unvisited adjacent node from A. Both S and D are adjacent to A but we are
concerned for unvisited nodes only.

Visit D and mark it as visited and put onto the


stack. Here, we have B and C nodes, which are adjacent to D and both are unvisited.
However, we shall again choose in an alphabetical order.

We choose B, mark it as visited and put onto


the stack. Here B does not have any unvisited adjacent node. So, we pop B from the stack.
We check the stack top for return to the
previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the
stack.

Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and
put it onto the stack.

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.

TOPOLOGICAL SORT

Topological Sort is a linear ordering of the vertices in such a way that


if there is an edge in the DAG going from vertex ‘u’ to vertex ‘v’,
then ‘u’ comes before ‘v’ in the ordering.

It is important to note that-


 Topological Sorting is possible if and only if the graph is a Directed Acyclic Graph.
 There may exist multiple different topological orderings for a given directed acyclic graph.
Topological Sort Example-

Consider the following directed acyclic graph-

Advantages of the Topological Sort

Some advantages of the topological sort in Java include:

 Efficient ordering.

 Easy implementation using adjacency lists.

 Wide range of applications.

 Low time and space complexity.

 Scalability for large-scale data processing.


Disadvantages of the Topological Sort

Some disadvantages of the topological sort in Java include:

 It only works on directed acyclic graphs (DAGs) and cannot handle cyclic graphs.

 This does not provide a unique solution if there are multiple valid orders for the nodes.

 It may not be the best algorithm for certain graph-related problems, depending on the specific
requirements and constraints.

 These may require additional data structures and memory overhead for larger graphs, which
can affect performance.

BICONNECTED GRAPH
An undirected graph is said to be a biconnected graph, if there are two vertex-disjoint paths
between any two vertices are present. In other words, we can say that there is a cycle
between any two vertices.

We can say that a graph G is a bi-connected graph if it is connected, and there are no
articulation points or cut vertex are present in the graph.

To solve this problem, we will use the DFS traversal. Using DFS, we will try to find if there
is any articulation point is present or not. We also check whether all vertices are visited by
the DFS or not, if not we can say that the graph is not connected.

Input:
The adjacency matrix of the graph.
01110
10100
11001
10001
00110
Output:
The Graph is a biconnected graph.
CUT VERTEX OF GRAPH

Connectivity

A graph is said to be connected if there is a path between every pair of vertex. From every
vertex to any other vertex, there should be some path to traverse. That is called the
connectivity of a graph. A graph with multiple disconnected vertices and edges is said to be
disconnected.

Cut Vertex

Let 'G' be a connected graph. A vertex V ∈ G is called a cut vertex of 'G', if 'G-V' (Delete 'V'
from 'G') results in a disconnected graph. Removing a cut vertex from a graph breaks it in to
two or more graphs.

Note − Removing a cut vertex may render a graph disconnected.

A connected graph 'G' may have at most (n–2) cut vertices.

Example

In the following graph, vertices 'e' and 'c' are the cut vertices.

By removing 'e' or 'c', the graph will become a disconnected graph.


Without 'g', there is no path between vertex 'c' and vertex 'h' and many other. Hence it is a
disconnected graph with cut vertex as 'e'. Similarly, 'c' is also a cut vertex for the above
graph.

EULER CIRCUITS IN GRAPHS

A sequence x0, x1, x2, …, xt of vertices is called an euler circuit in a graph G if:

1. x0 = xt;

2. For every i = 0, 1, 2, …, t-1, xi xi+1 is an edge of G; and

3. For every edge e of G, there is a unique i with 0 ≤ i < t so that e = xi xi+1.

Here is an euler circuit for this graph:


(1,8,3,6,8,7,2,4,5,6,2,3,1)

Euler’s Theorem
A graph G has an euler circuit if and only if it is connected and every vertex has even
degree.

Algorithm for Euler Circuits


 Choose a root vertex r and start with the trivial partial circuit (r).
 Given a partial circuit (r = x0,x1,…,xt = r) that traverses some but not all of the edges
of G containing r, remove these edges from G. Let i be the least integer for which
xi is incident with one of the remaining edges. Form a greedy partial circuit among
the remaining edges of the form (xi = y0,y1,…,ys = xi).
 Expand the original circuit:
r =(x0,x1,…, xi-1, xi = y0,y1,…,ys = xi, xi+1,…, xt=r)

APPLICATIONS OF GRAPH THEORY

Graph Theory is used in vast area of science and technologies. Some of them are given
below:

1. Computer Science

In computer science graph theory is used for the study of algorithms like:

o Dijkstra's Algorithm
o Prims's Algorithm
o Kruskal's Algorithm

o Graphs are used to define the flow of computation.


o Graphs are used to represent networks of communication.
o Graphs are used to represent data organization.
o Graph transformation systems work on rule-based in-memory manipulation of graphs.
Graph databases ensure transaction-safe, persistent storing and querying of graph
structured data.
o Graph theory is used to find shortest path in road or a network.
o In Google Maps, various locations are represented as vertices or nodes and the roads
are represented as edges and graph theory is used to find the shortest path between
two nodes.

You might also like