// C++ program to demonstrate Adjacency Matrix
// representation of undirected and unweighted graph
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector<vector<int>> &mat, int i, int j)
mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
void displayMatrix(vector<vector<int>> &mat)
int V = mat.size();
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
cout << mat[i][j] << " ";
cout << endl;
int main()
// Create a graph with 4 vertices and no edges
// Note that all values are initialized as 0
int V = 4;
vector<vector<int>> mat(V, vector<int>(V, 0));
// Now add edges one by one
addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);
/* Alternatively we can also create using below
code if we know all edges in advacem
vector<vector<int>> mat = {{ 0, 1, 0, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 0 } }; */
cout << "Adjacency Matrix Representation" << endl;
displayMatrix(mat);
return 0;
Introduction to Graph Data Structure
Last Updated : 09 Aug, 2024
Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in fields
such as social network analysis, recommendation systems, and computer networks. In the field of sports
data science, graph data structure can be used to analyze and understand the dynamics of team
performance and player interactions on the field.
Table of Content
What is Graph Data Structure?
Components of Graph Data Structure
Types Of Graph Data Structure
Representation of Graph Data Structure
o Adjacency Matrix Representation of Graph Data Structure
o Adjacency List Representation of Graph
Basic Operations on Graph Data Structure
Difference between Tree and Graph
Real-Life Applications of Graph Data Structure
Advantages of Graph Data Structure
Disadvantages of Graph Data Structure
Frequently Asked Questions(FAQs) on Graph Data Structure
What is Graph Data Structure?
Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More
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).
Imagine a game of football as a web of connections, where players are the nodes and their interactions
on the field are the edges. This web of connections is exactly what a graph data structure represents,
and it’s the key to unlocking insights into team performance and player dynamics in sports.
Components of Graph Data Structure
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as
vertex or nodes. Every node/vertex can be labeled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of
nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge can be labelled/unlabelled.
Types Of Graphs in Data Structure and Algorithms
1. Null Graph
A graph is known as a null graph if there are no edges in the graph.
2. Trivial Graph
Graph having only a single vertex, it is also the smallest graph
possible.
3. Undirected Graph
A graph in which edges do not have any direction. That is the nodes are unordered pairs in the definition
of every edge.
4. Directed Graph
A graph in which edge has direction. That is the nodes are ordered pairs in the definition of every edge.
5. Connected Graph
The graph in which from one node we can visit any other node in the graph is known as a connected
graph.
6. Disconnected Graph
The graph in which at least one node is not reachable from a node is known as a disconnected graph.
7. Regular Graph
The graph in which the degree of every vertex is equal to K is called K regular graph.
8. Complete Graph
The graph in which from each node there is an edge to each other node.
9. Cycle Graph
The graph in which the graph is a cycle in itself, the minimum value of degree of each vertex is 2.
10. Cyclic Graph
A graph containing at least one cycle is known as a Cyclic graph.
11. Directed Acyclic Graph
A Directed Graph that does not contain any cycle.
12. Bipartite Graph
A graph in which vertex can be divided into two sets such that vertex in each set does not contain any
edge between them.
13. Weighted Graph
A graph in which the edges are already specified with suitable weight is known as a weighted
graph.
Weighted graphs can be further classified as directed weighted graphs and undirected weighted
graphs.
Representation of Graph Data Structure:
There are multiple ways to store a graph: The following are the most common representations.
Adjacency Matrix
Adjacency List