Professional Documents
Culture Documents
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.
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.
1. Adjacency matrix
2. Adjacency list
3. Incidence matrix
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.
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.
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
Pros:
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
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
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
Efficient ordering.
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.
Example
In the following graph, vertices 'e' and 'c' are the cut vertices.
A sequence x0, x1, x2, …, xt of vertices is called an euler circuit in a graph G if:
1. x0 = xt;
Euler’s Theorem
A graph G has an euler circuit if and only if it is connected and every vertex has even
degree.
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