[Document title]
“Graph: Adjacency Matrix”
LAB # 12
SPRING 2023
CSE-210L DATA STRUCTURES AND ALGORITHMS LAB
SUBMITTED BY:
Abdullah Khan (21PWCSE2024)
Jawad Irfan (21PWCSE2009)
Mujeeb Ur Rehman (21PWCSE2021)
Abdul Rasheed (21PWCSE2063)
Section: B
“On our honor, as students of University of Engineering and Technology, we
have neither given not received unauthorized assistance on this academic
work.”
SUBMITTED TO:
Dr. Khurram Shahzad Khattak
June 7th, 2023
Department of Computer Systems Engineering
University of Engineering and Technology, Peshawar
[Document title]
Objectives of the lab
Implementing graph using adjacency matrix.
Adjacency Matrix
An adjacency matrix is a way of representing a graph G = {V, E} as a
matrix of booleans.
Adjacency Matrix Representation :
The size of the matrix is VxV where V is the number of
vertices in the graph and the value of an entry Aij is either 1 or 0 depending on whether
there is an edge from vertex i to vertex j.
The image below shows a graph and its equivalent adjacency matrix.
A---B
/\ \
1 4 2
/ \/
C-------D
A B C D
A 0 1 1 0
B 1 0 0 1
C 1 0 0 1
D 0 1 1 0
In the adjacency matrix:
The rows and columns represent the vertices of the graph.
[Document title]
The value at matrix[i][j] indicates whether there is an edge between vertex i
and vertex j.
In this example, a value of 1 indicates the presence of an edge, and a
value of 0 indicates no edge.
In case of undirected graph, the matrix is symmetric about the diagonal
because of every edge (i,j), there is also an edge (j,i).
Adjacency Matrix Operations (undirected graph) :
- void addEdge(int i, int j)
set Aij to 1 (indicates that edge exists)
- void removeEdge (int i, int j)
set a[i][j] to 0 (indicates edge does not exist)
- bool isEdge (int I, int j)
return Aij
Activity :
Implement graphs with the following operations
a) Graph Creation
b) Adding Vertex to Graph
c) Removal of Vertex from the graph
d) Checking whether an edge exist between two vertices
e) Printing graph
[Document title]
CODE :
#include <iostream>
#include <vector>
using namespace std;
class Graph {
private:
int numVertices;
vector<vector<int>> adjacencyMatrix;
public:
Graph(int vertices) {
numVertices = vertices;
adjacencyMatrix.resize(numVertices, vector<int>(numVertices, 0));
void AddVertex() {
numVertices++;
adjacencyMatrix.resize(numVertices, vector<int>(numVertices, 0));
for (int i = 0; i < numVertices - 1; i++) {
adjacencyMatrix[i].resize(numVertices, 0);
void RemoveVertex(int vertex) {
if (vertex < 0 || vertex >= numVertices) {
[Document title]
cout << "Invalid vertex!" << endl;
return;
// Shift rows up
for (int i = vertex; i < numVertices - 1; i++) {
adjacencyMatrix[i] = adjacencyMatrix[i + 1];
// Shift columns left
for (int i = 0; i < numVertices; i++) {
for (int j = vertex; j < numVertices - 1; j++) {
adjacencyMatrix[i][j] = adjacencyMatrix[i][j + 1];
adjacencyMatrix[i].pop_back();
numVertices--;
void AddEdge(int start, int end) {
if (start < 0 || start >= numVertices || end < 0 || end >= numVertices) {
cout << "Invalid vertices!" << endl;
return;
adjacencyMatrix[start][end] = 1;
adjacencyMatrix[end][start] = 1;
[Document title]
void RemoveEdge(int start, int end) {
if (start < 0 || start >= numVertices || end < 0 || end >= numVertices) {
cout << "Invalid vertices!" << endl;
return;
adjacencyMatrix[start][end] = 0;
adjacencyMatrix[end][start] = 0;
bool HasEdge(int start, int end) {
if (start < 0 || start >= numVertices || end < 0 || end >= numVertices) {
cout << "Invalid vertices!" << endl;
return false;
return adjacencyMatrix[start][end] == 1;
void PrintGraph() {
cout << "Graph adjacency matrix:" << endl;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
cout << adjacencyMatrix[i][j] << " ";
}
[Document title]
cout << endl;
};
int main() {
Graph myGraph(4);
myGraph.AddVertex();
myGraph.AddEdge(0, 4);
myGraph.AddEdge(1, 4);
myGraph.AddEdge(2, 4);
myGraph.AddEdge(3, 4);
myGraph.PrintGraph();
myGraph.RemoveVertex(2);
myGraph.RemoveEdge(0, 4);
myGraph.PrintGraph();
cout << "Edge between vertices 1 and 4: " << (myGraph.HasEdge(1, 4) ?
"Yes" : "No") << endl;
return 0;